Skip to content
Snippets Groups Projects
Commit d3217029 authored by Klaus Reuter's avatar Klaus Reuter
Browse files

add support for pip requirements.txt file

parent a89d6ea1
No related branches found
No related tags found
No related merge requests found
Pipeline #181649 passed
...@@ -106,8 +106,10 @@ to the same directory. ...@@ -106,8 +106,10 @@ to the same directory.
### Build and compress an environment using `cnd build` ### Build and compress an environment using `cnd build`
Build the conda environment specified in `environment.yml` and Build the conda environment specified in `environment.yml`. In case
create a compressed squashfs image. a file `requirements.txt` is present, its contents will be installed
additionally using `pip`. Finally, create a compressed
squashfs image, and delete the files from staging the environment.
### Execute a command using `cnd exec` ### Execute a command using `cnd exec`
......
...@@ -27,12 +27,13 @@ class termcol: ...@@ -27,12 +27,13 @@ class termcol:
def get_example_environment_yml(): def get_example_environment_yml():
raw = \ raw = \
""" """
name: basicnumpy name: basic
channels: channels:
- conda-forge - conda-forge
dependencies: dependencies:
- python=3.9 - python=3.9
- numpy - pip
#- numpy
""" """
return yaml.safe_load(raw) return yaml.safe_load(raw)
...@@ -174,18 +175,40 @@ def create_condainer_environment(cfg): ...@@ -174,18 +175,40 @@ def create_condainer_environment(cfg):
exe = os.path.join(os.path.join(env_directory, 'bin'), cfg['conda_exe']) exe = os.path.join(os.path.join(env_directory, 'bin'), cfg['conda_exe'])
environment_yml = cfg["environment_yml"] environment_yml = cfg["environment_yml"]
cmd = f"{exe} env create --file {environment_yml} --name condainer".split() cmd = f"{exe} env create --file {environment_yml} --name condainer".split()
proc = subprocess.Popen(cmd, shell=False) env = copy.deepcopy(os.environ)
if "PYTHONPATH" in env:
del env["PYTHONPATH"]
proc = subprocess.Popen(cmd, shell=False, env=env)
proc.communicate() proc.communicate()
assert(proc.returncode == 0) assert(proc.returncode == 0)
def pip_condainer_environment(cfg):
"""Install user-defined software stack (requirements.txt) into 'condainer' environment.
"""
env_directory = get_env_directory(cfg)
exe = os.path.join(os.path.join(env_directory, 'bin'), 'pip3')
requirements_txt = cfg["requirements_txt"]
if os.path.isfile(requirements_txt):
cmd = f"{exe} install --requirement {requirements_txt} --no-cache-dir".split()
env = copy.deepcopy(os.environ)
if "PYTHONPATH" in env:
del env["PYTHONPATH"]
proc = subprocess.Popen(cmd, shell=False, env=env)
proc.communicate()
assert(proc.returncode == 0)
def clean_environment(cfg): def clean_environment(cfg):
"""Delete pkg files and other unnecessary files from base environment. """Delete pkg files and other unnecessary files from base environment.
""" """
env_directory = get_env_directory(cfg) env_directory = get_env_directory(cfg)
exe = os.path.join(os.path.join(env_directory, 'bin'), cfg['conda_exe']) exe = os.path.join(os.path.join(env_directory, 'bin'), cfg['conda_exe'])
cmd = f"{exe} clean --all --yes".split() cmd = f"{exe} clean --all --yes".split()
proc = subprocess.Popen(cmd, shell=False) env = copy.deepcopy(os.environ)
if "PYTHONPATH" in env:
del env["PYTHONPATH"]
proc = subprocess.Popen(cmd, shell=False, env=env)
proc.communicate() proc.communicate()
assert(proc.returncode == 0) assert(proc.returncode == 0)
...@@ -227,6 +250,7 @@ def init(args): ...@@ -227,6 +250,7 @@ def init(args):
cfg = {} cfg = {}
cfg['environment_yml'] = 'environment.yml' cfg['environment_yml'] = 'environment.yml'
cfg["requirements_txt"] = 'requirements.txt'
cfg['installer_url'] = installer_url cfg['installer_url'] = installer_url
cfg['conda_exe'] = 'mamba' cfg['conda_exe'] = 'mamba'
cfg['mount_base_directory'] = '/tmp' cfg['mount_base_directory'] = '/tmp'
...@@ -276,6 +300,7 @@ def build(args): ...@@ -276,6 +300,7 @@ def build(args):
os.makedirs(env_directory, exist_ok=True, mode=0o700) os.makedirs(env_directory, exist_ok=True, mode=0o700)
create_base_environment(cfg) create_base_environment(cfg)
create_condainer_environment(cfg) create_condainer_environment(cfg)
pip_condainer_environment(cfg)
clean_environment(cfg) clean_environment(cfg)
compress_environment(cfg) compress_environment(cfg)
write_activate_script(cfg) write_activate_script(cfg)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment