~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
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
# Copyright 2010 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 code for source package builds."""

__metaclass__ = type
__all__ = [
    'SourcePackageRecipeBuild',
    ]

import datetime
import sys

from psycopg2 import ProgrammingError
from pytz import utc
from storm.locals import (
    Int,
    Reference,
    Storm,
    )
from storm.store import Store
from zope.component import (
    getUtility,
    )
from zope.interface import (
    classProvides,
    implements,
    )
from zope.security.proxy import ProxyFactory

from canonical.database.constants import UTC_NOW
from canonical.launchpad.browser.librarian import ProxiedLibraryFileAlias
from canonical.launchpad.interfaces.lpstorm import (
    IMasterStore,
    IStore,
    )
from canonical.launchpad.webapp import errorlog
from lp.app.errors import NotFoundError
from lp.buildmaster.enums import (
    BuildFarmJobType,
    BuildStatus,
    )
from lp.buildmaster.model.buildfarmjob import BuildFarmJobOldDerived
from lp.buildmaster.model.buildqueue import BuildQueue
from lp.buildmaster.model.packagebuild import (
    PackageBuild,
    PackageBuildDerived,
    )
from lp.code.errors import BuildAlreadyPending
from lp.code.interfaces.sourcepackagerecipebuild import (
    ISourcePackageRecipeBuild,
    ISourcePackageRecipeBuildJob,
    ISourcePackageRecipeBuildJobSource,
    ISourcePackageRecipeBuildSource,
    )
from lp.code.mail.sourcepackagerecipebuild import (
    SourcePackageRecipeBuildMailer,
    )
from lp.code.model.sourcepackagerecipedata import SourcePackageRecipeData
from lp.registry.interfaces.pocket import PackagePublishingPocket
from lp.services.job.model.job import Job
from lp.soyuz.adapters.archivedependencies import (
    default_component_dependency_name,
    )
from lp.soyuz.interfaces.component import IComponentSet
from lp.soyuz.model.binarypackagebuild import BinaryPackageBuild
from lp.soyuz.model.buildfarmbuildjob import BuildFarmBuildJob
from lp.soyuz.model.sourcepackagerelease import SourcePackageRelease


class SourcePackageRecipeBuild(PackageBuildDerived, Storm):

    __storm_table__ = 'SourcePackageRecipeBuild'

    implements(ISourcePackageRecipeBuild)
    classProvides(ISourcePackageRecipeBuildSource)

    package_build_id = Int(name='package_build', allow_none=False)
    package_build = Reference(package_build_id, 'PackageBuild.id')

    build_farm_job_type = BuildFarmJobType.RECIPEBRANCHBUILD

    id = Int(primary=True)

    is_private = False

    @property
    def binary_builds(self):
        """See `ISourcePackageRecipeBuild`."""
        return Store.of(self).find(BinaryPackageBuild,
            BinaryPackageBuild.source_package_release==
            SourcePackageRelease.id,
            SourcePackageRelease.source_package_recipe_build==self.id)

    @property
    def current_component(self):
        return getUtility(IComponentSet)[default_component_dependency_name]

    distroseries_id = Int(name='distroseries', allow_none=True)
    distroseries = Reference(distroseries_id, 'DistroSeries.id')
    distro_series = distroseries

    @property
    def distribution(self):
        """See `IPackageBuild`."""
        return self.distroseries.distribution

    is_virtualized = True

    recipe_id = Int(name='recipe')
    recipe = Reference(recipe_id, 'SourcePackageRecipe.id')

    manifest = Reference(
        id, 'SourcePackageRecipeData.sourcepackage_recipe_build_id',
        on_remote=True)

    def setManifestText(self, text):
        if text is None:
            if self.manifest is not None:
                IStore(self.manifest).remove(self.manifest)
        elif self.manifest is None:
            SourcePackageRecipeData.createManifestFromText(text, self)
        else:
            from bzrlib.plugins.builder.recipe import RecipeParser
            self.manifest.setRecipe(RecipeParser(text).parse())

    def getManifestText(self):
        if self.manifest is None:
            return None
        return str(self.manifest.getRecipe())

    requester_id = Int(name='requester', allow_none=False)
    requester = Reference(requester_id, 'Person.id')

    @property
    def buildqueue_record(self):
        """See `IBuildFarmJob`."""
        store = Store.of(self)
        results = store.find(
            BuildQueue,
            SourcePackageRecipeBuildJob.job == BuildQueue.jobID,
            SourcePackageRecipeBuildJob.build == self.id)
        return results.one()

    @property
    def source_package_release(self):
        """See `ISourcePackageRecipeBuild`."""
        return Store.of(self).find(
            SourcePackageRelease, source_package_recipe_build=self).one()

    @property
    def title(self):
        return '%s recipe build' % self.recipe.base_branch.unique_name

    def __init__(self, package_build, distroseries, recipe, requester):
        """Construct a SourcePackageRecipeBuild."""
        super(SourcePackageRecipeBuild, self).__init__()
        self.package_build = package_build
        self.distroseries = distroseries
        self.recipe = recipe
        self.requester = requester

    @classmethod
    def new(cls, distroseries, recipe, requester, archive, pocket=None,
            date_created=None, duration=None):
        """See `ISourcePackageRecipeBuildSource`."""
        store = IMasterStore(SourcePackageRecipeBuild)
        if pocket is None:
            pocket = PackagePublishingPocket.RELEASE
        if date_created is None:
            date_created = UTC_NOW
        packagebuild = PackageBuild.new(cls.build_farm_job_type,
            True, archive, pocket, date_created=date_created)
        spbuild = cls(
            packagebuild,
            distroseries,
            recipe,
            requester)
        store.add(spbuild)
        return spbuild

    @staticmethod
    def makeDailyBuilds(logger=None):
        from lp.code.model.sourcepackagerecipe import SourcePackageRecipe
        recipes = SourcePackageRecipe.findStaleDailyBuilds()
        builds = []
        for recipe in recipes:
            for distroseries in recipe.distroseries:
                try:
                    build = recipe.requestBuild(
                        recipe.daily_build_archive, recipe.owner,
                        distroseries, PackagePublishingPocket.RELEASE)
                except BuildAlreadyPending:
                    continue
                except ProgrammingError:
                    raise
                except:
                    info = sys.exc_info()
                    errorlog.globalErrorUtility.raising(info)
                else:
                    if logger is not None:
                        logger.debug(
                            'Build for %s/%s requested',
                            recipe.owner.name, recipe.name)
                    builds.append(build)
            recipe.is_stale = False
        return builds

    def _unqueueBuild(self):
        """Remove the build's queue and job."""
        store = Store.of(self)
        if self.buildqueue_record is not None:
            job = self.buildqueue_record.job
            store.remove(self.buildqueue_record)
            store.find(
                SourcePackageRecipeBuildJob,
                SourcePackageRecipeBuildJob.build == self.id).remove()
            store.remove(job)

    def cancelBuild(self):
        """See `ISourcePackageRecipeBuild.`"""
        self._unqueueBuild()
        self.status = BuildStatus.SUPERSEDED

    def destroySelf(self):
        self._unqueueBuild()
        store = Store.of(self)
        releases = store.find(
            SourcePackageRelease,
            SourcePackageRelease.source_package_recipe_build == self.id)
        for release in releases:
            release.source_package_recipe_build = None
        package_build = self.package_build
        store.remove(self)
        package_build.destroySelf()

    @classmethod
    def getById(cls, build_id):
        """See `ISourcePackageRecipeBuildSource`."""
        store = IMasterStore(SourcePackageRecipeBuild)
        return store.find(cls, cls.id == build_id).one()

    @classmethod
    def getRecentBuilds(cls, requester, recipe, distroseries, _now=None):
        from lp.buildmaster.model.buildfarmjob import BuildFarmJob
        if _now is None:
            _now = datetime.datetime.now(utc)
        store = IMasterStore(SourcePackageRecipeBuild)
        old_threshold = _now - datetime.timedelta(days=1)
        return store.find(cls, cls.distroseries_id == distroseries.id,
            cls.requester_id == requester.id, cls.recipe_id == recipe.id,
            BuildFarmJob.date_created > old_threshold,
            BuildFarmJob.id == PackageBuild.build_farm_job_id,
            PackageBuild.id == cls.package_build_id)

    def makeJob(self):
        """See `ISourcePackageRecipeBuildJob`."""
        store = Store.of(self)
        job = Job()
        store.add(job)
        specific_job = getUtility(
            ISourcePackageRecipeBuildJobSource).new(self, job)
        return specific_job

    def estimateDuration(self):
        """See `IPackageBuild`."""
        median = self.recipe.getMedianBuildDuration()
        if median is not None:
            return median
        return datetime.timedelta(minutes=10)

    def verifySuccessfulUpload(self):
        return self.source_package_release is not None

    def notify(self, extra_info=None):
        """See `IPackageBuild`."""
        mailer = SourcePackageRecipeBuildMailer.forStatus(self)
        mailer.sendAll()

    def lfaUrl(self, lfa):
        """Return the URL for a LibraryFileAlias, in the context of self.
        """
        if lfa is None:
            return None
        return ProxiedLibraryFileAlias(lfa, self).http_url

    @property
    def log_url(self):
        """See `IPackageBuild`.

        Overridden here so that it uses the SourcePackageRecipeBuild as
        context.
        """
        return self.lfaUrl(self.log)

    @property
    def upload_log_url(self):
        """See `IPackageBuild`.

        Overridden here so that it uses the SourcePackageRecipeBuild as
        context.
        """
        return self.lfaUrl(self.upload_log)

    def getFileByName(self, filename):
        """See `ISourcePackageRecipeBuild`."""
        files = dict((lfa.filename, lfa)
                     for lfa in [self.log, self.upload_log]
                     if lfa is not None)
        try:
            return files[filename]
        except KeyError:
            raise NotFoundError(filename)

    def _handleStatus_OK(self, librarian, slave_status, logger):
        """See `IPackageBuild`."""
        d = super(SourcePackageRecipeBuild, self)._handleStatus_OK(
            librarian, slave_status, logger)
        def uploaded_build(ignored):
            # Base implementation doesn't notify on success.
            if self.status == BuildStatus.FULLYBUILT:
                self.notify()
        return d.addCallback(uploaded_build)

    def getUploader(self, changes):
        """See `IPackageBuild`."""
        return self.requester


class SourcePackageRecipeBuildJob(BuildFarmJobOldDerived, Storm):
    classProvides(ISourcePackageRecipeBuildJobSource)
    implements(ISourcePackageRecipeBuildJob)

    __storm_table__ = 'sourcepackagerecipebuildjob'

    id = Int(primary=True)

    job_id = Int(name='job', allow_none=False)
    job = Reference(job_id, 'Job.id')

    build_id = Int(name='sourcepackage_recipe_build', allow_none=False)
    build = Reference(
        build_id, 'SourcePackageRecipeBuild.id')

    @property
    def processor(self):
        return self.build.distroseries.nominatedarchindep.default_processor

    @property
    def virtualized(self):
        """See `IBuildFarmJob`."""
        return self.build.is_virtualized

    def __init__(self, build, job):
        self.build = build
        self.job = job
        super(SourcePackageRecipeBuildJob, self).__init__()

    def _set_build_farm_job(self):
        """Setup the IBuildFarmJob delegate.

        We override this to provide a delegate specific to package builds."""
        self.build_farm_job = BuildFarmBuildJob(self.build)

    @classmethod
    def new(cls, build, job):
        """See `ISourcePackageRecipeBuildJobSource`."""
        specific_job = cls(build, job)
        store = IMasterStore(cls)
        store.add(specific_job)
        return specific_job

    def getName(self):
        return "%s-%s" % (self.id, self.build_id)

    def score(self):
        return 2505 + self.build.archive.relative_build_score


def get_recipe_build_for_build_farm_job(build_farm_job):
    """Return the SourcePackageRecipeBuild associated with a BuildFarmJob."""
    store = Store.of(build_farm_job)
    result = store.find(SourcePackageRecipeBuild,
        SourcePackageRecipeBuild.package_build_id == PackageBuild.id,
        PackageBuild.build_farm_job_id == build_farm_job.id)
    return ProxyFactory(result.one())