Skip to content

Commit

Permalink
Introduce "target" and "toolchain" which allow setting up default tar…
Browse files Browse the repository at this point in the history
…get and toolchain (for compile and export)

Gracefully handle lack of -m and -t options for compile when no defaults are set
  • Loading branch information
screamerbg committed Apr 5, 2016
1 parent 8c24d8d commit 3b934fa
Showing 1 changed file with 112 additions and 34 deletions.
146 changes: 112 additions & 34 deletions neo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -651,14 +683,78 @@ 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):
if not os.path.isdir('mbed-os'):
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'):
Expand All @@ -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)

Expand All @@ -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'):
Expand All @@ -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()
Expand Down

0 comments on commit 3b934fa

Please sign in to comment.