Skip to content

Commit

Permalink
Require python 3 for deployment
Browse files Browse the repository at this point in the history
I believe all systems have python3 available.

This allows us to remove some awkward imports and to use f-strings
instead of format commands.
  • Loading branch information
xylar committed Feb 28, 2023
1 parent 5bf8ea2 commit 9f1c219
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 72 deletions.
12 changes: 5 additions & 7 deletions conda/bootstrap.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#!/usr/bin/env python

from __future__ import print_function
#!/usr/bin/env python3

import glob
import grp
Expand Down Expand Up @@ -899,10 +897,10 @@ def main(): # noqa: C901

if local_mache:
print('Install local mache\n')
commands = f'source {conda_base}/etc/profile.d/conda.sh; ' \
f'source {conda_base}/etc/profile.d/mamba.sh; ' \
f'conda activate {conda_env_name}; ' \
'cd ../build_mache/mache; ' \
commands = f'source {conda_base}/etc/profile.d/conda.sh && ' \
f'source {conda_base}/etc/profile.d/mamba.sh && ' \
f'conda activate {conda_env_name} && ' \
'cd ../build_mache/mache && ' \
'python -m pip install .'
check_call(commands, logger=logger)

Expand Down
71 changes: 26 additions & 45 deletions conda/configure_compass_env.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,8 @@
#!/usr/bin/env python

from __future__ import print_function
#!/usr/bin/env python3

import os
import sys

try:
from configparser import ConfigParser
except ImportError:
import six
from six.moves import configparser

if six.PY2:
ConfigParser = configparser.SafeConfigParser
else:
ConfigParser = configparser.ConfigParser
from configparser import ConfigParser

from shared import (
check_call,
Expand All @@ -41,13 +29,11 @@ def get_config(config_file):
def bootstrap(activate_install_env, source_path, local_conda_build):

print('Creating the compass conda environment\n')
bootstrap_command = '{}/conda/bootstrap.py'.format(source_path)
command = '{} && ' \
'{} {}'.format(activate_install_env, bootstrap_command,
' '.join(sys.argv[1:]))
bootstrap_command = f'{source_path}/conda/bootstrap.py'
command = f'{activate_install_env} && ' \
f'{bootstrap_command} {" ".join(sys.argv[1:])}'
if local_conda_build is not None:
command = '{} --local_conda_build {}'.format(command,
local_conda_build)
command = f'{command} --local_conda_build {local_conda_build}'
check_call(command)


Expand All @@ -58,19 +44,15 @@ def setup_install_env(env_name, activate_base, use_local, logger, recreate,
channels = '--use-local'
else:
channels = ''
packages = 'progressbar2 jinja2 {}'.format(mache)
packages = f'progressbar2 jinja2 {mache}'
if recreate or not os.path.exists(env_path):
print('Setting up a conda environment for installing compass\n')
commands = '{} && ' \
'mamba create -y -n {} {} {}'.format(activate_base,
env_name, channels,
packages)
commands = f'{activate_base} && ' \
f'mamba create -y -n {env_name} {channels} {packages}'
else:
print('Updating conda environment for installing compass\n')
commands = '{} && ' \
'mamba install -y -n {} {} {}'.format(activate_base,
env_name, channels,
packages)
commands = f'{activate_base} && ' \
f'mamba install -y -n {env_name} {channels} {packages}'

check_call(commands, logger=logger)

Expand All @@ -87,14 +69,14 @@ def main():
env_name = 'compass_bootstrap'

source_activation_scripts = \
'source {}/etc/profile.d/conda.sh && ' \
'source {}/etc/profile.d/mamba.sh'.format(conda_base, conda_base)
f'source {conda_base}/etc/profile.d/conda.sh && ' \
f'source {conda_base}/etc/profile.d/mamba.sh'

activate_base = '{} && mamba activate'.format(source_activation_scripts)
activate_base = f'{source_activation_scripts} && conda activate'

activate_install_env = \
'{} && ' \
'mamba activate {}'.format(source_activation_scripts, env_name)
f'{source_activation_scripts} && ' \
f'conda activate {env_name}'
try:
os.makedirs('conda/logs')
except OSError:
Expand All @@ -117,24 +99,23 @@ def main():

if local_mache:
print('Clone and install local mache\n')
commands = '{}; ' \
'rm -rf conda/build_mache; ' \
'mkdir -p conda/build_mache; ' \
'cd conda/build_mache; ' \
'git clone -b {} [email protected]:{}.git mache; ' \
'cd mache; ' \
'python -m pip install .'.format(activate_install_env,
args.mache_branch,
args.mache_fork)
commands = f'{activate_install_env} && ' \
f'rm -rf conda/build_mache && ' \
f'mkdir -p conda/build_mache && ' \
f'cd conda/build_mache && ' \
f'git clone -b {args.mache_branch} ' \
f'[email protected]:{args.mache_fork}.git mache && ' \
f'cd mache && ' \
f'python -m pip install .'

check_call(commands, logger=logger)

env_type = config.get('deploy', 'env_type')
if env_type not in ['dev', 'test_release', 'release']:
raise ValueError('Unexpected env_type: {}'.format(env_type))
raise ValueError(f'Unexpected env_type: {env_type}')

if env_type == 'test_release' and args.use_local:
local_conda_build = os.path.abspath('{}/conda-bld'.format(conda_base))
local_conda_build = os.path.abspath(f'{conda_base}/conda-bld')
else:
local_conda_build = None

Expand Down
39 changes: 19 additions & 20 deletions conda/shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,9 @@ def get_conda_base(conda_base, config, shared=False, warn=False):
conda_base = os.path.abspath(
os.path.join(conda_exe, '..', '..'))
if warn:
print('\nWarning: --conda path not supplied. Using conda '
'installed at:\n'
' {}\n'.format(conda_base))
print(f'\nWarning: --conda path not supplied. Using conda '
f'installed at:\n'
f' {conda_base}\n')
else:
raise ValueError('No conda base provided with --conda and '
'none could be inferred.')
Expand All @@ -120,9 +120,9 @@ def get_spack_base(spack_base, config):
def check_call(commands, env=None, logger=None):
print_command = '\n '.join(commands.split(' && '))
if logger is None:
print('\n Running:\n {}\n'.format(print_command))
print(f'\n Running:\n {print_command}\n')
else:
logger.info('\nrunning:\n {}\n'.format(print_command))
logger.info(f'\nrunning:\n {print_command}\n')

if logger is None:
process = subprocess.Popen(commands, env=env, executable='/bin/bash',
Expand Down Expand Up @@ -156,8 +156,8 @@ def install_miniconda(conda_base, activate_base, logger):
system = 'MacOSX'
else:
system = 'Linux'
miniconda = 'Mambaforge-{}-x86_64.sh'.format(system)
url = 'https://github.com/conda-forge/miniforge/releases/latest/download/{}'.format(miniconda) # noqa: E501
miniconda = f'Mambaforge-{system}-x86_64.sh'
url = f'https://github.com/conda-forge/miniforge/releases/latest/download/{miniconda}' # noqa: E501
print(url)
req = Request(url, headers={'User-Agent': 'Mozilla/5.0'})
f = urlopen(req)
Expand All @@ -166,31 +166,30 @@ def install_miniconda(conda_base, activate_base, logger):
outfile.write(html)
f.close()

command = '/bin/bash {} -b -p {}'.format(miniconda, conda_base)
command = f'/bin/bash {miniconda} -b -p {conda_base}'
check_call(command, logger=logger)
os.remove(miniconda)

backup_bashrc()

print('Doing initial setup\n')

commands = '{} && ' \
'conda config --add channels conda-forge && ' \
'conda config --set channel_priority strict' \
''.format(activate_base)
commands = f'{activate_base} && ' \
f'conda config --add channels conda-forge && ' \
f'conda config --set channel_priority strict'

check_call(commands, logger=logger)

commands = '{} && ' \
'conda remove -y boa'.format(activate_base)
commands = f'{activate_base} && ' \
f'conda remove -y boa'
try:
check_call(commands, logger=logger)
except subprocess.CalledProcessError:
pass

commands = '{} && ' \
'mamba update -y --all && ' \
'mamba init'.format(activate_base)
commands = f'{activate_base} && ' \
f'mamba update -y --all && ' \
f'mamba init'

check_call(commands, logger=logger)

Expand All @@ -202,7 +201,7 @@ def backup_bashrc():
files = ['.bashrc', '.bash_profile']
for filename in files:
src = os.path.join(home_dir, filename)
dst = os.path.join(home_dir, '{}.conda_bak'.format(filename))
dst = os.path.join(home_dir, f'{filename}.conda_bak')
if os.path.exists(src):
shutil.copyfile(src, dst)

Expand All @@ -211,14 +210,14 @@ def restore_bashrc():
home_dir = os.path.expanduser('~')
files = ['.bashrc', '.bash_profile']
for filename in files:
src = os.path.join(home_dir, '{}.conda_bak'.format(filename))
src = os.path.join(home_dir, f'{filename}.conda_bak')
dst = os.path.join(home_dir, filename)
if os.path.exists(src):
shutil.move(src, dst)


def get_logger(name, log_filename):
print('Logging to: {}\n'.format(log_filename))
print(f'Logging to: {log_filename}\n')
try:
os.remove(log_filename)
except OSError:
Expand Down

0 comments on commit 9f1c219

Please sign in to comment.