Skip to content

Commit f786681

Browse files
authored
fix: Zip source directory should read from sh_work_dir (#560)
* fix: :zip:embedded source directory should read from sh_work_dir Signed-off-by: ANGkeith <[email protected]> * refactor: write to a tempfile instead of using fd * refactor: move tests out to own file * docs: add comments to explain code --------- Signed-off-by: ANGkeith <[email protected]>
1 parent 54b8256 commit f786681

File tree

2 files changed

+75
-19
lines changed

2 files changed

+75
-19
lines changed

package.py

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -916,27 +916,37 @@ def execute(self, build_plan, zip_stream, query):
916916
# XXX: timestamp=0 - what actually do with it?
917917
zs.write_dirs(rd, prefix=prefix, timestamp=0)
918918
elif cmd == "sh":
919-
path, script = action[1:]
920-
p = subprocess.Popen(
921-
script,
922-
shell=True,
923-
stdout=subprocess.PIPE,
924-
stderr=subprocess.PIPE,
925-
cwd=path,
926-
)
919+
with tempfile.NamedTemporaryFile(mode="w+t", delete=True) as temp_file:
920+
path, script = action[1:]
921+
# NOTE: Execute `pwd` to determine the subprocess shell's working directory after having executed all other commands.
922+
script = f"{script} && pwd >{temp_file.name}"
923+
p = subprocess.Popen(
924+
script,
925+
shell=True,
926+
stdout=subprocess.PIPE,
927+
stderr=subprocess.PIPE,
928+
cwd=path,
929+
)
927930

928-
p.wait()
929-
call_stdout, call_stderr = p.communicate()
930-
exit_code = p.returncode
931-
log.info("exit_code: %s", exit_code)
932-
if exit_code != 0:
933-
raise RuntimeError(
934-
"Script did not run successfully, exit code {}: {} - {}".format(
935-
exit_code,
936-
call_stdout.decode("utf-8").strip(),
937-
call_stderr.decode("utf-8").strip(),
931+
p.wait()
932+
temp_file.seek(0)
933+
934+
# NOTE: This var `sh_work_dir` is consumed in cmd == "zip" loop
935+
sh_work_dir = temp_file.read().strip()
936+
937+
log.info("WD: %s", sh_work_dir)
938+
939+
call_stdout, call_stderr = p.communicate()
940+
exit_code = p.returncode
941+
log.info("exit_code: %s", exit_code)
942+
if exit_code != 0:
943+
raise RuntimeError(
944+
"Script did not run successfully, exit code {}: {} - {}".format(
945+
exit_code,
946+
call_stdout.decode("utf-8").strip(),
947+
call_stderr.decode("utf-8").strip(),
948+
)
938949
)
939-
)
940950
elif cmd == "set:filter":
941951
patterns = action[1]
942952
pf = ZipContentFilter(args=self._args)

tests/test_zip_source.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import os
2+
from unittest.mock import MagicMock, Mock
3+
4+
from package import BuildPlanManager
5+
6+
7+
def test_zip_source_path_sh_work_dir():
8+
zs = Mock()
9+
zs.write_dirs = MagicMock()
10+
11+
bpm = BuildPlanManager(args=Mock())
12+
13+
bpm.execute(
14+
build_plan=[
15+
["sh", ".", "cd $(mktemp -d)\n echo pip install"],
16+
["zip:embedded", ".", "./python"],
17+
],
18+
zip_stream=zs,
19+
query=None,
20+
)
21+
22+
zs.write_dirs.assert_called_once()
23+
24+
zip_source_path = zs.write_dirs.call_args_list[0][0][0]
25+
assert zip_source_path != f"{os.getcwd()}"
26+
27+
28+
def test_zip_source_path():
29+
zs = Mock()
30+
zs.write_dirs = MagicMock()
31+
32+
bpm = BuildPlanManager(args=Mock())
33+
34+
bpm.execute(
35+
build_plan=[
36+
["sh", ".", "echo pip install"],
37+
["zip:embedded", ".", "./python"],
38+
],
39+
zip_stream=zs,
40+
query=None,
41+
)
42+
43+
zs.write_dirs.assert_called_once()
44+
45+
zip_source_path = zs.write_dirs.call_args_list[0][0][0]
46+
assert zip_source_path == f"{os.getcwd()}"

0 commit comments

Comments
 (0)