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