summaryrefslogtreecommitdiff
path: root/morphlib/yapp.py
blob: c0e41fdb110a93857b997f69c3eb8988ceaeb52c (plain)
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# Copyright (C) 2014  Codethink Limited
#
# 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; version 2 of the License.
#
# 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, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# =*= License: GPL-2 =*=

import contextlib
import os
import datetime
import shutil
from subprocess import check_output
from subprocess import call
from multiprocessing import cpu_count
settings = {}


def log(component, message='', data=''):
    ''' Print a timestamped log. '''
    name = component
    try:
        name = component['name']
    except Exception, e:
        pass

    timestamp = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
    log_entry = '%s [%s] %s %s\n' % (timestamp, name, message, data)
    print(log_entry),


def run_cmd(this, command):
    log(this, 'Running command\n\n', command)
    with open(settings['logfile'], "a") as logfile:
        if call(['sh', '-c', command], stdout=logfile, stderr=logfile):
            log(this, 'ERROR: in directory %s command failed:' % os.getcwd(),
                command)
            raise SystemExit


@contextlib.contextmanager
def setup(target, arch):
    try:
        settings['arch'] = arch
        settings['no-ccache'] = True
        settings['cache-server-url'] = \
            'http://git.baserock.org:8080/1.0/sha1s?'
        settings['base'] = os.path.expanduser('~/.ybd/')
        if os.path.exists('/src'):
            settings['base'] = '/src'
        settings['caches'] = os.path.join(settings['base'], 'cache')
        settings['artifacts'] = os.path.join(settings['caches'],
                                             'ybd-artifacts')
        settings['gits'] = os.path.join(settings['caches'], 'gits')
        settings['staging'] = os.path.join(settings['base'], 'staging')
        timestamp = datetime.datetime.now().strftime('%Y%m%d-%H%M%S')
        settings['assembly'] = os.path.join(settings['staging'],
                                            target + '-' + timestamp)
        settings['max_jobs'] = max(int(cpu_count() * 1.5 + 0.5), 1)

        for directory in ['base', 'caches', 'artifacts', 'gits',
                          'staging', 'assembly']:
            if not os.path.exists(settings[directory]):
                os.mkdir(settings[directory])

        # git replace means we can't trust that just the sha1 of a branch
        # is enough to say what it contains, so we turn it off by setting
        # the right flag in an environment variable.
        os.environ['GIT_NO_REPLACE_OBJECTS'] = '1'


        yield

    finally:
        # assuming success, we can remove the 'assembly' directory
        # shutil.rmtree(settings['assembly'])
        log(target, 'Assembly directory is still at', settings['assembly'])


@contextlib.contextmanager
def chdir(dirname=None, env={}):
    currentdir = os.getcwd()
    currentenv = {}
    try:
        if dirname is not None:
            os.chdir(dirname)
        for key, value in env.items():
            currentenv[key] = os.environ.get(key)
            os.environ[key] = value
        yield
    finally:
        for key, value in currentenv.items():
            if value:
                os.environ[key] = value
            else:
                del os.environ[key]
        os.chdir(currentdir)


@contextlib.contextmanager
def timer(this, start_message=''):
    starttime = datetime.datetime.now()
    log(this, start_message)
    try:
        yield
    finally:
        td = datetime.datetime.now() - starttime
        hours, remainder = divmod(int(td.total_seconds()), 60*60)
        minutes, seconds = divmod(remainder, 60)
        td_string = "%02d:%02d:%02d" % (hours, minutes, seconds)
        log(this, 'Elapsed time', td_string)