Skip to content

Commit 3d16cc9

Browse files
author
Nathan Lee
committed
Further restructuring to ensure app_profile/config is sourced ASAP
1 parent 3efc488 commit 3d16cc9

4 files changed

Lines changed: 46 additions & 38 deletions

File tree

pyrunner/cli.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,8 @@ def main():
3030
setup()
3131
else:
3232
try:
33-
pyrunner = PyRunner()
34-
pyrunner.parse_args()
35-
exit_status = pyrunner.execute()
33+
app = PyRunner()
34+
exit_status = app.execute()
3635
except ValueError as value_error:
3736
exit_status = 2
3837
print(str(value_error))

pyrunner/core/constants.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,15 @@
4242
from pyrunner import PyRunner
4343
from pathlib import Path
4444
45+
# Determine absolute path of this file's parent directory at runtime
4546
abs_dir_path = os.path.dirname(os.path.realpath(__file__))
4647
47-
app = PyRunner()
48+
# Store path to default config and .lst file
49+
config_file = '{{}}/config/app_profile'.format(abs_dir_path)
50+
proc_file = '{{}}/config/{app_name}.lst'.format(abs_dir_path)
4851
49-
# Assign default config and .lst file
50-
app.config_file = '{{}}/config/app_profile'.format(abs_dir_path)
51-
app.proc_file = '{{}}/config/{app_name}.lst'.format(abs_dir_path)
52-
53-
# Parse command line args
54-
app.parse_args()
52+
# Init PyRunner and assign default config and .lst file
53+
app = PyRunner(config_file=config_file, proc_file=proc_file)
5554
5655
if __name__ == '__main__':
5756
# Initiate job and exit driver with return code

pyrunner/core/pyrunner.py

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -47,35 +47,31 @@ def __init__(self, **kwargs):
4747
self.engine = ExecutionEngine()
4848

4949
self._init_params = {
50-
'restart' : False,
51-
'config_file' : None,
52-
'proc_file' : None,
53-
'exec_proc_name' : None,
54-
'exec_only_list' : [],
50+
'config_file' : kwargs.get('config_file'),
51+
'proc_file' : kwargs.get('proc_file'),
52+
'restart' : False,
53+
'cvar_list' : [],
54+
'exec_proc_name' : None,
55+
'exec_only_list' : [],
5556
'exec_disable_list' : [],
56-
'exec_from_id' : None,
57-
'exec_to_id' : None
57+
'exec_from_id' : None,
58+
'exec_to_id' : None
5859
}
5960

61+
self._on_fresh_start_func = None
6062
self._on_start_func = None
6163
self._on_restart_func = None
6264
self._on_success_func = None
6365
self._on_fail_func = None
6466
self._on_exit_func = None
6567

66-
# Backwards compatability
67-
self.source_config_file = self.config.source_config_file
68-
self.load_proc_list_file = self.load_from_file
69-
self.load_last_failed = self.load_state
70-
71-
if kwargs.get('parse_args', False) == True:
72-
self.parse_args()
68+
self.parse_args()
7369

7470
def reset_env(self):
7571
os.environ.clear()
7672
os.environ.update(self._environ)
7773

78-
def load_from_file(self, proc_file, restart=False):
74+
def load_proc_file(self, proc_file, restart=False):
7975
if not proc_file or not os.path.isfile(proc_file):
8076
return False
8177

@@ -140,16 +136,15 @@ def on_exit(self, func):
140136
self._on_exit_func = func
141137

142138
def prepare(self):
143-
# Source config
144-
self.source_config_file(self._init_params['config_file'])
145-
146139
# Initialize NodeRegister
147140
if self._init_params['restart']:
148-
if not self.load_last_failed():
149-
self._init_params['restart'] = False
150-
self.load_proc_list_file(self._init_params['proc_file'])
151-
else:
152-
self.load_proc_list_file(self._init_params['proc_file'])
141+
self.load_state()
142+
elif self._init_params.get('proc_file'):
143+
self.load_proc_file(self._init_params['proc_file'])
144+
145+
# Inject Context var overrides
146+
for k,v in self._init_params['cvar_list']:
147+
self.engine.context.set(k, v)
153148

154149
# Modify NodeRegister
155150
if self._init_params['exec_proc_name']:
@@ -323,7 +318,7 @@ def save_state(self, suppress_output=False):
323318
return
324319

325320
def load_state(self):
326-
if not self.load_from_file(self.config.ctllog_file, True):
321+
if not self.load_proc_file(self.config.ctllog_file, True):
327322
return False
328323

329324
if not os.path.isfile(self.config.ctx_file):
@@ -346,6 +341,13 @@ def delete_state(self):
346341
if os.path.isfile(self.config.ctx_file):
347342
os.remove(self.config.ctx_file)
348343

344+
def is_restartable(self):
345+
if not os.path.isfile(self.config.ctllog_file):
346+
return False
347+
if not os.path.isfile(self.config.ctx_file):
348+
return False
349+
return True
350+
349351
# NodeRegister wiring
350352
def add_node(self, **kwargs) : return self.register.add_node(**kwargs)
351353
def exec_only(self, id_list) : return self.register.exec_only(id_list)
@@ -402,13 +404,11 @@ def parse_args(self):
402404
os.environ[parts[0]] = parts[1]
403405
elif opt == '--cvar':
404406
parts = arg.split('=')
405-
self.engine.context.set(parts[0], parts[1])
407+
self._init_params['cvar_list'].append((parts[0], parts[1]))
406408
elif opt == '--nozip':
407409
self.config['nozip'] = True
408410
elif opt == '--dump-logs':
409411
self.config['dump_logs'] = True
410-
#elif opt == '--context':
411-
# ctx_file = arg
412412
elif opt == '--dryrun':
413413
self.config['dryrun'] = True
414414
elif opt in ['-i', '--interactive']:
@@ -434,6 +434,16 @@ def parse_args(self):
434434
sys.exit(0)
435435
else:
436436
raise ValueError("Error during parsing of opts")
437+
438+
# We need to check for and source the app_profile/config file ASAP,
439+
# but only after --env vars are processed
440+
if not self._init_params.get('config_file'):
441+
raise RuntimeError('Config file (app_profile) has not been provided')
442+
self.config.source_config_file(self._init_params['config_file'])
443+
444+
# Check if restart is possible (ctllog/ctx files exist)
445+
if self._init_params['restart'] and not self.is_restartable():
446+
self._init_params['restart'] = False
437447

438448
def show_help(self):
439449
print("Required:")

pyrunner/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '4.1.3'
1+
__version__ = '4.2.0'

0 commit comments

Comments
 (0)