def test_publish(self):
        upload = self.uploadTestData()
        transaction.commit()
        upload.queue_root.realiseUpload(self.logger)

        # Test if the job was created correctly
        jobs = list(PackageTranslationsUploadJob.iterReady())
        self.assertEqual(1, len(jobs))

        job = jobs[0]
        # Assert if the job corresponds to the file we uploaded
        self.assertEqual(job.sourcepackagename, self.spr.sourcepackagename)
        self.assertEqual(job.libraryfilealias.filename, self.translations_file)

        # Test if the pmount translations tarball files were added to the
        # translation import queue
        with dbuser("upload_package_translations_job"):
            job.run()
        self.assertContentEqual(self._getImportableFilesFromTarball(),
                                self._getQueuePaths())

        self.factory.makePOTemplate(
            distroseries=self.breezy_autotest,
            sourcepackagename=self.spr.sourcepackagename,
            path="po/pmount.pot",
            translation_domain=self.package_name)

        # Approve all translations in the queue
        with dbuser("translations_import_queue_gardener"):
            gardener = ImportQueueGardener(
                'translations-import-queue-gardener',
                logger=self.logger,
                test_args=[])
            gardener.main()

        # Import all approved translations
        with dbuser("poimport"):
            importer = TranslationsImport('poimport',
                                          logger=self.logger,
                                          test_args=[])
            importer.main()
        # Test if all translations in the queue were successfully imported
        self.assertContentEqual(
            self._getImportableFilesFromTarball(),
            self._getQueuePaths(import_status=RosettaImportStatus.IMPORTED))
 def setUp(self):
     super(TestTranslationsImport, self).setUp()
     self.queue = TranslationImportQueue()
     self.script = TranslationsImport('poimport', test_args=[])
     self.script.logger.setLevel(logging.FATAL)
     self.owner = self.factory.makePerson()
class TestTranslationsImport(TestCaseWithFactory):

    layer = LaunchpadScriptLayer

    def setUp(self):
        super(TestTranslationsImport, self).setUp()
        self.queue = TranslationImportQueue()
        self.script = TranslationsImport('poimport', test_args=[])
        self.script.logger.setLevel(logging.FATAL)
        self.owner = self.factory.makePerson()

    def _makeProductSeries(self):
        """Make a product series called 'trunk'."""
        return self.factory.makeProduct(owner=self.owner).getSeries('trunk')

    def _makeEntry(self, path, **kwargs):
        """Produce a queue entry."""
        uploader = kwargs.pop('uploader', self.owner)
        return self.queue.addOrUpdateEntry(
            path, '# Nothing here', False, uploader, **kwargs)

    def _makeApprovedEntry(self, uploader):
        """Produce an approved queue entry."""
        path = '%s.pot' % self.factory.getUniqueString()
        series = self.factory.makeProductSeries()
        template = self.factory.makePOTemplate(series)
        entry = self._makeEntry(
            path, uploader=uploader, potemplate=template,
            productseries=template.productseries)
        entry.status = RosettaImportStatus.APPROVED
        return entry

    def _getEmailRecipients(self):
        """List the recipients of all pending outgoing emails."""
        return sum([
            recipients
            for sender, recipients, text in stub.test_emails], [])

    def test_describeEntry_without_target(self):
        productseries = self._makeProductSeries()
        entry = self._makeEntry('foo.po', productseries=productseries)
        description = self.script._describeEntry(entry)
        pattern = "'foo.po' \(id [0-9]+\) in [A-Za-z0-9_-]+ trunk series$"
        self.assertNotEqual(None, re.match(pattern, description))

    def test_describeEntry_for_pofile(self):
        productseries = self._makeProductSeries()
        template = self.factory.makePOTemplate(productseries=productseries)
        pofile = template.newPOFile('nl')
        entry = self._makeEntry(
            'foo.po', productseries=productseries, potemplate=template,
            pofile=pofile)
        description = self.script._describeEntry(entry)
        pattern = "Dutch \(nl\) translation of .* in .* trunk \(id [0-9]+\)$"
        self.assertNotEqual(None, re.match(pattern, description))

    def test_describeEntry_for_template(self):
        productseries = self._makeProductSeries()
        template = self.factory.makePOTemplate(productseries=productseries)
        entry = self._makeEntry(
            'foo.pot', productseries=productseries, potemplate=template)
        description = self.script._describeEntry(entry)
        pattern = 'Template "[^"]+" in [A-Za-z0-9_-]+ trunk \(id [0-9]+\)$'
        self.assertNotEqual(None, re.match(pattern, description))

    def test_checkEntry(self):
        productseries = self._makeProductSeries()
        template = self.factory.makePOTemplate(productseries=productseries)
        entry = self._makeEntry(
            'foo.pot', productseries=productseries, potemplate=template)
        self.assertTrue(self.script._checkEntry(entry))

    def test_checkEntry_without_target(self):
        productseries = self._makeProductSeries()
        entry = self._makeEntry('foo.pot', productseries=productseries)
        self.assertFalse(self.script._checkEntry(entry))
        self.assertIn(
            "Entry is approved but has no place to import to.",
            self.script.failures.keys())

    def test_checkEntry_misapproved_product(self):
        productseries = self._makeProductSeries()
        template = self.factory.makePOTemplate()
        entry = self._makeEntry(
            'foo.pot', productseries=productseries, potemplate=template)
        self.assertNotEqual(None, entry.import_into)

        self.assertFalse(self.script._checkEntry(entry))
        self.assertIn(
            "Entry was approved for the wrong productseries.",
            self.script.failures.keys())

    def test_checkEntry_misapproved_package(self):
        package = self.factory.makeSourcePackage()
        other_series = self.factory.makeDistroSeries(
            distribution=package.distroseries.distribution)
        template = self.factory.makePOTemplate(
            distroseries=package.distroseries,
            sourcepackagename=package.sourcepackagename)
        entry = self._makeEntry(
            'foo.pot', sourcepackagename=package.sourcepackagename,
            distroseries=other_series, potemplate=template)
        self.assertNotEqual(None, entry.import_into)

        self.assertFalse(self.script._checkEntry(entry))
        self.assertIn(
            "Entry was approved for the wrong distroseries.",
            self.script.failures.keys())

    def test_handle_serious_error(self):
        productseries = self._makeProductSeries()
        template = self.factory.makePOTemplate(productseries=productseries)
        entry = self._makeEntry(
            'snaf.pot', productseries=productseries, potemplate=template)
        entry.potemplate = template
        entry.status = RosettaImportStatus.APPROVED
        self.assertNotEqual(None, entry.import_into)

        message = "The system has exploded."
        self.script._importEntry = FakeMethod(
            failure=OutrageousSystemError(message))
        self.assertRaises(OutrageousSystemError, self.script.main)

        self.assertEqual(RosettaImportStatus.FAILED, entry.status)
        self.assertEqual(message, entry.error_output)

    def test_handle_unexpected_exception(self):
        # Unexpected exceptions during import are caught and reported.
        productseries = self._makeProductSeries()
        template = self.factory.makePOTemplate(productseries=productseries)
        entry = self._makeEntry(
            'foo.pot', productseries=productseries, potemplate=template)
        entry.potemplate = template
        entry.status = RosettaImportStatus.APPROVED
        self.assertNotEqual(None, entry.import_into)

        message = "Nobody expects the Spanish Inquisition!"
        self.script._importEntry = FakeMethod(
            failure=UnexpectedException(message))
        self.script.main()

        self.assertEqual(RosettaImportStatus.FAILED, entry.status)
        self.assertEqual(message, entry.error_output)

    def test_main_leaves_oops_handling_alone(self):
        """Ensure that script.main is not altering oops reporting."""
        self.script.main()
        default_reporting = errorlog.ErrorReportingUtility()
        default_reporting.configure('error_reports')
        self.assertEqual(default_reporting.oops_prefix,
                         errorlog.globalErrorUtility.oops_prefix)

    def test_notifies_uploader(self):
        entry = self._makeApprovedEntry(self.owner)
        transaction.commit()
        self.script._importEntry(entry)
        transaction.commit()
        self.assertEqual(
            [self.owner.preferredemail.email], self._getEmailRecipients())

    def test_does_not_notify_vcs_imports(self):
        vcs_imports = getUtility(ILaunchpadCelebrities).vcs_imports
        entry = self._makeApprovedEntry(vcs_imports)
        transaction.commit()
        self.script._importEntry(entry)
        transaction.commit()
        self.assertEqual([], self._getEmailRecipients())
#!/usr/bin/python -S
#
# Copyright 2009 Canonical Ltd.  This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).

import _pythonpath

from lp.services.config import config
from lp.translations.scripts.po_import import TranslationsImport


if __name__ == '__main__':
    script = TranslationsImport(
        'rosetta-poimport', dbuser=config.poimport.dbuser)
    script.lock_and_run()
    def test_copyFromPPAToArchiveWithTranslations(self):
        # Upload the package to a PPA and process it.
        src_upload, spph, bin_upload = self.uploadToPPA()
        target_archive = self.breezy_autotest.main_archive

        # Copy the source package with binaries from PPA to the main archive.
        # Give QueueAdmin permissions for the user so the upload will be
        # accepted right away.
        person = target_archive.owner
        for component in self.breezy_autotest.components:
            getUtility(IArchivePermissionSet).newQueueAdmin(
                target_archive, person, component)

        self.assertTrue(
            target_archive.canAdministerQueue(person,
                                              self.breezy_autotest.components,
                                              spph.pocket,
                                              self.breezy_autotest))

        target_archive.copyPackage(source_name=self.package_name,
                                   version=self.version,
                                   from_archive=self.name16.archive,
                                   to_pocket='RELEASE',
                                   to_series=self.breezy_autotest.name,
                                   person=person,
                                   include_binaries=True,
                                   unembargo=True,
                                   auto_approve=True)

        # Get copy job and run it, should be enough to publish it
        copy_job_source = getUtility(IPlainPackageCopyJobSource)
        copy_job = copy_job_source.getActiveJobs(target_archive).one()
        with dbuser('copy_packages'):
            copy_job.run()

        published_source = target_archive.getPublishedSources(name='pmount')[0]
        self.assertIsNotNone(published_source)
        self.assertEqual(
            published_source.sourcepackagerelease.upload_archive.displayname,
            'PPA for Foo Bar')
        self.assertEqual(published_source.archive.displayname,
                         'Primary Archive for Ubuntu Test')

        # Move package to main, as only main and restricted packages have
        # their translations processed.
        published_source = published_source.changeOverride(
            new_component="main")

        self.spr = published_source.sourcepackagerelease
        self.assertIsNotNone(published_source.getPublishedBinaries())

        # Process the upload for it to create the PTUJ
        upload = IStore(PackageUpload).find(PackageUpload).order_by(
            Desc(PackageUpload.id)).first()
        upload.realiseUpload(self.logger)

        # Assert the job is created correctly
        ptu_jobs = list(PackageTranslationsUploadJob.iterReady())
        self.assertNotEqual(len(ptu_jobs), 0)

        # Let's attachTranslations
        ptu_job = ptu_jobs[0]
        with dbuser('upload_package_translations_job'):
            ptu_job.run()

        # Test if the pmount translations tarball files were added to the
        # translation import queue
        self.assertContentEqual(self._getImportableFilesFromTarball(),
                                self._getQueuePaths())

        self.factory.makePOTemplate(
            distroseries=self.breezy_autotest,
            sourcepackagename=self.spr.sourcepackagename,
            path="po/pmount.pot",
            translation_domain=self.package_name)

        # Approve all translations in the queue
        with dbuser("translations_import_queue_gardener"):
            gardener = ImportQueueGardener(
                'translations-import-queue-gardener',
                logger=self.logger,
                test_args=[])
            gardener.main()

        # Import all approved translations
        with dbuser("poimport"):
            importer = TranslationsImport('poimport',
                                          logger=self.logger,
                                          test_args=[])
            importer.main()

        # Test if all translations in the queue were successfully imported
        self.assertContentEqual(
            self._getImportableFilesFromTarball(),
            self._getQueuePaths(import_status=RosettaImportStatus.IMPORTED))
Beispiel #6
0
#!/usr/bin/python -S
#
# Copyright 2009 Canonical Ltd.  This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).

import _pythonpath

from lp.services.config import config
from lp.translations.scripts.po_import import TranslationsImport

if __name__ == '__main__':
    script = TranslationsImport('rosetta-poimport',
                                dbuser=config.poimport.dbuser)
    script.lock_and_run()
 def setUp(self):
     super(TestTranslationsImport, self).setUp()
     self.queue = TranslationImportQueue()
     self.script = TranslationsImport('poimport', test_args=[])
     self.script.logger.setLevel(logging.FATAL)
     self.owner = self.factory.makePerson()
class TestTranslationsImport(TestCaseWithFactory):

    layer = LaunchpadScriptLayer

    def setUp(self):
        super(TestTranslationsImport, self).setUp()
        self.queue = TranslationImportQueue()
        self.script = TranslationsImport('poimport', test_args=[])
        self.script.logger.setLevel(logging.FATAL)
        self.owner = self.factory.makePerson()

    def _makeProductSeries(self):
        """Make a product series called 'trunk'."""
        return self.factory.makeProduct(owner=self.owner).getSeries('trunk')

    def _makeEntry(self, path, **kwargs):
        """Produce a queue entry."""
        uploader = kwargs.pop('uploader', self.owner)
        return self.queue.addOrUpdateEntry(path, '# Nothing here', False,
                                           uploader, **kwargs)

    def _makeApprovedEntry(self, uploader):
        """Produce an approved queue entry."""
        path = '%s.pot' % self.factory.getUniqueString()
        series = self.factory.makeProductSeries()
        template = self.factory.makePOTemplate(series)
        entry = self._makeEntry(path,
                                uploader=uploader,
                                potemplate=template,
                                productseries=template.productseries)
        entry.status = RosettaImportStatus.APPROVED
        return entry

    def _getEmailRecipients(self):
        """List the recipients of all pending outgoing emails."""
        return sum(
            [recipients for sender, recipients, text in stub.test_emails], [])

    def test_describeEntry_without_target(self):
        productseries = self._makeProductSeries()
        entry = self._makeEntry('foo.po', productseries=productseries)
        description = self.script._describeEntry(entry)
        pattern = "'foo.po' \(id [0-9]+\) in [A-Za-z0-9_-]+ trunk series$"
        self.assertNotEqual(None, re.match(pattern, description))

    def test_describeEntry_for_pofile(self):
        productseries = self._makeProductSeries()
        template = self.factory.makePOTemplate(productseries=productseries)
        pofile = template.newPOFile('nl')
        entry = self._makeEntry('foo.po',
                                productseries=productseries,
                                potemplate=template,
                                pofile=pofile)
        description = self.script._describeEntry(entry)
        pattern = "Dutch \(nl\) translation of .* in .* trunk \(id [0-9]+\)$"
        self.assertNotEqual(None, re.match(pattern, description))

    def test_describeEntry_for_template(self):
        productseries = self._makeProductSeries()
        template = self.factory.makePOTemplate(productseries=productseries)
        entry = self._makeEntry('foo.pot',
                                productseries=productseries,
                                potemplate=template)
        description = self.script._describeEntry(entry)
        pattern = 'Template "[^"]+" in [A-Za-z0-9_-]+ trunk \(id [0-9]+\)$'
        self.assertNotEqual(None, re.match(pattern, description))

    def test_checkEntry(self):
        productseries = self._makeProductSeries()
        template = self.factory.makePOTemplate(productseries=productseries)
        entry = self._makeEntry('foo.pot',
                                productseries=productseries,
                                potemplate=template)
        self.assertTrue(self.script._checkEntry(entry))

    def test_checkEntry_without_target(self):
        productseries = self._makeProductSeries()
        entry = self._makeEntry('foo.pot', productseries=productseries)
        self.assertFalse(self.script._checkEntry(entry))
        self.assertIn("Entry is approved but has no place to import to.",
                      self.script.failures.keys())

    def test_checkEntry_misapproved_product(self):
        productseries = self._makeProductSeries()
        template = self.factory.makePOTemplate()
        entry = self._makeEntry('foo.pot',
                                productseries=productseries,
                                potemplate=template)
        self.assertNotEqual(None, entry.import_into)

        self.assertFalse(self.script._checkEntry(entry))
        self.assertIn("Entry was approved for the wrong productseries.",
                      self.script.failures.keys())

    def test_checkEntry_misapproved_package(self):
        package = self.factory.makeSourcePackage()
        other_series = self.factory.makeDistroSeries(
            distribution=package.distroseries.distribution)
        template = self.factory.makePOTemplate(
            distroseries=package.distroseries,
            sourcepackagename=package.sourcepackagename)
        entry = self._makeEntry('foo.pot',
                                sourcepackagename=package.sourcepackagename,
                                distroseries=other_series,
                                potemplate=template)
        self.assertNotEqual(None, entry.import_into)

        self.assertFalse(self.script._checkEntry(entry))
        self.assertIn("Entry was approved for the wrong distroseries.",
                      self.script.failures.keys())

    def test_handle_serious_error(self):
        productseries = self._makeProductSeries()
        template = self.factory.makePOTemplate(productseries=productseries)
        entry = self._makeEntry('snaf.pot',
                                productseries=productseries,
                                potemplate=template)
        entry.potemplate = template
        entry.status = RosettaImportStatus.APPROVED
        self.assertNotEqual(None, entry.import_into)

        message = "The system has exploded."
        self.script._importEntry = FakeMethod(
            failure=OutrageousSystemError(message))
        self.assertRaises(OutrageousSystemError, self.script.main)

        self.assertEqual(RosettaImportStatus.FAILED, entry.status)
        self.assertEqual(message, entry.error_output)

    def test_handle_unexpected_exception(self):
        # Unexpected exceptions during import are caught and reported.
        productseries = self._makeProductSeries()
        template = self.factory.makePOTemplate(productseries=productseries)
        entry = self._makeEntry('foo.pot',
                                productseries=productseries,
                                potemplate=template)
        entry.potemplate = template
        entry.status = RosettaImportStatus.APPROVED
        self.assertNotEqual(None, entry.import_into)

        message = "Nobody expects the Spanish Inquisition!"
        self.script._importEntry = FakeMethod(
            failure=UnexpectedException(message))
        self.script.main()

        self.assertEqual(RosettaImportStatus.FAILED, entry.status)
        self.assertEqual(message, entry.error_output)

    def test_main_leaves_oops_handling_alone(self):
        """Ensure that script.main is not altering oops reporting."""
        self.script.main()
        default_reporting = errorlog.ErrorReportingUtility()
        default_reporting.configure('error_reports')
        self.assertEqual(default_reporting.oops_prefix,
                         errorlog.globalErrorUtility.oops_prefix)

    def test_notifies_uploader(self):
        entry = self._makeApprovedEntry(self.owner)
        transaction.commit()
        self.script._importEntry(entry)
        transaction.commit()
        self.assertEqual([self.owner.preferredemail.email],
                         self._getEmailRecipients())

    def test_does_not_notify_vcs_imports(self):
        vcs_imports = getUtility(ILaunchpadCelebrities).vcs_imports
        entry = self._makeApprovedEntry(vcs_imports)
        transaction.commit()
        self.script._importEntry(entry)
        transaction.commit()
        self.assertEqual([], self._getEmailRecipients())