Skip to content

Commit

Permalink
Use subprocess's env argument over env command-line utility
Browse files Browse the repository at this point in the history
  • Loading branch information
blowekamp committed Nov 13, 2024
1 parent c27fea9 commit 0f4b664
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
8 changes: 2 additions & 6 deletions em_workflows/dm_conversion/flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,6 @@ def convert_if_int16_tiff(file_path: FilePath) -> None:
shrink_factor = _calculate_shrink_factor(file_path.fp_in)

cmd = [
"env",
"IMOD_OUTPUT_FORMAT=TIF",
DMConfig.newstack_loc,
"-shrink",
f"{shrink_factor:.3f}",
Expand All @@ -92,7 +90,7 @@ def convert_if_int16_tiff(file_path: FilePath) -> None:
str(tif_8_bit),
]
utils.log(f"Generated cmd {cmd}")
FilePath.run(cmd, log_fp)
FilePath.run(cmd, log_fp, env={"IMOD_OUTPUT_FORMAT": "TIF"})



Expand Down Expand Up @@ -127,8 +125,6 @@ def convert_2d_mrc_to_tiff(file_path: FilePath) -> None:
utils.log(f"{file_path.fp_in.as_posix()} is a mrc file, will convert to {out_fp}.")
# work out meansd
cmd = [
"env",
"IMOD_OUTPUT_FORMAT=TIF",
DMConfig.newstack_loc,
"-shrink",
shrink_factor_3,
Expand All @@ -142,7 +138,7 @@ def convert_2d_mrc_to_tiff(file_path: FilePath) -> None:
out_fp,
]
utils.log(f"Generated cmd {cmd}")
FilePath.run(cmd, log_fp)
FilePath.run(cmd, log_fp, env={"IMOD_OUTPUT_FORMAT": "TIF"})


@task(
Expand Down
21 changes: 18 additions & 3 deletions em_workflows/file_path.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import datetime
import shutil
import os
from typing import List, Dict
from typing import List, Dict, Optional, AnyStr
from pathlib import Path
import tempfile
import subprocess
Expand Down Expand Up @@ -272,15 +272,30 @@ def rm_workdir(self):
shutil.rmtree(self.working_dir, ignore_errors=True)

@staticmethod
def run(cmd: List[str], log_file: str) -> int:
def run(cmd: List[str], log_file: str, env: Optional[Dict[AnyStr, AnyStr]] = None, *, copy_env: bool = True) -> int:
"""Runs a Unix command as a subprocess
- Captures stderr & stddout and writes them to the `log_file` input parameter.
- If final returncode is not 0, raises a FAIL signal
:param cmd: list of strings representing the command to run
:param log_file: path to the log file to write the stdout and stderr to
:param env: dictionary of additional environment variables to pass to the subprocess
:param copy_env: if True, the subprocess inherits the parent's environment
"""

if env is None:
if not copy_env:
env = {}
# Note: if env is not and copy_env is True, the subprocess inherits the parent's environment,
# by passing env=None
elif copy_env:
# merge dictionaries python 3.9+
env = os.environ | env

log(f"Running subprocess: {' '.join(cmd)} logfile: {log_file}")

with (subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, bufsize=1) as p,
with (subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, bufsize=1, env=env) as p,
open(log_file, 'ab') as file):
file.write(f"Running subprocess: {' '.join(cmd)}\n".encode())

Expand Down

0 comments on commit 0f4b664

Please sign in to comment.