micropython

Annotated test.py

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