Source code for schrodinger.job.files
import contextlib
import itertools
from schrodinger.utils import subprocess
from schrodinger.utils.fileutils import tempfilename
[docs]@contextlib.contextmanager
def get_file(job, filename):
"""
Yields a filename that is readable for the lifetime of the context manager.
:param job: schrodinger.jobcontrol.Job
:type job: schrodinger.jobcontrol.Job
:param str filename: a filename of an output or logfile for a given jobid
:type filename: a filename of an output or logfile for a given jobid
:raises RuntimeError: if the job has been downloaded already
"""
if filename not in itertools.chain(job.OutputFiles, job.LogFiles):
raise RuntimeError(
f"Specifying a file {filename} that is not an OutputFile or LogFile for {job}"
)
with tempfilename() as output_filename:
with open(output_filename, "w") as fh:
proc = subprocess.run(
["jsc", "tail-file", job.JobId, "--name", filename],
stdout=fh,
stderr=subprocess.PIPE,
universal_newlines=True)
if proc.returncode != 0:
raise RuntimeError(
f"Could not get {filename} for {job}: {proc.stderr}")
yield output_filename