paulb@214 | 1 | #!/usr/bin/env python |
paulb@214 | 2 | |
paulb@214 | 3 | "A simple Jython-based installer for the Web applications." |
paulb@214 | 4 | |
paulb@214 | 5 | import os |
paulb@214 | 6 | |
paulb@214 | 7 | def copy_file(source, destination): |
paulb@214 | 8 | |
paulb@214 | 9 | """ |
paulb@214 | 10 | Copy a file from 'source' to 'destination'. Note that 'destination' must |
paulb@214 | 11 | include the name of the file - it cannot be a directory name. |
paulb@214 | 12 | """ |
paulb@214 | 13 | |
paulb@214 | 14 | print "Copying", source, "to", destination |
paulb@214 | 15 | |
paulb@214 | 16 | # Do things by the book, since Jython/Java won't copy the file contents in |
paulb@214 | 17 | # all cases presumably due to finalisation issues. |
paulb@214 | 18 | |
paulb@214 | 19 | f = open(source, "rb") |
paulb@214 | 20 | s = f.read() |
paulb@214 | 21 | f.close() |
paulb@214 | 22 | |
paulb@214 | 23 | f = open(destination, "wb") |
paulb@214 | 24 | f.write(s) |
paulb@214 | 25 | f.close() |
paulb@214 | 26 | |
paulb@214 | 27 | def recurse(dirs_and_files, dirname, names): |
paulb@214 | 28 | |
paulb@214 | 29 | """ |
paulb@214 | 30 | A recursive directory and file collector for os.path.walk. The provided |
paulb@214 | 31 | 'dirs_and_files' list must contain two lists (one for directory names, one |
paulb@214 | 32 | for filenames). The 'dirname' and 'names' parameters are supplied by the |
paulb@214 | 33 | os.path.walk mechanism. |
paulb@214 | 34 | """ |
paulb@214 | 35 | |
paulb@214 | 36 | if dirname.endswith("/CVS"): |
paulb@214 | 37 | return |
paulb@214 | 38 | dirs_and_files[0].append(dirname) |
paulb@214 | 39 | for name in names: |
paulb@214 | 40 | if os.path.isfile(os.path.join(dirname, name)): |
paulb@214 | 41 | dirs_and_files[1].append(os.path.join(dirname, name)) |
paulb@214 | 42 | |
paulb@214 | 43 | def copy_directory(source, destination): |
paulb@214 | 44 | |
paulb@214 | 45 | """ |
paulb@214 | 46 | Copy a directory found at 'source' in the filesystem to the 'destination'. |
paulb@214 | 47 | Note that 'destination' is the parent directory of the newly created |
paulb@214 | 48 | directory. |
paulb@214 | 49 | """ |
paulb@214 | 50 | |
paulb@214 | 51 | # Remove trailing directory separators. |
paulb@214 | 52 | |
paulb@214 | 53 | source = os.path.normpath(source) |
paulb@214 | 54 | prefix = os.path.split(source)[0] |
paulb@214 | 55 | dirs_and_files = [[], []] |
paulb@214 | 56 | os.path.walk(source, recurse, dirs_and_files) |
paulb@214 | 57 | |
paulb@214 | 58 | for dirname in dirs_and_files[0]: |
paulb@214 | 59 | |
paulb@214 | 60 | # Remove the prefix from the name and create the object under the destination. |
paulb@214 | 61 | # NOTE: Joining "" to the path in Jython doesn't add the path separator. |
paulb@214 | 62 | |
paulb@214 | 63 | new_dirname = dirname[len(os.path.join(prefix, "x")) - 1:] |
paulb@214 | 64 | print "Making", new_dirname, "under", destination |
paulb@214 | 65 | os.mkdir(os.path.join(destination, new_dirname)) |
paulb@214 | 66 | |
paulb@214 | 67 | for filename in dirs_and_files[1]: |
paulb@214 | 68 | |
paulb@214 | 69 | # Remove the prefix from the name and create the object under the destination. |
paulb@214 | 70 | # NOTE: Joining "" to the path in Jython doesn't add the path separator. |
paulb@214 | 71 | |
paulb@214 | 72 | new_filename = filename[len(os.path.join(prefix, "x")) - 1:] |
paulb@214 | 73 | copy_file(filename, os.path.join(destination, new_filename)) |
paulb@214 | 74 | |
paulb@214 | 75 | def get_appname(handler): |
paulb@214 | 76 | return os.path.split(os.path.splitext(handler)[0])[1] |
paulb@214 | 77 | |
paulb@291 | 78 | def make_app(handler, appdir, webstack_home, web_xml_template_name): |
paulb@214 | 79 | |
paulb@214 | 80 | """ |
paulb@214 | 81 | Make the application directory from the given 'handler', application |
paulb@291 | 82 | directory 'appdir', the 'webstack_home' where the WebStack package can be |
paulb@291 | 83 | found, and the deployment descriptor with the given 'web_xml_template_name'. |
paulb@214 | 84 | """ |
paulb@214 | 85 | |
paulb@214 | 86 | appname = get_appname(handler) |
paulb@214 | 87 | print "Making", appname |
paulb@214 | 88 | |
paulb@214 | 89 | os.mkdir(appname) |
paulb@214 | 90 | os.mkdir(os.path.join(appname, "WEB-INF")) |
paulb@214 | 91 | os.mkdir(os.path.join(appname, "WEB-INF", "jython")) |
paulb@214 | 92 | os.mkdir(os.path.join(appname, "WEB-INF", "lib")) |
paulb@214 | 93 | |
paulb@214 | 94 | # Copy the Jython runtime. |
paulb@214 | 95 | |
paulb@214 | 96 | jython_home = sys.exec_prefix |
paulb@214 | 97 | copy_file(os.path.join(jython_home, "jython.jar"), |
paulb@214 | 98 | os.path.join(appname, "WEB-INF", "lib", "jython.jar")) |
paulb@214 | 99 | |
paulb@214 | 100 | # Copy the special PyServlet classes. |
paulb@214 | 101 | |
paulb@214 | 102 | copy_directory(os.path.join(webstack_home, "tools", "JavaServlet", "classes"), |
paulb@214 | 103 | os.path.join(appname, "WEB-INF")) |
paulb@214 | 104 | |
paulb@214 | 105 | # Copy the WebStack package. |
paulb@214 | 106 | |
paulb@214 | 107 | copy_directory(os.path.join(webstack_home, "WebStack"), |
paulb@214 | 108 | os.path.join(appname, "WEB-INF", "jython")) |
paulb@214 | 109 | |
paulb@214 | 110 | # Copy the application itself. |
paulb@214 | 111 | |
paulb@214 | 112 | copy_directory(appdir, os.path.join(appname, "WEB-INF", "jython")) |
paulb@214 | 113 | |
paulb@214 | 114 | # Copy the handler. |
paulb@214 | 115 | |
paulb@214 | 116 | handler_filename = os.path.split(handler)[1] |
paulb@214 | 117 | copy_file(handler, os.path.join(appname, handler_filename)) |
paulb@214 | 118 | |
paulb@214 | 119 | # Configure the deployment descriptor. |
paulb@214 | 120 | |
paulb@291 | 121 | f = open(os.path.join(webstack_home, "tools", "JavaServlet", web_xml_template_name)) |
paulb@214 | 122 | web_xml = f.read() |
paulb@214 | 123 | f.close() |
paulb@214 | 124 | web_xml = web_xml % (jython_home, handler_filename) |
paulb@214 | 125 | |
paulb@214 | 126 | # Write the deployment descriptor. |
paulb@214 | 127 | |
paulb@214 | 128 | f = open(os.path.join(appname, "WEB-INF", "web.xml"), "w") |
paulb@214 | 129 | f.write(web_xml) |
paulb@214 | 130 | f.close() |
paulb@214 | 131 | |
paulb@214 | 132 | if __name__ == "__main__": |
paulb@214 | 133 | import sys |
paulb@291 | 134 | if len(sys.argv) < 5: |
paulb@214 | 135 | print "Please specify..." |
paulb@214 | 136 | print " * The location of the application handler." |
paulb@214 | 137 | print " eg. .../WebStack-x.y/examples/JavaServlet/SimpleApp.py" |
paulb@214 | 138 | print " * The location of the application." |
paulb@214 | 139 | print " eg. .../WebStack-x.y/examples/Common/Simple" |
paulb@214 | 140 | print " * The location of the WebStack package distribution." |
paulb@214 | 141 | print " eg. .../WebStack-x.y" |
paulb@291 | 142 | print " * The name of the deployment descriptor template." |
paulb@291 | 143 | print " eg. web.xml" |
paulb@214 | 144 | print "You can also specify some additional libraries for the application..." |
paulb@214 | 145 | print " eg. $CATALINA_HOME/common/lib/activation.jar" |
paulb@214 | 146 | print " $CATALINA_HOME/common/lib/mail.jar" |
paulb@214 | 147 | sys.exit(1) |
paulb@214 | 148 | |
paulb@214 | 149 | print "Making application directory..." |
paulb@291 | 150 | make_app(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4]) |
paulb@214 | 151 | |
paulb@291 | 152 | if len(sys.argv) > 5: |
paulb@214 | 153 | print "Copying additional libraries..." |
paulb@214 | 154 | appname = get_appname(sys.argv[1]) |
paulb@291 | 155 | for library in sys.argv[5:]: |
paulb@214 | 156 | library_dir, library_name = os.path.split(library) |
paulb@214 | 157 | library_dest = os.path.join(appname, "WEB-INF", "lib", library_name) |
paulb@214 | 158 | copy_file(library, library_dest) |
paulb@214 | 159 | |
paulb@214 | 160 | print "Now copy or move the application directory to your servlet container." |
paulb@214 | 161 | |
paulb@214 | 162 | # vim: tabstop=4 expandtab shiftwidth=4 |