deploy_conda_package.py 3.3 KB
Newer Older
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/usr/bin/env python
#
# Deploy a built conda package to a FSL conda channel.
#
# Deploying a built conda package is simply a matter of copying the
# built packages into the channel directory and running "conda index".
#
# Author: Paul McCarthy <pauldmccarthy@gmail.com>
#


import               glob
import itertools  as it
import os.path    as op
import               os
import               shutil
import               sys

from fsl_ci import fprint, sprun, lockdir


def reindex_channel(channeldir):
    """Run "conda index" on the conda channel directory.
    """
    with lockdir(channeldir):
        # The conda index command doesn't seem to reliably
        # refresh information about newly added packages,
        # so we delete the old index metadata before running
        # it.
        sprun(f'find {channeldir} -name "*.html"     -delete')
        sprun(f'find {channeldir} -name "*.json"     -delete')
        sprun(f'find {channeldir} -name "*.json.bz2" -delete')
        sprun(f'conda index {channeldir}')


def copy_packages(builddir, channeldir):
    """copy all package files from the conda-build output directory into
    the conda channel directory. Returns the number of package files
    found and copied.
    """

    count = 0

    # The build_conda_package.py script stores its
    # outputs in a platform-specific subdirectory -
    # one of _linux_, _macos_, or _noarch_. Within
    # each one of those is a conventional conda
    # channel structure.
    fslplats   = ('_linux_', '_macos_', '_noarch_')
    condaplats = ('noarch', 'linux-64', 'osx-64', 'win-32', 'win64')

    for fslplat, condaplat in it.product(fslplats, condaplats):
        # copy any package files that we find in
        # the build dir into the channel directory.
        pkgs    = glob.glob(op.join(builddir, fslplat, condaplat, '*.tar.bz2'))
        destdir = op.join(channeldir, condaplat)
        count  += len(pkgs)

        for pkg in pkgs:
            fprint(f'Installing {pkg}...')
            os.makedirs(destdir, exist_ok=True)
            shutil.copy(pkg, destdir)

    return count


def main():

    recipename = os.environ['CI_PROJECT_NAME']
    recipeurl  = os.environ['CI_PROJECT_URL']
    projrepo   = os.environ.get('FSLCONDA_=REPOSITORY', '')
    projrev    = os.environ.get('FSLCONDA_REVISION',    '')
    builddir   = op.join(os.getcwd(), 'conda_build')

    if os.environ.get('STAGING', 'true'):
        channeldir = os.environ['FSLCONDA_STAGING_CHANNEL_DIRECTORY']
    else:
        channeldir = os.environ['FSLCONDA_PRODUCTION_CHANNEL_DIRECTORY']

    fprint( '************************************')
    fprint(f'Deploying built conda package for:      {recipename}')
    fprint(f'Recipe URL:                             {recipeurl}')
    fprint( 'Project repository (empty means build ')
    fprint(f'  is from repo specified in meta.yaml): {projrepo}')
    fprint( 'Project revision (empty means build is ')
    fprint(f'  from ref specified in meta.yaml):     {projrev}')
    fprint(f'FSL conda channel:                      {channeldir}')
    fprint( '************************************')

    npkgs = copy_packages(builddir, channeldir)

    if npkgs > 0:
        reindex_channel(channeldir)
    else:
        print('No built conda packages found - aborting.')
        return 1

    return 0


if __name__ == '__main__':
    sys.exit(main())