File: conda_env.py

package info (click to toggle)
gau2grid 2.0.8-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 688 kB
  • sloc: python: 5,326; makefile: 31; sh: 24
file content (39 lines) | stat: -rw-r--r-- 1,295 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import argparse
import json
import os
import shutil
import subprocess as sp

# Args
parser = argparse.ArgumentParser(description='Creates a conda environment from file for a given Python version.')
parser.add_argument('-n', '--name', type=str, nargs=1,
                    help='The name of the created Python environment')
parser.add_argument('-p', '--python', type=str, nargs=1,
                    help='The version of the created Python environment')
parser.add_argument('conda_file', nargs='*',
                    help='The file for the created Python environment')

args = parser.parse_args()

with open(args.conda_file[0], "r") as handle:
    script = handle.read()

tmp_file = "tmp_env.yaml"
script = script.replace("- python", "- python {}*".format(args.python[0]))

with open(tmp_file, "w") as handle:
    handle.write(script)

# Figure out conda path
if "CONDA_EXE" in os.environ:
    conda_path = os.environ["CONDA_EXE"]
else:
    conda_path = shutil.which("conda")

print("CONDA ENV NAME  {}".format(args.name[0]))
print("PYTHON VERSION  {}".format(args.python[0]))
print("CONDA FILE NAME {}".format(args.conda_file[0]))
print("CONDA path      {}".format(conda_path))

sp.call("{} env create -n {} -f {}".format(conda_path, args.name[0], tmp_file), shell=True)
os.unlink(tmp_file)