Skip to content
This repository was archived by the owner on Jan 15, 2021. It is now read-only.

Commit 63651da

Browse files
committed
Merge pull request #768 from marshall/moreScriptVars
Add $build_dir and $target_dir variables for target scripts
2 parents 4aa51d6 + 8bc0844 commit 63651da

File tree

2 files changed

+50
-21
lines changed

2 files changed

+50
-21
lines changed

yotta/lib/target.py

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,17 @@ def findProgram(self, builddir, program):
591591
logging.error('could not find program "%s" to debug' % program)
592592
return None
593593

594+
def buildProgEnvAndVars(self, program, build_dir):
595+
prog_env = os.environ.copy()
596+
prog_env['YOTTA_PROGRAM'] = _encodePathForEnv(program)
597+
prog_env['YOTTA_BUILD_DIR'] = _encodePathForEnv(build_dir)
598+
prog_env['YOTTA_TARGET_DIR'] = _encodePathForEnv(self.path)
599+
prog_vars = dict(program=program,
600+
build_dir=build_dir,
601+
target_dir=self.path)
602+
603+
return (prog_env, prog_vars)
604+
594605
@fsutils.dropRootPrivs
595606
def start(self, builddir, program, forward_args):
596607
''' Launch the specified program. Uses the `start` script if specified
@@ -603,20 +614,18 @@ def start(self, builddir, program, forward_args):
603614
if prog_path is None:
604615
return
605616

606-
env = os.environ.copy()
607-
env['YOTTA_PROGRAM'] = _encodePathForEnv(prog_path)
608-
617+
start_env, start_vars = self.buildProgEnvAndVars(prog_path, builddir)
609618
if self.getScript('start'):
610619
cmd = [
611-
os.path.expandvars(string.Template(x).safe_substitute(program=prog_path))
620+
os.path.expandvars(string.Template(x).safe_substitute(**start_vars))
612621
for x in self.getScript('start')
613622
] + forward_args
614623
else:
615624
cmd = shlex.split('./' + prog_path) + forward_args
616625

617626
logger.debug('starting program: %s', cmd)
618627
child = subprocess.Popen(
619-
cmd, cwd = builddir, env = env
628+
cmd, cwd = builddir, env = start_env
620629
)
621630
child.wait()
622631
if child.returncode:
@@ -663,16 +672,15 @@ def _debugWithScript(self, builddir, program):
663672
if prog_path is None:
664673
return
665674

666-
env = os.environ.copy()
667-
env['YOTTA_PROGRAM'] = _encodePathForEnv(prog_path)
675+
debug_env, debug_vars = self.buildProgEnvAndVars(prog_path, builddir)
668676

669677
cmd = [
670-
os.path.expandvars(string.Template(x).safe_substitute(program=prog_path))
678+
os.path.expandvars(string.Template(x).safe_substitute(**debug_vars))
671679
for x in self.getScript('debug')
672680
]
673681
logger.debug('starting debugger: %s', cmd)
674682
child = subprocess.Popen(
675-
cmd, cwd = builddir, env = env
683+
cmd, cwd = builddir, env = debug_env
676684
)
677685
child.wait()
678686
if child.returncode:
@@ -749,11 +757,14 @@ def test(self, test_dir, module_dir, test_command, filter_command, forward_args)
749757
# to use filter scripts shipped with the module)
750758
test_command = './' + test_command
751759
test_script = self.getScript('test')
760+
761+
test_env, test_vars = self.buildProgEnvAndVars(os.path.abspath(os.path.join(test_dir, test_command)), test_dir)
762+
752763
if test_script is None:
753764
cmd = shlex.split(test_command) + forward_args
754765
else:
755766
cmd = [
756-
os.path.expandvars(string.Template(x).safe_substitute(program=os.path.abspath(os.path.join(test_dir, test_command))))
767+
os.path.expandvars(string.Template(x).safe_substitute(**test_vars))
757768
for x in test_script
758769
] + forward_args
759770

@@ -768,21 +779,18 @@ def test(self, test_dir, module_dir, test_command, filter_command, forward_args)
768779
python_interpreter = sys.executable
769780
filter_command = [python_interpreter] + filter_command
770781

771-
env = os.environ.copy()
772-
env['YOTTA_PROGRAM'] = _encodePathForEnv(test_command)
773-
774782
test_child = None
775783
test_filter = None
776784
try:
777785
logger.debug('running test: %s', cmd)
778786
if filter_command:
779787
logger.debug('using output filter command: %s', filter_command)
780788
test_child = subprocess.Popen(
781-
cmd, cwd = test_dir, stdout = subprocess.PIPE, env = env
789+
cmd, cwd = test_dir, stdout = subprocess.PIPE, env = test_env
782790
)
783791
try:
784792
test_filter = subprocess.Popen(
785-
filter_command, cwd = module_dir, stdin = test_child.stdout, env = env
793+
filter_command, cwd = module_dir, stdin = test_child.stdout, env = test_env
786794
)
787795
except OSError as e:
788796
logger.error('error starting test output filter "%s": %s', filter_command, e)
@@ -803,7 +811,7 @@ def test(self, test_dir, module_dir, test_command, filter_command, forward_args)
803811
else:
804812
try:
805813
test_child = subprocess.Popen(
806-
cmd, cwd = test_dir, env = env
814+
cmd, cwd = test_dir, env = test_env
807815
)
808816
logger.debug('waiting for test child')
809817
except OSError as e:

yotta/test/cli/test_debug.py

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55
# See LICENSE file for details.
66

77
# standard library modules, , ,
8+
import json
89
import unittest
910
import os
1011

1112
# internal modules:
1213
from yotta.test.cli import util
1314
from yotta.test.cli import cli
1415

16+
JSON_MARKER = '###---###'
1517
def _nopDebugTargetDescription(name):
1618
native_target = util.nativeTarget()
1719
if ',' in native_target:
@@ -25,24 +27,43 @@ def _nopDebugTargetDescription(name):
2527
"%s": "*"
2628
},
2729
"scripts": {
28-
"debug": ["./scripts/nop.py", "$program"]
30+
"debug": ["./scripts/nop.py", "$program", "$build_dir", "$target_dir"]
2931
}
3032
}
3133
''' % (name, native_target),
3234
'./scripts/nop.py':'''
3335
import os
34-
print('would debug %s' % os.environ['YOTTA_PROGRAM'])
35-
'''
36+
import sys
37+
import json
38+
39+
env_keys = ["YOTTA_PROGRAM", "YOTTA_BUILD_DIR", "YOTTA_TARGET_DIR"]
40+
print(json.dumps({"argv": sys.argv[1:], "env": {k: v for k, v in os.environ.items() if k in env_keys}}))
41+
print('%s')
42+
''' % JSON_MARKER
3643
}
3744

3845
class TestCLIDebug(unittest.TestCase):
3946
@unittest.skipIf(not util.canBuildNatively(), "can't build natively on windows yet")
4047
def test_noop_debug(self):
4148
test_dir = util.writeTestFiles(util.Test_Trivial_Exe, True)
42-
util.writeTestFiles(_nopDebugTargetDescription('debug-test-target'), test_dir=os.path.join(test_dir, 'yotta_targets', 'debug-test-target'))
49+
target_dir = os.path.realpath(os.path.join(test_dir, 'yotta_targets', 'debug-test-target'))
50+
build_dir = os.path.realpath(os.path.join(test_dir, 'build', 'debug-test-target'))
51+
52+
util.writeTestFiles(_nopDebugTargetDescription('debug-test-target'), test_dir=target_dir)
4353
output = util.runCheckCommand(['--target', 'debug-test-target', 'build'], test_dir)
4454
output = util.runCheckCommand(['--target', 'debug-test-target', 'debug'], test_dir)
45-
self.assertIn('would debug source/test-trivial-exe', output)
55+
json_output = output[:output.index(JSON_MARKER)]
56+
result = json.loads(json_output)
57+
58+
self.assertTrue(result is not None)
59+
self.assertEqual(len(result['argv']), 3)
60+
self.assertEqual(result['argv'][0], 'source/test-trivial-exe')
61+
self.assertEqual(result['env']['YOTTA_PROGRAM'], 'source/test-trivial-exe')
62+
self.assertEqual(result['argv'][1], build_dir)
63+
self.assertEqual(result['env']['YOTTA_BUILD_DIR'], build_dir)
64+
self.assertEqual(result['argv'][2], target_dir)
65+
self.assertEqual(result['env']['YOTTA_TARGET_DIR'], target_dir)
66+
4667
util.rmRf(test_dir)
4768

4869
@unittest.skipIf(not util.canBuildNatively(), "can't build natively on windows yet")

0 commit comments

Comments
 (0)