~salgado/launchpad/use-default-python-for-ec2

« back to all changes in this revision

Viewing changes to lib/lp/archiveuploader/uploadprocessor.py

  • Committer: Guilherme Salgado
  • Date: 2010-05-03 14:05:59 UTC
  • mfrom: (10669.1.1 use-default-python)
  • Revision ID: salgado@canonical.com-20100503140559-c7ppsjriq863uf3f
merge from use-default-python branch

Show diffs side-by-side

added added

removed removed

Lines of Context:
59
59
from lp.archiveuploader.nascentupload import (
60
60
    NascentUpload, FatalUploadError, EarlyReturnUploadError)
61
61
from lp.archiveuploader.uploadpolicy import (
62
 
    findPolicyByOptions, UploadPolicyError)
 
62
    UploadPolicyError)
63
63
from lp.soyuz.interfaces.archive import IArchiveSet, NoSuchPPA
64
64
from lp.registry.interfaces.distribution import IDistributionSet
65
65
from lp.registry.interfaces.person import IPersonSet
108
108
class UploadProcessor:
109
109
    """Responsible for processing uploads. See module docstring."""
110
110
 
111
 
    def __init__(self, options, ztm, log):
112
 
        self.options = options
 
111
    def __init__(self, base_fsroot, dry_run, no_mails, keep, policy_for_distro,
 
112
                 ztm, log):
 
113
        """Create a new upload processor.
 
114
 
 
115
        :param base_fsroot: Root path for queue to use
 
116
        :param dry_run: Run but don't commit changes to database
 
117
        :param no_mails: Don't send out any emails
 
118
        :param keep: Leave the files in place, don't move them away
 
119
        :param policy_for_distro: callback to obtain Policy object for a
 
120
            distribution
 
121
        :param ztm: Database transaction to use
 
122
        :param log: Logger to use for reporting
 
123
        """
 
124
        self.base_fsroot = base_fsroot
 
125
        self.dry_run = dry_run
 
126
        self.keep = keep
 
127
        self.last_processed_upload = None
 
128
        self.log = log
 
129
        self.no_mails = no_mails
 
130
        self._getPolicyForDistro = policy_for_distro
113
131
        self.ztm = ztm
114
 
        self.log = log
115
 
        self.last_processed_upload = None
116
132
 
117
 
    def processUploadQueue(self):
 
133
    def processUploadQueue(self, leaf_name=None):
118
134
        """Search for uploads, and process them.
119
135
 
120
 
        Uploads are searched for in the 'incoming' directory inside the
 
136
        Uploads are searched for in the 'incoming' directory inside the
121
137
        base_fsroot.
122
138
 
123
139
        This method also creates the 'incoming', 'accepted', 'rejected', and
124
140
        'failed' directories inside the base_fsroot if they don't yet exist.
125
141
        """
 
142
        self.ztm.commit()
126
143
        try:
127
144
            self.log.debug("Beginning processing")
128
145
 
129
146
            for subdir in ["incoming", "accepted", "rejected", "failed"]:
130
 
                full_subdir = os.path.join(self.options.base_fsroot, subdir)
 
147
                full_subdir = os.path.join(self.base_fsroot, subdir)
131
148
                if not os.path.exists(full_subdir):
132
149
                    self.log.debug("Creating directory %s" % full_subdir)
133
150
                    os.mkdir(full_subdir)
134
151
 
135
 
            fsroot = os.path.join(self.options.base_fsroot, "incoming")
 
152
            fsroot = os.path.join(self.base_fsroot, "incoming")
136
153
            uploads_to_process = self.locateDirectories(fsroot)
137
154
            self.log.debug("Checked in %s, found %s"
138
155
                           % (fsroot, uploads_to_process))
139
156
            for upload in uploads_to_process:
140
157
                self.log.debug("Considering upload %s" % upload)
141
 
                self.processUpload(fsroot, upload)
 
158
                self.processUpload(fsroot, upload, leaf_name)
142
159
 
143
160
        finally:
144
161
            self.log.debug("Rolling back any remaining transactions.")
145
162
            self.ztm.abort()
146
163
 
147
 
    def processUpload(self, fsroot, upload):
 
164
    def processUpload(self, fsroot, upload, leaf_name=None):
148
165
        """Process an upload's changes files, and move it to a new directory.
149
166
 
150
167
        The destination directory depends on the result of the processing
152
169
        is 'failed', otherwise it is the worst of the results from the
153
170
        individual changes files, in order 'failed', 'rejected', 'accepted'.
154
171
 
155
 
        If the leafname option is set but its value is not the same as the
 
172
        If the leaf_name option is set but its value is not the same as the
156
173
        name of the upload directory, skip it entirely.
157
174
 
158
175
        """
159
 
        if (self.options.leafname is not None and
160
 
            upload != self.options.leafname):
 
176
        if (leaf_name is not None and
 
177
            upload != leaf_name):
161
178
            self.log.debug("Skipping %s -- does not match %s" % (
162
 
                upload, self.options.leafname))
 
179
                upload, leaf_name))
163
180
            return
164
181
 
165
182
        upload_path = os.path.join(fsroot, upload)
242
259
            # Skip lockfile deletion, see similar code in lp.poppy.hooks.
243
260
            fsroot_lock.release(skip_delete=True)
244
261
 
245
 
        sorted_dir_names =  sorted(
 
262
        sorted_dir_names = sorted(
246
263
            dir_name
247
264
            for dir_name in dir_names
248
265
            if os.path.isdir(os.path.join(fsroot, dir_name)))
321
338
                         "https://help.launchpad.net/Packaging/PPA#Uploading "
322
339
                         "and update your configuration.")))
323
340
        self.log.debug("Finding fresh policy")
324
 
        self.options.distro = distribution.name
325
 
        policy = findPolicyByOptions(self.options)
 
341
        policy = self._getPolicyForDistro(distribution)
326
342
        policy.archive = archive
327
343
 
328
344
        # DistroSeries overriding respect the following precedence:
395
411
            # when transaction is committed) this will cause any emails sent
396
412
            # sent by do_reject to be lost.
397
413
            notify = True
398
 
            if self.options.dryrun or self.options.nomails:
 
414
            if self.dry_run or self.no_mails:
399
415
                notify = False
400
416
            if upload.is_rejected:
401
417
                result = UploadStatusEnum.REJECTED
414
430
                for msg in upload.rejections:
415
431
                    self.log.warn("\t%s" % msg)
416
432
 
417
 
            if self.options.dryrun:
 
433
            if self.dry_run:
418
434
                self.log.info("Dry run, aborting transaction.")
419
435
                self.ztm.abort()
420
436
            else:
433
449
        This includes moving the given upload directory and moving the
434
450
        matching .distro file, if it exists.
435
451
        """
436
 
        if self.options.keep or self.options.dryrun:
 
452
        if self.keep or self.dry_run:
437
453
            self.log.debug("Keeping contents untouched")
438
454
            return
439
455
 
440
456
        pathname = os.path.basename(upload)
441
457
 
442
458
        target_path = os.path.join(
443
 
            self.options.base_fsroot, subdir_name, pathname)
 
459
            self.base_fsroot, subdir_name, pathname)
444
460
        self.log.debug("Moving upload directory %s to %s" %
445
461
            (upload, target_path))
446
462
        shutil.move(upload, target_path)
447
463
 
448
464
        distro_filename = upload + ".distro"
449
465
        if os.path.isfile(distro_filename):
450
 
            target_path = os.path.join(self.options.base_fsroot, subdir_name,
 
466
            target_path = os.path.join(self.base_fsroot, subdir_name,
451
467
                                       os.path.basename(distro_filename))
452
468
            self.log.debug("Moving distro file %s to %s" % (distro_filename,
453
469
                                                            target_path))