~joetalbott/+junk/fix_tmpdir

« back to all changes in this revision

Viewing changes to called-by-tarmac.py

  • Committer: Joe Talbott
  • Date: 2015-05-19 21:37:40 UTC
  • Revision ID: joe.talbott@canonical.com-20150519213740-48l0u6bmecbyg9mt
Add core-image-publisher code with name changes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python3
 
2
#
 
3
# core-image-builder
 
4
# Copyright (C) 2015 Canonical
 
5
#
 
6
# This program is free software: you can redistribute it and/or modify
 
7
# it under the terms of the GNU General Public License as published by
 
8
# the Free Software Foundation, either version 3 of the License, or
 
9
# (at your option) any later version.
 
10
#
 
11
# This program is distributed in the hope that it will be useful,
 
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
# GNU General Public License for more details.
 
15
#
 
16
# You should have received a copy of the GNU General Public License
 
17
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
18
#
 
19
# called-by-tarmac.py is intended to be called by the tarmac merge engine
 
20
#   to perform automatic unit testing on branch landing.
 
21
#   It replaces the manual commands:
 
22
#       bzr branch $PIP_CACHE_BRANCH pip_cache
 
23
#       virtualenv -p python3 ve && \
 
24
#           ./ve/bin/pip install --no-index --find-links=pip-cache \
 
25
#              -r requirements.txt && \
 
26
#           ./ve/bin/pip install -r test_requirements.txt && \
 
27
#           ./ve/bin/python3 setup.py test && \
 
28
#           rm -rf ve
 
29
#
 
30
 
 
31
import os
 
32
import subprocess
 
33
import sys
 
34
import tempfile
 
35
 
 
36
SERVICE_NAME = 'core-image-builder'
 
37
VENV_DIR = 'venv-{}'.format(SERVICE_NAME)
 
38
PIP_DIR = 'pip-cache-'.format(SERVICE_NAME)
 
39
PIP_CACHE_BRANCH = 'lp:~canonical-ci-engineering/{}/' \
 
40
    'pip-cache'.format(SERVICE_NAME)
 
41
 
 
42
 
 
43
def _run_command(cmd):
 
44
    '''Run the command and return the return code or 0 on success.'''
 
45
 
 
46
    print('INFO: Executing: {}'.format(' '.join(cmd)))
 
47
    try:
 
48
        subprocess.check_call(cmd, stderr=subprocess.STDOUT)
 
49
    except subprocess.CalledProcessError as e:
 
50
        print('ERROR: Calling `{}` failed (with code {})'.format(
 
51
            e.cmd, e.returncode))
 
52
        return e.returncode
 
53
    return 0
 
54
 
 
55
 
 
56
def main():
 
57
    with tempfile.TemporaryDirectory(PIP_DIR) as pip_dir, \
 
58
            tempfile.TemporaryDirectory(prefix=VENV_DIR) as ve_dir:
 
59
        pip_cache = os.path.join(pip_dir, 'cache')
 
60
        all_cmds = (
 
61
            [
 
62
                'bzr',
 
63
                'branch',
 
64
                PIP_CACHE_BRANCH,
 
65
                pip_cache,
 
66
            ],
 
67
            [
 
68
                'virtualenv',
 
69
                '-p',
 
70
                'python3',
 
71
                ve_dir,
 
72
            ],
 
73
            [
 
74
                os.path.join(ve_dir, 'bin', 'pip'),
 
75
                'install',
 
76
                '--no-index',
 
77
                '--find-links={}'.format(pip_cache),
 
78
                '-r',
 
79
                'requirements.txt',
 
80
            ],
 
81
            [
 
82
                os.path.join(ve_dir, 'bin', 'pip'),
 
83
                'install',
 
84
                '-r',
 
85
                'test_requirements.txt',
 
86
            ],
 
87
            [
 
88
                os.path.join(ve_dir, 'bin', 'python3'),
 
89
                'setup.py',
 
90
                'test',
 
91
            ],
 
92
            [
 
93
                os.path.join(ve_dir, 'bin', 'python3'),
 
94
                'setup.py',
 
95
                'flake8',
 
96
            ],
 
97
        )
 
98
        for cmd in all_cmds:
 
99
            ret = _run_command(cmd)
 
100
            if ret:
 
101
                # A cmd failed, bubble up the return code to the caller
 
102
                return ret
 
103
    return 0
 
104
 
 
105
 
 
106
if __name__ == '__main__':
 
107
    sys.exit(main())