Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Make source_path blocks independent #640

Merged
merged 5 commits into from
Nov 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 54 additions & 25 deletions package.py
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,10 @@ def compile(self, patterns):
rules.append((None, r))
self._rules = rules

def reset(self):
self._log.debug("reset filter patterns")
self._rules = None

def filter(self, path, prefix=None):
path = os.path.normpath(path)
if prefix:
Expand Down Expand Up @@ -676,8 +680,11 @@ def plan(self, source_path, query):
source_paths = []
build_plan = []

step = lambda *x: build_plan.append(x)
hash = source_paths.append
def step(*x):
build_plan.append(x)

def hash(path):
source_paths.append(path)

def pip_requirements_step(path, prefix=None, required=False, tmp_dir=None):
command = runtime
Expand Down Expand Up @@ -753,13 +760,6 @@ def commands_step(path, commands):
if c.startswith(":zip"):
if path:
hash(path)
else:
# If path doesn't defined for a block with
# commands it will be set to Terraform's
# current working directory
# NB: cwd may vary when using Terraform 0.14+ like:
# `terraform -chdir=...`
path = query.paths.cwd
if batch:
step("sh", path, "\n".join(batch))
batch.clear()
Expand All @@ -770,12 +770,14 @@ def commands_step(path, commands):
_path = os.path.normpath(os.path.join(path, _path))
step("zip:embedded", _path, prefix)
elif len(c) == 2:
prefix = None
_, _path = c
prefix = None
_path = os.path.normpath(_path)
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 @@ -787,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 @@ -862,6 +866,7 @@ def commands_step(path, commands):
tmp_dir=claim.get("npm_tmp_dir"),
)
if path:
path = os.path.normpath(path)
step("zip", path, prefix)
if patterns:
# Take patterns into account when computing hash
Expand All @@ -882,6 +887,10 @@ def commands_step(path, commands):
return build_plan

def execute(self, build_plan, zip_stream, query):
sh_log = logging.getLogger("sh")

tf_work_dir = os.getcwd()

zs = zip_stream
sh_work_dir = None
pf = None
Expand All @@ -891,10 +900,16 @@ 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.debug("WORKDIR: %s", sh_work_dir)
if source_path:
if not os.path.isabs(source_path):
source_path = os.path.normpath(
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 @@ -942,10 +957,22 @@ 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.normpath(os.path.join(tf_work_dir, path))

if log.isEnabledFor(DEBUG2):
log.debug("exec shell script ...")
for line in script.splitlines():
sh_log.debug(line)

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 @@ -960,17 +987,9 @@ def execute(self, build_plan, zip_stream, query):
cwd=path,
)

p.wait()
temp_file.seek(0)

# 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)

call_stdout, call_stderr = p.communicate()
exit_code = p.returncode
log.info("exit_code: %s", exit_code)
log.debug("exit_code: %s", exit_code)
if exit_code != 0:
raise RuntimeError(
"Script did not run successfully, exit code {}: {} - {}".format(
Expand All @@ -979,11 +998,21 @@ def execute(self, build_plan, zip_stream, query):
call_stderr.decode("utf-8").strip(),
)
)

temp_file.seek(0)
# NOTE: This var `sh_work_dir` is consumed in cmd == "zip" loop
sh_work_dir = temp_file.read().strip()
log.debug("WORKDIR: %s", sh_work_dir)

elif cmd == "reset:workdir":
sh_work_dir = tf_work_dir
log.debug("WORKDIR: %s", sh_work_dir)
elif cmd == "set:filter":
patterns = action[1]
pf = ZipContentFilter(args=self._args)
pf.compile(patterns)
elif cmd == "clear:filter":
pf.reset()
pf = None

@staticmethod
Expand Down