Skip to content

Commit c567b9e

Browse files
committed
Add type to run_experiment, respect when users define AUTOSUBMIT_CONFIGURATION
1 parent 16b8edd commit c567b9e

File tree

3 files changed

+110
-61
lines changed

3 files changed

+110
-61
lines changed

autosubmit/autosubmit.py

Lines changed: 51 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@
8080
import autosubmit.history.utils as HUtils
8181
import autosubmit.helpers.autosubmit_helper as AutosubmitHelper
8282
import autosubmit.statistics.utils as StatisticsUtils
83-
from autosubmit.helpers.utils import check_jobs_file_exists
83+
from autosubmit.helpers.utils import check_jobs_file_exists, get_rc_path
8484

8585
from contextlib import suppress
8686

@@ -2149,7 +2149,7 @@ def refresh_log_recovery_process(platforms: List[Platform], as_conf: AutosubmitC
21492149
p.work_event.set()
21502150

21512151
@staticmethod
2152-
def run_experiment(expid, notransitive=False, start_time=None, start_after=None, run_only_members=None, profile=False):
2152+
def run_experiment(expid, notransitive=False, start_time=None, start_after=None, run_only_members=None, profile=False) -> int:
21532153
"""
21542154
Runs and experiment (submitting all the jobs properly and repeating its execution in case of failure).
21552155
:param expid: the experiment id
@@ -2158,7 +2158,7 @@ def run_experiment(expid, notransitive=False, start_time=None, start_after=None,
21582158
:param start_after: the expid after which the experiment should start
21592159
:param run_only_members: the members to run
21602160
:param profile: if True, the function will be profiled
2161-
:return: None
2161+
:return: exit status
21622162
21632163
"""
21642164
# Start profiling if the flag has been used
@@ -3406,7 +3406,7 @@ def describe(input_experiment_list="*",get_from_user=""):
34063406

34073407
@staticmethod
34083408
def configure(advanced, database_path, database_filename, local_root_path, platforms_conf_path, jobs_conf_path,
3409-
smtp_hostname, mail_from, machine, local):
3409+
smtp_hostname, mail_from, machine: bool, local: bool):
34103410
"""
34113411
Configure several paths for autosubmit: database, local root and others. Can be configured at system,
34123412
user or local levels. Local level configuration precedes user level and user level precedes system
@@ -3486,61 +3486,54 @@ def configure(advanced, database_path, database_filename, local_root_path, platf
34863486
Log.error("jobs.yml path does not exist.")
34873487
return False
34883488

3489-
if machine:
3490-
rc_path = '/etc'
3491-
elif local:
3492-
rc_path = '.'
3493-
else:
3494-
rc_path = home_path
3495-
rc_path = rc_path.joinpath('.autosubmitrc')
3489+
rc_path: Path = get_rc_path(machine, local)
34963490

3497-
config_file = open(rc_path, 'w')
3498-
Log.info("Writing configuration file...")
3499-
try:
3500-
parser = ConfigParser()
3501-
parser.add_section('database')
3502-
parser.set('database', 'path', str(database_path))
3503-
if database_filename is not None and len(str(database_filename)) > 0:
3504-
parser.set('database', 'filename', str(database_filename))
3505-
parser.add_section('local')
3506-
parser.set('local', 'path', str(local_root_path))
3507-
if (jobs_conf_path is not None and len(str(jobs_conf_path)) > 0) or (
3508-
platforms_conf_path is not None and len(str(platforms_conf_path)) > 0):
3509-
parser.add_section('conf')
3510-
if jobs_conf_path is not None:
3511-
parser.set('conf', 'jobs', str(jobs_conf_path))
3512-
if platforms_conf_path is not None:
3513-
parser.set('conf', 'platforms', str(platforms_conf_path))
3514-
if smtp_hostname is not None or mail_from is not None:
3515-
parser.add_section('mail')
3516-
parser.set('mail', 'smtp_server', smtp_hostname)
3517-
parser.set('mail', 'mail_from', mail_from)
3518-
parser.add_section("globallogs")
3519-
parser.set("globallogs", "path", str(global_logs_path))
3520-
parser.add_section("structures")
3521-
parser.set("structures", "path", str(structures_path))
3522-
parser.add_section("historicdb")
3523-
parser.set("historicdb", "path", str(historicdb_path))
3524-
parser.add_section("historiclog")
3525-
parser.set("historiclog", "path", str(historiclog_path))
3526-
parser.add_section("autosubmitapi")
3527-
parser.set("autosubmitapi", "url", autosubmitapi_url)
3528-
# parser.add_section("hosts")
3529-
# parser.set("hosts", "whitelist", " localhost # Add your machine names")
3530-
parser.write(config_file)
3531-
config_file.close()
3532-
Log.result(f"Configuration file written successfully: \n\t{rc_path}")
3533-
HUtils.create_path_if_not_exists(local_root_path)
3534-
HUtils.create_path_if_not_exists(global_logs_path)
3535-
HUtils.create_path_if_not_exists(structures_path)
3536-
HUtils.create_path_if_not_exists(historicdb_path)
3537-
HUtils.create_path_if_not_exists(historiclog_path)
3538-
Log.result(f"Directories configured successfully: \n\t{str(database_path)} \n\t{str(local_root_path)}"
3539-
f" \n\t{str(global_logs_path)} \n\t{str(structures_path)} \n\t{str(historicdb_path)}"
3540-
f" \n\t{str(historiclog_path)}")
3541-
except (IOError, OSError) as e:
3542-
raise AutosubmitCritical(
3543-
"Can not write config file: {0}", 7012, e.message)
3491+
with open(rc_path, 'w') as config_file:
3492+
Log.info("Writing configuration file...")
3493+
try:
3494+
parser = ConfigParser()
3495+
parser.add_section('database')
3496+
parser.set('database', 'path', str(database_path))
3497+
if database_filename is not None and len(str(database_filename)) > 0:
3498+
parser.set('database', 'filename', str(database_filename))
3499+
parser.add_section('local')
3500+
parser.set('local', 'path', str(local_root_path))
3501+
if (jobs_conf_path is not None and len(str(jobs_conf_path)) > 0) or (
3502+
platforms_conf_path is not None and len(str(platforms_conf_path)) > 0):
3503+
parser.add_section('conf')
3504+
if jobs_conf_path is not None:
3505+
parser.set('conf', 'jobs', str(jobs_conf_path))
3506+
if platforms_conf_path is not None:
3507+
parser.set('conf', 'platforms', str(platforms_conf_path))
3508+
if smtp_hostname is not None or mail_from is not None:
3509+
parser.add_section('mail')
3510+
parser.set('mail', 'smtp_server', smtp_hostname)
3511+
parser.set('mail', 'mail_from', mail_from)
3512+
parser.add_section("globallogs")
3513+
parser.set("globallogs", "path", str(global_logs_path))
3514+
parser.add_section("structures")
3515+
parser.set("structures", "path", str(structures_path))
3516+
parser.add_section("historicdb")
3517+
parser.set("historicdb", "path", str(historicdb_path))
3518+
parser.add_section("historiclog")
3519+
parser.set("historiclog", "path", str(historiclog_path))
3520+
parser.add_section("autosubmitapi")
3521+
parser.set("autosubmitapi", "url", autosubmitapi_url)
3522+
# parser.add_section("hosts")
3523+
# parser.set("hosts", "whitelist", " localhost # Add your machine names")
3524+
parser.write(config_file)
3525+
Log.result(f"Configuration file written successfully: \n\t{rc_path}")
3526+
HUtils.create_path_if_not_exists(local_root_path)
3527+
HUtils.create_path_if_not_exists(global_logs_path)
3528+
HUtils.create_path_if_not_exists(structures_path)
3529+
HUtils.create_path_if_not_exists(historicdb_path)
3530+
HUtils.create_path_if_not_exists(historiclog_path)
3531+
Log.result(f"Directories configured successfully: \n\t{str(database_path)} \n\t{str(local_root_path)}"
3532+
f" \n\t{str(global_logs_path)} \n\t{str(structures_path)} \n\t{str(historicdb_path)}"
3533+
f" \n\t{str(historiclog_path)}")
3534+
except (IOError, OSError) as e:
3535+
raise AutosubmitCritical(
3536+
"Can not write config file: {0}", 7012, e.message)
35443537
except (AutosubmitCritical, AutosubmitError) as e:
35453538
raise
35463539
except BaseException as e:

autosubmit/helpers/utils.py

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
import pwd
33
import re
44
from itertools import zip_longest
5-
6-
from autosubmitconfigparser.config.basicconfig import BasicConfig
5+
from pathlib import Path
76

87
from autosubmit.notifications.mail_notifier import MailNotifier
98
from autosubmit.notifications.notifier import Notifier
9+
from autosubmitconfigparser.config.basicconfig import BasicConfig
1010
from log.log import AutosubmitCritical, Log
1111

1212

@@ -119,6 +119,7 @@ def restore_platforms(platform_to_test, mail_notify=False, as_conf=None, expid=N
119119
else:
120120
raise AutosubmitCritical("Issues while checking the connectivity of platforms.", 7010, issues + "\n" + ssh_config_issues)
121121

122+
122123
# Source: https://github.com/cylc/cylc-flow/blob/a722b265ad0bd68bc5366a8a90b1dbc76b9cd282/cylc/flow/tui/util.py#L226
123124
class NaturalSort:
124125
"""An object to use as a sort key for sorting strings as a human would.
@@ -220,3 +221,31 @@ def strtobool(val):
220221
return 0
221222
else:
222223
raise ValueError("invalid truth value %r" % (val,))
224+
225+
226+
def get_rc_path(machine: bool, local: bool) -> Path:
227+
"""Get the ``.autosubmit.rc`` path.
228+
229+
If the environment variable ``AUTOSUBMIT_CONFIGURATION`` is specified in the
230+
system, this function will return a ``Path`` pointing to that value.
231+
232+
If ``machine`` is ``True``, it will use the file from ``/etc/.autosubmitrc``
233+
(pay attention to the dot prefix).
234+
235+
Else, if ``local`` is ``True``, it will use the file from ``./.autosubmitrc``
236+
(i.e. it will use the current working directory for the process).
237+
238+
Otherwise, it will load the file from ``~/.autosubmitrc``, for the user
239+
currently running Autosubmit.
240+
"""
241+
if 'AUTOSUBMIT_CONFIGURATION' in os.environ:
242+
return Path(os.environ['AUTOSUBMIT_CONFIGURATION'])
243+
244+
if machine:
245+
rc_path = '/etc'
246+
elif local:
247+
rc_path = '.'
248+
else:
249+
rc_path = Path.home()
250+
251+
return Path(rc_path) / '.autosubmitrc'

test/unit/helpers/test_utils.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@
1515
# You should have received a copy of the GNU General Public License
1616
# along with Autosubmit. If not, see <http://www.gnu.org/licenses/>.
1717

18+
from pathlib import Path
19+
1820
import pytest
1921

20-
from autosubmit.helpers.utils import strtobool
22+
from autosubmit.helpers.utils import strtobool, get_rc_path
2123

2224

2325
@pytest.mark.parametrize(
@@ -54,3 +56,28 @@ def test_strtobool(val, expected):
5456
strtobool(val)
5557
else:
5658
assert expected == strtobool(val)
59+
60+
61+
@pytest.mark.parametrize(
62+
'expected,machine,local,env_vars',
63+
[
64+
(Path('/tmp/hello/scooby/doo/ooo.txt'), True, True, {
65+
'AUTOSUBMIT_CONFIGURATION': '/tmp/hello/scooby/doo/ooo.txt'
66+
}),
67+
(Path('/etc/.autosubmitrc'), True, True, {}),
68+
(Path('/etc/.autosubmitrc'), True, False, {}),
69+
(Path('./.autosubmitrc'), False, True, {}),
70+
(Path(Path.home(), '.autosubmitrc'), False, False, {})
71+
],
72+
ids=[
73+
'Use env var',
74+
'Use machine, even if local is true',
75+
'Use machine',
76+
'Use local',
77+
'Use home'
78+
]
79+
)
80+
def test_get_rc_path(expected: Path, machine: bool, local: bool, env_vars: dict, mocker):
81+
mocker.patch.dict('autosubmit.helpers.utils.os.environ', env_vars, clear=True)
82+
83+
assert expected == get_rc_path(machine, local)

0 commit comments

Comments
 (0)