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 from os.path import exists, join 24 import micropython 25 26 def parse_optimisations(args): 27 28 "Parse 'args' for optimisation flags." 29 30 if "-omax" in args: 31 requested_optimisations = micropython.Program.supported_optimisations 32 else: 33 requested_optimisations = [] 34 for arg in args: 35 if arg.startswith("-o"): 36 for arg_part in arg[2:].split(","): 37 requested_optimisations.append(arg_part) 38 39 return requested_optimisations 40 41 def show_optimisations(): 42 43 "Show available optimisation flags." 44 45 return ",".join(micropython.Program.supported_optimisations) 46 47 def program(path, requested_optimisations, verbose=0): 48 49 """ 50 Return a program object for the given module search 'path' and 51 'requested_optimisations'. 52 """ 53 54 i = micropython.Importer(path, verbose, requested_optimisations) 55 p = micropython.Program(i, requested_optimisations) 56 57 for d in path: 58 builtins = join(d, "builtins.py") 59 if exists(builtins): 60 i.load_from_file(builtins, "__builtins__") 61 break 62 return p 63 64 # Convenience functions. 65 66 def get_program(path, args): 67 68 """ 69 Return the program object for the given module search 'path' and command 70 'args'. 71 """ 72 73 requested_optimisations = parse_optimisations(args) 74 return program(path, requested_optimisations, "-v" in args) 75 76 # vim: tabstop=4 expandtab shiftwidth=4