aboutsummaryrefslogtreecommitdiffstats
path: root/layerindex/tools/import_layer.py
blob: 377a0f099d80ea9778ea50f0f7256809fad48f45 (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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
#!/usr/bin/env python3

# Import a layer into the database
#
# Copyright (C) 2016 Intel Corporation
# Author: Paul Eggleton <paul.eggleton@linux.intel.com>
#
# Licensed under the MIT license, see COPYING.MIT for details


import sys
import os.path

sys.path.insert(0, os.path.realpath(os.path.join(os.path.dirname(__file__), '..')))

import optparse
import re
import glob
import utils
import logging
import subprocess
from layerconfparse import LayerConfParse

class DryRunRollbackException(Exception):
    pass

logger = utils.logger_create('LayerIndexImport')

link_re = re.compile(r'\[(http.*) +link\]')

def set_vcs_fields(layer, repoval):
    layer.vcs_url = repoval
    if repoval.startswith('git://git.openembedded.org/'):
        reponame = re.sub('^.*/', '', repoval)
        layer.vcs_web_url = 'http://cgit.openembedded.org/cgit.cgi/' + reponame
        layer.vcs_web_tree_base_url = 'http://cgit.openembedded.org/cgit.cgi/' + reponame + '/tree/%path%?h=%branch%'
        layer.vcs_web_file_base_url = 'http://cgit.openembedded.org/cgit.cgi/' + reponame + '/tree/%path%?h=%branch%'
    elif 'git.yoctoproject.org/' in repoval:
        reponame = re.sub('^.*/', '', repoval)
        layer.vcs_web_url = 'http://git.yoctoproject.org/cgit/cgit.cgi/' + reponame
        layer.vcs_web_tree_base_url = 'http://git.yoctoproject.org/cgit/cgit.cgi/' + reponame + '/tree/%path%?h=%branch%'
        layer.vcs_web_file_base_url = 'http://git.yoctoproject.org/cgit/cgit.cgi/' + reponame + '/tree/%path%?h=%branch%'
    elif 'github.com/' in repoval:
        reponame = re.sub('^.*github.com/', '', repoval)
        reponame = re.sub('.git$', '', reponame)
        layer.vcs_web_url = 'http://github.com/' + reponame
        layer.vcs_web_tree_base_url = 'http://github.com/' + reponame + '/tree/%branch%/'
        layer.vcs_web_file_base_url = 'http://github.com/' + reponame + '/blob/%branch%/'
    elif 'gitorious.org/' in repoval:
        reponame = re.sub('^.*gitorious.org/', '', repoval)
        reponame = re.sub('.git$', '', reponame)
        layer.vcs_web_url = 'http://gitorious.org/' + reponame
        layer.vcs_web_tree_base_url = 'http://gitorious.org/' + reponame + '/trees/%branch%/'
        layer.vcs_web_file_base_url = 'http://gitorious.org/' + reponame + '/blobs/%branch%/'
    elif 'bitbucket.org/' in repoval:
        reponame = re.sub('^.*bitbucket.org/', '', repoval)
        reponame = re.sub('.git$', '', reponame)
        layer.vcs_web_url = 'http://bitbucket.org/' + reponame
        layer.vcs_web_tree_base_url = 'http://bitbucket.org/' + reponame + '/src/%branch%/%path%?at=%branch%'
        layer.vcs_web_file_base_url = 'http://bitbucket.org/' + reponame + '/src/%branch%/%path%?at=%branch%'


def readme_extract(readmefn):
    maintainer_re = re.compile('maintaine[r(s)ed by]*[:\n\r]', re.IGNORECASE)
    deps_re = re.compile('depend[sencies upon]*[:\n\r]', re.IGNORECASE)

    maintlines = []
    deps = []
    desc = ''
    maint_mode = False
    blank_seen = False
    deps_mode = False
    desc_mode = True
    with open(readmefn, 'r') as f:
        for line in f.readlines():
            if deps_mode:
                if maintainer_re.search(line):
                    deps_mode = False
                else:
                    if ':' in line:
                        blank_seen = False
                        if line.startswith('URI:'):
                            deps.append(line.split(':', 1)[-1].strip())
                        if line.startswith('layers:'):
                            deps[len(deps)-1] = (deps[len(deps)-1], line.split(':', 1)[-1].strip())
                    elif not (line.startswith('====') or line.startswith('----')):
                        if blank_seen:
                            deps_mode = False
                        else:
                            blank_seen = True
                    continue

            if maint_mode:
                line = line.strip()
                if line and '@' in line or ' at ' in line:
                    maintlines.append(line)
                elif not (line.startswith('====') or line.startswith('----')):
                    if maintlines or blank_seen:
                        maint_mode = False
                    else:
                        blank_seen = True
            elif maintainer_re.search(line):
                desc_mode = False
                maint_mode = True
                blank_seen = False
                if ':' in line:
                    line = line.rsplit(":", 1)[-1].strip()
                    if line:
                        maintlines.append(line)
            elif deps_re.search(line):
                desc_mode = False
                deps_mode = True
                blank_seen = False
            elif desc_mode:
                if not line.strip():
                    if blank_seen:
                        desc_mode = False
                    blank_seen = True
                elif line.startswith('====') or line.startswith('----'):
                    # Assume we just got the title, we don't need that
                    desc = ''
                else:
                    desc += line

    maintainers = []
    for line in maintlines:
        for maint in line.split(','):
            if '@' in maint or ' at ' in maint and not 'yyyyyy@zzzzz.com' in maint:
                maintainers.append(maint.strip())
    return desc, maintainers, deps


def maintainers_extract(maintfn):
    maintainers = []
    with open(maintfn, 'r') as f:
        for line in f.readlines():
            if line.startswith('M:'):
                line = line.split(':', 1)[-1].strip()
                if line and '@' in line or ' at ' in line:
                    maintainers.append(line)
    return list(set(maintainers))


def get_github_layerinfo(layer_url, username = None, password = None):
    import http.client
    import json
    from layerindex.models import LayerMaintainer

    def github_api_call(path):
        conn = http.client.HTTPSConnection('api.github.com')
        headers = {"User-Agent": "test_github.py"}
        if username:
            import base64
            auth = base64.encodestring('%s:%s' % (username, password)).replace('\n', '')
            headers['Authorization'] = "Basic %s" % auth

        conn.request("GET", path, headers=headers)
        resp = conn.getresponse()
        return resp

    json_data = None
    owner_json_data = None

    if layer_url.endswith('.git'):
        layer_url = layer_url[:-4]
    resp = github_api_call('/repos/%s' % layer_url.split('github.com/')[-1].rstrip('/'))
    if resp.status in [200, 302]:
        data = resp.read().decode('utf-8')
        json_data = json.loads(data)
        #headers = dict((key, value) for key, value in resp.getheaders())
        #print(headers)
        owner_resp = github_api_call(json_data['owner']['url'].split('api.github.com')[-1])
        if resp.status in [200, 302]:
            owner_data = owner_resp.read().decode('utf-8')
            owner_json_data = json.loads(owner_data)
        else:
            logger.error('HTTP status %s reading owner info from github API: %s' % (resp.status, resp.read().decode('utf-8')))
    else:
        logger.error('HTTP status %s reading repo info from github API: %s' % (resp.status, resp.read().decode('utf-8')))

    return (json_data, owner_json_data)


def main():
    valid_layer_name = re.compile('[-\w]+$')

    parser = optparse.OptionParser(
        usage = """
    %prog [options] <url> [name]""")

    parser.add_option("-s", "--subdir",
            help = "Specify subdirectory",
            action="store", dest="subdir")
    parser.add_option("-n", "--dry-run",
            help = "Don't write any data back to the database",
            action="store_true", dest="dryrun")
    parser.add_option("-d", "--debug",
            help = "Enable debug output",
            action="store_const", const=logging.DEBUG, dest="loglevel", default=logging.INFO)
    parser.add_option("", "--github-auth",
            help = "Specify github username:password",
            action="store", dest="github_auth")
    parser.add_option("-q", "--quiet",
            help = "Hide all output except error messages",
            action="store_const", const=logging.ERROR, dest="loglevel")
    parser.add_option("-a", "--actual-branch",
            help = "Set actual branch",
            action="store", dest="actual_branch")

    options, args = parser.parse_args(sys.argv)

    if len(args) < 2:
        print("Please specify URL of repository for layer")
        sys.exit(1)

    layer_url = args[1]

    if len(args) > 2:
        layer_name = args[2]
    else:
        if options.subdir:
            layer_name = options.subdir
        else:
            layer_name = [x for x in layer_url.split('/') if x][-1]
            if layer_name.endswith('.git'):
                layer_name = layer_name[:-4]

    if not valid_layer_name.match(layer_name):
        logger.error('Invalid layer name "%s" -  Layer name can only include letters, numbers and dashes.', layer_name)
        sys.exit(1)

    if options.github_auth:
        if not ':' in options.github_auth:
            logger.error('--github-auth value must be specified as username:password')
            sys.exit(1)
        splitval = options.github_auth.split(':')
        github_login = splitval[0]
        github_password = splitval[1]
    else:
        github_login = None
        github_password = None

    utils.setup_django()
    import settings
    from layerindex.models import LayerItem, LayerBranch, LayerDependency, LayerMaintainer
    from django.db import transaction

    logger.setLevel(options.loglevel)

    fetchdir = settings.LAYER_FETCH_DIR
    if not fetchdir:
        logger.error("Please set LAYER_FETCH_DIR in settings.py")
        sys.exit(1)

    if not os.path.exists(fetchdir):
        os.makedirs(fetchdir)

    master_branch = utils.get_branch('master')
    core_layer = None
    try:
        with transaction.atomic():
            # Fetch layer
            logger.info('Fetching repository %s' % layer_url)

            layer = LayerItem()
            layer.name = layer_name
            layer.status = 'P'
            layer.layer_type = 'M'
            layer.summary = 'tempvalue'
            layer.description = layer.summary

            set_vcs_fields(layer, layer_url)

            urldir = layer.get_fetch_dir()
            repodir = os.path.join(fetchdir, urldir)
            out = None
            try:
                if not os.path.exists(repodir):
                    out = utils.runcmd("git clone %s %s" % (layer.vcs_url, urldir), fetchdir, logger=logger)
                else:
                    out = utils.runcmd("git fetch", repodir, logger=logger)
            except Exception as e:
                logger.error("Fetch failed: %s" % str(e))
                sys.exit(1)

            actual_branch = 'master'
            if (options.actual_branch):
                actual_branch = options.actual_branch
            try:
                out = utils.runcmd("git checkout origin/%s" % actual_branch, repodir, logger=logger)
            except subprocess.CalledProcessError:
                actual_branch = None
                branches = utils.runcmd("git branch -r", repodir, logger=logger)
                for line in branches.splitlines():
                    if 'origin/HEAD ->' in line:
                        actual_branch = line.split('-> origin/')[-1]
                        break
                if not actual_branch:
                    logger.error("Repository has no master branch nor origin/HEAD")
                    sys.exit(1)
                out = utils.runcmd("git checkout origin/%s" % actual_branch, repodir, logger=logger)

            layer_paths = []
            if options.subdir:
                layerdir = os.path.join(repodir, options.subdir)
                if not os.path.exists(layerdir):
                    logger.error("Subdirectory %s does not exist in repository for master branch" % options.subdir)
                    sys.exit(1)
                if not os.path.exists(os.path.join(layerdir, 'conf/layer.conf')):
                    logger.error("conf/layer.conf not found in subdirectory %s" % options.subdir)
                    sys.exit(1)
                layer_paths.append(layerdir)
            else:
                if os.path.exists(os.path.join(repodir, 'conf/layer.conf')):
                    layer_paths.append(repodir)
                # Find subdirs with a conf/layer.conf
                for subdir in os.listdir(repodir):
                    subdir_path = os.path.join(repodir, subdir)
                    if os.path.isdir(subdir_path):
                        if os.path.exists(os.path.join(subdir_path, 'conf/layer.conf')):
                            layer_paths.append(subdir_path)
                if not layer_paths:
                    logger.error("conf/layer.conf not found in repository or first level subdirectories - is subdirectory set correctly?")
                    sys.exit(1)

            if 'github.com' in layer.vcs_url:
                json_data, owner_json_data = get_github_layerinfo(layer.vcs_url, github_login, github_password)

            for layerdir in layer_paths:
                layer.pk = None
                if layerdir != repodir:
                    subdir = os.path.relpath(layerdir, repodir)
                    if len(layer_paths) > 1:
                        layer.name = subdir
                else:
                    subdir = ''
                if LayerItem.objects.filter(name=layer.name).exists():
                    if LayerItem.objects.filter(name=layer.name).exclude(vcs_url=layer.vcs_url).exists():
                        conflict_list = LayerItem.objects.filter(name=layer.name).exclude(vcs_url=layer.vcs_url)
                        conflict_list_urls = []
                        for conflict in conflict_list:
                            conflict_list_urls.append(conflict.vcs_url)
                        cln = ', '.join(conflict_list_urls)
                        logger.error('A layer named "%s" already exists in the database.  Possible name collision with %s.vcs_url = %s' % (layer.name, layer.name, cln))
                        sys.exit(1)
                    else:
                        logger.info('The layer named "%s" already exists in the database. Skipping this layer with same vcs_url' % layer.name)
                        layer_paths = [x for x in layer_paths if x != layerdir]
                        continue



                logger.info('Creating layer %s' % layer.name)
                # Guess layer type
                if glob.glob(os.path.join(layerdir, 'conf/distro/*.conf')):
                    layer.layer_type = 'D'
                elif glob.glob(os.path.join(layerdir, 'conf/machine/*.conf')):
                    layer.layer_type = 'B'
                layer.save()
                layerbranch = LayerBranch()
                layerbranch.layer = layer
                layerbranch.branch = master_branch
                if layerdir != repodir:
                    layerbranch.vcs_subdir = subdir
                if actual_branch:
                    layerbranch.actual_branch = actual_branch
                layerbranch.save()
                if layer.name != settings.CORE_LAYER_NAME:
                    if not core_layer:
                        core_layer = utils.get_layer(settings.CORE_LAYER_NAME)

                    if core_layer:
                        logger.debug('Adding dep %s to %s' % (core_layer.name, layer.name))
                        layerdep = LayerDependency()
                        layerdep.layerbranch = layerbranch
                        layerdep.dependency = core_layer
                        layerdep.save()
                    try:
                        layerconfparser = LayerConfParse(logger=logger)
                        config_data = layerconfparser.parse_layer(layerbranch, layerdir)
                    finally:
                        layerconfparser.shutdown()
                    if config_data:
                        utils.add_dependencies(layerbranch, config_data, logger=logger)
                        utils.add_recommends(layerbranch, config_data, logger=logger)

                # Get some extra meta-information
                readme_files = glob.glob(os.path.join(layerdir, 'README*'))
                if (not readme_files) and subdir:
                    readme_files = glob.glob(os.path.join(repodir, 'README*'))
                maintainer_files = glob.glob(os.path.join(layerdir, 'MAINTAINERS'))
                if (not maintainer_files) and subdir:
                    maintainer_files = glob.glob(os.path.join(repodir, 'MAINTAINERS'))

                maintainers = []
                if readme_files:
                    (desc, maintainers, deps) = readme_extract(readme_files[0])
                    if desc:
                        layer.summary = layer.name
                        layer.description = desc
                if maintainer_files:
                    maintainers.extend(maintainers_extract(readme_files[0]))

                if (not maintainers) and 'github.com' in layer.vcs_url:
                    if json_data:
                        layer.summary = json_data['description']
                        layer.description = layer.summary
                    if owner_json_data:
                        owner_name = owner_json_data.get('name', None)
                        owner_email = owner_json_data.get('email', None)
                        if owner_name and owner_email:
                            maintainers.append('%s <%s>' % (owner_name, owner_email))

                if layer.name == 'openembedded-core':
                    layer.summary = 'Core metadata'
                    layer.layer_type = 'A'
                elif layer.name == 'meta-oe':
                    layer.summary = 'Additional shared OE metadata'
                    layer.description = layer.summary
                    layer.layer_type = 'A'

                if maintainers:
                    maint_re = re.compile(r'^"?([^"@$<>]+)"? *<([^<> ]+)>[ -]*(.+)?$')
                    for maintentry in maintainers:
                        res = maint_re.match(maintentry)
                        if res:
                            maintainer = LayerMaintainer()
                            maintainer.layerbranch = layerbranch
                            maintainer.name = res.group(1).strip()
                            maintainer.email = res.group(2)
                            if res.group(3):
                                maintainer.responsibility = res.group(3).strip()
                            maintainer.save()

                layer.save()

            if not layer_paths:
                logger.error('No layers added.')
                sys.exit(1);

            if options.dryrun:
                raise DryRunRollbackException()
    except DryRunRollbackException:
        pass

    sys.exit(0)


if __name__ == "__main__":
    main()