~cprov/tanuki-continuous-deployer/local-provider

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
#!/usr/bin/env python3
#
# Copyright (C) 2015 Canonical
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# called-by-tarmac.py is intended to be called by the tarmac merge engine
#   to perform automatic unit testing on branch landing.
#   It replaces the manual commands:
#       bzr branch $PIP_CACHE_BRANCH pip_cache
#       virtualenv -p python3 ve && \
#           ./ve/bin/pip install -r test_requirements.txt && \
#           ./ve/bin/python3 setup.py test && \
#           ./ve/bin/python3 setup.py flake8 && \
#           rm -rf ve
#

import os
import subprocess
import sys
import tempfile

SERVICE_NAME = 'adt-continuous-deployer'
VENV_DIR = 'venv-{}'.format(SERVICE_NAME)


def _run_command(cmd):
    '''Run the command and return the return code or 0 on success.'''

    print('INFO: Executing: {}'.format(' '.join(cmd)))
    try:
        subprocess.check_call(cmd, stderr=subprocess.STDOUT)
    except subprocess.CalledProcessError as e:
        print('ERROR: Calling `{}` failed (with code {})'.format(
            e.cmd, e.returncode))
        return e.returncode
    return 0


def main():
    with tempfile.TemporaryDirectory(prefix=VENV_DIR) as ve_dir:
        all_cmds = (
            [
                'virtualenv',
                '-p',
                'python3',
                ve_dir,
            ],
            [
                os.path.join(ve_dir, 'bin', 'pip'),
                'install',
                '-r',
                'test_requirements.txt',
            ],
            [
                os.path.join(ve_dir, 'bin', 'python3'),
                'setup.py',
                'test',
            ],
            [
                os.path.join(ve_dir, 'bin', 'python3'),
                'setup.py',
                'flake8',
            ],
        )
        for cmd in all_cmds:
            ret = _run_command(cmd)
            if ret:
                # A cmd failed, bubble up the return code to the caller
                return ret
    return 0


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