summaryrefslogtreecommitdiff
path: root/morphlib/builder.py
blob: befe8485553e9c8527af8f714beb6af6c69d9e16 (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
# Copyright (C) 2011  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; either 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.


import logging
import os

import morphlib


class Builder(object):

    '''Build binary objects for Baserock.
    
    The objects may be chunks or strata.'''
    
    def __init__(self, tempdir, msg):
        self.tempdir = tempdir
        self.msg = msg

    def build(self, morph):
        '''Build a binary based on a morphology.'''
        if morph.kind == 'chunk':
            self.build_chunk(morph)
        elif morph.kind == 'stratum':
            self.build_stratum(morph)
        else:
            raise Exception('Unknown kind of morphology: %s' % morph.kind)

    def build_chunk(self, morph):
        '''Build a chunk from a morphology.'''
        logging.debug('Building chunk')
        self.ex = morphlib.execute.Execute(self._build, self.msg)
        self.ex.env['WORKAREA'] = self.tempdir.dirname
        self.ex.env['DESTDIR'] = self._inst + '/'
        self.create_build_tree(morph)
        self.ex.run(morph.configure_commands)
        self.ex.run(morph.build_commands)
        self.ex.run(morph.test_commands)
        self.ex.run(morph.install_commands)
        self.create_chunk(morph)
        self.tempdir.clear()
        
    def create_build_tree(self, morph):
        '''Export sources from git into the ``self._build`` directory.'''

        logging.debug('Creating build tree at %s' % self._build)
        tarball = self.tempdir.join('sources.tar')
        self.ex.runv(['git', 'archive',
                      '--output', tarball,
                      '--remote', morph.source['repo'],
                      morph.source['ref']])
        os.mkdir(self._build)
        self.ex.runv(['tar', '-C', self._build, '-xf', tarball])
        os.remove(tarball)

    def create_chunk(self, morph):
        '''Create a Baserock chunk from the ``self._inst`` directory.
        
        The directory must be filled in with all the relevant files already.
        
        '''

        dirname = os.path.dirname(morph.filename)
        filename = os.path.join(dirname, '%s.chunk' % morph.name)
        logging.debug('Creating chunk %s at %s' % (morph.name, filename))
        self.ex.runv(['tar', '-C', self._inst, '-czf', filename, '.'])

    def build_stratum(self, morph):
        '''Build a stratum from a morphology.'''
        os.mkdir(self._inst)
        self.ex = morphlib.execute.Execute(self.tempdir.dirname, self.msg)
        for chunk_name in morph.sources:
            filename = self._chunk_filename(morph, chunk_name)
            self.unpack_chunk(filename)
        self.create_stratum(morph)
        self.tempdir.clear()

    def unpack_chunk(self, filename):
        self.ex.runv(['tar', '-C', self._inst, '-xf', filename])

    def create_stratum(self, morph):
        '''Create a Baserock stratum from the ``self._inst`` directory.
        
        The directory must be filled in with all the relevant files already.
        
        '''

        dirname = os.path.dirname(morph.filename)
        filename = os.path.join(dirname, '%s.stratum' % morph.name)
        logging.debug('Creating stratum %s at %s' % (morph.name, filename))
        self.ex.runv(['tar', '-C', self._inst, '-czf', filename, '.'])

    @property
    def _build(self):
        return self.tempdir.join('build')

    @property
    def _inst(self):
        return self.tempdir.join('inst')

    def _chunk_filename(self, morph, chunk_name):
        dirname = os.path.dirname(morph.filename)
        return os.path.join(dirname, '%s.chunk' % chunk_name)