1 #!/usr/bin/env python 2 3 from errors import * 4 from os.path import abspath, exists, join, split 5 from time import time 6 import importer, deducer, optimiser 7 import sys 8 9 libdirs = [ 10 join(split(__file__)[0], "lib"), 11 "/usr/share/lichen/lib" 12 ] 13 14 def load_module(filename, module_name): 15 for libdir in libdirs: 16 path = join(libdir, filename) 17 if exists(path): 18 return i.load_from_file(path, module_name) 19 return None 20 21 def show_missing(missing): 22 missing = list(missing) 23 missing.sort() 24 for module_name, name in missing: 25 print >>sys.stderr, "Module %s references an unknown object: %s" % (module_name, name) 26 27 def stopwatch(activity, now): 28 print >>sys.stderr, "%s took %.2f seconds" % (activity, time() - now) 29 return time() 30 31 # Main program. 32 33 if __name__ == "__main__": 34 args = sys.argv[2:] 35 path = libdirs + sys.path[:] 36 37 filename = abspath(sys.argv[1]) 38 path.append(split(filename)[0]) 39 40 verbose = "-v" in args 41 reset = "-r" in args 42 43 # Load the program. 44 45 try: 46 start = now = time() 47 48 i = importer.Importer(path, "_cache", verbose) 49 m = i.initialise(filename, reset) 50 success = i.finalise() 51 52 now = stopwatch("Inspection", now) 53 54 # Check for success, indicating missing references otherwise. 55 56 if not success: 57 show_missing(i.missing) 58 if "-exit" in args: 59 sys.exit(1) 60 else: 61 sys.exit(0) 62 63 d = deducer.Deducer(i, "_deduced") 64 d.to_output() 65 66 now = stopwatch("Deduction", now) 67 68 o = optimiser.Optimiser(i, d, "_output") 69 o.to_output() 70 71 now = stopwatch("Optimisation", now) 72 73 # Report any errors. 74 75 except ProcessingError, exc: 76 print exc 77 if "-tb" in args: 78 raise 79 elif "-exit" in args: 80 sys.exit(1) 81 82 except KeyboardInterrupt: 83 if "-exit" in args: 84 sys.exit(2) 85 else: 86 raise 87 88 else: 89 if "-exit" in args: 90 sys.exit(0) 91 92 # vim: tabstop=4 expandtab shiftwidth=4