1 #!/usr/bin/env python 2 3 import micropython.cmd 4 import micropython.report 5 from micropython.graph import get_graph 6 import rsvp 7 import sys 8 import os 9 10 libdirs = [ 11 os.path.join(os.path.split(__file__)[0], "lib"), 12 "/usr/share/micropython/lib" 13 ] 14 15 def show_code(code): 16 for i, x in enumerate(code): 17 print i, x 18 19 def show_table_usage(raw_table, slice_size=100): 20 for x in xrange(0, len(raw_table), slice_size): 21 table_slice = raw_table[x:x+slice_size] 22 print "%6d" % (len(table_slice) - table_slice.count(None)), \ 23 "".join(entry and "#" or "_" for entry in table_slice) 24 25 def attrs(obj): 26 for name, attr in obj.items(): 27 print name, attr 28 29 def reset(): 30 global rm 31 rm = rsvp.machine(p) 32 33 # Main program. 34 35 if __name__ == "__main__": 36 args = sys.argv[2:] 37 path = libdirs + sys.path[:] 38 39 if "--help" in sys.argv: 40 print "Optimisations:" 41 print micropython.cmd.show_optimisations() 42 sys.exit(1) 43 44 if len(sys.argv) > 1 and sys.argv[1] != "-": 45 filename = os.path.abspath(sys.argv[1]) 46 path.append(os.path.split(filename)[0]) 47 else: 48 filename = None 49 50 # Load the program. 51 52 try: 53 p = micropython.cmd.get_program(path, args) 54 i = p.get_importer() 55 56 if filename is None or filename == "-": 57 print "Loading module micropython ..." 58 m = i.load("micropython") 59 else: 60 print "Loading from", filename, "..." 61 m = i.load_from_file(filename) 62 63 p.finalise() 64 b = i.get_module("__builtins__") 65 66 # Make a report. 67 68 if "-d" in args: 69 try: 70 directory = args[args.index("-d") + 1] 71 except IndexError: 72 print "No directory specified. Not generating report." 73 else: 74 print "Generating report in", directory 75 micropython.report.report(p, directory) 76 77 if "-G" in args: 78 try: 79 graph = args[args.index("-G") + 1] 80 except IndexError: 81 print "No file specified. Not generating graph." 82 else: 83 print "Generating graph as", graph 84 f = open(graph, "w") 85 try: 86 get_graph(p, f, filename) 87 finally: 88 f.close() 89 90 # Build the program. 91 92 if "-m" in args or "-t" in args: 93 rm = rsvp.machine(p, debug=("-g" in args), abort_upon_exception=("-x" in args)) 94 95 if "-t" in args: 96 success = rm.test(m) 97 print "Test successful?", success 98 99 print "RSVP machine: rm = %r" % rm 100 101 ot = p.get_object_table() 102 pt = p.get_parameter_table() 103 104 print "Object table: ot = %r" % ot 105 print "Parameter table: pt = %r" % pt 106 print "Importer: i = %r" % i 107 print "Program: p = %r" % p 108 print "Module: m = %r" % m 109 print "Built-ins: b = %r" % b 110 111 # Report any errors. 112 113 except micropython.ProcessingError, exc: 114 print repr(exc) 115 if "-tb" in args: 116 raise 117 elif "-exit" in args: 118 sys.exit(1) 119 120 except KeyboardInterrupt: 121 if "-exit" in args: 122 sys.exit(2) 123 else: 124 raise 125 126 else: 127 if "-exit" in args and "-t" in args: 128 if success: 129 sys.exit(0) 130 else: 131 sys.exit(1) 132 133 # vim: tabstop=4 expandtab shiftwidth=4