[damned-lies] Converted management commands from optparse to argparse syntax



commit 23d5586ff67cb03321f1c9e081fc84beb9ae465c
Author: Claude Paroz <claude 2xlibre net>
Date:   Thu Sep 22 11:24:27 2016 +0200

    Converted management commands from optparse to argparse syntax

 stats/management/commands/archive-release.py  |   18 ++++-----
 stats/management/commands/compile-trans.py    |    2 -
 stats/management/commands/copy-release.py     |   25 +++++++-------
 stats/management/commands/update-from-doap.py |   10 ++---
 stats/management/commands/update-stats.py     |   45 +++++++++++-------------
 stats/management/commands/update-trans.py     |   17 +++------
 6 files changed, 52 insertions(+), 65 deletions(-)
---
diff --git a/stats/management/commands/archive-release.py b/stats/management/commands/archive-release.py
index 45ce053..8fd75cc 100644
--- a/stats/management/commands/archive-release.py
+++ b/stats/management/commands/archive-release.py
@@ -1,24 +1,22 @@
 # -*- coding: utf-8 -*-
 import csv
-import sys
 import StringIO
-from django.core.management.base import BaseCommand
+from django.core.management.base import BaseCommand, CommandError
+
 from stats.models import Release, Statistics
 
+
 class Command(BaseCommand):
     help = "Write statistics of a release in a CSV structure (stdout)"
-    args = "RELEASE"
 
-    def handle(self, *args, **options):
-        if len(args) != 1:
-            sys.stderr.write("You must provide one and only one release name as parameter of this 
command.\n")
-            sys.exit(1)
+    def add_arguments(self, parser):
+        parser.add_argument('release')
 
+    def handle(self, **options):
         try:
-            release = Release.objects.get(name=args[0])
+            release = Release.objects.get(name=options['release'])
         except Release.DoesNotExist:
-            sys.stderr.write("The release name '%s' is not known.\n" % args[0])
-            sys.exit(1)
+            raise CommandError("The release name '%s' is not known.\n" % options['release'])
 
         out = StringIO.StringIO()
         headers = ['Module', 'Branch', 'Domain', 'Language', 'Translated', 'Fuzzy', 'Untranslated']
diff --git a/stats/management/commands/compile-trans.py b/stats/management/commands/compile-trans.py
index e496663..85e4d28 100644
--- a/stats/management/commands/compile-trans.py
+++ b/stats/management/commands/compile-trans.py
@@ -9,8 +9,6 @@ from django.core.management.base import BaseCommand
 class Command(BaseCommand):
     help = "Compile translations of djamnedlies"
 
-    output_transaction = False
-
     def handle(self, **options):
         # Copy all po/ll.po files in locale/ll/LC_MESSAGES/django.po
         podir = os.path.abspath('po')
diff --git a/stats/management/commands/copy-release.py b/stats/management/commands/copy-release.py
index 5ca2f2d..5e8c220 100644
--- a/stats/management/commands/copy-release.py
+++ b/stats/management/commands/copy-release.py
@@ -20,23 +20,24 @@
 from django.core.management.base import BaseCommand, CommandError
 from stats.models import Release, Category, Module
 
-class Command(BaseCommand):
-    help = "Copy an existing release and use trunk branches"
-    args = "RELEASE_TO_COPY, NEW_RELEASE"
 
-    output_transaction = False
+class Command(BaseCommand):
+    help = "Copy an existing release and use master branches"
 
-    def handle(self, *args, **options):
-        if len(args) != 2:
-            raise CommandError("Usage: copy-release RELEASE_TO_COPY NEW_RELEASE")
+    def add_arguments(self, parser):
+        parser.add_argument('release_to_copy')
+        parser.add_argument('new_release')
 
+    def handle(self, **options):
         try:
-            rel_to_copy = Release.objects.get(name=args[0])
+            rel_to_copy = Release.objects.get(name=options['release_to_copy'])
         except Release.DoesNotExist:
-            raise CommandError("No release named '%s'" % args[0])
+            raise CommandError("No release named '%s'" % options['release_to_copy'])
 
-        new_rel = Release(name=args[1], description=args[1], string_frozen=False, status=rel_to_copy.status)
-        new_rel.save()
+        new_rel = Release.objects.create(
+            name=options['new_release'], description=options['new_release'],
+            string_frozen=False, status=rel_to_copy.status
+        )
 
         for cat in rel_to_copy.category_set.all():
             if not cat.branch.is_head():
@@ -46,4 +47,4 @@ class Command(BaseCommand):
                 branch = cat.branch
             new_rel.category_set.add(Category(release=new_rel, branch=branch, name=cat.name))
 
-        return "New release '%s' created" % args[1]
+        return "New release '%s' created" % new_rel.name
diff --git a/stats/management/commands/update-from-doap.py b/stats/management/commands/update-from-doap.py
index 10f1c68..407e978 100644
--- a/stats/management/commands/update-from-doap.py
+++ b/stats/management/commands/update-from-doap.py
@@ -7,14 +7,12 @@ from stats.doap import update_doap_infos
 
 class Command(BaseCommand):
     help = "Update module information from doap file"
-    args = "MODULE"
 
-    def handle(self, *args, **options):
-        if len(args) == 0:
-            sys.stderr.write("You must provide at least one module name for this command.\n")
-            sys.exit(1)
+    def add_arguments(self, parser):
+        parser.add_argument('module', nargs='+')
 
-        for mod_name in args:
+    def handle(self, **options):
+        for mod_name in options['module']:
             try:
                 mod = Module.objects.get(name=mod_name)
             except Module.DoesNotExist:
diff --git a/stats/management/commands/update-stats.py b/stats/management/commands/update-stats.py
index 6576056..48352b5 100644
--- a/stats/management/commands/update-stats.py
+++ b/stats/management/commands/update-stats.py
@@ -1,6 +1,5 @@
 # -*- coding: utf-8 -*-
-import sys, traceback
-from optparse import make_option
+import traceback
 
 from django.core.management.base import BaseCommand, CommandError
 from django.core.mail import mail_admins
@@ -8,36 +7,34 @@ from django.db.models import Q
 
 from stats.models import Module, Branch
 
-class Command(BaseCommand):
-    help = "Update statistics about po file"
-    args = "[MODULE [BRANCH]]"
 
-    option_list = BaseCommand.option_list + (
-        make_option('--force', action='store_true', dest='force', default=False,
-            help="force statistics generation, even if files didn't change"),
-        make_option('--non-gnome', action='store_true', dest='non-gnome', default=False,
-            help="generate statistics for non-gnome modules (externally hosted)"),
-        make_option('--debug', action='store_true', dest='debug', default=False,
-            help="activate interactive debug mode"),
-    )
+class Command(BaseCommand):
+    help = "Update statistics about po files"
 
-    output_transaction = False
+    def add_arguments(self, parser):
+        parser.add_argument('module', nargs='?', default=None)
+        parser.add_argument('branch', nargs='*')
+        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,
+            help="generate statistics for non-gnome modules (externally hosted)")
+        parser.add_argument('--debug', action='store_true', default=False,
+            help="activate interactive debug mode")
 
-    def handle(self, *args, **options):
+    def handle(self, **options):
         if options['debug']:
             import pdb; pdb.set_trace()
-        if len(args) >= 2:
+        if options['module'] and options['branch']:
             # Update the specific branch(es) of a module
-            module_arg = args[0]
-            branch_list = args[1:]
-
             # This allows several modules (differently named) to point to the same vcs repo
-            modules = Module.objects.filter(Q(name=module_arg) | Q(vcs_root__endswith='/%s' % module_arg))
+            modules = Module.objects.filter(
+                Q(name=options['module']) | Q(vcs_root__endswith='/%s' % options['module'])
+            )
             for i, module in enumerate(modules):
                 if module.archived and not options['force']:
                     self.stderr.write("The module '%s' is archived. Skipping..." % module.name)
                     continue
-                for branch_arg in branch_list:
+                for branch_arg in options['branch']:
                     if branch_arg == "trunk":
                         branch_arg = "HEAD"
 
@@ -56,12 +53,12 @@ class Command(BaseCommand):
                         mail_admins("Error while updating %s %s" % (module.name, branch_arg), tbtext)
                         raise CommandError("Error during updating, mail sent to admins")
 
-        elif len(args) == 1:
+        elif options['module']:
             # Update all branches of a module
             try:
-                module = Module.objects.get(name=args[0])
+                module = Module.objects.get(name=options['module'])
             except Module.DoesNotExist:
-                raise CommandError("Unable to find a module named '%s' in the database" % args[0])
+                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:
diff --git a/stats/management/commands/update-trans.py b/stats/management/commands/update-trans.py
index 0139971..0c68ef0 100644
--- a/stats/management/commands/update-trans.py
+++ b/stats/management/commands/update-trans.py
@@ -15,19 +15,14 @@ from stats.models import Module, Domain, Release, CategoryName
 
 class Command(BaseCommand):
     help = "Update translations of djamnedlies ('en' is a special case, and generate damned-lies.pot)"
-    args = "LANG_CODE"
 
-    #option_list = BaseCommand.option_list + (
-    #    make_option('--pot', action='store_true', dest='pot', default=False,
-    #        help="create a pot file"),
-    #)
+    def add_arguments(self, parser):
+        parser.add_argument('lang_code', help="language code ('it', 'pt_BR', etc.)")
+        # parser.add_argument('--pot', action='store_true', dest='pot', default=False,
+        #        help="create a pot file"),
 
-    output_transaction = False
-
-    def handle(self, *args, **options):
-        if len(args)!=1:
-            return "You have to specify language code as first and only argument."
-        lang_code = args[0]
+    def handle(self, **options):
+        lang_code = options['lang_code']
 
         # Copy po/ll.po in locale/ll/LC_MESSAGES/django.po
         podir = os.path.abspath('po')


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