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

__metaclass__ = type
__all__ = [
    'HARDCODED_TRANSLATIONTEMPLATESBUILD_SCORE',
    'TranslationTemplatesBuildJob',
    ]

from datetime import timedelta
import logging
import re

from storm.store import Store
from zope.component import getUtility
from zope.interface import (
    classProvides,
    implements,
    )
from zope.security.proxy import removeSecurityProxy

from lp.app.interfaces.launchpad import ILaunchpadCelebrities
from lp.buildmaster.enums import BuildFarmJobType
from lp.buildmaster.interfaces.buildfarmbranchjob import IBuildFarmBranchJob
from lp.buildmaster.interfaces.buildfarmjob import IBuildFarmJobSource
from lp.buildmaster.interfaces.buildqueue import IBuildQueueSet
from lp.buildmaster.model.buildfarmjob import (
    BuildFarmJobOld,
    BuildFarmJobOldDerived,
    )
from lp.buildmaster.model.buildqueue import BuildQueue
from lp.code.interfaces.branchjob import IRosettaUploadJobSource
from lp.code.model.branchjob import (
    BranchJob,
    BranchJobDerived,
    BranchJobType,
    )
from lp.services.config import config
from lp.services.database.bulk import load_related
from lp.services.database.lpstorm import (
    IMasterStore,
    IStore,
    )
from lp.translations.interfaces.translationtemplatesbuild import (
    ITranslationTemplatesBuildSource,
    )
from lp.translations.interfaces.translationtemplatesbuildjob import (
    ITranslationTemplatesBuildJobSource,
    )
from lp.translations.pottery.detect_intltool import is_intltool_structure


HARDCODED_TRANSLATIONTEMPLATESBUILD_SCORE = 2510


class TranslationTemplatesBuildJob(BuildFarmJobOldDerived, BranchJobDerived):
    """An `IBuildFarmJob` implementation that generates templates.

    Implementation-wise, this is actually a `BranchJob`.
    """
    implements(IBuildFarmBranchJob)
    class_job_type = BranchJobType.TRANSLATION_TEMPLATES_BUILD

    classProvides(ITranslationTemplatesBuildJobSource)

    duration_estimate = timedelta(seconds=10)

    unsafe_chars = '[^a-zA-Z0-9_+-]'

    def __init__(self, branch_job):
        super(TranslationTemplatesBuildJob, self).__init__(branch_job)

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

        We override this to provide a non-database delegate that simply
        provides required functionality to the queue system."""
        self.build_farm_job = BuildFarmJobOld()

    def score(self):
        """See `IBuildFarmJob`."""
        # Hard-code score for now.  Most PPA jobs start out at 2505;
        # TranslationTemplateBuildJobs are fast so we want them at a
        # higher priority.
        return HARDCODED_TRANSLATIONTEMPLATESBUILD_SCORE

    def getLogFileName(self):
        """See `IBuildFarmJob`."""
        sanitized_name = re.sub(self.unsafe_chars, '_', self.getName())
        return "translationtemplates_%s" % sanitized_name

    def getName(self):
        """See `IBuildFarmJob`."""
        buildqueue = getUtility(IBuildQueueSet).getByJob(self.job)
        return '%s-%d' % (self.branch.name, buildqueue.id)

    def getTitle(self):
        """See `IBuildFarmJob`."""
        return '%s translation templates build' % self.branch.bzr_identity

    def cleanUp(self):
        """See `IBuildFarmJob`."""
        # This class is not itself database-backed.  But it delegates to
        # one that is.  We can't call its SQLObject destroySelf method
        # though, because then the BuildQueue and the BranchJob would
        # both try to delete the attached Job.
        Store.of(self.context).remove(self.context)

    @property
    def build(self):
        """Return a TranslationTemplateBuild for this build job."""
        build_id = self.context.metadata.get('build_id', None)
        if build_id is None:
            return None
        else:
            return getUtility(ITranslationTemplatesBuildSource).getByID(
                int(build_id))

    @classmethod
    def _hasPotteryCompatibleSetup(cls, branch):
        """Does `branch` look as if pottery can generate templates for it?

        :param branch: A `Branch` object.
        """
        bzr_branch = removeSecurityProxy(branch).getBzrBranch()
        return is_intltool_structure(bzr_branch.basis_tree())

    @classmethod
    def generatesTemplates(cls, branch):
        """See `ITranslationTemplatesBuildJobSource`."""
        logger = logging.getLogger('translation-templates-build')
        if branch.private:
            # We don't support generating template from private branches
            # at the moment.
            logger.debug("Branch %s is private.", branch.unique_name)
            return False

        utility = getUtility(IRosettaUploadJobSource)
        if not utility.providesTranslationFiles(branch):
            # Nobody asked for templates generated from this branch.
            logger.debug(
                    "No templates requested for branch %s.",
                    branch.unique_name)
            return False

        if not cls._hasPotteryCompatibleSetup(branch):
            # Nothing we could do with this branch if we wanted to.
            logger.debug(
                "Branch %s is not pottery-compatible.", branch.unique_name)
            return False

        # Yay!  We made it.
        return True

    @classmethod
    def create(cls, branch, testing=False):
        """See `ITranslationTemplatesBuildJobSource`."""
        logger = logging.getLogger('translation-templates-build')
        # XXX Danilo Segan bug=580429: we hard-code processor to the Ubuntu
        # default processor architecture.  This stops the buildfarm from
        # accidentally dispatching the jobs to private builders.
        processor = cls._getBuildArch()

        build_farm_job = getUtility(IBuildFarmJobSource).new(
            BuildFarmJobType.TRANSLATIONTEMPLATESBUILD, processor=processor)
        build = getUtility(ITranslationTemplatesBuildSource).create(
            build_farm_job, branch)
        logger.debug(
            "Made BuildFarmJob %s, TranslationTemplatesBuild %s.",
            build_farm_job.id, build.id)

        specific_job = build.makeJob()
        if testing:
            removeSecurityProxy(specific_job)._constructed_build = build
        logger.debug("Made %s.", specific_job)

        duration_estimate = cls.duration_estimate

        build_queue_entry = BuildQueue(
            estimated_duration=duration_estimate,
            job_type=BuildFarmJobType.TRANSLATIONTEMPLATESBUILD,
            job=specific_job.job, processor=processor)
        IMasterStore(BuildQueue).add(build_queue_entry)

        logger.debug("Made BuildQueue %s.", build_queue_entry.id)

        return specific_job

    @classmethod
    def _getBuildArch(cls):
        """Returns an `IProcessor` to queue a translation build for."""
        ubuntu = getUtility(ILaunchpadCelebrities).ubuntu
        # A round-about way of hard-coding i386.
        return ubuntu.currentseries.nominatedarchindep.default_processor

    @classmethod
    def scheduleTranslationTemplatesBuild(cls, branch):
        """See `ITranslationTemplatesBuildJobSource`."""
        logger = logging.getLogger('translation-templates-build')
        if not config.rosetta.generate_templates:
            # This feature is disabled by default.
            logging.debug("Templates generation is disabled.")
            return

        try:
            if cls.generatesTemplates(branch):
                # This branch is used for generating templates.
                logger.info(
                    "Requesting templates build for branch %s.",
                    branch.unique_name)
                cls.create(branch)
        except Exception, e:
            logger.error(e)
            raise

    @classmethod
    def getByJob(cls, job):
        """See `IBuildFarmJob`.

        Overridden here to search via a BranchJob, rather than a Job.
        """
        store = IStore(BranchJob)
        branch_job = store.find(BranchJob, BranchJob.job == job).one()
        if branch_job is None:
            return None
        else:
            return cls(branch_job)

    @classmethod
    def getByJobs(cls, jobs):
        """See `IBuildFarmJob`.

        Overridden here to search via a BranchJob, rather than a Job.
        """
        store = IStore(BranchJob)
        job_ids = [job.id for job in jobs]
        branch_jobs = store.find(
            BranchJob, BranchJob.jobID.is_in(job_ids))
        return [cls(branch_job) for branch_job in branch_jobs]

    @classmethod
    def preloadJobsData(cls, jobs):
        # Circular imports.
        from lp.code.model.branch import Branch
        from lp.registry.model.product import Product
        from lp.code.model.branchcollection import GenericBranchCollection
        from lp.services.job.model.job import Job
        contexts = [job.context for job in jobs]
        load_related(Job, contexts, ['jobID'])
        branches = load_related(Branch, contexts, ['branchID'])
        GenericBranchCollection.preloadDataForBranches(branches)
        load_related(Product, branches, ['productID'])

    @classmethod
    def getByBranch(cls, branch):
        """See `ITranslationTemplatesBuildJobSource`."""
        store = IStore(BranchJob)
        branch_job = store.find(BranchJob, BranchJob.branch == branch).one()
        if branch_job is None:
            return None
        else:
            return cls(branch_job)