1 #!/usr/bin/env python 2 3 """ 4 Command option processing and other utilities for compiling and testing 5 programs. 6 """ 7 8 import micropython 9 10 def parse_optimisations(args): 11 12 "Parse 'args' for optimisation flags." 13 14 if "-omax" in args: 15 requested_optimisations = micropython.Program.supported_optimisations 16 else: 17 requested_optimisations = [] 18 for arg in args: 19 if arg.startswith("-o"): 20 for arg_part in arg[2:].split(","): 21 requested_optimisations.append(arg_part) 22 23 return requested_optimisations 24 25 def program(filename, path, requested_optimisations, verbose=0): 26 27 """ 28 Return the program object for the program specified by the given 'filename', 29 module search 'path' and 'requested_optimisations'. 30 """ 31 32 i = micropython.Importer(path, verbose, requested_optimisations) 33 p = micropython.Program(i, requested_optimisations) 34 35 i.load_from_file("lib/builtins.py", "__builtins__") 36 37 if filename is not None: 38 i.load_from_file(filename) 39 40 return p 41 42 # Convenience functions. 43 44 def get_program(filename, path, args): 45 46 """ 47 Return the program object for the program specified by the given 'filename', 48 module search 'path' and command 'args'. 49 """ 50 51 requested_optimisations = parse_optimisations(args) 52 return program(filename, path, requested_optimisations, "-v" in args) 53 54 # vim: tabstop=4 expandtab shiftwidth=4