[damned-lies] Allow updating all release branches with update-stats command



commit a46f5fb0afcbb01c60e6e925b301c7c67bc1362a
Author: Claude Paroz <claude 2xlibre net>
Date:   Thu Sep 22 15:46:51 2016 +0200

    Allow updating all release branches with update-stats command

 stats/management/commands/update-stats.py |   62 ++++++++++++++++------------
 1 files changed, 35 insertions(+), 27 deletions(-)
---
diff --git a/stats/management/commands/update-stats.py b/stats/management/commands/update-stats.py
index 48352b5..f2f0875 100644
--- a/stats/management/commands/update-stats.py
+++ b/stats/management/commands/update-stats.py
@@ -5,7 +5,7 @@ from django.core.management.base import BaseCommand, CommandError
 from django.core.mail import mail_admins
 from django.db.models import Q
 
-from stats.models import Module, Branch
+from stats.models import Module, Branch, Release
 
 
 class Command(BaseCommand):
@@ -14,6 +14,8 @@ class Command(BaseCommand):
     def add_arguments(self, parser):
         parser.add_argument('module', nargs='?', default=None)
         parser.add_argument('branch', nargs='*')
+        parser.add_argument('--release',
+            help="update all modules and branches linked to the specified release name")
         parser.add_argument('--force', action='store_true', default=False,
             help="force statistics generation, even if files didn't change")
         parser.add_argument('--non-gnome', action='store_true', default=False,
@@ -24,6 +26,7 @@ class Command(BaseCommand):
     def handle(self, **options):
         if options['debug']:
             import pdb; pdb.set_trace()
+        self.force_stats = options['force']
         if options['module'] and options['branch']:
             # Update the specific branch(es) of a module
             # This allows several modules (differently named) to point to the same vcs repo
@@ -31,27 +34,21 @@ class Command(BaseCommand):
                 Q(name=options['module']) | Q(vcs_root__endswith='/%s' % options['module'])
             )
             for i, module in enumerate(modules):
-                if module.archived and not options['force']:
+                if module.archived and not self.force_stats:
                     self.stderr.write("The module '%s' is archived. Skipping..." % module.name)
                     continue
+                branches = []
                 for branch_arg in options['branch']:
                     if branch_arg == "trunk":
                         branch_arg = "HEAD"
 
                     try:
-                        branch = module.branch_set.get(name=branch_arg)
+                        branches.append(module.branch_set.get(name=branch_arg))
                     except Branch.DoesNotExist:
                         self.stderr.write("Unable to find branch '%s' for module '%s' in the database." % (
                             branch_arg, module.name))
                         continue
-
-                    self.stdout.write("Updating stats for %s.%s..." % (module.name, branch_arg))
-                    try:
-                        branch.update_stats(options['force'], checkout=(i<1))
-                    except Exception:
-                        tbtext = traceback.format_exc()
-                        mail_admins("Error while updating %s %s" % (module.name, branch_arg), tbtext)
-                        raise CommandError("Error during updating, mail sent to admins")
+                self.update_branches(branches, checkout=(i < 1))
 
         elif options['module']:
             # Update all branches of a module
@@ -59,30 +56,41 @@ class Command(BaseCommand):
                 module = Module.objects.get(name=options['module'])
             except Module.DoesNotExist:
                 raise CommandError("Unable to find a module named '%s' in the database" % options['module'])
-            self.stdout.write("Updating stats for %s..." % (module.name))
-            for branch in module.branch_set.all():
-                try:
-                    branch.update_stats(options['force'])
-                except Exception as exc:
-                    raise CommandError("Error while updating stats for %s (branch '%s'): %s" % (
-                        module.name, branch.name, exc))
+            if module.archived and not self.force_stats:
+                self.stderr.write("The module '%s' is archived. Skipping..." % module.name)
+            else:
+                self.update_branches(module.branch_set.all())
+
+        elif options['release']:
+            if options['module']:
+                raise CommandError("The --release option cannot be combined with module/branch parameters.")
+            try:
+                release = Release.objects.get(name=options['release'])
+            except Release.DoesNotExist:
+                raise CommandError("Unable to find a release named '%s' in the database" % 
options['release'])
+            self.update_branches(release.branches.all())
+
         else:
             # Update all modules
             if options['non-gnome']:
                 modules = Module.objects.exclude(vcs_root__contains='git.gnome.org')
             else:
                 modules = Module.objects.all()
-            if not options['force']:
+            if not self.force_stats:
                 modules = modules.exclude(archived=True)
 
             for mod in modules:
-                self.stdout.write("Updating stats for %s..." % mod.name)
-                branches = Branch.objects.filter(module__name=mod)
-                for branch in branches.all():
-                    try:
-                        branch.update_stats(options['force'])
-                    except Exception as exc:
-                        self.stderr.write("Error while updating stats for %s (branch '%s'): %s" % (
-                            mod.name, branch.name, exc))
+                self.update_branches(mod.branch_set.all())
 
         return "Update completed.\n"
+
+    def update_branches(self, branches, checkout=True):
+        for branch in branches:
+            self.stdout.write("Updating stats for %s.%s..." % (branch.module.name, branch.name))
+            try:
+                branch.update_stats(self.force_stats, checkout=checkout)
+            except Exception as exc:
+                tbtext = traceback.format_exc()
+                mail_admins("Error while updating %s %s" % (branch.module.name, branch.name), tbtext)
+                self.stderr.write("Error while updating stats for %s %s: %s" % (
+                    branch.module.name, branch.name, exc))


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]