ensure the main process raise an exception when the subprocess fails (#18663)

This commit is contained in:
Haifeng Jin 2023-10-21 02:52:38 -07:00 committed by GitHub
parent e3c68cd1d4
commit 353aaff034
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -20,14 +20,13 @@ def setup_package():
shell=True, shell=True,
) )
print(build_process.stdout) print(build_process.stdout)
match = re.search( whl_path = re.findall(
r"\s[^\s]*\.whl", r"[^\s]*\.whl",
build_process.stdout, build_process.stdout,
) )[-1]
if not match: if not whl_path:
raise ValueError("Installing Keras package unsuccessful. ")
print(build_process.stderr) print(build_process.stderr)
whl_path = match.group() raise ValueError("Installing Keras package unsuccessful. ")
return whl_path return whl_path
@ -82,35 +81,39 @@ def cleanup():
def run_commands_local(commands): def run_commands_local(commands):
for command in commands: for command in commands:
print(f"Running command: {command}")
subprocess.run(command, shell=True) subprocess.run(command, shell=True)
def run_commands_venv(commands): def run_commands_venv(commands):
for command in commands: for command in commands:
print(f"Running command: {command}")
cmd_with_args = command.split(" ") cmd_with_args = command.split(" ")
cmd_with_args[0] = "test_env/bin/" + cmd_with_args[0] cmd_with_args[0] = "test_env/bin/" + cmd_with_args[0]
p = subprocess.Popen(cmd_with_args) p = subprocess.Popen(cmd_with_args)
p.wait() assert p.wait() == 0
def test_keras_imports(): def test_keras_imports():
# Ensures packages from all backends are installed. try:
# Builds Keras core package and returns package file path. # Ensures packages from all backends are installed.
whl_path = setup_package() # Builds Keras core package and returns package file path.
whl_path = setup_package()
# Creates and activates a virtual environment. # Creates and activates a virtual environment.
create_virtualenv() create_virtualenv()
# Ensures the backend's package is installed # Ensures the backend's package is installed
# and the other backends are uninstalled. # and the other backends are uninstalled.
manage_venv_installs(whl_path) manage_venv_installs(whl_path)
# Runs test of basic flow in Keras Core. # Runs test of basic flow in Keras Core.
# Tests for backend-specific imports and `model.fit()`. # Tests for backend-specific imports and `model.fit()`.
run_keras_flow() run_keras_flow()
# Removes virtual environment and associated files # Removes virtual environment and associated files
cleanup() finally:
cleanup()
if __name__ == "__main__": if __name__ == "__main__":