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 105 print "Object table: ot = %r" % ot 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