aboutsummaryrefslogtreecommitdiff
path: root/delivery_merge/cli/merge.py
blob: a9b94b40939ec5d122b41e0492d4840944b27436 (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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import os
from ..conda import conda, conda_installer, conda_init_path, ei_touch
from ..merge import (
    env_combine,
    testable_packages,
    integration_test,
    force_yaml_channels
)
from argparse import ArgumentParser


def main():
    parser = ArgumentParser()
    parser.add_argument('--env-name', default='delivery',
                        help='name of conda environment')
    parser.add_argument('--output-dir',
                        default=os.path.normpath('./delivery'),
                        help='path to store delivery data')
    parser.add_argument('--installer-version',
                        required=True,
                        help='miniconda3 installer version')
    parser.add_argument('--run-tests',
                        action='store_true',
                        help='scan packages in base_spec for tests to execute')
    parser.add_argument('--dmfile',
                        required=True,
                        help='file with list providing packages to merge')
    parser.add_argument('base_spec',
                        help='@EXPLICIT dump file')
    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 = args.output_dir
    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)
    ei_touch()

    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())
        proc.check_returncode()

    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("Fix up yaml channel order...")
    force_yaml_channels(yamlfile, channels)

    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:
        results = os.path.join(delivery_root, 'results')
        for package in testable_packages(args.dmfile, prefix):
            print(f"Running tests: {package}")
            integration_test(package, name, results_root=results)

    print("Done!")