@@ -572,6 +572,10 @@ def compile(self, patterns):
572572 rules .append ((None , r ))
573573 self ._rules  =  rules 
574574
575+  def  reset (self ):
576+  self ._log .debug ("reset filter patterns" )
577+  self ._rules  =  None 
578+ 575579 def  filter (self , path , prefix = None ):
576580 path  =  os .path .normpath (path )
577581 if  prefix :
@@ -676,8 +680,11 @@ def plan(self, source_path, query):
676680 source_paths  =  []
677681 build_plan  =  []
678682
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 )
681688
682689 def  pip_requirements_step (path , prefix = None , required = False , tmp_dir = None ):
683690 command  =  runtime 
@@ -753,13 +760,6 @@ def commands_step(path, commands):
753760 if  c .startswith (":zip" ):
754761 if  path :
755762 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 
763763 if  batch :
764764 step ("sh" , path , "\n " .join (batch ))
765765 batch .clear ()
@@ -770,12 +770,14 @@ def commands_step(path, commands):
770770 _path  =  os .path .normpath (os .path .join (path , _path ))
771771 step ("zip:embedded" , _path , prefix )
772772 elif  len (c ) ==  2 :
773-  prefix  =  None 
774773 _ , _path  =  c 
774+  prefix  =  None 
775+  _path  =  os .path .normpath (_path )
775776 step ("zip:embedded" , _path , prefix )
776777 elif  len (c ) ==  1 :
777778 prefix  =  None 
778-  step ("zip:embedded" , path , prefix )
779+  _path  =  None 
780+  step ("zip:embedded" , _path , prefix )
779781 else :
780782 raise  ValueError (
781783 ":zip invalid call signature, use: " 
@@ -787,6 +789,8 @@ def commands_step(path, commands):
787789 step ("sh" , path , "\n " .join (batch ))
788790 batch .clear ()
789791
792+  step ("reset:workdir" )
793+ 790794 for  claim  in  claims :
791795 if  isinstance (claim , str ):
792796 path  =  claim 
@@ -862,6 +866,7 @@ def commands_step(path, commands):
862866 tmp_dir = claim .get ("npm_tmp_dir" ),
863867 )
864868 if  path :
869+  path  =  os .path .normpath (path )
865870 step ("zip" , path , prefix )
866871 if  patterns :
867872 # Take patterns into account when computing hash 
@@ -882,6 +887,10 @@ def commands_step(path, commands):
882887 return  build_plan 
883888
884889 def  execute (self , build_plan , zip_stream , query ):
890+  sh_log  =  logging .getLogger ("sh" )
891+ 892+  tf_work_dir  =  os .getcwd ()
893+ 885894 zs  =  zip_stream 
886895 sh_work_dir  =  None 
887896 pf  =  None 
@@ -891,10 +900,16 @@ def execute(self, build_plan, zip_stream, query):
891900 if  cmd .startswith ("zip" ):
892901 ts  =  0  if  cmd  ==  "zip:embedded"  else  None 
893902 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 
898913 if  os .path .isdir (source_path ):
899914 if  pf :
900915 self ._zip_write_with_filter (
@@ -942,10 +957,22 @@ def execute(self, build_plan, zip_stream, query):
942957 elif  cmd  ==  "sh" :
943958 with  tempfile .NamedTemporaryFile (mode = "w+t" , delete = True ) as  temp_file :
944959 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+ 946971 script  =  "\n " .join (
947972 (
948973 script ,
974+  # NOTE: Execute `pwd` to determine the subprocess shell's 
975+  # working directory after having executed all other commands. 
949976 "retcode=$?" ,
950977 f"pwd >{ temp_file .name }  ,
951978 "exit $retcode" ,
@@ -960,17 +987,9 @@ def execute(self, build_plan, zip_stream, query):
960987 cwd = path ,
961988 )
962989
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- 971990 call_stdout , call_stderr  =  p .communicate ()
972991 exit_code  =  p .returncode 
973-  log .info ("exit_code: %s" , exit_code )
992+  log .debug ("exit_code: %s" , exit_code )
974993 if  exit_code  !=  0 :
975994 raise  RuntimeError (
976995 "Script did not run successfully, exit code {}: {} - {}" .format (
@@ -979,11 +998,21 @@ def execute(self, build_plan, zip_stream, query):
979998 call_stderr .decode ("utf-8" ).strip (),
980999 )
9811000 )
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 )
9821010 elif  cmd  ==  "set:filter" :
9831011 patterns  =  action [1 ]
9841012 pf  =  ZipContentFilter (args = self ._args )
9851013 pf .compile (patterns )
9861014 elif  cmd  ==  "clear:filter" :
1015+  pf .reset ()
9871016 pf  =  None 
9881017
9891018 @staticmethod  
0 commit comments