From 3b934fa9758b63c6d549ae23a244d168fc10c43a Mon Sep 17 00:00:00 2001 From: Mihail Stoyanov Date: Tue, 5 Apr 2016 03:21:42 +0100 Subject: [PATCH] Introduce "target" and "toolchain" which allow setting up default target and toolchain (for compile and export) Gracefully handle lack of -m and -t options for compile when no defaults are set --- neo.py | 146 +++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 112 insertions(+), 34 deletions(-) diff --git a/neo.py b/neo.py index 8399742a..f9df341e 100755 --- a/neo.py +++ b/neo.py @@ -139,6 +139,36 @@ def pquery(command, stdin=None, **kwargs): return stdout +# Defaults config file +def set_cfg(file, var, val): + try: + with open(file) as f: + lines = f.read().splitlines() + except: + lines = [] + + for line in lines: + m = re.match('^([\w+-]+)\=(.*)?$', line) + if m and m.group(1) == var: + lines.remove(line) + + lines += [var+"="+val] + + with open(file, 'w') as f: + f.write('\n'.join(lines) + '\n') + +def get_cfg(file, var): + try: + with open(file) as f: + lines = f.read().splitlines() + except: + lines = [] + + for line in lines: + m = re.match('^([\w+-]+)\=(.*)?$', line) + if m and m.group(1) == var: + return m.group(2) + # Directory navigation @contextlib.contextmanager def cd(newdir): @@ -171,6 +201,7 @@ def staticclass(cls): @staticclass class Hg(object): name = 'hg' + store = '.hg' def clone(url, name=None, hash=None): action("Cloning "+name+" from "+url) @@ -292,6 +323,7 @@ def unignore(file): @staticclass class Git(object): name = 'git' + store = '.git' def clone(url, name=None, hash=None): action("Cloning "+name+" from "+url) @@ -651,7 +683,52 @@ def sync(top=True): with cd(lib.path): sync(False) -# Compile command +@subcommand('ls', 'opt*', + help='List repositories recursively.') +def list_(opt=False, prefix=''): + repo = Repo.fromrepo() + print prefix + repo.name, '(%s)' % (repo.url if "-a" in opt else repo.hash) + + for i, lib in enumerate(repo.libs): + if prefix: + nprefix = prefix[:-3] + ('| ' if prefix[-3] == '|' else ' ') + else: + nprefix = '' + + nprefix += '|- ' if i < len(repo.libs)-1 else '`- ' + + with cd(lib.path): + list_(opt, nprefix) + +@subcommand('status', + help='Show status of nested repositories') +def status(): + repo = Repo.fromrepo() + if repo.scm.dirty(): + print '---', repo.name, '---' + repo.scm.status() + + for lib in repo.libs: + with cd(lib.path): + status() + +# Build system and exporters +@subcommand('target', 'target', + help='Sets default target when compiling and exporting') +def target(target): + repo = Repo.fromrepo() + + file = os.path.join(repo.scm.store, 'neo') + set_cfg(file, 'TARGET', target) + +@subcommand('toolchain', 'toolchain', + help='Sets default toolchain when compiling') +def target(toolchain): + repo = Repo.fromrepo() + + file = os.path.join(repo.scm.store, 'neo') + set_cfg(file, 'TOOLCHAIN', toolchain) + @subcommand('compile', 'args*', help='Compile project using mbed OS build system') def compile(args): @@ -659,6 +736,25 @@ def compile(args): error('mbed-os not found?\n', -1) repo = Repo.fromrepo() + file = os.path.join(repo.scm.store, 'neo') + + target = get_cfg(file, 'TARGET') + if '-m' in args: + target = args[args.index('-m')+1] + args.remove('-m') + args.remove(target) + + if target is None: + error('Please specify compile target using the -m switch or set default target using command "target"', 1) + + toolchain = get_cfg(file, 'TOOLCHAIN') + if '-t' in args: + toolchain = args[args.index('-t')+1] + args.remove('-t') + args.remove(toolchain) + + if toolchain is None: + error('Please specify compile toolchain using the -m switch or set default toolchain using command "toolchain"', 1) macros = [] if os.path.isfile('MACROS.txt'): @@ -669,8 +765,9 @@ def compile(args): env['PYTHONPATH'] = '.' popen(['python', 'mbed-os/tools/make.py'] + list(chain.from_iterable(izip(repeat('-D'), macros))) - + ['--source=%s' % repo.path, - '--build=%s' % os.path.join(repo.path, '.build')] + + ['-t', toolchain, '-m', target, + '--source=%s' % repo.path, + '--build=%s' % os.path.join(repo.path, '.build', target, toolchain)] + args, env=env) @@ -682,6 +779,16 @@ def export(args): error('mbed-os not found?\n', -1) repo = Repo.fromrepo() + file = os.path.join(repo.scm.store, 'neo') + + target = get_cfg(file, 'TARGET') + if '-m' in args: + target = args[args.index('-m')+1] + args.remove('-m') + args.remove(target) + + if target is None: + error('Please specify export target using the -m switch or set default target using command "target"', 1) macros = [] if os.path.isfile('MACROS.txt'): @@ -690,41 +797,12 @@ def export(args): env = os.environ.copy() env['PYTHONPATH'] = '.' - popen(['python', 'mbed-os/tools/project.py', - '--source=%s' % repo.path] + popen(['python', 'mbed-os/tools/project.py'] + list(chain.from_iterable(izip(repeat('-D'), macros))) + + ['-m', target, '--source=%s' % repo.path] + args, env=env) -# Helpful status commands -@subcommand('ls', 'opt*', - help='List repositories recursively.') -def list_(opt=False, prefix=''): - repo = Repo.fromrepo() - print prefix + repo.name, '(%s)' % (repo.url if "-a" in opt else repo.hash) - - for i, lib in enumerate(repo.libs): - if prefix: - nprefix = prefix[:-3] + ('| ' if prefix[-3] == '|' else ' ') - else: - nprefix = '' - - nprefix += '|- ' if i < len(repo.libs)-1 else '`- ' - - with cd(lib.path): - list_(opt, nprefix) - -@subcommand('status', - help='Show status of nested repositories') -def status(): - repo = Repo.fromrepo() - if repo.scm.dirty(): - print '---', repo.name, '---' - repo.scm.status() - - for lib in repo.libs: - with cd(lib.path): - status() # Parse/run command args, remainder = parser.parse_known_args()