~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/lp/code/model/branchjob.py

  • Committer: William Grant
  • Date: 2011-07-18 20:46:27 UTC
  • mto: This revision was merged to the branch mainline in revision 13465.
  • Revision ID: william.grant@canonical.com-20110718204627-s9nc1jv3r0vsl6ix
Revert r13420 (the relanding of r13292, which broke production in similar ways).

Show diffs side-by-side

added added

removed removed

Lines of Context:
13
13
]
14
14
 
15
15
import contextlib
16
 
from itertools import chain
17
16
import operator
18
17
import os
19
18
import shutil
107
106
from lp.scripts.helpers import TransactionFreeOperation
108
107
from lp.services.job.interfaces.job import JobStatus
109
108
from lp.services.job.model.job import Job
110
 
from lp.services.job.runner import (
111
 
    BaseRunnableJob,
112
 
    BaseRunnableJobSource,
113
 
    )
114
 
from lp.services.utils import RegisteredSubclass
 
109
from lp.services.job.runner import BaseRunnableJob
115
110
from lp.translations.interfaces.translationimportqueue import (
116
111
    ITranslationImportQueue,
117
112
    )
222
217
 
223
218
class BranchJobDerived(BaseRunnableJob):
224
219
 
225
 
    __metaclass__ = RegisteredSubclass
226
 
    _subclass = {}
227
 
 
228
 
    @staticmethod
229
 
    def _register_subclass(cls):
230
 
        """Register this class with its enumeration."""
231
 
        # Why not a classmethod?  See RegisteredSubclass.__init__.
232
 
        job_type = getattr(cls, 'class_job_type', None)
233
 
        if job_type is not None:
234
 
            value = cls._subclass.setdefault(job_type, cls)
235
 
            assert value is cls, (
236
 
                '%s already registered to %s.' % (
237
 
                    job_type.name, value.__name__))
238
 
 
239
220
    delegates(IBranchJob)
240
221
 
241
222
    def __init__(self, branch_job):
270
251
            And(BranchJob.job_type == cls.class_job_type,
271
252
                BranchJob.job == Job.id,
272
253
                Job.id.is_in(Job.ready_jobs)))
273
 
        return (cls._getInstance(job) for job in jobs)
274
 
 
275
 
    @classmethod
276
 
    def _getInstance(cls, job):
277
 
        return cls._subclass[job.job_type](job)
278
 
 
279
 
    @classmethod
280
 
    def get(cls, key, desired_classes=None):
281
 
        """Return the instance of this class (or a subclass) whose key
282
 
        is supplied.  Calling this method on a subclass returns only values
283
 
        for that subclass.
 
254
        return (cls(job) for job in jobs)
 
255
 
 
256
    @classmethod
 
257
    def get(cls, key):
 
258
        """Return the instance of this class whose key is supplied.
284
259
 
285
260
        :raises: SQLObjectNotFound
286
261
        """
287
 
        if desired_classes is None:
288
 
            desired_classes = cls
289
 
        branchjob = IStore(BranchJob).get(BranchJob, key)
290
 
        if branchjob is not None:
291
 
            job = cls._getInstance(branchjob)
292
 
            if isinstance(job, cls):
293
 
                return job
294
 
        raise SQLObjectNotFound(
295
 
            'No occurrence of %s has key %s' % (cls.__name__, key))
 
262
        instance = IStore(BranchJob).get(BranchJob, key)
 
263
        if instance is None or instance.job_type != cls.class_job_type:
 
264
            raise SQLObjectNotFound(
 
265
                'No occurrence of %s has key %s' % (cls.__name__, key))
 
266
        return cls(instance)
296
267
 
297
268
    def getOopsVars(self):
298
269
        """See `IRunnableJob`."""
793
764
        return outf.getvalue()
794
765
 
795
766
 
796
 
class BranchMailJobSource(BaseRunnableJobSource):
797
 
    """Source of jobs that send mail about branches."""
798
 
 
799
 
    # This is the same limit used in the wrapper script.
800
 
    memory_limit = 1843200
801
 
 
802
 
    @staticmethod
803
 
    def contextManager():
804
 
        return get_ro_server()
805
 
 
806
 
    @staticmethod
807
 
    def iterReady():
808
 
        return chain(
809
 
            RevisionMailJob.iterReady(), RevisionsAddedJob.iterReady())
810
 
 
811
 
    @staticmethod
812
 
    def get(key):
813
 
        return BranchJobDerived.get(key, (RevisionMailJob, RevisionsAddedJob))
814
 
 
815
 
 
816
767
class RosettaUploadJob(BranchJobDerived):
817
768
    """A Job that uploads translation files to Rosetta."""
818
769