@@ -591,6 +591,17 @@ def findProgram(self, builddir, program):
591
591
logging .error ('could not find program "%s" to debug' % program )
592
592
return None
593
593
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
+
594
605
@fsutils .dropRootPrivs
595
606
def start (self , builddir , program , forward_args ):
596
607
''' Launch the specified program. Uses the `start` script if specified
@@ -603,20 +614,18 @@ def start(self, builddir, program, forward_args):
603
614
if prog_path is None :
604
615
return
605
616
606
- env = os .environ .copy ()
607
- env ['YOTTA_PROGRAM' ] = _encodePathForEnv (prog_path )
608
-
617
+ start_env , start_vars = self .buildProgEnvAndVars (prog_path , builddir )
609
618
if self .getScript ('start' ):
610
619
cmd = [
611
- os .path .expandvars (string .Template (x ).safe_substitute (program = prog_path ))
620
+ os .path .expandvars (string .Template (x ).safe_substitute (** start_vars ))
612
621
for x in self .getScript ('start' )
613
622
] + forward_args
614
623
else :
615
624
cmd = shlex .split ('./' + prog_path ) + forward_args
616
625
617
626
logger .debug ('starting program: %s' , cmd )
618
627
child = subprocess .Popen (
619
- cmd , cwd = builddir , env = env
628
+ cmd , cwd = builddir , env = start_env
620
629
)
621
630
child .wait ()
622
631
if child .returncode :
@@ -663,16 +672,15 @@ def _debugWithScript(self, builddir, program):
663
672
if prog_path is None :
664
673
return
665
674
666
- env = os .environ .copy ()
667
- env ['YOTTA_PROGRAM' ] = _encodePathForEnv (prog_path )
675
+ debug_env , debug_vars = self .buildProgEnvAndVars (prog_path , builddir )
668
676
669
677
cmd = [
670
- os .path .expandvars (string .Template (x ).safe_substitute (program = prog_path ))
678
+ os .path .expandvars (string .Template (x ).safe_substitute (** debug_vars ))
671
679
for x in self .getScript ('debug' )
672
680
]
673
681
logger .debug ('starting debugger: %s' , cmd )
674
682
child = subprocess .Popen (
675
- cmd , cwd = builddir , env = env
683
+ cmd , cwd = builddir , env = debug_env
676
684
)
677
685
child .wait ()
678
686
if child .returncode :
@@ -749,11 +757,14 @@ def test(self, test_dir, module_dir, test_command, filter_command, forward_args)
749
757
# to use filter scripts shipped with the module)
750
758
test_command = './' + test_command
751
759
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
+
752
763
if test_script is None :
753
764
cmd = shlex .split (test_command ) + forward_args
754
765
else :
755
766
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 ))
757
768
for x in test_script
758
769
] + forward_args
759
770
@@ -768,21 +779,18 @@ def test(self, test_dir, module_dir, test_command, filter_command, forward_args)
768
779
python_interpreter = sys .executable
769
780
filter_command = [python_interpreter ] + filter_command
770
781
771
- env = os .environ .copy ()
772
- env ['YOTTA_PROGRAM' ] = _encodePathForEnv (test_command )
773
-
774
782
test_child = None
775
783
test_filter = None
776
784
try :
777
785
logger .debug ('running test: %s' , cmd )
778
786
if filter_command :
779
787
logger .debug ('using output filter command: %s' , filter_command )
780
788
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
782
790
)
783
791
try :
784
792
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
786
794
)
787
795
except OSError as e :
788
796
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)
803
811
else :
804
812
try :
805
813
test_child = subprocess .Popen (
806
- cmd , cwd = test_dir , env = env
814
+ cmd , cwd = test_dir , env = test_env
807
815
)
808
816
logger .debug ('waiting for test child' )
809
817
except OSError as e :
0 commit comments