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 sys 9 import os 10 11 libdirs = [ 12 os.path.join(os.path.split(__file__)[0], "lib"), 13 "/usr/share/micropython/lib" 14 ] 15 16 def show_warnings(attribute_usage_failures): 17 failures = list(attribute_usage_failures) 18 failures.sort() 19 for unit_name, name, attrnames, all_attributes in failures: 20 attrnames = list(attrnames) 21 attrnames.sort() 22 print >>sys.stderr, "%s: Name %r with %s attributes %r" % ( 23 unit_name, name, all_attributes and "all" or "any", ", ".join(attrnames)) 24 25 # Main program. 26 27 if __name__ == "__main__": 28 args = sys.argv[2:] 29 path = libdirs + sys.path[:] 30 31 if len(sys.argv) > 1 and sys.argv[1] != "-": 32 filename = os.path.abspath(sys.argv[1]) 33 path.append(os.path.split(filename)[0]) 34 else: 35 filename = None 36 37 # Load the program. 38 39 try: 40 p = micropython.cmd.get_program(path, args) 41 i = p.get_importer() 42 43 if filename is None or filename == "-": 44 print "Loading module micropython ..." 45 m = i.load("micropython") 46 else: 47 print "Loading from", filename, "..." 48 m = i.load_from_file(filename) 49 50 p.finalise() 51 52 # Show warnings. 53 54 if "-w" in sys.argv: 55 print >>sys.stderr 56 print >>sys.stderr, "Warnings:" 57 show_warnings(i.attribute_usage_failures) 58 print >>sys.stderr 59 60 # Make the builtins module available through a variable. 61 62 b = i.get_module("__builtins__") 63 64 # Perform deductions. 65 66 micropython.deduce.deduce(p) 67 68 # Make a report. 69 70 if "-d" in args: 71 try: 72 directory = args[args.index("-d") + 1] 73 except IndexError: 74 print "No directory specified. Not generating report." 75 else: 76 print "Generating report in", directory 77 micropython.report.report(p, directory) 78 79 # Make the syspython representation. 80 81 if "-s" in args: 82 try: 83 directory = args[args.index("-s") + 1] 84 except IndexError: 85 print "No directory specified. Not generating syspython program." 86 else: 87 print "Generating syspython program in", directory 88 micropython.syspython.translate(p, directory) 89 90 if "-G" in args: 91 try: 92 graph = args[args.index("-G") + 1] 93 except IndexError: 94 print "No file specified. Not generating graph." 95 else: 96 print "Generating graph as", graph 97 f = open(graph, "w") 98 try: 99 get_graph(p, f, filename) 100 finally: 101 f.close() 102 103 ot = p.get_object_table() 104 pt = p.get_parameter_table() 105 106 print "Object table: ot = %r" % ot 107 print "Parameter table: pt = %r" % pt 108 print "Importer: i = %r" % i 109 print "Program: p = %r" % p 110 print "Module: m = %r" % m 111 print "Built-ins: b = %r" % b 112 113 # Report any errors. 114 115 except micropython.ProcessingError, exc: 116 print repr(exc) 117 if "-tb" in args: 118 raise 119 elif "-exit" in args: 120 sys.exit(1) 121 122 except KeyboardInterrupt: 123 if "-exit" in args: 124 sys.exit(2) 125 else: 126 raise 127 128 else: 129 if "-exit" in args and "-t" in args: 130 if success: 131 sys.exit(0) 132 else: 133 sys.exit(1) 134 135 # vim: tabstop=4 expandtab shiftwidth=4