Skip to content

Commit 3e757a1

Browse files
authored
Merge pull request #523 from xylar/simplify_local_mache
Enable easier mache testing
2 parents c301cc4 + 0f0ab0f commit 3e757a1

File tree

4 files changed

+95
-74
lines changed

4 files changed

+95
-74
lines changed

conda/bootstrap.py

+20-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
#!/usr/bin/env python
2-
3-
from __future__ import print_function
1+
#!/usr/bin/env python3
42

53
import glob
64
import grp
@@ -238,10 +236,10 @@ def get_env_setup(args, config, machine, compiler, mpi, env_type, source_path,
238236
activ_path, env_path, env_name, activate_env, spack_env
239237

240238

241-
def build_conda_env(env_type, recreate, machine, mpi, conda_mpi, version,
239+
def build_conda_env(env_type, recreate, mpi, conda_mpi, version,
242240
python, source_path, conda_template_path, conda_base,
243241
env_name, env_path, activate_base, use_local,
244-
local_conda_build, logger):
242+
local_conda_build, logger, local_mache):
245243

246244
if env_type != 'dev':
247245
install_miniconda(conda_base, activate_base, logger)
@@ -284,7 +282,8 @@ def build_conda_env(env_type, recreate, machine, mpi, conda_mpi, version,
284282
conda_openmp = ''
285283
spec_file = template.render(supports_otps=supports_otps,
286284
mpi=conda_mpi, openmp=conda_openmp,
287-
mpi_prefix=mpi_prefix)
285+
mpi_prefix=mpi_prefix,
286+
include_mache=not local_mache)
288287

289288
spec_filename = f'spec-file-{conda_mpi}.txt'
290289
with open(spec_filename, 'w') as handle:
@@ -423,7 +422,7 @@ def build_spack_env(config, update_spack, machine, compiler, mpi, spack_env,
423422
f'scorpio@{scorpio}+pnetcdf~timing+internal-timing~tools+malloc')
424423

425424
if albany != 'None':
426-
specs.append(f'albany@{albany}+mpas+cxx17')
425+
specs.append(f'albany@{albany}+mpas')
427426

428427
yaml_template = f'{spack_template_path}/{machine}_{compiler}_{mpi}.yaml'
429428
if not os.path.exists(yaml_template):
@@ -802,6 +801,8 @@ def main(): # noqa: C901
802801

803802
compass_version = get_version()
804803

804+
local_mache = args.mache_fork is not None and args.mache_branch is not None
805+
805806
machine = None
806807
if not args.env_only:
807808
if args.machine is None:
@@ -889,10 +890,20 @@ def main(): # noqa: C901
889890

890891
if previous_conda_env != conda_env_name:
891892
build_conda_env(
892-
env_type, recreate, machine, mpi, conda_mpi, compass_version,
893+
env_type, recreate, mpi, conda_mpi, compass_version,
893894
python, source_path, conda_template_path, conda_base,
894895
conda_env_name, conda_env_path, activate_base, args.use_local,
895-
args.local_conda_build, logger)
896+
args.local_conda_build, logger, local_mache)
897+
898+
if local_mache:
899+
print('Install local mache\n')
900+
commands = f'source {conda_base}/etc/profile.d/conda.sh && ' \
901+
f'source {conda_base}/etc/profile.d/mamba.sh && ' \
902+
f'conda activate {conda_env_name} && ' \
903+
'cd ../build_mache/mache && ' \
904+
'python -m pip install .'
905+
check_call(commands, logger=logger)
906+
896907
previous_conda_env = conda_env_name
897908

898909
if env_type != 'dev':

conda/compass_env/spec-file.template

+2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ jigsaw=0.9.14
1515
jigsawpy=0.3.3
1616
jupyter
1717
lxml
18+
{% if include_mache %}
1819
mache=1.10.0
20+
{% endif %}
1921
matplotlib-base
2022
metis
2123
mpas_tools=0.17.0

conda/configure_compass_env.py

+45-38
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,8 @@
1-
#!/usr/bin/env python
2-
3-
from __future__ import print_function
1+
#!/usr/bin/env python3
42

53
import os
64
import sys
7-
8-
try:
9-
from configparser import ConfigParser
10-
except ImportError:
11-
import six
12-
from six.moves import configparser
13-
14-
if six.PY2:
15-
ConfigParser = configparser.SafeConfigParser
16-
else:
17-
ConfigParser = configparser.ConfigParser
5+
from configparser import ConfigParser
186

197
from shared import (
208
check_call,
@@ -41,36 +29,30 @@ def get_config(config_file):
4129
def bootstrap(activate_install_env, source_path, local_conda_build):
4230

4331
print('Creating the compass conda environment\n')
44-
bootstrap_command = '{}/conda/bootstrap.py'.format(source_path)
45-
command = '{} && ' \
46-
'{} {}'.format(activate_install_env, bootstrap_command,
47-
' '.join(sys.argv[1:]))
32+
bootstrap_command = f'{source_path}/conda/bootstrap.py'
33+
command = f'{activate_install_env} && ' \
34+
f'{bootstrap_command} {" ".join(sys.argv[1:])}'
4835
if local_conda_build is not None:
49-
command = '{} --local_conda_build {}'.format(command,
50-
local_conda_build)
36+
command = f'{command} --local_conda_build {local_conda_build}'
5137
check_call(command)
5238

5339

5440
def setup_install_env(env_name, activate_base, use_local, logger, recreate,
55-
conda_base):
41+
conda_base, mache):
5642
env_path = os.path.join(conda_base, 'envs', env_name)
5743
if use_local:
5844
channels = '--use-local'
5945
else:
6046
channels = ''
61-
packages = 'progressbar2 jinja2 "mache=1.10.0"'
47+
packages = f'progressbar2 jinja2 {mache}'
6248
if recreate or not os.path.exists(env_path):
6349
print('Setting up a conda environment for installing compass\n')
64-
commands = '{} && ' \
65-
'mamba create -y -n {} {} {}'.format(activate_base,
66-
env_name, channels,
67-
packages)
50+
commands = f'{activate_base} && ' \
51+
f'mamba create -y -n {env_name} {channels} {packages}'
6852
else:
6953
print('Updating conda environment for installing compass\n')
70-
commands = '{} && ' \
71-
'mamba install -y -n {} {} {}'.format(activate_base,
72-
env_name, channels,
73-
packages)
54+
commands = f'{activate_base} && ' \
55+
f'mamba install -y -n {env_name} {channels} {packages}'
7456

7557
check_call(commands, logger=logger)
7658

@@ -79,6 +61,12 @@ def main():
7961
args = parse_args(bootstrap=False)
8062
source_path = os.getcwd()
8163

64+
if args.tmpdir is not None:
65+
try:
66+
os.makedirs(args.tmpdir)
67+
except FileExistsError:
68+
pass
69+
8270
config = get_config(args.config_file)
8371

8472
conda_base = get_conda_base(args.conda_base, config, warn=True)
@@ -87,14 +75,14 @@ def main():
8775
env_name = 'compass_bootstrap'
8876

8977
source_activation_scripts = \
90-
'source {}/etc/profile.d/conda.sh && ' \
91-
'source {}/etc/profile.d/mamba.sh'.format(conda_base, conda_base)
78+
f'source {conda_base}/etc/profile.d/conda.sh && ' \
79+
f'source {conda_base}/etc/profile.d/mamba.sh'
9280

93-
activate_base = '{} && mamba activate'.format(source_activation_scripts)
81+
activate_base = f'{source_activation_scripts} && conda activate'
9482

9583
activate_install_env = \
96-
'{} && ' \
97-
'mamba activate {}'.format(source_activation_scripts, env_name)
84+
f'{source_activation_scripts} && ' \
85+
f'conda activate {env_name}'
9886
try:
9987
os.makedirs('conda/logs')
10088
except OSError:
@@ -106,15 +94,34 @@ def main():
10694
# install miniconda if needed
10795
install_miniconda(conda_base, activate_base, logger)
10896

97+
local_mache = args.mache_fork is not None and args.mache_branch is not None
98+
if local_mache:
99+
mache = ''
100+
else:
101+
mache = '"mache=1.10.0"'
102+
109103
setup_install_env(env_name, activate_base, args.use_local, logger,
110-
args.recreate, conda_base)
104+
args.recreate, conda_base, mache)
105+
106+
if local_mache:
107+
print('Clone and install local mache\n')
108+
commands = f'{activate_install_env} && ' \
109+
f'rm -rf conda/build_mache && ' \
110+
f'mkdir -p conda/build_mache && ' \
111+
f'cd conda/build_mache && ' \
112+
f'git clone -b {args.mache_branch} ' \
113+
f'[email protected]:{args.mache_fork}.git mache && ' \
114+
f'cd mache && ' \
115+
f'python -m pip install .'
116+
117+
check_call(commands, logger=logger)
111118

112119
env_type = config.get('deploy', 'env_type')
113120
if env_type not in ['dev', 'test_release', 'release']:
114-
raise ValueError('Unexpected env_type: {}'.format(env_type))
121+
raise ValueError(f'Unexpected env_type: {env_type}')
115122

116123
if env_type == 'test_release' and args.use_local:
117-
local_conda_build = os.path.abspath('{}/conda-bld'.format(conda_base))
124+
local_conda_build = os.path.abspath(f'{conda_base}/conda-bld')
118125
else:
119126
local_conda_build = None
120127

conda/shared.py

+28-27
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,11 @@
1-
from __future__ import print_function
2-
31
import argparse
42
import logging
53
import os
64
import platform
75
import shutil
86
import subprocess
97
import sys
10-
11-
try:
12-
from urllib.request import Request, urlopen
13-
except ImportError:
14-
from urllib2 import Request, urlopen
8+
from urllib.request import Request, urlopen
159

1610

1711
def parse_args(bootstrap):
@@ -47,6 +41,10 @@ def parse_args(bootstrap):
4741
"packages")
4842
parser.add_argument("--use_local", dest="use_local", action='store_true',
4943
help="Use locally built conda packages (for testing).")
44+
parser.add_argument("--mache_fork", dest="mache_fork",
45+
help="Point to a mache fork (and branch) for testing")
46+
parser.add_argument("--mache_branch", dest="mache_branch",
47+
help="Point to a mache branch (and fork) for testing")
5048
parser.add_argument("--update_spack", dest="update_spack",
5149
action='store_true',
5250
help="If the shared spack environment should be "
@@ -78,6 +76,10 @@ def parse_args(bootstrap):
7876

7977
args = parser.parse_args(sys.argv[1:])
8078

79+
if (args.mache_fork is None) != (args.mache_branch is None):
80+
raise ValueError('You must supply both or neither of '
81+
'--mache_fork and --mache_branch')
82+
8183
return args
8284

8385

@@ -92,9 +94,9 @@ def get_conda_base(conda_base, config, shared=False, warn=False):
9294
conda_base = os.path.abspath(
9395
os.path.join(conda_exe, '..', '..'))
9496
if warn:
95-
print('\nWarning: --conda path not supplied. Using conda '
96-
'installed at:\n'
97-
' {}\n'.format(conda_base))
97+
print(f'\nWarning: --conda path not supplied. Using conda '
98+
f'installed at:\n'
99+
f' {conda_base}\n')
98100
else:
99101
raise ValueError('No conda base provided with --conda and '
100102
'none could be inferred.')
@@ -118,9 +120,9 @@ def get_spack_base(spack_base, config):
118120
def check_call(commands, env=None, logger=None):
119121
print_command = '\n '.join(commands.split(' && '))
120122
if logger is None:
121-
print('\n Running:\n {}\n'.format(print_command))
123+
print(f'\n Running:\n {print_command}\n')
122124
else:
123-
logger.info('\nrunning:\n {}\n'.format(print_command))
125+
logger.info(f'\nrunning:\n {print_command}\n')
124126

125127
if logger is None:
126128
process = subprocess.Popen(commands, env=env, executable='/bin/bash',
@@ -154,8 +156,8 @@ def install_miniconda(conda_base, activate_base, logger):
154156
system = 'MacOSX'
155157
else:
156158
system = 'Linux'
157-
miniconda = 'Mambaforge-{}-x86_64.sh'.format(system)
158-
url = 'https://github.com/conda-forge/miniforge/releases/latest/download/{}'.format(miniconda) # noqa: E501
159+
miniconda = f'Mambaforge-{system}-x86_64.sh'
160+
url = f'https://github.com/conda-forge/miniforge/releases/latest/download/{miniconda}' # noqa: E501
159161
print(url)
160162
req = Request(url, headers={'User-Agent': 'Mozilla/5.0'})
161163
f = urlopen(req)
@@ -164,31 +166,30 @@ def install_miniconda(conda_base, activate_base, logger):
164166
outfile.write(html)
165167
f.close()
166168

167-
command = '/bin/bash {} -b -p {}'.format(miniconda, conda_base)
169+
command = f'/bin/bash {miniconda} -b -p {conda_base}'
168170
check_call(command, logger=logger)
169171
os.remove(miniconda)
170172

171173
backup_bashrc()
172174

173175
print('Doing initial setup\n')
174176

175-
commands = '{} && ' \
176-
'conda config --add channels conda-forge && ' \
177-
'conda config --set channel_priority strict' \
178-
''.format(activate_base)
177+
commands = f'{activate_base} && ' \
178+
f'conda config --add channels conda-forge && ' \
179+
f'conda config --set channel_priority strict'
179180

180181
check_call(commands, logger=logger)
181182

182-
commands = '{} && ' \
183-
'conda remove -y boa'.format(activate_base)
183+
commands = f'{activate_base} && ' \
184+
f'conda remove -y boa'
184185
try:
185186
check_call(commands, logger=logger)
186187
except subprocess.CalledProcessError:
187188
pass
188189

189-
commands = '{} && ' \
190-
'mamba update -y --all && ' \
191-
'mamba init'.format(activate_base)
190+
commands = f'{activate_base} && ' \
191+
f'mamba update -y --all && ' \
192+
f'mamba init'
192193

193194
check_call(commands, logger=logger)
194195

@@ -200,7 +201,7 @@ def backup_bashrc():
200201
files = ['.bashrc', '.bash_profile']
201202
for filename in files:
202203
src = os.path.join(home_dir, filename)
203-
dst = os.path.join(home_dir, '{}.conda_bak'.format(filename))
204+
dst = os.path.join(home_dir, f'{filename}.conda_bak')
204205
if os.path.exists(src):
205206
shutil.copyfile(src, dst)
206207

@@ -209,14 +210,14 @@ def restore_bashrc():
209210
home_dir = os.path.expanduser('~')
210211
files = ['.bashrc', '.bash_profile']
211212
for filename in files:
212-
src = os.path.join(home_dir, '{}.conda_bak'.format(filename))
213+
src = os.path.join(home_dir, f'{filename}.conda_bak')
213214
dst = os.path.join(home_dir, filename)
214215
if os.path.exists(src):
215216
shutil.move(src, dst)
216217

217218

218219
def get_logger(name, log_filename):
219-
print('Logging to: {}\n'.format(log_filename))
220+
print(f'Logging to: {log_filename}\n')
220221
try:
221222
os.remove(log_filename)
222223
except OSError:

0 commit comments

Comments
 (0)