update_manifest.py 2.88 KB
Newer Older
1
2
3
4
#!/usr/bin/env python
#
# This script is called when a new version of the fslinstaller.py script is
# tagged. It opens a merge request on the GitLab fsl/conda/manifest project,
Paul McCarthy's avatar
Paul McCarthy committed
5
# to update the FSL release manifest file so that it contains the new installer
6
7
8
9
10
# version string.
#


import os
Paul McCarthy's avatar
Paul McCarthy committed
11
import re
12
13
14
15
16
17

from fsl_ci        import (USERNAME,
                           EMAIL,
                           indir,
                           tempdir,
                           sprun)
18
19
from fsl_ci.gitlab import (gen_repository_url,
                           open_merge_request,
20
21
22
23
24
                           gen_branch_name)


MANIFEST_PATH = 'fsl/conda/manifest'

Paul McCarthy's avatar
Paul McCarthy committed
25
COMMIT_MSG = 'MNT: Update fslinstaller version to latest [{}] ' \
26
27
28
             'in FSL installer manifest'

MERGE_REQUEST_MSG = """
Paul McCarthy's avatar
Paul McCarthy committed
29
30
31
This MR was automatically opened as a result of a new tag being added
to the fsl/installer> project. It updates the installer version in
fsl-release.yml to the latest installer version.
32
33
34
""".strip()


Paul McCarthy's avatar
Paul McCarthy committed
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
def update_manifest(version):

    with open('fsl-release.yml', 'rt') as f:
        lines = list(f.readlines())

    pat     = r'installer:.*'
    updated = False

    for i, line in enumerate(lines):
        if re.match(pat, line):
            lines[i] = f'installer: {version}\n'
            updated = True
            break

    if not updated:
        for line in lines:
            print(line.rstrip())
        raise RuntimeError('Could not find installer section '
                           'in fsl-release.yml')

    with open('fsl-release.yml', 'wt') as f:
        for line in lines:
            f.write(line)


60
def checkout_and_update_manifest(server, token, tag, base_branch):
61

62
    manifest_url  = gen_repository_url(MANIFEST_PATH, server, token)
Paul McCarthy's avatar
Paul McCarthy committed
63
    branch        = f'mnt/{base_branch}/installer-{tag}'
64
    branch        = gen_branch_name(branch, MANIFEST_PATH, server, token)
Paul McCarthy's avatar
Paul McCarthy committed
65
    msg           = COMMIT_MSG.format(tag)
Paul McCarthy's avatar
Paul McCarthy committed
66
67
68
69
70
71

    with tempdir():
        sprun(f'git clone {manifest_url} manifest')
        with indir('manifest'):
            sprun(f'git config user.name  {USERNAME}')
            sprun(f'git config user.email {EMAIL}')
Paul McCarthy's avatar
Paul McCarthy committed
72
            sprun(f'git checkout -b {branch} origin/{base_branch}')
Paul McCarthy's avatar
Paul McCarthy committed
73
74
75
76
            update_manifest(tag)
            sprun( 'git add *')
            sprun(f'git commit -m "{msg}"')
            sprun(f'git push origin {branch}')
77
78
79
80
81
82
83
84
85
86

    return branch


def main(server=None, token=None, tag=None):

    if server is None: server = os.environ['CI_SERVER_URL']
    if token  is None: token  = os.environ['FSL_CI_API_TOKEN']
    if tag    is None: tag    = os.environ['CI_COMMIT_TAG']

87
88
89
90
91
92
93
94
    destination = 'master'
    branch = checkout_and_update_manifest(server, token, tag, destination)
    open_merge_request(MANIFEST_PATH,
                       branch,
                       MERGE_REQUEST_MSG,
                       server,
                       token,
                       destination)
95
96
97
98


if __name__ == '__main__':
    main()