Skip to content

Commit

Permalink
Updated chi_run path handling
Browse files Browse the repository at this point in the history
  • Loading branch information
lamsoa729 committed Dec 19, 2023
1 parent d74926d commit 1a9be3f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 16 deletions.
6 changes: 0 additions & 6 deletions chi_pet/chi_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,6 @@ def find_leaf_dirs(path, ignore=['result', 'analysis', 'scripts']):
yield path


def touch(fname, times=None):
""" Replicates the UNIX touch command """
with open(fname, 'a'):
os.utime(fname, times)


def find_dirs(path):
""" Find all the child directories one level deep and return a list
of the absolute paths.
Expand Down
24 changes: 14 additions & 10 deletions chi_pet/chi_run.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#!/usr/bin/env python
import sys
# TODO NEXT change os to pathlib functions
import os
from subprocess import call, run
from subprocess import run
from .chi_lib import load_yaml_in_order, dump_yaml_in_order
import argparse
from pathlib import Path
Expand Down Expand Up @@ -32,18 +31,20 @@ def run_parse_args():

def run_args(workdir, state, args):
action = state + '-ing'
action_file = Path('.' + action)
print("Started {} sim using args {}".format(action, args))
sys.stdout.flush()
if workdir.exists():
os.chdir(workdir)
open('.' + action, 'a').close()
action_file.touch()
status = run(args)
os.remove('.' + action)
action_file.unlink()
if status.returncode:
print(f"Run failed: State {state} did not succeed.")
return status.returncode
else:
print(f"Run failed: could not find work directory {workdir}.")
print(
f"Run failed: could not find work directory {workdir} to do action {state}.")
return 1


Expand All @@ -55,8 +56,9 @@ def Run(self, opts):
workdir_path = Path(opts.workdir)
args_file_path = workdir_path / opts.args_file
if not workdir_path.exists():
raise FileNotFoundError("Run failed. Directory {} does not exists.".format(
opts.workdir))
raise FileNotFoundError(
"Run failed. Directory {} does not exists.".format(
opts.workdir))

elif not args_file_path.exists():
raise FileNotFoundError(
Expand All @@ -69,16 +71,18 @@ def Run(self, opts):
print(dump_yaml_in_order(args_dict, default_flow_style=False))
for k, vals in args_dict.items():
args = [str(a) for a in vals]
sim_action = Path('sim.{}'.format(k))

if k in opts.states:
if run_args(opts.workdir, k, args):
open('.error', 'a').close()
elif os.path.exists('sim.{}'.format(k)):
os.remove('sim.{}'.format(k))
(opts.workdir / '.error').touch()
elif sim_action.exists():
sim_action.unlink()


if __name__ == '__main__':

# TODO NEXT add this as a subparser to chi_pet.py
opts = run_parse_args()

c = ChiRun(opts)
Expand Down

0 comments on commit 1a9be3f

Please sign in to comment.