← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] lp:~jameinel/launchpad/fix-translations into lp:launchpad

 

John A Meinel has proposed merging lp:~jameinel/launchpad/fix-translations into lp:launchpad.

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)

For more details, see:
https://code.launchpad.net/~jameinel/launchpad/fix-translations/+merge/110502

We had a script 'in the wild' for cleaning up cruft left from a broken copy-translations-from-parent run. I'm bringing that script into the source tree, so that it can be slightly more maintained.

The code base already had a test for 'run everything in scripts/ with -h to make sure they are capable of running' which would have caught the problem I'm fixing. (test_all_scripts.py)

There are 3 changes of this script from what is on the wiki page: https://wiki.canonical.com/Launchpad/Translations/UbuntuOpenings

1) Deryck had already done the work to rename 'precise' to 'quantal' where appropriate.
2) Update the 'python' line at the top so that it doesn't require python2.6 (we use 2.7 now anyway)
3) Change the imports from canonical.launchpad.* to lp.services.*

Ideally if this is going to be used often, we should probably change it from hard-coding the distro series to having it as a command line option. However, I'm hesitant to touch anything at this point.
-- 
https://code.launchpad.net/~jameinel/launchpad/fix-translations/+merge/110502
Your team Launchpad code reviewers is requested to review the proposed merge of lp:~jameinel/launchpad/fix-translations into lp:launchpad.
=== added file 'scripts/fix-translations-opening.py'
--- scripts/fix-translations-opening.py	1970-01-01 00:00:00 +0000
+++ scripts/fix-translations-opening.py	2012-06-15 11:16:25 +0000
@@ -0,0 +1,159 @@
+#!/usr/bin/python -S
+
+__metaclass__ = type
+
+import _pythonpath
+
+from zope.component import getUtility
+from zope.interface import implements
+
+from lp.services.looptuner import (
+    DBLoopTuner,
+    ITunableLoop,
+    )
+from lp.services.webapp.interfaces import (
+    IStoreSelector,
+    MAIN_STORE,
+    MASTER_FLAVOR,
+    )
+from lp.services.scripts.base import LaunchpadScript
+
+
+select_quantal = """\
+SELECT DistroSeries.id
+  FROM DistroSeries
+  JOIN Distribution ON
+           Distribution.id = DistroSeries.distribution
+ WHERE Distribution.name = 'ubuntu'
+   AND DistroSeries.name = 'quantal'
+"""
+
+delete_pofiletranslator = """\
+DELETE FROM POFileTranslator
+ WHERE POFileTranslator.id IN (
+    SELECT POFileTranslator.id
+      FROM POFileTranslator, POFile, POTemplate
+     WHERE POFileTranslator.pofile = POFile.id
+       AND POFile.potemplate = POTemplate.id
+       AND POTemplate.distroseries = (%s)
+     LIMIT ?)
+""" % select_quantal
+
+null_translationimportqueueentry_pofile = """\
+UPDATE TranslationImportQueueEntry
+   SET pofile = NULL
+ WHERE TranslationImportQueueEntry.id IN (
+    SELECT TranslationImportQueueEntry.id
+      FROM TranslationImportQueueEntry, POFile, POTemplate
+     WHERE TranslationImportQueueEntry.pofile = POFile.id
+       AND POFile.potemplate = POTemplate.id
+       AND POTemplate.distroseries = (%s)
+     LIMIT ?)
+""" % select_quantal
+
+delete_pofile = """\
+DELETE FROM POFile
+ WHERE POFile.id IN (
+    SELECT POFile.id
+      FROM POFile, POTemplate
+     WHERE POFile.potemplate = POTemplate.id
+       AND POTemplate.distroseries = (%s)
+     LIMIT ?)
+""" % select_quantal
+
+delete_translationtemplateitem = """\
+DELETE FROM TranslationTemplateItem
+ WHERE TranslationTemplateItem.id IN (
+    SELECT TranslationTemplateItem.id
+      FROM TranslationTemplateItem, POTemplate
+     WHERE TranslationTemplateItem.potemplate = POTemplate.id
+       AND POTemplate.distroseries = (%s)
+     LIMIT ?)
+""" % select_quantal
+
+delete_packagingjob = """\
+DELETE FROM PackagingJob
+ WHERE PackagingJob.id IN (
+    SELECT PackagingJob.id
+      FROM PackagingJob, POTemplate
+     WHERE PackagingJob.potemplate = POTemplate.id
+       AND POTemplate.distroseries = (%s)
+     LIMIT ?)
+""" % select_quantal
+
+null_translationimportqueueentry_potemplate = """\
+UPDATE TranslationImportQueueEntry
+   SET potemplate = NULL
+ WHERE TranslationImportQueueEntry.id IN (
+    SELECT TranslationImportQueueEntry.id
+      FROM TranslationImportQueueEntry, POTemplate
+     WHERE TranslationImportQueueEntry.potemplate = POTemplate.id
+       AND POTemplate.distroseries = (%s)
+     LIMIT ?)
+""" % select_quantal
+
+delete_potemplate = """\
+DELETE FROM POTemplate
+ WHERE POTemplate.id IN (
+    SELECT POTemplate.id
+      FROM POTemplate
+     WHERE POTemplate.distroseries = (%s)
+     LIMIT ?)
+""" % select_quantal
+
+statements = [
+    delete_pofiletranslator,
+    null_translationimportqueueentry_pofile,
+    delete_pofile,
+    delete_translationtemplateitem,
+    delete_packagingjob,
+    null_translationimportqueueentry_potemplate,
+    delete_potemplate,
+    ]
+
+
+class ExecuteLoop:
+
+    implements(ITunableLoop)
+
+    def __init__(self, statement, logger):
+        self.statement = statement
+        self.logger = logger
+        self.done = False
+
+    def isDone(self):
+        return self.done
+
+    def __call__(self, chunk_size):
+        self.logger.info(
+            "%s (limited to %d rows)",
+            self.statement.splitlines()[0],
+            chunk_size)
+        store = getUtility(IStoreSelector).get(MAIN_STORE, MASTER_FLAVOR)
+        result = store.execute(self.statement, (chunk_size,))
+        self.done = (result.rowcount == 0)
+        self.logger.info(
+            "%d rows deleted (%s)", result.rowcount,
+            ("done" if self.done else "not done"))
+        store.commit()
+
+
+class WipeQuantalTranslationsScript(LaunchpadScript):
+
+    description = "Wipe Ubuntu Quantal's translations."
+
+    def add_my_options(self):
+        self.parser.epilog = (
+            "Before running this script you must `GRANT DELETE ON TABLE "
+            "PackagingJob TO rosettaadmin` and afterwards you ought to "
+            "`REVOKE DELETE ON PackagingJob FROM rosettaadmin`.")
+
+    def main(self):
+        for statement in statements:
+            delete = ExecuteLoop(statement, self.logger)
+            tuner = DBLoopTuner(delete, 2.0, maximum_chunk_size=5000)
+            tuner.run()
+
+
+if __name__ == '__main__':
+    WipeQuantalTranslationsScript(dbuser='rosettaadmin').run()


Follow ups