@@ -572,6 +572,10 @@ def compile(self, patterns):
572
572
rules .append ((None , r ))
573
573
self ._rules = rules
574
574
575
+ def reset (self ):
576
+ self ._log .debug ("reset filter patterns" )
577
+ self ._rules = None
578
+
575
579
def filter (self , path , prefix = None ):
576
580
path = os .path .normpath (path )
577
581
if prefix :
@@ -676,8 +680,11 @@ def plan(self, source_path, query):
676
680
source_paths = []
677
681
build_plan = []
678
682
679
- step = lambda * x : build_plan .append (x )
680
- hash = source_paths .append
683
+ def step (* x ):
684
+ build_plan .append (x )
685
+
686
+ def hash (path ):
687
+ source_paths .append (path )
681
688
682
689
def pip_requirements_step (path , prefix = None , required = False , tmp_dir = None ):
683
690
command = runtime
@@ -753,13 +760,6 @@ def commands_step(path, commands):
753
760
if c .startswith (":zip" ):
754
761
if path :
755
762
hash (path )
756
- else :
757
- # If path doesn't defined for a block with
758
- # commands it will be set to Terraform's
759
- # current working directory
760
- # NB: cwd may vary when using Terraform 0.14+ like:
761
- # `terraform -chdir=...`
762
- path = query .paths .cwd
763
763
if batch :
764
764
step ("sh" , path , "\n " .join (batch ))
765
765
batch .clear ()
@@ -770,12 +770,14 @@ def commands_step(path, commands):
770
770
_path = os .path .normpath (os .path .join (path , _path ))
771
771
step ("zip:embedded" , _path , prefix )
772
772
elif len (c ) == 2 :
773
- prefix = None
774
773
_ , _path = c
774
+ prefix = None
775
+ _path = os .path .normpath (_path )
775
776
step ("zip:embedded" , _path , prefix )
776
777
elif len (c ) == 1 :
777
778
prefix = None
778
- step ("zip:embedded" , path , prefix )
779
+ _path = None
780
+ step ("zip:embedded" , _path , prefix )
779
781
else :
780
782
raise ValueError (
781
783
":zip invalid call signature, use: "
@@ -787,6 +789,8 @@ def commands_step(path, commands):
787
789
step ("sh" , path , "\n " .join (batch ))
788
790
batch .clear ()
789
791
792
+ step ("reset:workdir" )
793
+
790
794
for claim in claims :
791
795
if isinstance (claim , str ):
792
796
path = claim
@@ -862,6 +866,7 @@ def commands_step(path, commands):
862
866
tmp_dir = claim .get ("npm_tmp_dir" ),
863
867
)
864
868
if path :
869
+ path = os .path .normpath (path )
865
870
step ("zip" , path , prefix )
866
871
if patterns :
867
872
# Take patterns into account when computing hash
@@ -882,6 +887,10 @@ def commands_step(path, commands):
882
887
return build_plan
883
888
884
889
def execute (self , build_plan , zip_stream , query ):
890
+ sh_log = logging .getLogger ("sh" )
891
+
892
+ tf_work_dir = os .getcwd ()
893
+
885
894
zs = zip_stream
886
895
sh_work_dir = None
887
896
pf = None
@@ -891,10 +900,16 @@ def execute(self, build_plan, zip_stream, query):
891
900
if cmd .startswith ("zip" ):
892
901
ts = 0 if cmd == "zip:embedded" else None
893
902
source_path , prefix = action [1 :]
894
- if sh_work_dir :
895
- if source_path != sh_work_dir :
896
- if not os .path .isfile (source_path ):
897
- source_path = sh_work_dir
903
+ if not sh_work_dir :
904
+ sh_work_dir = tf_work_dir
905
+ log .debug ("WORKDIR: %s" , sh_work_dir )
906
+ if source_path :
907
+ if not os .path .isabs (source_path ):
908
+ source_path = os .path .normpath (
909
+ os .path .join (sh_work_dir , source_path )
910
+ )
911
+ else :
912
+ source_path = sh_work_dir
898
913
if os .path .isdir (source_path ):
899
914
if pf :
900
915
self ._zip_write_with_filter (
@@ -942,10 +957,22 @@ def execute(self, build_plan, zip_stream, query):
942
957
elif cmd == "sh" :
943
958
with tempfile .NamedTemporaryFile (mode = "w+t" , delete = True ) as temp_file :
944
959
path , script = action [1 :]
945
- # NOTE: Execute `pwd` to determine the subprocess shell's working directory after having executed all other commands.
960
+
961
+ if not path :
962
+ path = tf_work_dir
963
+ if not os .path .isabs (path ):
964
+ path = os .path .normpath (os .path .join (tf_work_dir , path ))
965
+
966
+ if log .isEnabledFor (DEBUG2 ):
967
+ log .debug ("exec shell script ..." )
968
+ for line in script .splitlines ():
969
+ sh_log .debug (line )
970
+
946
971
script = "\n " .join (
947
972
(
948
973
script ,
974
+ # NOTE: Execute `pwd` to determine the subprocess shell's
975
+ # working directory after having executed all other commands.
949
976
"retcode=$?" ,
950
977
f"pwd >{ temp_file .name } " ,
951
978
"exit $retcode" ,
@@ -960,17 +987,9 @@ def execute(self, build_plan, zip_stream, query):
960
987
cwd = path ,
961
988
)
962
989
963
- p .wait ()
964
- temp_file .seek (0 )
965
-
966
- # NOTE: This var `sh_work_dir` is consumed in cmd == "zip" loop
967
- sh_work_dir = temp_file .read ().strip ()
968
-
969
- log .info ("WD: %s" , sh_work_dir )
970
-
971
990
call_stdout , call_stderr = p .communicate ()
972
991
exit_code = p .returncode
973
- log .info ("exit_code: %s" , exit_code )
992
+ log .debug ("exit_code: %s" , exit_code )
974
993
if exit_code != 0 :
975
994
raise RuntimeError (
976
995
"Script did not run successfully, exit code {}: {} - {}" .format (
@@ -979,11 +998,21 @@ def execute(self, build_plan, zip_stream, query):
979
998
call_stderr .decode ("utf-8" ).strip (),
980
999
)
981
1000
)
1001
+
1002
+ temp_file .seek (0 )
1003
+ # NOTE: This var `sh_work_dir` is consumed in cmd == "zip" loop
1004
+ sh_work_dir = temp_file .read ().strip ()
1005
+ log .debug ("WORKDIR: %s" , sh_work_dir )
1006
+
1007
+ elif cmd == "reset:workdir" :
1008
+ sh_work_dir = tf_work_dir
1009
+ log .debug ("WORKDIR: %s" , sh_work_dir )
982
1010
elif cmd == "set:filter" :
983
1011
patterns = action [1 ]
984
1012
pf = ZipContentFilter (args = self ._args )
985
1013
pf .compile (patterns )
986
1014
elif cmd == "clear:filter" :
1015
+ pf .reset ()
987
1016
pf = None
988
1017
989
1018
@staticmethod
0 commit comments