~launchpad-pqm/launchpad/devel

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
# Copyright 2009-2011 Canonical Ltd.  This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).

# pylint: disable-msg=F0401,E1002

"""Implementation of the recipe storage.

This is purely an implementation detail of SourcePackageRecipe.recipe_data and
SourcePackageRecipeBuild.manifest, the classes in this file have no public
interfaces.
"""

__metaclass__ = type
__all__ = ['SourcePackageRecipeData']

from itertools import groupby

from bzrlib.plugins.builder.recipe import (
    BaseRecipeBranch,
    MergeInstruction,
    NestInstruction,
    NestPartInstruction,
    RecipeBranch,
    RecipeParser,
    SAFE_INSTRUCTIONS,
    )
from lazr.enum import (
    DBEnumeratedType,
    DBItem,
    )
from storm.expr import Union
from storm.locals import (
    And,
    Int,
    Reference,
    ReferenceSet,
    Select,
    Store,
    Storm,
    Unicode,
    )
from zope.component import getUtility

from lp.code.errors import (
    NoSuchBranch,
    PrivateBranchRecipe,
    TooNewRecipeFormat,
    )
from lp.code.interfaces.branchlookup import IBranchLookup
from lp.code.model.branch import Branch
from lp.services.database.bulk import (
    load_referencing,
    load_related,
    )
from lp.services.database.enumcol import EnumCol
from lp.services.database.lpstorm import IStore
from lp.services.propertycache import (
    cachedproperty,
    clear_property_cache,
    get_property_cache,
    )


class InstructionType(DBEnumeratedType):
    """The instruction type, for _SourcePackageRecipeDataInstruction.type."""

    MERGE = DBItem(1, """
        Merge instruction

        A merge instruction.""")

    NEST = DBItem(2, """
        Nest instruction

        A nest instruction.""")

    NEST_PART = DBItem(3, """
        Nest-part instruction

        A nest-part instruction.""")


class _SourcePackageRecipeDataInstruction(Storm):
    """A single line from a recipe."""

    __storm_table__ = "SourcePackageRecipeDataInstruction"

    def __init__(self, name, type, comment, line_number, branch, revspec,
                 directory, recipe_data, parent_instruction,
                 source_directory):
        super(_SourcePackageRecipeDataInstruction, self).__init__()
        self.name = unicode(name)
        self.type = type
        self.comment = comment
        self.line_number = line_number
        self.branch = branch
        if revspec is not None:
            revspec = unicode(revspec)
        self.revspec = revspec
        if directory is not None:
            directory = unicode(directory)
        self.directory = directory
        self.source_directory = source_directory
        self.recipe_data = recipe_data
        self.parent_instruction = parent_instruction

    id = Int(primary=True)

    name = Unicode(allow_none=False)
    type = EnumCol(notNull=True, schema=InstructionType)
    comment = Unicode(allow_none=True)
    line_number = Int(allow_none=False)

    branch_id = Int(name='branch', allow_none=False)
    branch = Reference(branch_id, 'Branch.id')

    revspec = Unicode(allow_none=True)
    directory = Unicode(allow_none=True)
    source_directory = Unicode(allow_none=True)

    recipe_data_id = Int(name='recipe_data', allow_none=False)
    recipe_data = Reference(recipe_data_id, 'SourcePackageRecipeData.id')

    parent_instruction_id = Int(name='parent_instruction', allow_none=True)
    parent_instruction = Reference(
        parent_instruction_id, '_SourcePackageRecipeDataInstruction.id')

    def appendToRecipe(self, recipe_branch):
        """Append a bzr-builder instruction to the recipe_branch object."""
        branch = RecipeBranch(
            self.name, self.branch.bzr_identity, self.revspec)
        if self.type == InstructionType.MERGE:
            recipe_branch.merge_branch(branch)
        elif self.type == InstructionType.NEST:
            recipe_branch.nest_branch(self.directory, branch)
        elif self.type == InstructionType.NEST_PART:
            recipe_branch.nest_part_branch(
                branch, self.source_directory, self.directory)
        else:
            raise AssertionError("Unknown type %r" % self.type)
        return branch


MAX_RECIPE_FORMAT = 0.4


class SourcePackageRecipeData(Storm):
    """The database representation of a BaseRecipeBranch from bzr-builder.

    This is referenced from the SourcePackageRecipe table as the 'recipe_data'
    column and from the SourcePackageRecipeBuild table as the 'manifest'
    column.
    """

    __storm_table__ = "SourcePackageRecipeData"

    id = Int(primary=True)

    base_branch_id = Int(name='base_branch', allow_none=False)
    base_branch = Reference(base_branch_id, 'Branch.id')

    recipe_format = Unicode(allow_none=False)
    deb_version_template = Unicode(allow_none=True)
    revspec = Unicode(allow_none=True)

    instructions = ReferenceSet(
        id, _SourcePackageRecipeDataInstruction.recipe_data_id,
        order_by=_SourcePackageRecipeDataInstruction.line_number)

    sourcepackage_recipe_id = Int(
        name='sourcepackage_recipe', allow_none=True)
    sourcepackage_recipe = Reference(
        sourcepackage_recipe_id, 'SourcePackageRecipe.id')

    sourcepackage_recipe_build_id = Int(
        name='sourcepackage_recipe_build', allow_none=True)
    sourcepackage_recipe_build = Reference(
        sourcepackage_recipe_build_id, 'SourcePackageRecipeBuild.id')

    @staticmethod
    def getParsedRecipe(recipe_text):
        parser = RecipeParser(recipe_text)
        return parser.parse(permitted_instructions=SAFE_INSTRUCTIONS)

    @staticmethod
    def findRecipes(branch):
        from lp.code.model.sourcepackagerecipe import SourcePackageRecipe
        store = Store.of(branch)
        return store.find(
            SourcePackageRecipe,
            SourcePackageRecipe.id.is_in(Union(
                Select(
                    SourcePackageRecipeData.sourcepackage_recipe_id,
                    SourcePackageRecipeData.base_branch == branch),
                Select(
                    SourcePackageRecipeData.sourcepackage_recipe_id,
                    And(
                        _SourcePackageRecipeDataInstruction.recipe_data_id ==
                        SourcePackageRecipeData.id,
                        _SourcePackageRecipeDataInstruction.branch == branch)
                    )
            ))
        )

    @classmethod
    def createManifestFromText(cls, text, sourcepackage_recipe_build):
        """Create a manifest for the specified build.

        :param text: The text of the recipe to create a manifest for.
        :param sourcepackage_recipe_build: The build to associate the manifest
            with.
        :return: an instance of SourcePackageRecipeData.
        """
        parsed = cls.getParsedRecipe(text)
        return cls(
            parsed, sourcepackage_recipe_build=sourcepackage_recipe_build)

    def getRecipe(self):
        """The BaseRecipeBranch version of the recipe."""
        base_branch = BaseRecipeBranch(
            self.base_branch.bzr_identity, self.deb_version_template,
            self.recipe_format, self.revspec)
        insn_stack = []
        for insn in self.instructions:
            while insn_stack and \
                      insn_stack[-1]['insn'] != insn.parent_instruction:
                insn_stack.pop()
            if insn_stack:
                target_branch = insn_stack[-1]['recipe_branch']
            else:
                target_branch = base_branch
            recipe_branch = insn.appendToRecipe(target_branch)
            insn_stack.append(
                dict(insn=insn, recipe_branch=recipe_branch))
        return base_branch

    def _scanInstructions(self, recipe_branch):
        """Check the recipe_branch doesn't use 'run' and look up the branches.

        We do all the lookups before we start constructing database objects to
        avoid flushing half-constructed objects to the database.

        :return: A map ``{branch_url: db_branch}``.
        """
        r = {}
        for instruction in recipe_branch.child_branches:
            db_branch = getUtility(IBranchLookup).getByUrl(
                instruction.recipe_branch.url)
            if db_branch is None:
                raise NoSuchBranch(instruction.recipe_branch.url)
            if db_branch.private:
                raise PrivateBranchRecipe(db_branch)
            r[instruction.recipe_branch.url] = db_branch
            r.update(self._scanInstructions(instruction.recipe_branch))
        return r

    def _recordInstructions(self, recipe_branch, parent_insn, branch_map,
                            line_number=0):
        """Build _SourcePackageRecipeDataInstructions for the recipe_branch.
        """
        for instruction in recipe_branch.child_branches:
            nest_path = instruction.nest_path
            source_directory = None
            if isinstance(instruction, MergeInstruction):
                type = InstructionType.MERGE
            elif isinstance(instruction, NestInstruction):
                type = InstructionType.NEST
            elif isinstance(instruction, NestPartInstruction):
                type = InstructionType.NEST_PART
                nest_path = instruction.target_subdir
                source_directory = instruction.subpath
            else:
                # Unsupported instructions should have been filtered out by
                # _scanInstructions; if we get surprised here, that's a bug.
                raise AssertionError(
                    "Unsupported instruction %r" % instruction)
            line_number += 1
            comment = None
            db_branch = branch_map[instruction.recipe_branch.url]
            insn = _SourcePackageRecipeDataInstruction(
                instruction.recipe_branch.name, type, comment,
                line_number, db_branch, instruction.recipe_branch.revspec,
                nest_path, self, parent_insn, source_directory)
            line_number = self._recordInstructions(
                instruction.recipe_branch, insn, branch_map, line_number)
        return line_number

    def setRecipe(self, builder_recipe):
        """Convert the BaseRecipeBranch `builder_recipe` to the db form."""
        clear_property_cache(self)
        if builder_recipe.format > MAX_RECIPE_FORMAT:
            raise TooNewRecipeFormat(builder_recipe.format, MAX_RECIPE_FORMAT)
        branch_map = self._scanInstructions(builder_recipe)
        # If this object hasn't been added to a store yet, there can't be any
        # instructions linking to us yet.
        if Store.of(self) is not None:
            self.instructions.find().remove()
        branch_lookup = getUtility(IBranchLookup)
        base_branch = branch_lookup.getByUrl(builder_recipe.url)
        if base_branch is None:
            raise NoSuchBranch(builder_recipe.url)
        if base_branch.private:
            raise PrivateBranchRecipe(base_branch)
        if builder_recipe.revspec is not None:
            self.revspec = unicode(builder_recipe.revspec)
        self._recordInstructions(
            builder_recipe, parent_insn=None, branch_map=branch_map)
        self.base_branch = base_branch
        if builder_recipe.deb_version is None:
            self.deb_version_template = None
        else:
            self.deb_version_template = unicode(builder_recipe.deb_version)
        self.recipe_format = unicode(builder_recipe.format)

    def __init__(self, recipe, sourcepackage_recipe=None,
                 sourcepackage_recipe_build=None):
        """Initialize from the bzr-builder recipe and link it to a db recipe.
        """
        super(SourcePackageRecipeData, self).__init__()
        self.setRecipe(recipe)
        self.sourcepackage_recipe = sourcepackage_recipe
        self.sourcepackage_recipe_build = sourcepackage_recipe_build

    @staticmethod
    def preLoadReferencedBranches(sourcepackagerecipedatas):
        # Load the related Branch, _SourcePackageRecipeDataInstruction.
        load_related(
            Branch, sourcepackagerecipedatas, ['base_branch_id'])
        sprd_instructions = load_referencing(
            _SourcePackageRecipeDataInstruction,
            sourcepackagerecipedatas, ['recipe_data_id'])
        sub_branches = load_related(
            Branch, sprd_instructions, ['branch_id'])
        # Store the pre-fetched objects on the sourcepackagerecipedatas
        # objects.
        branch_to_recipe_data = dict([
            (instr.branch_id, instr.recipe_data_id)
                for instr in sprd_instructions])
        caches = dict((sprd.id, [sprd, get_property_cache(sprd)])
            for sprd in sourcepackagerecipedatas)
        for unused, [sprd, cache] in caches.items():
            cache._referenced_branches = [sprd.base_branch]
        for recipe_data_id, branches in groupby(
            sub_branches, lambda branch: branch_to_recipe_data[branch.id]):
            cache = caches[recipe_data_id][1]
            cache._referenced_branches.extend(list(branches))

    def getReferencedBranches(self):
        """Return an iterator of the Branch objects referenced by this recipe.
        """
        return self._referenced_branches

    @cachedproperty
    def _referenced_branches(self):
        referenced_branches = [self.base_branch]
        sub_branches = IStore(self).find(
            Branch,
            _SourcePackageRecipeDataInstruction.recipe_data == self,
            Branch.id == _SourcePackageRecipeDataInstruction.branch_id)
        referenced_branches.extend(sub_branches)
        return referenced_branches