Skip to content

Commit

Permalink
refactor: write to a tempfile instead of using fd
Browse files Browse the repository at this point in the history
  • Loading branch information
ANGkeith committed Apr 10, 2024
1 parent b7b89e8 commit 4589683
Showing 1 changed file with 25 additions and 27 deletions.
52 changes: 25 additions & 27 deletions package.py
Original file line number Diff line number Diff line change
Expand Up @@ -916,35 +916,33 @@ def execute(self, build_plan, zip_stream, query):
# XXX: timestamp=0 - what actually do with it?
zs.write_dirs(rd, prefix=prefix, timestamp=0)
elif cmd == "sh":
r, w = os.pipe()
side_ch = os.fdopen(r)
path, script = action[1:]
script = "{} && pwd >&{}".format(script, w)
p = subprocess.Popen(
script,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
cwd=path,
pass_fds=(w,),
)
with tempfile.NamedTemporaryFile(mode="w+t", delete=True) as temp_file:
path, script = action[1:]
script = f"{script} && pwd >{temp_file.name}"
p = subprocess.Popen(
script,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
cwd=path,
)

os.close(w)
sh_work_dir = side_ch.read().strip()
p.wait()
log.info("WD: %s", sh_work_dir)
side_ch.close()
call_stdout, call_stderr = p.communicate()
exit_code = p.returncode
log.info("exit_code: %s", exit_code)
if exit_code != 0:
raise RuntimeError(
"Script did not run successfully, exit code {}: {} - {}".format(
exit_code,
call_stdout.decode("utf-8").strip(),
call_stderr.decode("utf-8").strip(),
p.wait()
temp_file.seek(0)
sh_work_dir = temp_file.read().strip()
log.info("WD: %s", sh_work_dir)

call_stdout, call_stderr = p.communicate()
exit_code = p.returncode
log.info("exit_code: %s", exit_code)
if exit_code != 0:
raise RuntimeError(
"Script did not run successfully, exit code {}: {} - {}".format(
exit_code,
call_stdout.decode("utf-8").strip(),
call_stderr.decode("utf-8").strip(),
)
)
)
elif cmd == "set:filter":
patterns = action[1]
pf = ZipContentFilter(args=self._args)
Expand Down

0 comments on commit 4589683

Please sign in to comment.