summaryrefslogtreecommitdiff
path: root/scripts/scriptslib.py
blob: 1f79fb955355d8bae29705b822ecd5b41f4da7f5 (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# Copyright (C) 2016 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.

# Small library of useful things for the scripts that live here.

import yaml
import subprocess
import os
import sys

aliases = {
  'baserock:': 'git://%(trove)s/baserock/',
  'freedesktop:': 'git://anongit.freedesktop.org/',
  'github:': 'git://github.com/',
  'gnome:': 'git://git.gnome.org/',
  'upstream:': 'git://%(trove)s/delta/'
}

def parse_repo_alias(repo, trove_host='git.baserock.org'):
    global aliases
    remote = repo[:repo.find(':') + 1]
    aliases = {k: v % {'trove': trove_host} for k, v in aliases.iteritems()}
    try:
        return repo.replace(remote, aliases[remote])
    except KeyError as e:
        raise Exception("Unknown repo-alias \"%s\"" % repo)

def definitions_root():
    return subprocess.check_output(
        ["git", "rev-parse", "--show-toplevel"]).strip()

def load_yaml_file(yaml_file):
    with open(yaml_file, 'r') as f:
        return yaml.safe_load(f)


class BaserockMeta(object):
    '''An object representing Baserock metadata contained in a Baserock
       system image, for available metadata formats'''

    def __init__(self):
        self.metas = {}

    def get_each(self):
        '''Yield an iterable for the whole list of metas'''
        for key in self.metas:
            yield self.metas[key]

    def get_name(self, name):
        '''Yield an iterable of metadata matched by name, e.g. `bash`'''
        for key in self.metas:
            if self.metas[key]['source-name'] == name:
                yield self.metas[key]

    def import_meta(self, meta_text):
        importers = (self.import_meta_ybd,
                     self.import_meta_morph)

        for i in importers:
            try:
                i(meta_text)
                return
            except (KeyError, Exception) as err:
                pass

        # Shouldn't get here
        sys.stderr.write('Metadata format not recognised.\n'
                         'Error:\n')
        raise err

    def import_meta_morph(self, meta_text):
        self._add_meta(yaml.load(meta_text))

    def import_meta_ybd(self, meta_text):
        source = yaml.load(meta_text)

        null = '0' * 32

        if 'repo' not in source:
            kind = 'stratum'
            contents = 'components'
            source['repo'] = 'upstream:definitions'
            source['ref'] = null # No ref info
        else:
            kind = 'chunk'
            contents = 'files'

        repo = parse_repo_alias(source['repo'])
        source_name = '-'.join(
                      source['products'][0]['artifact'].split('-')[:-1])

        # Needed until YBD provides cache-key in metadata
        if not 'cache-key' in source:
            source['cache-key'] = null

        for product in source['products']:

            self._add_meta({
                'kind':           kind,
                'source-name':    source_name,
                'artifact-name':  product['artifact'],
                'contents':       product[contents],
                'repo':           repo,
                'repo-alias':     source['repo'],
                'sha1':           source['ref'],
                'original_ref':   source['ref'],
                'cache-key':      source['cache-key']
                })

    def _add_meta(self, meta_dict):
        '''Validate and add a meta'''

        ignore = ('configuration',
                  'system-artifact-name')

        for i in ignore:
            if i in meta_dict:
                return

        required_fields = ('repo', 'sha1', 'contents')
        for f in required_fields:
            if not f in meta_dict:
                raise Exception('Metadata format not recognised, no '
                    'value for \'%s\'. Data: \'%s\''% (f, str(meta_dict)))

        self.metas[meta_dict['artifact-name']] = meta_dict


def meta_load_from_dir(meta_dir_path):
    '''Read Baserock metadata from a directory'''

    files = [f for f in os.listdir(meta_dir_path)
             if os.path.isfile(os.path.join(meta_dir_path, f))]

    meta = BaserockMeta()
    for f in files:
        if f.endswith('.meta'):
            meta.import_meta(
                open(os.path.join(meta_dir_path, f), 'r').read())

    return meta