diff --git a/craft_parts/executor/step_handler.py b/craft_parts/executor/step_handler.py index 5eab915..bc26252 100644 --- a/craft_parts/executor/step_handler.py +++ b/craft_parts/executor/step_handler.py @@ -445,7 +445,8 @@ def _create_and_run_script( ) -> None: """Create a script with step-specific commands and execute it.""" with script_path.open("w") as run_file: - print("#!/bin/bash", file=run_file) + import shutil + print(f"#!{shutil.which('bash')}", file=run_file) print("set -euo pipefail", file=run_file) if environment_script_path: diff --git a/craft_parts/plugins/java_plugin.py b/craft_parts/plugins/java_plugin.py index f2b4064..cb4e9e8 100644 --- a/craft_parts/plugins/java_plugin.py +++ b/craft_parts/plugins/java_plugin.py @@ -71,9 +71,8 @@ class JavaPlugin(Plugin): return None, "" def _find_javac(self) -> list[str]: - cmd = ["find", "/usr/lib/jvm", "-path", "*/bin/javac", "-print"] - output = subprocess.check_output(cmd, text=True) - return [x for x in output.split("\n") if len(x) > 0] + import shutil + return [shutil.which("javac")] @override def get_build_environment(self) -> dict[str, str]: diff --git a/craft_parts/plugins/validator.py b/craft_parts/plugins/validator.py index 5b4c735..d7a446e 100644 --- a/craft_parts/plugins/validator.py +++ b/craft_parts/plugins/validator.py @@ -142,8 +142,9 @@ class PluginEnvironmentValidator: print(cmd, file=env_file) env_file.flush() + import shutil proc = subprocess.run( - ["/bin/bash", env_file.name], + [shutil.which("bash"), env_file.name], check=True, capture_output=True, text=True, diff --git a/tests/unit/executor/test_step_handler.py b/tests/unit/executor/test_step_handler.py index 4e73c2b..a5f9374 100644 --- a/tests/unit/executor/test_step_handler.py +++ b/tests/unit/executor/test_step_handler.py @@ -209,9 +209,10 @@ class TestStepHandlerBuiltins: assert get_mode(build_script_path) == 0o755 with open(build_script_path) as file: + import shutil assert file.read() == dedent( f"""\ - #!/bin/bash + #!{shutil.which("bash")} set -euo pipefail source {environment_script_path} set -x diff --git a/tests/unit/utils/test_process.py b/tests/unit/utils/test_process.py index a025494..a76cfa2 100644 --- a/tests/unit/utils/test_process.py +++ b/tests/unit/utils/test_process.py @@ -33,7 +33,8 @@ _RUN_TEST_CASES = [ @pytest.mark.parametrize(("out", "err"), _RUN_TEST_CASES) def test_run(out, err): - result = process.run(["/usr/bin/sh", "-c", f"echo {out};sleep 0.1;echo {err} >&2"]) + import shutil + result = process.run([shutil.which("sh"), "-c", f"echo {out};sleep 0.1;echo {err} >&2"]) assert result.returncode == 0 assert result.stdout == (out + "\n").encode() assert result.stderr == (err + "\n").encode() @@ -42,8 +43,9 @@ def test_run(out, err): @pytest.mark.parametrize(("out", "err"), _RUN_TEST_CASES) def test_run_devnull(out, err): + import shutil result = process.run( - ["/usr/bin/sh", "-c", f"echo {out};echo {err} >&2"], + [shutil.which("sh"), "-c", f"echo {out};echo {err} >&2"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, ) @@ -78,9 +80,10 @@ def test_run_selector(out, err, new_dir): selector.register(sock, selectors.EVENT_READ, accept) + import shutil result = process.run( [ - "/usr/bin/sh", + shutil.which("sh"), "-c", f"echo {out};sleep 0.1;echo {err} >&2; echo -n {out}|socat - UNIX-CONNECT:{new_dir}/test.socket", ],