def test_pending_transaction(self):
     """When a transaction is pending before the operation, raise."""
     transaction.begin()
     with ExpectedException(AssertionError,
                            'Transaction open before operation'):
         with TransactionFreeOperation():
             pass
 def test_run_missing_ready_not_enabled(self):
     """run_missing_ready does nothing if the class isn't enabled."""
     self.createMissingJob()
     with monitor_celery() as responses:
         with dbuser('run_missing_ready'):
             with TransactionFreeOperation.require():
                 self.RunMissingReady().run(_no_init=True)
     self.assertEqual([], responses)
 def test_run_missing_ready_not_enabled(self):
     """run_missing_ready does nothing if the class isn't enabled."""
     self.createMissingJob()
     with monitor_celery() as responses:
         with dbuser("run_missing_ready"):
             with TransactionFreeOperation.require():
                 self.RunMissingReady().run(_no_init=True)
     self.assertEqual([], responses)
 def test_run_missing_ready(self):
     """run_missing_ready requests the job to run if not scheduled."""
     self.createMissingJob()
     self.useFixture(FeatureFixture({"jobs.celery.enabled_classes": "BranchScanJob"}))
     with monitor_celery() as responses:
         with dbuser("run_missing_ready"):
             with TransactionFreeOperation.require():
                 self.RunMissingReady().run(_no_init=True)
     self.assertEqual(1, len(responses))
 def test_run_missing_ready(self):
     """run_missing_ready requests the job to run if not scheduled."""
     self.createMissingJob()
     self.useFixture(
         FeatureFixture({'jobs.celery.enabled_classes': 'BranchScanJob'}))
     with monitor_celery() as responses:
         with dbuser('run_missing_ready'):
             with TransactionFreeOperation.require():
                 self.RunMissingReady().run(_no_init=True)
     self.assertEqual(1, len(responses))
Beispiel #6
0
    def run(self, _check_transaction=False):
        """See `IBranchUpgradeJob`."""
        # Set up the new branch structure
        with server(get_rw_server(), no_replace=True):
            upgrade_branch_path = tempfile.mkdtemp()
            try:
                upgrade_transport = get_transport(upgrade_branch_path)
                upgrade_transport.mkdir('.bzr')
                source_branch_transport = get_transport(
                    self.branch.getInternalBzrUrl())
                source_branch_transport.clone('.bzr').copy_tree_to_transport(
                    upgrade_transport.clone('.bzr'))
                transaction.commit()
                upgrade_branch = BzrBranch.open_from_transport(
                    upgrade_transport)

                # No transactions are open so the DB connection won't be
                # killed.
                with TransactionFreeOperation():
                    # Perform the upgrade.
                    upgrade(upgrade_branch.base)

                # Re-open the branch, since its format has changed.
                upgrade_branch = BzrBranch.open_from_transport(
                    upgrade_transport)
                source_branch = BzrBranch.open_from_transport(
                    source_branch_transport)

                source_branch.lock_write()
                upgrade_branch.pull(source_branch)
                upgrade_branch.fetch(source_branch)
                source_branch.unlock()

                # Move the branch in the old format to backup.bzr
                try:
                    source_branch_transport.delete_tree('backup.bzr')
                except NoSuchFile:
                    pass
                source_branch_transport.rename('.bzr', 'backup.bzr')
                source_branch_transport.mkdir('.bzr')
                upgrade_transport.clone('.bzr').copy_tree_to_transport(
                    source_branch_transport.clone('.bzr'))

                # Re-open the source branch again.
                source_branch = BzrBranch.open_from_transport(
                    source_branch_transport)

                formats = get_branch_formats(source_branch)

                self.branch.branchChanged(
                    self.branch.stacked_on,
                    self.branch.last_scanned_id,
                    *formats)
            finally:
                shutil.rmtree(upgrade_branch_path)
 def run(self, _no_init=False):
     if not _no_init:
         task_init('run_missing_ready')
     with TransactionFreeOperation():
         count = 0
         for job in find_missing_ready(BranchScanJob):
             if not celery_enabled(job.__class__.__name__):
                 continue
             job.celeryCommitHook(True)
             count += 1
         info('Scheduled %d missing jobs.', count)
         transaction.commit()
Beispiel #8
0
 def run(self, is_careful):
     """Do the entire generation and run process."""
     self.createEmptyPocketRequests(is_careful)
     self.log.debug("Preparing file lists and overrides.")
     self.generateOverrides(is_careful)
     self.log.debug("Generating overrides for the distro.")
     self.generateFileLists(is_careful)
     self.log.debug("Doing apt-ftparchive work.")
     apt_config_filename = self.generateConfig(is_careful)
     transaction.commit()
     with TransactionFreeOperation():
         self.runApt(apt_config_filename)
     self.cleanCaches()
Beispiel #9
0
def run_missing_ready(_no_init=False):
    """Task to run any jobs that are ready but not scheduled.

    Currently supports only BranchScanJob.
    :param _no_init: For tests.  If True, do not perform the initialization.
    """
    if not _no_init:
        task_init('run_missing_ready')
    with TransactionFreeOperation():
        count = 0
        for job in find_missing_ready(BranchScanJob):
            if not celery_enabled(job.__class__.__name__):
                continue
            job.celeryCommitHook(True)
            count += 1
        info('Scheduled %d missing jobs.', count)
        transaction.commit()
Beispiel #10
0
    def cleanCaches(self):
        """Clean apt-ftparchive caches.

        This takes a few minutes and doesn't need to be done on every run,
        but it should be done every so often so that the cache files don't
        get too large and slow down normal runs of apt-ftparchive.
        """
        apt_config_filename = os.path.join(self._config.miscroot,
                                           "apt-cleanup.conf")
        try:
            last_cleanup = os.stat(apt_config_filename).st_mtime
            if last_cleanup > time.time() - CLEANUP_FREQUENCY:
                return
        except OSError:
            pass

        apt_config = StringIO()
        apt_config.write(CONFIG_HEADER %
                         (self._config.archiveroot, self._config.overrideroot,
                          self._config.cacheroot, self._config.miscroot))

        # "apt-ftparchive clean" doesn't care what suite it's given, but it
        # needs to know the union of all architectures and components for
        # each suite we might publish.
        archs = set()
        comps = set()
        for distroseries in self.publisher.consider_series:
            for a in distroseries.enabled_architectures:
                archs.add(a.architecturetag)
            for comp in self.publisher.archive.getComponentsForSeries(
                    distroseries):
                comps.add(comp.name)
        self.writeAptConfig(apt_config, "nonexistent-suite", sorted(comps),
                            sorted(archs), True,
                            [IndexCompressionType.UNCOMPRESSED])

        with open(apt_config_filename, "w") as fp:
            fp.write(apt_config.getvalue())
        apt_config.close()
        transaction.commit()
        with TransactionFreeOperation():
            self.runAptWithArgs(apt_config_filename, "clean")
 def test_require_with_TransactionFreeOperation(self):
     """If TransactionFreeOperation is used, do not raise."""
     with TransactionFreeOperation.require():
         with TransactionFreeOperation():
             pass
 def test_require_no_TransactionFreeOperation(self):
     """If TransactionFreeOperation is not used, raise."""
     with ExpectedException(AssertionError,
                            'TransactionFreeOperation was not used.'):
         with TransactionFreeOperation.require():
             pass
 def test_transaction_free(self):
     """When there are no transactions, do not raise."""
     with TransactionFreeOperation():
         pass
 def test_transaction_during_operation(self):
     """When the operation creates a transaction, raise."""
     with ExpectedException(AssertionError,
                            'Operation opened transaction!'):
         with TransactionFreeOperation():
             transaction.begin()
 def test_require_with_TransactionFreeOperation(self):
     """If TransactionFreeOperation is used, do not raise."""
     with TransactionFreeOperation.require():
         with TransactionFreeOperation():
             pass
 def test_require_no_TransactionFreeOperation(self):
     """If TransactionFreeOperation is not used, raise."""
     with ExpectedException(
             AssertionError, 'TransactionFreeOperation was not used.'):
         with TransactionFreeOperation.require():
             pass