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 show_warnings(attribute_usage_failures): 26 failures = list(attribute_usage_failures) 27 failures.sort() 28 for unit_name, name, attrnames, all_attributes in failures: 29 attrnames = list(attrnames) 30 attrnames.sort() 31 print >>sys.stderr, "%s: Name %r with %s attributes %r" % ( 32 unit_name, name, all_attributes and "all" or "any", ", ".join(attrnames)) 33 34 def attrs(obj): 35 for name, attr in obj.items(): 36 print name, attr 37 38 def reset(): 39 global rm 40 rm = rsvp.machine(p) 41 42 # Main program. 43 44 if __name__ == "__main__": 45 args = sys.argv[2:] 46 path = libdirs + sys.path[:] 47 48 if "--help" in sys.argv: 49 print "Optimisations:" 50 print micropython.cmd.show_optimisations() 51 sys.exit(1) 52 53 if len(sys.argv) > 1 and sys.argv[1] != "-": 54 filename = os.path.abspath(sys.argv[1]) 55 path.append(os.path.split(filename)[0]) 56 else: 57 filename = None 58 59 # Load the program. 60 61 try: 62 p = micropython.cmd.get_program(path, args) 63 i = p.get_importer() 64 65 if filename is None or filename == "-": 66 print "Loading module micropython ..." 67 m = i.load("micropython") 68 else: 69 print "Loading from", filename, "..." 70 m = i.load_from_file(filename) 71 72 p.finalise() 73 74 # Show warnings. 75 76 if "-w" in sys.argv: 77 print >>sys.stderr 78 print >>sys.stderr, "Warnings:" 79 show_warnings(i.attribute_usage_failures) 80 print >>sys.stderr 81 82 # Make the builtins module available through a variable. 83 84 b = i.get_module("__builtins__") 85 86 # Make a report. 87 88 if "-d" in args: 89 try: 90 directory = args[args.index("-d") + 1] 91 except IndexError: 92 print "No directory specified. Not generating report." 93 else: 94 print "Generating report in", directory 95 micropython.report.report(p, directory) 96 97 if "-G" in args: 98 try: 99 graph = args[args.index("-G") + 1] 100 except IndexError: 101 print "No file specified. Not generating graph." 102 else: 103 print "Generating graph as", graph 104 f = open(graph, "w") 105 try: 106 get_graph(p, f, filename) 107 finally: 108 f.close() 109 110 # Build the program. 111 112 if "-m" in args or "-t" in args: 113 rm = rsvp.machine(p, debug=("-g" in args), abort_upon_exception=("-x" in args)) 114 115 if "-t" in args: 116 success = rm.test(m) 117 print "Test successful?", success 118 119 print "RSVP machine: rm = %r" % rm 120 121 ot = p.get_object_table() 122 pt = p.get_parameter_table() 123 124 print "Object table: ot = %r" % ot 125 print "Parameter table: pt = %r" % pt 126 print "Importer: i = %r" % i 127 print "Program: p = %r" % p 128 print "Module: m = %r" % m 129 print "Built-ins: b = %r" % b 130 131 # Report any errors. 132 133 except micropython.ProcessingError, exc: 134 print repr(exc) 135 if "-tb" in args: 136 raise 137 elif "-exit" in args: 138 sys.exit(1) 139 140 except KeyboardInterrupt: 141 if "-exit" in args: 142 sys.exit(2) 143 else: 144 raise 145 146 else: 147 if "-exit" in args and "-t" in args: 148 if success: 149 sys.exit(0) 150 else: 151 sys.exit(1) 152 153 # vim: tabstop=4 expandtab shiftwidth=4