-
Notifications
You must be signed in to change notification settings - Fork 1
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
Log and write subprocess output as produced. #485
Open
blowekamp
wants to merge
3
commits into
main
Choose a base branch
from
run_live_output
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,125 @@ | ||
import os | ||
|
||
from em_workflows.file_path import FilePath | ||
from pathlib import Path | ||
import sys | ||
|
||
|
||
def test_gen_output_fp(mock_nfs_mount, tmp_path): | ||
input_dir = tmp_path / "Projects" | ||
input_dir.mkdir() | ||
p = input_dir / "some_file_rec.mrc" | ||
p.write_text("content") | ||
assert str(p).endswith("Projects/some_file_rec.mrc") | ||
input_dir = Path("test/input_files/brt/Projects/2013-1220-dA30_5-BSC-1_10.mrc").absolute() | ||
|
||
file_base_name = input_dir.stem | ||
|
||
fp_in = FilePath(share_name="test", input_dir=input_dir, fp_in=p) | ||
fp_in = FilePath(share_name="test", input_dir=input_dir.parent, fp_in=input_dir) | ||
|
||
zarr = fp_in.gen_output_fp(output_ext=".zarr") | ||
rec_mrc = fp_in.gen_output_fp(output_ext="_rec.mrc") | ||
base_mrc = fp_in.gen_output_fp(output_ext=".mrc", out_fname="adjusted.mrc") | ||
input_mrc = fp_in.fp_in | ||
print(f"{rec_mrc=}\n{base_mrc=}\n{input_mrc=}") | ||
assert zarr.name == "some_file_rec.zarr" | ||
assert zarr.name == f"{file_base_name}.zarr" | ||
assert base_mrc.name == "adjusted.mrc" | ||
assert input_mrc.name == "some_file_rec.mrc" | ||
assert input_mrc.name == f"{file_base_name}.mrc" | ||
# Below is simply a temporary outfile loc | ||
assert rec_mrc.name == "some_file_rec_rec.mrc" | ||
assert rec_mrc.name == f"{file_base_name}_rec.mrc" | ||
|
||
|
||
# Test the FilePath.run function by executing the python shell, and producing putput to stderr and stdout | ||
def test_filepath_run(mock_nfs_mount, tmp_path, request): | ||
current_test_name = request.node.name | ||
log_file = tmp_path/f"{current_test_name}.log" | ||
|
||
input_filename = "test/input_files/dm_inputs/Projects/Lab/PI/PrP - Protein.007.tif" | ||
input_dir = Path(input_filename).absolute() | ||
|
||
fp_in = FilePath(share_name="test", input_dir=input_dir.parent, fp_in=input_dir) | ||
|
||
# Run the python shell, and produce output to stderr and stdout | ||
cmd = [sys.executable, "-c", "import sys; print('stdout'); print('stderr', file=sys.stderr)"] | ||
fp_in.run(cmd, log_file=str(log_file)) | ||
|
||
# Check if the log file was created | ||
assert log_file.exists() | ||
|
||
# Check that the log contain "stdout" and "stderr" strings | ||
with open(log_file, "r") as f: | ||
log_content = f.read() | ||
assert "stdout" in log_content | ||
assert "stderr" in log_content | ||
|
||
|
||
# Write a pytest for FilePath.run function by executing the python shell which print the environment variables. | ||
# Combinations of setting the "env" parameter and "copy_env" need to be tested. | ||
def test_filepath_run_env(mock_nfs_mount, tmp_path, request): | ||
current_test_name = request.node.name | ||
log_file = tmp_path/f"{current_test_name}.log" | ||
|
||
input_filename = "test/input_files/dm_inputs/Projects/Lab/PI/PrP - Protein.007.tif" | ||
input_dir = Path(input_filename).absolute() | ||
|
||
fp_in = FilePath(share_name="test", input_dir=input_dir.parent, fp_in=input_dir) | ||
|
||
os.environ["PARENT_ENV"] = "parent_env_value" | ||
|
||
# Case 1: env is set, and copy_env is set to False | ||
|
||
# Run the python shell, and print all environment variables | ||
cmd = [sys.executable, "-c", "import os; print(os.environ)"] | ||
fp_in.run(cmd, log_file=str(log_file), env={"ENV_VAR": "env_var_value"}, copy_env=False) | ||
|
||
# Check if the log file was created | ||
assert log_file.exists() | ||
|
||
with open(log_file, "r") as f: | ||
log_content = f.read() | ||
assert "env_var_value" in log_content | ||
assert "parent_env_value" not in log_content | ||
log_file.unlink() | ||
|
||
# Case 2: env is set, and copy_env is set to True | ||
|
||
# Run the python shell, and print all environment variables | ||
cmd = [sys.executable, "-c", "import os; print(os.environ)"] | ||
fp_in.run(cmd, log_file=str(log_file), env={"ENV_VAR": "env_var_value"}, copy_env=True) | ||
|
||
# Check if the log file was created | ||
assert log_file.exists() | ||
|
||
# Check that the log contain "env_var_value" string | ||
with open(log_file, "r") as f: | ||
log_content = f.read() | ||
assert "env_var_value" in log_content | ||
assert "parent_env_value" in log_content | ||
log_file.unlink() | ||
|
||
# Case 3: env is not set, and copy_env is set to False | ||
|
||
# Run the python shell, and print all environment variables | ||
cmd = [sys.executable, "-c", "import os; print(os.environ)"] | ||
fp_in.run(cmd, log_file=str(log_file), env=None, copy_env=True) | ||
|
||
# Check if the log file was created | ||
assert log_file.exists() | ||
|
||
# Check that the log contain "env_var_value" string | ||
with open(log_file, "r") as f: | ||
log_content = f.read() | ||
assert "env_var_value" not in log_content | ||
assert "parent_env_value" in log_content | ||
log_file.unlink() | ||
|
||
# Case 4: env is not set, and copy_env is set to False | ||
|
||
# Run the python shell, and print all environment variables | ||
cmd = [sys.executable, "-c", "import os; print(os.environ)"] | ||
fp_in.run(cmd, log_file=str(log_file), env=None, copy_env=False) | ||
|
||
# Check if the log file was created | ||
assert log_file.exists() | ||
|
||
# Check that the log contain "env_var_value" string | ||
with open(log_file, "r") as f: | ||
log_content = f.read() | ||
assert "env_var_value" not in log_content | ||
assert "parent_env_value" not in log_content | ||
log_file.unlink() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh, cool
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The important things added is the usage of pipes with streaming output so that the output of the process can be seen live or if the task is killed by slum the partial output would be there.