| 1 |
import argparse |
| 2 |
import json |
| 3 |
import os |
| 4 |
import shutil |
| 5 |
import subprocess as sp |
| 6 |
|
| 7 |
|
| 8 |
parser = argparse.ArgumentParser(description='Creates a conda environment from file for a given Python version.') |
| 9 |
parser.add_argument('-n', '--name', type=str, nargs=1, |
| 10 |
help='The name of the created Python environment') |
| 11 |
parser.add_argument('-p', '--python', type=str, nargs=1, |
| 12 |
help='The version of the created Python environment') |
| 13 |
parser.add_argument('conda_file', nargs='*', |
| 14 |
help='The file for the created Python environment') |
| 15 |
|
| 16 |
args = parser.parse_args() |
| 17 |
|
| 18 |
with open(args.conda_file[0], "r") as handle: |
| 19 |
script = handle.read() |
| 20 |
|
| 21 |
tmp_file = "tmp_env.yaml" |
| 22 |
script = script.replace("- python", "- python {}*".format(args.python[0])) |
| 23 |
|
| 24 |
with open(tmp_file, "w") as handle: |
| 25 |
handle.write(script) |
| 26 |
|
| 27 |
|
| 28 |
if "CONDA_EXE" in os.environ: |
| 29 |
conda_path = os.environ["CONDA_EXE"] |
| 30 |
else: |
| 31 |
conda_path = shutil.which("conda") |
| 32 |
|
| 33 |
print("CONDA ENV NAME {}".format(args.name[0])) |
| 34 |
print("PYTHON VERSION {}".format(args.python[0])) |
| 35 |
print("CONDA FILE NAME {}".format(args.conda_file[0])) |
| 36 |
print("CONDA path {}".format(conda_path)) |
| 37 |
|
| 38 |
sp.call("{} env create -n {} -f {}".format(conda_path, args.name[0], tmp_file), shell=True) |
| 39 |
os.unlink(tmp_file) |