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, generator, translator 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 debug = "-g" in args 43 44 # Load the program. 45 46 try: 47 start = now = time() 48 49 i = importer.Importer(path, "_cache", verbose) 50 m = i.initialise(filename, reset) 51 success = i.finalise() 52 53 now = stopwatch("Inspection", now) 54 55 # Check for success, indicating missing references otherwise. 56 57 if not success: 58 show_missing(i.missing) 59 if "-exit" in args: 60 sys.exit(1) 61 else: 62 sys.exit(0) 63 64 d = deducer.Deducer(i, "_deduced") 65 d.to_output() 66 67 now = stopwatch("Deduction", now) 68 69 o = optimiser.Optimiser(i, d, "_output") 70 o.to_output() 71 72 now = stopwatch("Optimisation", now) 73 74 g = generator.Generator(i, o, "_generated") 75 g.to_output(debug) 76 77 now = stopwatch("Generation", now) 78 79 t = translator.Translator(i, d, o, "_generated") 80 t.to_output() 81 82 stopwatch("Translation", now) 83 84 # Report any errors. 85 86 except ProcessingError, exc: 87 print exc 88 if "-tb" in args: 89 raise 90 elif "-exit" in args: 91 sys.exit(1) 92 93 except KeyboardInterrupt: 94 if "-exit" in args: 95 sys.exit(2) 96 else: 97 raise 98 99 else: 100 if "-exit" in args: 101 sys.exit(0) 102 103 # vim: tabstop=4 expandtab shiftwidth=4