aboutsummaryrefslogtreecommitdiffstats
path: root/rrs/tools/rrs_distros.py
blob: d408b330c63d7c00d557fc8dbf2c5dbbf1e434f1 (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
#!/usr/bin/env python3

# Update current distro information for recipes.
#
# Copyright (C) 2015 Intel Corporation
# Author: Anibal Limon <anibal.limon@linux.intel.com>
#
# Licensed under the MIT license, see COPYING.MIT for details

import sys
import os.path
import optparse
import logging
from datetime import datetime
import shutil

sys.path.insert(0, os.path.realpath(os.path.join(os.path.dirname(__file__))))
from common import common_setup, load_recipes, \
        get_pv_type, get_logger, DryRunRollbackException
common_setup()
from layerindex import utils

utils.setup_django()
from django.db import transaction
import settings

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

# setup bitbake path
bitbakepath = os.path.join(fetchdir, 'bitbake')
sys.path.insert(0, os.path.join(bitbakepath, 'lib'))


from layerindex.models import Recipe, LayerBranch
from rrs.models import RecipeDistro, MaintenancePlan

"""
    Searches the recipe's package in major distributions.
    Returns a dictionary containing pairs of (distro name, package aliases).
"""
def search_package_in_distros(pkglst_dir, recipe, data):
    distros = {}
    distro_aliases = {}

    recipe_name = recipe.pn

    removes = ['nativesdk-', '-native', '-cross', '-initial']
    for r in removes:
        recipe_name.replace(r, '')

    distro_alias = data.getVar('DISTRO_PN_ALIAS', True)
    if distro_alias:
        # Gets info from DISTRO_PN_ALIAS into a dictionary containing 
        # the distribution as a key and the package name as value.
        for alias in distro_alias.split():
            if alias.find("=") != -1:
                (dist, pn_alias) = alias.split('=')
                distro_aliases[dist.strip().lower()] = pn_alias.strip()

    for distro_file in os.listdir(pkglst_dir):
        (distro, distro_release) = distro_file.split("-")

        if distro.lower() in distro_aliases:
            pn = distro_aliases[distro.lower()]
        else:
            pn = recipe_name

        f = open(os.path.join(pkglst_dir, distro_file), "r")
        for line in f:
            (pkg, section) = line.split(":")
            if pn == pkg:
                distro_complete = distro + "-" + section[:-1]
                distros[distro_complete] = pn
                f.close()
                break
        f.close()

    return distros

if __name__=="__main__":
    parser = optparse.OptionParser(usage = """%prog [options]""")
    
    parser.add_option("-d", "--debug",
            help = "Enable debug output",
            action="store_const", const=logging.DEBUG, dest="loglevel", default=logging.INFO)
    
    parser.add_option("--dry-run",
            help = "Do not write any data back to the database",
            action="store_true", dest="dry_run", default=False)

    parser.add_option("-p", "--plan",
            help="Specify maintenance plan to operate on (default is all plans that have updates enabled)",
            action="store", dest="plan", default=None)

    options, args = parser.parse_args(sys.argv)
    logger.setLevel(options.loglevel)

    if options.plan:
        maintplans = MaintenancePlan.objects.filter(id=int(options.plan))
        if not maintplans.exists():
            logger.error('No maintenance plan with ID %s found' % options.plan)
            sys.exit(1)
    else:
        maintplans = MaintenancePlan.objects.filter(updates_enabled=True)
        if not maintplans.exists():
            logger.error('No enabled maintenance plans found')
            sys.exit(1)

    logger.debug("Starting recipe distros update ...")

    try:
        origsyspath = sys.path
        with transaction.atomic():
            for maintplan in maintplans:
                for item in maintplan.maintenanceplanlayerbranch_set.all():
                    layerbranch = item.layerbranch
                    sys.path = origsyspath
                    (tinfoil, d, recipes, tempdir) = load_recipes(layerbranch, bitbakepath,
                            fetchdir, settings, logger)
                    try:
                        if not recipes:
                            continue

                        utils.setup_core_layer_sys_path(settings, layerbranch.branch.name)

                        from oe import distro_check
                        logger.debug("Downloading distro's package information ...")
                        distro_check.create_distro_packages_list(fetchdir, d)
                        pkglst_dir = os.path.join(fetchdir, "package_lists")

                        RecipeDistro.objects.filter(recipe__layerbranch = layerbranch).delete()

                        for recipe_data in recipes:
                            pn = recipe_data.getVar('PN', True)

                            try:
                                recipe = Recipe.objects.get(pn = pn, layerbranch = layerbranch)
                            except:
                                logger.warn('%s: layer branch %s, NOT found' % (pn,
                                    str(layerbranch)))
                                continue

                            distro_info = search_package_in_distros(pkglst_dir, recipe, recipe_data)
                            for distro, alias in distro_info.items():
                                recipedistro = RecipeDistro()
                                recipedistro.recipe = recipe
                                recipedistro.distro = distro
                                recipedistro.alias = alias
                                recipedistro.save()
                                logger.debug('%s: layer branch %s, add distro %s alias %s' % (pn,
                                    str(layerbranch), distro, alias))
                    finally:
                        tinfoil.shutdown()
                        shutil.rmtree(tempdir)
            if options.dry_run:
                raise DryRunRollbackException
    except DryRunRollbackException:
        pass