1 #!/usr/bin/env python 2 3 """ 4 Command option processing and other utilities for compiling and testing 5 programs. 6 7 Copyright (C) 2009 Paul Boddie <paul@boddie.org.uk> 8 9 This program is free software; you can redistribute it and/or modify it under 10 the terms of the GNU General Public License as published by the Free Software 11 Foundation; either version 3 of the License, or (at your option) any later 12 version. 13 14 This program is distributed in the hope that it will be useful, but WITHOUT 15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 16 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 17 details. 18 19 You should have received a copy of the GNU General Public License along with 20 this program. If not, see <http://www.gnu.org/licenses/>. 21 """ 22 23 import micropython 24 25 def parse_optimisations(args): 26 27 "Parse 'args' for optimisation flags." 28 29 if "-omax" in args: 30 requested_optimisations = micropython.Program.supported_optimisations 31 else: 32 requested_optimisations = [] 33 for arg in args: 34 if arg.startswith("-o"): 35 for arg_part in arg[2:].split(","): 36 requested_optimisations.append(arg_part) 37 38 return requested_optimisations 39 40 def program(filename, path, requested_optimisations, verbose=0): 41 42 """ 43 Return the program object for the program specified by the given 'filename', 44 module search 'path' and 'requested_optimisations'. 45 """ 46 47 i = micropython.Importer(path, verbose, requested_optimisations) 48 p = micropython.Program(i, requested_optimisations) 49 50 i.load_from_file("lib/builtins.py", "__builtins__") 51 52 if filename is not None: 53 i.load_from_file(filename) 54 55 return p 56 57 # Convenience functions. 58 59 def get_program(filename, path, args): 60 61 """ 62 Return the program object for the program specified by the given 'filename', 63 module search 'path' and command 'args'. 64 """ 65 66 requested_optimisations = parse_optimisations(args) 67 return program(filename, path, requested_optimisations, "-v" in args) 68 69 # vim: tabstop=4 expandtab shiftwidth=4