2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
2.2 +++ b/tools/JavaServlet/build.py Sun Aug 29 19:09:13 2004 +0000
2.3 @@ -0,0 +1,160 @@
2.4 +#!/usr/bin/env python
2.5 +
2.6 +"A simple Jython-based installer for the Web applications."
2.7 +
2.8 +import os
2.9 +
2.10 +def copy_file(source, destination):
2.11 +
2.12 + """
2.13 + Copy a file from 'source' to 'destination'. Note that 'destination' must
2.14 + include the name of the file - it cannot be a directory name.
2.15 + """
2.16 +
2.17 + print "Copying", source, "to", destination
2.18 +
2.19 + # Do things by the book, since Jython/Java won't copy the file contents in
2.20 + # all cases presumably due to finalisation issues.
2.21 +
2.22 + f = open(source, "rb")
2.23 + s = f.read()
2.24 + f.close()
2.25 +
2.26 + f = open(destination, "wb")
2.27 + f.write(s)
2.28 + f.close()
2.29 +
2.30 +def recurse(dirs_and_files, dirname, names):
2.31 +
2.32 + """
2.33 + A recursive directory and file collector for os.path.walk. The provided
2.34 + 'dirs_and_files' list must contain two lists (one for directory names, one
2.35 + for filenames). The 'dirname' and 'names' parameters are supplied by the
2.36 + os.path.walk mechanism.
2.37 + """
2.38 +
2.39 + if dirname.endswith("/CVS"):
2.40 + return
2.41 + dirs_and_files[0].append(dirname)
2.42 + for name in names:
2.43 + if os.path.isfile(os.path.join(dirname, name)):
2.44 + dirs_and_files[1].append(os.path.join(dirname, name))
2.45 +
2.46 +def copy_directory(source, destination):
2.47 +
2.48 + """
2.49 + Copy a directory found at 'source' in the filesystem to the 'destination'.
2.50 + Note that 'destination' is the parent directory of the newly created
2.51 + directory.
2.52 + """
2.53 +
2.54 + # Remove trailing directory separators.
2.55 +
2.56 + source = os.path.normpath(source)
2.57 + prefix = os.path.split(source)[0]
2.58 + dirs_and_files = [[], []]
2.59 + os.path.walk(source, recurse, dirs_and_files)
2.60 +
2.61 + for dirname in dirs_and_files[0]:
2.62 +
2.63 + # Remove the prefix from the name and create the object under the destination.
2.64 + # NOTE: Joining "" to the path in Jython doesn't add the path separator.
2.65 +
2.66 + new_dirname = dirname[len(os.path.join(prefix, "x")) - 1:]
2.67 + print "Making", new_dirname, "under", destination
2.68 + os.mkdir(os.path.join(destination, new_dirname))
2.69 +
2.70 + for filename in dirs_and_files[1]:
2.71 +
2.72 + # Remove the prefix from the name and create the object under the destination.
2.73 + # NOTE: Joining "" to the path in Jython doesn't add the path separator.
2.74 +
2.75 + new_filename = filename[len(os.path.join(prefix, "x")) - 1:]
2.76 + copy_file(filename, os.path.join(destination, new_filename))
2.77 +
2.78 +def get_appname(handler):
2.79 + return os.path.split(os.path.splitext(handler)[0])[1]
2.80 +
2.81 +def make_app(handler, appdir, webstack_home):
2.82 +
2.83 + """
2.84 + Make the application directory from the given 'handler', application
2.85 + directory 'appdir' and the 'webstack_home' where the WebStack package can be
2.86 + found.
2.87 + """
2.88 +
2.89 + appname = get_appname(handler)
2.90 + print "Making", appname
2.91 +
2.92 + os.mkdir(appname)
2.93 + os.mkdir(os.path.join(appname, "WEB-INF"))
2.94 + os.mkdir(os.path.join(appname, "WEB-INF", "jython"))
2.95 + os.mkdir(os.path.join(appname, "WEB-INF", "lib"))
2.96 +
2.97 + # Copy the Jython runtime.
2.98 +
2.99 + jython_home = sys.exec_prefix
2.100 + copy_file(os.path.join(jython_home, "jython.jar"),
2.101 + os.path.join(appname, "WEB-INF", "lib", "jython.jar"))
2.102 +
2.103 + # Copy the special PyServlet classes.
2.104 +
2.105 + copy_directory(os.path.join(webstack_home, "tools", "JavaServlet", "classes"),
2.106 + os.path.join(appname, "WEB-INF"))
2.107 +
2.108 + # Copy the WebStack package.
2.109 +
2.110 + copy_directory(os.path.join(webstack_home, "WebStack"),
2.111 + os.path.join(appname, "WEB-INF", "jython"))
2.112 +
2.113 + # Copy the application itself.
2.114 +
2.115 + copy_directory(appdir, os.path.join(appname, "WEB-INF", "jython"))
2.116 +
2.117 + # Copy the handler.
2.118 +
2.119 + handler_filename = os.path.split(handler)[1]
2.120 + copy_file(handler, os.path.join(appname, handler_filename))
2.121 +
2.122 + # Configure the deployment descriptor.
2.123 +
2.124 + f = open(os.path.join(webstack_home, "tools", "JavaServlet", "web.xml"))
2.125 + web_xml = f.read()
2.126 + f.close()
2.127 + web_xml = web_xml % (jython_home, handler_filename)
2.128 +
2.129 + # Write the deployment descriptor.
2.130 +
2.131 + f = open(os.path.join(appname, "WEB-INF", "web.xml"), "w")
2.132 + f.write(web_xml)
2.133 + f.close()
2.134 +
2.135 +if __name__ == "__main__":
2.136 + import sys
2.137 + if len(sys.argv) < 4:
2.138 + print "Please specify..."
2.139 + print " * The location of the application handler."
2.140 + print " eg. .../WebStack-x.y/examples/JavaServlet/SimpleApp.py"
2.141 + print " * The location of the application."
2.142 + print " eg. .../WebStack-x.y/examples/Common/Simple"
2.143 + print " * The location of the WebStack package distribution."
2.144 + print " eg. .../WebStack-x.y"
2.145 + print "You can also specify some additional libraries for the application..."
2.146 + print " eg. $CATALINA_HOME/common/lib/activation.jar"
2.147 + print " $CATALINA_HOME/common/lib/mail.jar"
2.148 + sys.exit(1)
2.149 +
2.150 + print "Making application directory..."
2.151 + make_app(sys.argv[1], sys.argv[2], sys.argv[3])
2.152 +
2.153 + if len(sys.argv) > 4:
2.154 + print "Copying additional libraries..."
2.155 + appname = get_appname(sys.argv[1])
2.156 + for library in sys.argv[4:]:
2.157 + library_dir, library_name = os.path.split(library)
2.158 + library_dest = os.path.join(appname, "WEB-INF", "lib", library_name)
2.159 + copy_file(library, library_dest)
2.160 +
2.161 + print "Now copy or move the application directory to your servlet container."
2.162 +
2.163 +# vim: tabstop=4 expandtab shiftwidth=4