# encoding:UTF-8 # Listing 9-15. Example Script: Builder.py ''' So we've discussed a few of the modules that tend to come in handy when writing scripts for Jython. Now we'll put together a simple script to show off what can be done. We’ve chosen to write a script that will help handle the compilation of java files to .class files in a directory, and clean the directory of .class files as a separate task. We will want to be able to create a directory structure, delete the directory structure for a clean build, and of course compile our java source files. ''' import os import sys import glob from javax.tools import (ForwardingJavaFileManager, ToolProvider, DiagnosticCollector,) tasks = {} def task(func): tasks[func.func_name] = func @task def clean(): files = glob.glob('./*.class') #print('files=%s' % files) for file in files: os.unlink(file) @task def compile(): files = glob.glob('./*.java') _log('compiling %s' % files) if not _compile(files): quit() _log('compiled') def _log(message): if options.verbose: print message def _compile(names): compiler = ToolProvider.getSystemJavaCompiler() diagnostic = DiagnosticCollector() manager = compiler.getStandardFileManager(diagnostic, None, None) units = manager.getJavaFileObjectsFromStrings(names) comp_task = compiler.getTask(None, manager, diagnostic, None, None, units) success = comp_task.call() manager.close() return success if __name__ == '__main__': from optparse import OptionParser parser = OptionParser() parser.add_option('-q', '--quiet', action='store_false', dest='verbose', default=True, help="don't print out task messages.") parser.add_option('-p', '--projecthelp', action='store_true', dest='projecthelp', help='print out list of tasks.') (options, args) = parser.parse_args() if options.projecthelp: for task in tasks: print task sys.exit(0) if len(args) < 1: print 'usage: jython builder.py [options] task' sys.exit(1) try: current = tasks[args[0]] except KeyError: print 'task %s not defined.' % args[0] sys.exit(1) current() ''' The script defines a ‘task’ decorator that gathers the names of the functions and puts them in a dictionary. We have an optionparser class that defines two options –projecthelp and –quiet. By default the script logs its actions to standard out. The option –quiet turns this logging off, and –projecthelp lists the available tasks. We have defined two tasks, ‘compile’ and ‘clean.’ The ‘compile’ task globs for all of the .java files in your directory and compiles them. The ‘clean’ task globs for all of the .class files in your directory and deletes them. Do be careful! The .class files are deleted without prompting! So let’s give it a try. If you create a Java class in the same directory as builder.py, say the classic ‘Hello World’ program: ''' ''' D:\CODE\PYTHON\JYTHON>jython builder.py --help Usage: builder.py [options] Options: -h, --help show this help message and exit -q, --quiet don't print out task messages. -p, --projecthelp print out list of tasks. D:\CODE\PYTHON\JYTHON>jython builder.py --projecthelp compile clean D:\CODE\PYTHON\JYTHON>jython builder.py compile compiling ['.\\HelloWorld.java'] compiled D:\CODE\PYTHON\JYTHON>dir *.class 驱动器 D 中的卷是 Technology 卷的序列号是 2412-63C2 D:\CODE\PYTHON\JYTHON 的目录 2014-09-13 17:26 426 HelloWorld.class 1 个文件 426 字节 0 个目录 996,593,664 可用字节 D:\CODE\PYTHON\JYTHON>jython builder.py clean D:\CODE\PYTHON\JYTHON>dir *.class 驱动器 D 中的卷是 Technology 卷的序列号是 2412-63C2 D:\CODE\PYTHON\JYTHON 的目录 找不到文件 D:\CODE\PYTHON\JYTHON>jython builder.py --quiet compile D:\CODE\PYTHON\JYTHON>dir *.class 驱动器 D 中的卷是 Technology 卷的序列号是 2412-63C2 D:\CODE\PYTHON\JYTHON 的目录 2014-09-13 17:27 426 HelloWorld.class 1 个文件 426 字节 0 个目录 996,593,664 可用字节 D:\CODE\PYTHON\JYTHON> ''' ''' This chapter has shown how to create scripts with Jython. We have gone from the most simple one- and two-line scripts to large scripts with lots of optional inputs. We hope this will help you create your own tools to help automate some of the repetition out of your days. '''