Skip to content

Commit

Permalink
fix: make source_path blocks independent
Browse files Browse the repository at this point in the history
The `source_path` blocks don't impact anymore to each other by workdir changes
  • Loading branch information
ahlinc committed Nov 18, 2024
1 parent 38d6715 commit aa2cb10
Showing 1 changed file with 25 additions and 7 deletions.
32 changes: 25 additions & 7 deletions package.py
Original file line number Diff line number Diff line change
Expand Up @@ -776,7 +776,8 @@ def commands_step(path, commands):
step("zip:embedded", _path, prefix)
elif len(c) == 1:
prefix = None
step("zip:embedded", path, prefix)
_path = None
step("zip:embedded", _path, prefix)
else:
raise ValueError(
":zip invalid call signature, use: "
Expand All @@ -788,6 +789,8 @@ def commands_step(path, commands):
step("sh", path, "\n".join(batch))
batch.clear()

step("reset:workdir")

for claim in claims:
if isinstance(claim, str):
path = claim
Expand Down Expand Up @@ -884,6 +887,8 @@ def commands_step(path, commands):
return build_plan

def execute(self, build_plan, zip_stream, query):
tf_work_dir = os.getcwd()

zs = zip_stream
sh_work_dir = None
pf = None
Expand All @@ -893,10 +898,14 @@ def execute(self, build_plan, zip_stream, query):
if cmd.startswith("zip"):
ts = 0 if cmd == "zip:embedded" else None
source_path, prefix = action[1:]
if sh_work_dir:
if source_path != sh_work_dir:
if not os.path.isfile(source_path):
source_path = sh_work_dir
if not sh_work_dir:
sh_work_dir = tf_work_dir
log.info("WORKDIR: %s", sh_work_dir)
if source_path:
if not os.path.isabs(source_path):
source_path = os.path.join(sh_work_dir, source_path)
else:
source_path = sh_work_dir
if os.path.isdir(source_path):
if pf:
self._zip_write_with_filter(
Expand Down Expand Up @@ -944,10 +953,17 @@ def execute(self, build_plan, zip_stream, query):
elif cmd == "sh":
with tempfile.NamedTemporaryFile(mode="w+t", delete=True) as temp_file:
path, script = action[1:]
# NOTE: Execute `pwd` to determine the subprocess shell's working directory after having executed all other commands.

if not path:
path = tf_work_dir
if not os.path.isabs(path):
path = os.path.join(tf_work_dir, path)

script = "\n".join(
(
script,
# NOTE: Execute `pwd` to determine the subprocess shell's
# working directory after having executed all other commands.
"retcode=$?",
f"pwd >{temp_file.name}",
"exit $retcode",
Expand All @@ -968,7 +984,7 @@ def execute(self, build_plan, zip_stream, query):
# NOTE: This var `sh_work_dir` is consumed in cmd == "zip" loop
sh_work_dir = temp_file.read().strip()

log.info("WD: %s", sh_work_dir)
log.info("WORKDIR: %s", sh_work_dir)

call_stdout, call_stderr = p.communicate()
exit_code = p.returncode
Expand All @@ -981,6 +997,8 @@ def execute(self, build_plan, zip_stream, query):
call_stderr.decode("utf-8").strip(),
)
)
elif cmd == "reset:workdir":
sh_work_dir = tf_work_dir
elif cmd == "set:filter":
patterns = action[1]
pf = ZipContentFilter(args=self._args)
Expand Down

0 comments on commit aa2cb10

Please sign in to comment.