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