1 #!/usr/bin/env jython 2 3 "A simple Jython-based installer for the Web applications." 4 5 import os 6 7 def copy_file(source, destination): 8 9 """ 10 Copy a file from 'source' to 'destination'. Note that 'destination' must 11 include the name of the file - it cannot be a directory name. 12 """ 13 14 print "Copying", source, "to", destination 15 16 # Do things by the book, since Jython/Java won't copy the file contents in 17 # all cases presumably due to finalisation issues. 18 19 f = open(source, "rb") 20 s = f.read() 21 f.close() 22 23 f = open(destination, "wb") 24 f.write(s) 25 f.close() 26 27 def recurse(dirs_and_files, dirname, names): 28 29 """ 30 A recursive directory and file collector for os.path.walk. The provided 31 'dirs_and_files' list must contain two lists (one for directory names, one 32 for filenames). The 'dirname' and 'names' parameters are supplied by the 33 os.path.walk mechanism. 34 """ 35 36 if dirname.endswith("/CVS"): 37 return 38 dirs_and_files[0].append(dirname) 39 for name in names: 40 if os.path.isfile(os.path.join(dirname, name)): 41 dirs_and_files[1].append(os.path.join(dirname, name)) 42 43 def copy_directory(source, destination): 44 45 """ 46 Copy a directory found at 'source' in the filesystem to the 'destination'. 47 Note that 'destination' is the parent directory of the newly created 48 directory. 49 """ 50 51 # Remove trailing directory separators. 52 53 source = os.path.normpath(source) 54 prefix = os.path.split(source)[0] 55 dirs_and_files = [[], []] 56 os.path.walk(source, recurse, dirs_and_files) 57 58 for dirname in dirs_and_files[0]: 59 60 # Remove the prefix from the name and create the object under the destination. 61 # NOTE: Joining "" to the path in Jython doesn't add the path separator. 62 63 new_dirname = dirname[len(os.path.join(prefix, "x")) - 1:] 64 print "Making", new_dirname, "under", destination 65 os.mkdir(os.path.join(destination, new_dirname)) 66 67 for filename in dirs_and_files[1]: 68 69 # Remove the prefix from the name and create the object under the destination. 70 # NOTE: Joining "" to the path in Jython doesn't add the path separator. 71 72 new_filename = filename[len(os.path.join(prefix, "x")) - 1:] 73 copy_file(filename, os.path.join(destination, new_filename)) 74 75 def get_appname(handler): 76 return os.path.split(os.path.splitext(handler)[0])[1] 77 78 def make_app(handler, root_resources, webstack_tools_home, webstack_package_home, 79 jython_cachedir, web_xml_template_name, packages, libraries): 80 81 """ 82 Make the application directory from the given 'handler' and 83 'root_resources', the 'webstack_tools_home' where the tools directory is 84 found, the 'webstack_package_home' where the WebStack package is found, the 85 'jython_cachedir' where Jython classes are cached, the deployment descriptor 86 with the given 'web_xml_template_name', and the specified 'packages' 87 (locations of application packages) and 'libraries' (locations of required 88 library files). 89 """ 90 91 appname = get_appname(handler) 92 print "Making", appname 93 94 os.mkdir(appname) 95 os.mkdir(os.path.join(appname, "WEB-INF")) 96 os.mkdir(os.path.join(appname, "WEB-INF", "jython")) 97 os.mkdir(os.path.join(appname, "WEB-INF", "lib")) 98 99 # Copy the Jython runtime. 100 101 jython_home = sys.exec_prefix 102 copy_file(os.path.join(jython_home, "jython.jar"), 103 os.path.join(appname, "WEB-INF", "lib", "jython.jar")) 104 105 # Copy the special PyServlet classes. 106 107 #copy_directory(os.path.join(webstack_tools_home, "tools", "JavaServlet", "classes"), 108 # os.path.join(appname, "WEB-INF")) 109 110 copy_file(os.path.join(webstack_tools_home, "tools", "JavaServlet", "classes", "webstack-pyservlet.jar"), 111 os.path.join(appname, "WEB-INF", "lib", "webstack-pyservlet.jar")) 112 113 # Copy the WebStack package. 114 115 copy_directory(os.path.join(webstack_package_home, "WebStack"), 116 os.path.join(appname, "WEB-INF", "jython")) 117 118 # Copy the application packages. 119 120 for appdir in packages: 121 copy_directory(appdir, os.path.join(appname, "WEB-INF", "jython")) 122 123 # Copy the libraries. 124 125 if libraries: 126 for library in libraries: 127 library_dir, library_name = os.path.split(library) 128 library_dest = os.path.join(appname, "WEB-INF", "lib", library_name) 129 copy_file(library, library_dest) 130 131 # Copy the handler. 132 133 handler_filename = os.path.split(handler)[1] 134 copy_file(handler, os.path.join(appname, handler_filename)) 135 136 # Copy the root resources. 137 138 for root_resource in root_resources: 139 root_resource_filename = os.path.split(root_resource)[1] 140 copy_file(root_resource, os.path.join(appname, root_resource_filename)) 141 142 # Find additional Jython paths. 143 144 jython_paths = [] 145 for path in sys.path: 146 if path.startswith(jython_home) and path != os.path.join(jython_home, "Lib"): 147 jython_paths.append(path) 148 149 jython_path = ":".join(jython_paths) 150 151 # Create the cache directory if necessary. 152 153 if not os.path.exists(jython_cachedir): 154 os.mkdir(jython_cachedir) 155 156 # Configure the deployment descriptor. 157 158 f = open(os.path.join(webstack_tools_home, "tools", "JavaServlet", web_xml_template_name)) 159 web_xml = f.read() 160 f.close() 161 web_xml = web_xml % (jython_home, jython_path, jython_cachedir, handler_filename) 162 163 # Write the deployment descriptor. 164 165 f = open(os.path.join(appname, "WEB-INF", "web.xml"), "w") 166 f.write(web_xml) 167 f.close() 168 169 if __name__ == "__main__": 170 import sys 171 if len(sys.argv) < 8: 172 print "Syntax:" 173 print 174 print "jython webstack_java_build.py <handler> [ <resource>... ] \\" 175 print " --webstack <home> <tools> <cachedir> <descriptor> <app> \\" 176 print " [ --libraries <library>... ]" 177 print 178 print "Please specify..." 179 print 180 print "The location of the application handler. For example:" 181 print 182 print " .../WebStack-x.y/examples/JavaServlet/SimpleApp.py" 183 print 184 print "Any other top-level resources, such as JSP templates. For example:" 185 print 186 print " .../WebStack-x.y/examples/JavaServlet/test.jsp" 187 print 188 print "After the --webstack marker, the details of the WebStack distribution" 189 print "are required, such as..." 190 print 191 print "The location of the WebStack package distribution. For example:" 192 print 193 print " .../WebStack-x.y" 194 print " /usr/lib/python2.4/site-packages" 195 print 196 print "The location of the WebStack tools have been installed. For example:" 197 print 198 print " .../WebStack-x.y" 199 print " /usr/share/doc/python2.4-webstack" 200 print 201 print "The location of the Jython cache directory. For example:" 202 print 203 print " /home/paulb/.jython-cache" 204 print 205 print "The name of the deployment descriptor template. For example:" 206 print 207 print " web.xml" 208 print " jsp-web.xml" 209 print " protected-web.xml" 210 print 211 print "The location of the application packages. For example:" 212 print 213 print " .../WebStack-x.y/examples/Common/Simple" 214 print 215 print "You can also specify some additional libraries for the application" 216 print "after a special '--libraries' marker. For example:" 217 print 218 print " $CATALINA_HOME/common/lib/activation.jar" 219 print " $CATALINA_HOME/common/lib/mail.jar" 220 print 221 print "With Apache Tomcat 4.1.x, activation.jar and mail.jar are usually" 222 print "required." 223 sys.exit(1) 224 225 webstack_index = sys.argv.index("--webstack") 226 227 try: 228 libraries_index = sys.argv.index("--libraries") 229 except ValueError: 230 libraries_index = len(sys.argv) 231 232 print "Handler, root resources are..." 233 handler, root_resources = sys.argv[1], sys.argv[2:webstack_index] 234 print handler, root_resources 235 236 print "WebStack distribution details are..." 237 webstack_package_home, webstack_tools_home, jython_cachedir, web_xml = sys.argv[webstack_index+1:webstack_index+5] 238 print webstack_package_home, webstack_tools_home 239 240 print "Packages..." 241 packages = sys.argv[webstack_index+5:libraries_index] 242 print packages 243 244 print "Libraries..." 245 libraries = sys.argv[libraries_index+1:] 246 print libraries 247 248 print "Making application directory..." 249 make_app(handler, root_resources, webstack_tools_home, webstack_package_home, jython_cachedir, web_xml, packages, libraries) 250 251 print "Now copy or move the application directory to your servlet container." 252 253 # vim: tabstop=4 expandtab shiftwidth=4