hgitaly-test-build 4.19 KB
Newer Older
1
2
3
4
5
#!/usr/bin/env python3
"""Clone and prepare py_heptapod for CI build of the Rails applications.

Expected to be run from a clone of the Rails application, and assumes
that `config/gitlab.yml` has already been initialized.
6
7
8
9
10

In the prebuild phase, the relevant that it does is to prepare the
clones. The installation in the interpreter will be lost and redone in
the actual testing phase. (TODO add a flag to skip it, although it's
probably negligible, given than most deps are in the base image)
11
12
"""
import os
13
import re
14
import subprocess
15
import sys
16

17
INTERPRETER = 'python3.8'
18
RELEASE_BRANCH_RX = re.compile(r'heptapod-\d+-\d+')
19
20

CWD = os.getcwd()
21
22
23
BASE_DIR = os.path.join(CWD, 'tmp', 'tests', 'hgitaly')
# lazy, but I'm tired of reimplenting `mkdir -p` over and over
subprocess.check_call(['mkdir', '-p', BASE_DIR])
24

25
26
PY_HEPTAPOD_CLONE_PATH = os.path.join(BASE_DIR, 'py-heptapod')
HGITALY_CLONE_PATH = os.path.join(BASE_DIR, 'hgitaly')
27

28
29
HG_BIN = '/usr/local/bin/hg'
PIP_BIN = 'pip3.8'
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

def write_config(req_hgrc_path):
    local_hgrc_path = os.path.join(CWD, 'config', 'tests.hgrc')
    with open(local_hgrc_path, 'w') as local_hgrcf:
        local_hgrcf.write("\n".join((
            "[heptapod]",
            "# we won't really send the pre- and post-receive hooks, ",
            "# but `heptapod.hooks.Hook` is currently still instantiated and ",
            "# that requires the configuration.",
            "gitlab-internal-api-secret-file = " + os.path.join(
                CWD, '.gitlab_shell_secret'),
            "")))

    yml_format = 'hgrc:\n      - %s\n      - %s\n    bin_path: %s'

    with open('config/gitlab.yml', 'r+') as conf:
        out_lines = [line.replace('# hgrc:',
                                  yml_format % (local_hgrc_path,
                                                req_hgrc_path,
                                                HG_BIN))
                     for line in conf.readlines()]
        conf.seek(0)
        conf.writelines(out_lines)


56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
CI_TAG = os.environ.get('CI_COMMIT_TAG')
# probably not set for tag pipelines:
CI_BRANCH = os.environ.get('CI_COMMIT_HG_BRANCH', '')
if CI_TAG or RELEASE_BRANCH_RX.match(CI_BRANCH):
    print("*** ATTENTION Pipeline running for a tag or a release branch ***")
    print("*** ==> installing the fixed `python/requirements.txt`       ***")
    # just install the fixed deps, that's what is meant
    subprocess.check_call((PIP_BIN, 'install', '--upgrade',
                           '-r', 'python/requirements.txt'))
    hepta_mod = subprocess.check_output(
        (INTERPRETER, '-c', 'import heptapod; print(heptapod.__file__)'),
        cwd='/')
    write_config(os.path.join(os.path.dirname(hepta_mod), 'required.hgrc'))
    sys.exit(0)
elif CI_BRANCH == 'heptapod-stable':
    PYDEPS_BRANCH = 'stable'
else:
    PYDEPS_BRANCH = 'default'

75
76
77
print("Upgrading hg-evolve")
subprocess.check_call((PIP_BIN, 'install', '--upgrade', 'hg-evolve'))

78
79
80
81
82
HG_CMD = (HG_BIN,
          '--config', 'extensions.evolve=',
          '--config', 'extensions.topic=',
          )

83
# yes, Mercurial is readily available in GitLab base CI image!
84
# It is installed in the Python 3.8 interpreter
85
86
87
88
for slug, target in (('py-heptapod', PY_HEPTAPOD_CLONE_PATH),
                     ('hgitaly', HGITALY_CLONE_PATH)):
    if os.path.exists(target):
        print("Updating %s clone at %s" % (slug, target))
89
        subprocess.check_call(HG_CMD + ('-R', target, 'pull'))
90
91
        subprocess.check_call(HG_CMD + ('-R', target, 'update',
                                        '-C', PYDEPS_BRANCH))
92
93
    else:
        print("Cloning %s into %s" % (slug, target))
94
95
        subprocess.check_call(HG_CMD + (
            'clone', '-q',
96
            'https://foss.heptapod.net/heptapod/' + slug,
97
98
            target,
        ))
99
100

    print("Current revision for %s:" % target)
101
    subprocess.check_call(HG_CMD + ('-R', target, 'log', '-r', '.'))
102
103
104
105

    pip_cmd = (PIP_BIN, 'install', '-e', target)
    print("Installing %s: %r" % (slug, pip_cmd))
    subprocess.check_call(pip_cmd)
106
107

pip_logging_cmd = (PIP_BIN, 'install', 'hg-loggingmod')
108
print("Installing hg-loggingmod: " + repr(pip_logging_cmd))
109
110
subprocess.check_call(pip_logging_cmd)

111
112
write_config(os.path.join(PY_HEPTAPOD_CLONE_PATH,
                          'heptapod', 'required.hgrc'))