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