Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed-os
neo.py
- Committer:
- Christopher Haster
- Date:
- 2016-03-29
- Revision:
- 7:caeff556648e
- Parent:
- 6:622a477ef55c
- Child:
- 8:1a673572accd
File content as of revision 7:caeff556648e:
#!/usr/bin/env python
import argparse
import sys
import re
import subprocess
import os
import contextlib
import collections
# Subparser handling
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers()
def subcommand(name, *args, **kwargs):
def subcommand(command):
subparser = subparsers.add_parser(name, **kwargs)
for arg in args:
if arg.endswith('?'):
subparser.add_argument(arg.strip('?'), nargs='?')
else:
subparser.add_argument(arg)
def thunk(parsed_args):
ordered_args = [vars(parsed_args)[arg.strip('?')] for arg in args]
return command(*ordered_args)
subparser.set_defaults(command=thunk)
return command
return subcommand
# Process execution
class ProcessException(Exception):
pass
def popen(command, stdin=None):
# print for debugging
print ' '.join(command)
proc = subprocess.Popen(command)
if proc.wait() != 0:
raise ProcessException(proc.returncode)
def pquery(command, stdin=None):
proc = subprocess.Popen(command, stdout=subprocess.PIPE)
stdout, _ = proc.communicate(stdin)
if proc.returncode != 0:
raise ProcessException(proc.returncode)
return stdout
# Directory navigation
@contextlib.contextmanager
def cd(newdir):
prevdir = os.getcwd()
os.chdir(newdir)
try:
yield
finally:
os.chdir(prevdir)
def iterlibs(dir=None):
for file in os.listdir(dir or os.getcwd()):
if file.startswith('.'):
continue
elif os.path.isfile(file) and file.endswith('.lib'):
with open(file) as f:
yield f.read().strip(), file[:-4]
def savelib(repo):
print repo.name, '->', repo.url
with open(repo.lib, 'w') as f:
f.write(repo.url + '\n')
# Handling for multiple version controls
class Repo(object):
def __init__(self, path=None):
self.path = path or os.getcwd()
self.name = os.path.basename(self.path)
self.update()
@classmethod
def fromurl(cls, url, name=None):
repo = cls.__new__(cls)
m = re.match('^(.*/([+a-zA-Z0-9_-]+)/?)(?:#(.*))?$', url)
repo.repo = m.group(1)
repo.name = name or m.group(2)
repo.hash = m.group(3)
repo.path = os.path.join(os.getcwd(), repo.name)
return repo
@property
def lib(self):
return self.path + '.lib'
@property
def url(self):
if self.repo:
if self.hash:
return self.repo + '#' + self.hash
else:
return self.repo
def update(self):
if os.path.isdir(self.path):
self.type = self.gettype()
self.hash = self.gethash()
if os.path.isfile(self.lib):
with open(self.lib) as f:
temp = Repo.fromurl(f.read())
self.repo = temp.repo
def gettype(self):
for type, dir in [('git', '.git'), ('hg', '.hg')]:
if os.path.isdir(os.path.join(self.path, dir)):
return type
def gethash(self):
with cd(self.path):
if self.type == 'git':
return pquery(['git', 'rev-parse', '--short', 'HEAD']).strip()
elif self.type == 'hg':
return pquery(['hg', 'id', '-i']).strip()
# Clone command
@subcommand('import', 'url', 'name?',
help='recursively import a repository')
def import_(url, name=None):
repo = Repo.fromurl(url, name)
for type in ['git', 'hg']:
try:
popen([type, 'clone', repo.repo, repo.path])
break
except:
pass
repo.type = repo.gettype()
if repo.hash:
with cd(repo.path):
popen([repo.type, 'checkout', repo.hash])
repo.update()
with cd(repo.path):
for url, lib in iterlibs():
import_(url, lib)
# Deploy command
@subcommand('deploy',
help='recursively import libraries in current directory')
def deploy():
for url, lib in iterlibs():
import_(url, lib)
# Install/uninstall command
@subcommand('add', 'url', 'name?'
help='add a library to the current repository')
def add(url, name):
cwd = Repo()
repo = Repo.fromurl(url, name)
import_(url)
repo.update()
savelib(repo)
popen([cwd.type, 'add', repo.lib])
@subcommand('remove', 'library',
help='remove a library from the current repository folder')
def remove(library):
cwd = Repo()
repo = Repo(library)
popen([cwd.type, 'rm', '-f', repo.lib])
if cwd.type == 'hg':
popen(['rm', '-f', repo.lib])
popen(['rm', '-rf', repo.path])
# Synch command
@subcommand('synch',
help='synchronize lib files')
def synch():
cwd = Repo()
for url, lib in iterlibs():
repo = Repo.fromurl(url, lib)
repo.update()
if lib == repo.url:
continue
savelib(repo)
if cwd.type == 'git':
popen([cwd.type, 'add', repo.lib])
# Compile command
@subcommand('compile', 'toolchain', 'target',
help='compile project using workspace_tools')
def compile(toolchain, target):
cwd = Repo()
popen(['python', 'mbed-os/tools/make.py',
'--source=%s' % cwd.path,
'--build=%s' % os.path.join(cwd.path, '.build'),
'-D', 'YOTTA_CFG', '-D', 'YOTTA_CONFIG',
'-D', 'TARGET_LIKE_MBED', '-D', 'TARGET_LIKE_CORTEX_M4',
'-t', toolchain.upper(), '-m', target.upper(), '-j', '0'])
# Parse/run command
args = parser.parse_args()
status = args.command(args)
sys.exit(status or 0)