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, 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 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 t = translator.Translator(i, d, o, "_generated") 74 t.to_output() 75 76 stopwatch("Translation", now) 77 78 # Report any errors. 79 80 except ProcessingError, exc: 81 print exc 82 if "-tb" in args: 83 raise 84 elif "-exit" in args: 85 sys.exit(1) 86 87 except KeyboardInterrupt: 88 if "-exit" in args: 89 sys.exit(2) 90 else: 91 raise 92 93 else: 94 if "-exit" in args: 95 sys.exit(0) 96 97 # vim: tabstop=4 expandtab shiftwidth=4