← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] lp:~stevenk/launchpad/gina-log-to-scriptactivity into lp:launchpad/devel

 

Steve Kowalik has proposed merging lp:~stevenk/launchpad/gina-log-to-scriptactivity into lp:launchpad/devel.

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)


This branch brutally hacks scripts/gina.py so that it sub-classes LaunchpadCronScript, which 1) Brings it into line with other cron scripts, and 2) Gets gina logging to scriptactivity, which means the LOSAs can check if gina has been running sucessfully.

I have fixed lint as I tripped over it, and have also cleaned up pyflakes.
-- 
https://code.launchpad.net/~stevenk/launchpad/gina-log-to-scriptactivity/+merge/33495
Your team Launchpad code reviewers is requested to review the proposed merge of lp:~stevenk/launchpad/gina-log-to-scriptactivity into lp:launchpad/devel.
=== modified file 'scripts/gina.py'
--- scripts/gina.py	2010-04-27 19:48:39 +0000
+++ scripts/gina.py	2010-08-24 07:58:41 +0000
@@ -1,6 +1,6 @@
 #!/usr/bin/python -S
 #
-# Copyright 2009 Canonical Ltd.  This software is licensed under the
+# Copyright 2009,2010 Canonical Ltd.  This software is licensed under the
 # GNU Affero General Public License version 3 (see the file LICENSE).
 
 # This module uses relative imports.
@@ -23,9 +23,7 @@
 # Set to non-zero if you'd like to be warned every so often
 COUNTDOWN = 0
 
-import _pythonpath
 
-from optparse import OptionParser
 import os
 import psycopg2
 import sys
@@ -33,20 +31,16 @@
 
 from zope.component import getUtility
 
-from contrib.glock import GlobalLock, LockAlreadyAcquired
-
 from canonical import lp
-from canonical.lp import initZopeless
 from canonical.config import config
+from canonical.launchpad.scripts import log
+
+from lp.services.scripts.base import LaunchpadCronScript
 from lp.soyuz.interfaces.component import IComponentSet
-from canonical.launchpad.scripts import (
-    execute_zcml_for_scripts, logger_options, log)
-
 from lp.soyuz.scripts.gina import ExecutionError
 from lp.soyuz.scripts.gina.katie import Katie
 from lp.soyuz.scripts.gina.archive import (ArchiveComponentItems,
     PackagesMap, MangledArchiveError)
-
 from lp.soyuz.scripts.gina.handlers import (ImporterHandler,
     MultiplePackageReleaseError, NoSourcePackageError, DataSetupError)
 from lp.soyuz.scripts.gina.packages import (SourcePackageData,
@@ -65,56 +59,6 @@
     return keyrings
 
 
-def main():
-    execute_zcml_for_scripts()
-    parser = OptionParser("Usage: %prog [OPTIONS] [target ...]")
-    logger_options(parser)
-
-    parser.add_option("-n", "--dry-run", action="store_true",
-            help="Don't commit changes to the database",
-            dest="dry_run", default=False)
-
-    parser.add_option("-a", "--all", action="store_true",
-            help="Run all sections defined in launchpad.conf (in order)",
-            dest="all", default=False)
-
-    parser.add_option( "-l", "--lockfile",
-            default="/var/lock/launchpad-gina.lock",
-            help="Ensure only one process is running that locks LOCKFILE",
-            metavar="LOCKFILE"
-            )
-
-    (options, targets) = parser.parse_args()
-
-    possible_targets = [target.category_and_section_names[1]
-                        for target in config.getByCategory('gina_target')]
-
-    if options.all:
-        targets = possible_targets[:]
-    else:
-        if not targets:
-            parser.error("Must specify at least one target to run, or --all")
-
-        for target in targets:
-            if target not in possible_targets:
-                parser.error("No Gina target %s in config file" % target)
-
-    lockfile = GlobalLock(options.lockfile, logger=log)
-    try:
-        lockfile.acquire()
-    except LockAlreadyAcquired:
-        log.info('Lockfile %s already locked. Exiting.', options.lockfile)
-        sys.exit(1)
-
-    ztm = initZopeless(dbuser=config.gina.dbuser)
-    try:
-        for target in targets:
-            target_section = config['gina_target.%s' % target]
-            run_gina(options, ztm, target_section)
-    finally:
-        lockfile.release()
-
-
 def run_gina(options, ztm, target_section):
     # Avoid circular imports.
     from lp.registry.interfaces.pocket import PackagePublishingPocket
@@ -352,6 +296,39 @@
     importer_handler.commit()
 
 
+class Gina(LaunchpadCronScript):
+
+    def __init__(self):
+        super(Gina, self).__init__(name='gina', dbuser=config.gina.dbuser)
+
+    def add_my_options(self):
+        self.parser.add_option("-n", "--dry-run", action="store_true",
+            help="Don't commit changes to the database",
+            dest="dry_run", default=False)
+        self.parser.add_option("-a", "--all", action="store_true",
+            help="Run all sections defined in launchpad.conf (in order)",
+            dest="all", default=False)
+
+    def main(self):
+        possible_targets = [target.category_and_section_names[1]
+            for target in config.getByCategory('gina_target')]
+        targets = self.args
+        if self.options.all:
+            targets = possible_targets[:]
+        else:
+            if not targets:
+                self.parser.error(
+                    "Must specify at least one target to run, or --all")
+            for target in targets:
+                if target not in possible_targets:
+                    self.parser.error(
+                        "No Gina target %s in config file" % target)
+
+        for target in targets:
+            target_section = config['gina_target.%s' % target]
+            run_gina(self.options, self.txn, target_section)
+
+
 if __name__ == "__main__":
-    main()
-
+    gina = Gina()
+    gina.lock_and_run()


Follow ups