~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/lp/soyuz/scripts/processaccepted.py

  • Committer: Launchpad Patch Queue Manager
  • Date: 2011-07-15 12:05:35 UTC
  • mfrom: (13437.1.8 bug-809841)
  • Revision ID: launchpad@pqm.canonical.com-20110715120535-mb0chmqg4khr7qd8
[r=jcsackett][bug=809841] 'All-Ubuntu-derivatives' option for
        process-accepted script.

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
from zope.component import getUtility
20
20
from zope.security.proxy import removeSecurityProxy
21
21
 
 
22
from canonical.launchpad.interfaces.lpstorm import IStore
22
23
from canonical.launchpad.webapp.errorlog import (
23
24
    ErrorReportingUtility,
24
25
    ScriptRequest,
30
31
from lp.bugs.interfaces.bugtask import BugTaskStatus
31
32
from lp.registry.interfaces.distribution import IDistributionSet
32
33
from lp.registry.interfaces.pocket import PackagePublishingPocket
 
34
from lp.registry.model.distroseriesparent import DistroSeriesParent
33
35
from lp.services.scripts.base import (
34
36
    LaunchpadCronScript,
35
37
    LaunchpadScriptFailure,
260
262
            help="Whether to treat this as a dry-run or not.")
261
263
 
262
264
        self.parser.add_option(
263
 
            '--derived', action="store_true", dest="derived", default=False,
264
 
            help="Process all Ubuntu-derived distributions.")
 
265
            '-D', '--derived', action="store_true", dest="derived",
 
266
            default=False, help="Process all Ubuntu-derived distributions.")
265
267
 
266
268
        self.parser.add_option(
267
269
            "--ppa", action="store_true",
285
287
        else:
286
288
            self.txn.commit()
287
289
 
288
 
    def findTargetDistribution(self):
289
 
        distro_name = self.args[0]
290
 
        self.logger.debug("Finding distribution %s." % distro_name)
291
 
        distribution = getUtility(IDistributionSet).getByName(distro_name)
292
 
        if distribution is None:
 
290
    def findDerivedDistros(self):
 
291
        """Find Ubuntu-derived distributions."""
 
292
        # Avoid circular imports.
 
293
        from lp.registry.model.distribution import Distribution
 
294
        from lp.registry.model.distroseries import DistroSeries
 
295
 
 
296
        ubuntu_id = getUtility(ILaunchpadCelebrities).ubuntu.id
 
297
        return IStore(DistroSeries).find(
 
298
            Distribution,
 
299
            Distribution.id == DistroSeries.distributionID,
 
300
            DistroSeries.id == DistroSeriesParent.derived_series_id,
 
301
            DistroSeries.distributionID != ubuntu_id).config(distinct=True)
 
302
 
 
303
    def findNamedDistro(self, distro_name):
 
304
        """Find the `Distribution` called `distro_name`."""
 
305
        self.logger.debug("Finding distribution %s.", distro_name)
 
306
        distro = getUtility(IDistributionSet).getByName(distro_name)
 
307
        if distro is None:
293
308
            raise LaunchpadScriptFailure(
294
309
                "Distribution '%s' not found." % distro_name)
295
 
        return distribution
 
310
        return distro
 
311
 
 
312
    def findTargetDistros(self):
 
313
        """Find the distribution(s) to process, based on arguments."""
 
314
        if self.options.derived:
 
315
            return self.findDerivedDistros()
 
316
        else:
 
317
            return [self.findNamedDistro(self.args[0])]
296
318
 
297
319
    def validateArguments(self):
298
 
        if len(self.args) != 1:
299
 
            raise OptionValueError(
300
 
                "Need to be given exactly one non-option argument. "
301
 
                "Namely the distribution to process.")
302
 
 
 
320
        """Validate command-line arguments."""
303
321
        if self.options.ppa and self.options.copy_archives:
304
322
            raise OptionValueError(
305
323
                "Specify only one of copy archives or ppa archives.")
 
324
        if self.options.derived:
 
325
            if len(self.args) != 0:
 
326
                raise OptionValueError(
 
327
                    "Can't combine --derived with a distribution name.")
 
328
        else:
 
329
            if len(self.args) != 1:
 
330
                raise OptionValueError(
 
331
                    "Need to be given exactly one non-option argument. "
 
332
                    "Namely the distribution to process.")
306
333
 
307
334
    def makeTargetPolicy(self):
308
335
        """Pick and instantiate a `TargetPolicy` based on given options."""
338
365
                "Successfully processed queue item %d", queue_item.id)
339
366
            return True
340
367
 
 
368
    def processForDistro(self, distribution, target_policy):
 
369
        """Process all queue items for a distribution.
 
370
 
 
371
        Commits between items, except in dry-run mode.
 
372
 
 
373
        :param distribution: The `Distribution` to process queue items for.
 
374
        :param target_policy: The applicable `TargetPolicy`.
 
375
        :return: A list of all successfully processed items' ids.
 
376
        """
 
377
        processed_queue_ids = []
 
378
        for archive in target_policy.getTargetArchives(distribution):
 
379
            description = target_policy.describeArchive(archive)
 
380
            for distroseries in distribution.series:
 
381
 
 
382
                self.logger.debug("Processing queue for %s %s" % (
 
383
                    distroseries.name, description))
 
384
 
 
385
                queue_items = distroseries.getPackageUploads(
 
386
                    status=PackageUploadStatus.ACCEPTED, archive=archive)
 
387
                for queue_item in queue_items:
 
388
                    if self.processQueueItem(queue_item):
 
389
                        processed_queue_ids.append(queue_item.id)
 
390
                    # Commit even on error; we may have altered the
 
391
                    # on-disk archive, so the partial state must
 
392
                    # make it to the DB.
 
393
                    self._commit()
 
394
        return processed_queue_ids
 
395
 
341
396
    def main(self):
342
397
        """Entry point for a LaunchpadScript."""
343
398
        self.validateArguments()
344
399
        target_policy = self.makeTargetPolicy()
345
 
        distribution = self.findTargetDistribution()
346
 
 
347
 
        processed_queue_ids = []
348
400
        try:
349
 
            for archive in target_policy.getTargetArchives(distribution):
350
 
                description = target_policy.describeArchive(archive)
351
 
                for distroseries in distribution.series:
352
 
 
353
 
                    self.logger.debug("Processing queue for %s %s" % (
354
 
                        distroseries.name, description))
355
 
 
356
 
                    queue_items = distroseries.getPackageUploads(
357
 
                        status=PackageUploadStatus.ACCEPTED, archive=archive)
358
 
                    for queue_item in queue_items:
359
 
                        if self.processQueueItem(queue_item):
360
 
                            processed_queue_ids.append(queue_item.id)
361
 
                        # Commit even on error; we may have altered the
362
 
                        # on-disk archive, so the partial state must
363
 
                        # make it to the DB.
364
 
                        self._commit()
365
 
 
366
 
            self._commit()
367
 
 
368
 
            target_policy.postprocessSuccesses(processed_queue_ids)
369
 
 
370
 
            self._commit()
 
401
            for distro in self.findTargetDistros():
 
402
                queue_ids = self.processForDistro(distro, target_policy)
 
403
                self._commit()
 
404
                target_policy.postprocessSuccesses(queue_ids)
 
405
                self._commit()
371
406
 
372
407
        finally:
373
408
            self.logger.debug("Rolling back any remaining transactions.")