aboutsummaryrefslogtreecommitdiff
path: root/delivery_merge/cli/merge.py
blob: 204ab91fd67ff288dae867c47ef5faa56b7d5dca (plain) (blame)
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import os
from ..conda import conda, conda_installer, conda_init_path
from ..merge import env_combine, testable_packages, integration_test
from argparse import ArgumentParser


def main():
    parser = ArgumentParser()
    parser.add_argument('--env-name', default='delivery',
                        help='name of environment')
    parser.add_argument('--installer-version', required=True,
                        help='miniconda3 installer version')
    parser.add_argument('--run-tests', action='store_true')
    parser.add_argument('--dmfile', required=True)
    parser.add_argument('base_spec')
    args = parser.parse_args()

    name = args.env_name
    base_spec = args.base_spec
    dmfile = args.dmfile
    channels = ['http://ssb.stsci.edu/astroconda',
                'defaults',
                'http://ssb.stsci.edu/astroconda-dev']
    delivery_root = 'delivery'
    yamlfile = os.path.join(delivery_root, name + '.yml')
    specfile = os.path.join(delivery_root, name + '.txt')

    if not os.path.exists(delivery_root):
        os.mkdir(delivery_root, 0o755)

    prefix = conda_installer(args.installer_version)
    conda_init_path(prefix)

    if not os.path.exists(os.path.join(prefix, 'envs', name)):
        print(f"Creating environment {name}...")
        proc = conda('create', '-q', '-y', '-n',  name, '--file', base_spec)
        if proc.stderr:
            print(proc.stderr.decode())

    print(f"Merging requested packages into environment: {name}")
    env_combine(dmfile, name, channels)

    print("Exporting yaml configuration...")
    conda('env', 'export', '-n', name, '--file', yamlfile)

    print("Exporting explicit dump...")
    with open(specfile, 'w+') as spec:
        proc = conda('list', '--explicit', '-n', name)
        spec.write(proc.stdout.decode())

    if args.run_tests:
        for package in testable_packages(args.dmfile, prefix):
            print(f"Running tests: {package}")
            integration_test(package, name)

    print("Done!")