1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/tools/JavaServlet/build.py Sun Aug 29 19:09:13 2004 +0000
1.3 @@ -0,0 +1,160 @@
1.4 +#!/usr/bin/env python
1.5 +
1.6 +"A simple Jython-based installer for the Web applications."
1.7 +
1.8 +import os
1.9 +
1.10 +def copy_file(source, destination):
1.11 +
1.12 + """
1.13 + Copy a file from 'source' to 'destination'. Note that 'destination' must
1.14 + include the name of the file - it cannot be a directory name.
1.15 + """
1.16 +
1.17 + print "Copying", source, "to", destination
1.18 +
1.19 + # Do things by the book, since Jython/Java won't copy the file contents in
1.20 + # all cases presumably due to finalisation issues.
1.21 +
1.22 + f = open(source, "rb")
1.23 + s = f.read()
1.24 + f.close()
1.25 +
1.26 + f = open(destination, "wb")
1.27 + f.write(s)
1.28 + f.close()
1.29 +
1.30 +def recurse(dirs_and_files, dirname, names):
1.31 +
1.32 + """
1.33 + A recursive directory and file collector for os.path.walk. The provided
1.34 + 'dirs_and_files' list must contain two lists (one for directory names, one
1.35 + for filenames). The 'dirname' and 'names' parameters are supplied by the
1.36 + os.path.walk mechanism.
1.37 + """
1.38 +
1.39 + if dirname.endswith("/CVS"):
1.40 + return
1.41 + dirs_and_files[0].append(dirname)
1.42 + for name in names:
1.43 + if os.path.isfile(os.path.join(dirname, name)):
1.44 + dirs_and_files[1].append(os.path.join(dirname, name))
1.45 +
1.46 +def copy_directory(source, destination):
1.47 +
1.48 + """
1.49 + Copy a directory found at 'source' in the filesystem to the 'destination'.
1.50 + Note that 'destination' is the parent directory of the newly created
1.51 + directory.
1.52 + """
1.53 +
1.54 + # Remove trailing directory separators.
1.55 +
1.56 + source = os.path.normpath(source)
1.57 + prefix = os.path.split(source)[0]
1.58 + dirs_and_files = [[], []]
1.59 + os.path.walk(source, recurse, dirs_and_files)
1.60 +
1.61 + for dirname in dirs_and_files[0]:
1.62 +
1.63 + # Remove the prefix from the name and create the object under the destination.
1.64 + # NOTE: Joining "" to the path in Jython doesn't add the path separator.
1.65 +
1.66 + new_dirname = dirname[len(os.path.join(prefix, "x")) - 1:]
1.67 + print "Making", new_dirname, "under", destination
1.68 + os.mkdir(os.path.join(destination, new_dirname))
1.69 +
1.70 + for filename in dirs_and_files[1]:
1.71 +
1.72 + # Remove the prefix from the name and create the object under the destination.
1.73 + # NOTE: Joining "" to the path in Jython doesn't add the path separator.
1.74 +
1.75 + new_filename = filename[len(os.path.join(prefix, "x")) - 1:]
1.76 + copy_file(filename, os.path.join(destination, new_filename))
1.77 +
1.78 +def get_appname(handler):
1.79 + return os.path.split(os.path.splitext(handler)[0])[1]
1.80 +
1.81 +def make_app(handler, appdir, webstack_home):
1.82 +
1.83 + """
1.84 + Make the application directory from the given 'handler', application
1.85 + directory 'appdir' and the 'webstack_home' where the WebStack package can be
1.86 + found.
1.87 + """
1.88 +
1.89 + appname = get_appname(handler)
1.90 + print "Making", appname
1.91 +
1.92 + os.mkdir(appname)
1.93 + os.mkdir(os.path.join(appname, "WEB-INF"))
1.94 + os.mkdir(os.path.join(appname, "WEB-INF", "jython"))
1.95 + os.mkdir(os.path.join(appname, "WEB-INF", "lib"))
1.96 +
1.97 + # Copy the Jython runtime.
1.98 +
1.99 + jython_home = sys.exec_prefix
1.100 + copy_file(os.path.join(jython_home, "jython.jar"),
1.101 + os.path.join(appname, "WEB-INF", "lib", "jython.jar"))
1.102 +
1.103 + # Copy the special PyServlet classes.
1.104 +
1.105 + copy_directory(os.path.join(webstack_home, "tools", "JavaServlet", "classes"),
1.106 + os.path.join(appname, "WEB-INF"))
1.107 +
1.108 + # Copy the WebStack package.
1.109 +
1.110 + copy_directory(os.path.join(webstack_home, "WebStack"),
1.111 + os.path.join(appname, "WEB-INF", "jython"))
1.112 +
1.113 + # Copy the application itself.
1.114 +
1.115 + copy_directory(appdir, os.path.join(appname, "WEB-INF", "jython"))
1.116 +
1.117 + # Copy the handler.
1.118 +
1.119 + handler_filename = os.path.split(handler)[1]
1.120 + copy_file(handler, os.path.join(appname, handler_filename))
1.121 +
1.122 + # Configure the deployment descriptor.
1.123 +
1.124 + f = open(os.path.join(webstack_home, "tools", "JavaServlet", "web.xml"))
1.125 + web_xml = f.read()
1.126 + f.close()
1.127 + web_xml = web_xml % (jython_home, handler_filename)
1.128 +
1.129 + # Write the deployment descriptor.
1.130 +
1.131 + f = open(os.path.join(appname, "WEB-INF", "web.xml"), "w")
1.132 + f.write(web_xml)
1.133 + f.close()
1.134 +
1.135 +if __name__ == "__main__":
1.136 + import sys
1.137 + if len(sys.argv) < 4:
1.138 + print "Please specify..."
1.139 + print " * The location of the application handler."
1.140 + print " eg. .../WebStack-x.y/examples/JavaServlet/SimpleApp.py"
1.141 + print " * The location of the application."
1.142 + print " eg. .../WebStack-x.y/examples/Common/Simple"
1.143 + print " * The location of the WebStack package distribution."
1.144 + print " eg. .../WebStack-x.y"
1.145 + print "You can also specify some additional libraries for the application..."
1.146 + print " eg. $CATALINA_HOME/common/lib/activation.jar"
1.147 + print " $CATALINA_HOME/common/lib/mail.jar"
1.148 + sys.exit(1)
1.149 +
1.150 + print "Making application directory..."
1.151 + make_app(sys.argv[1], sys.argv[2], sys.argv[3])
1.152 +
1.153 + if len(sys.argv) > 4:
1.154 + print "Copying additional libraries..."
1.155 + appname = get_appname(sys.argv[1])
1.156 + for library in sys.argv[4:]:
1.157 + library_dir, library_name = os.path.split(library)
1.158 + library_dest = os.path.join(appname, "WEB-INF", "lib", library_name)
1.159 + copy_file(library, library_dest)
1.160 +
1.161 + print "Now copy or move the application directory to your servlet container."
1.162 +
1.163 +# vim: tabstop=4 expandtab shiftwidth=4