Browse Source

scripts/buildhistory-diff: use of argparse instead of optparse

Optparse is deprecated since version 2.7 and won't be developed further.
Argparse should be used instead as it provides better tools for parsing
and handling arguments.

[YOCTO #9635]

Signed-off-by: Daniela Plascencia <daniela.plascencia@linux.intel.com>
Signed-off-by: Ross Burton <ross.burton@intel.com>
Daniela Plascencia 6 years ago
parent
commit
e67b40c01f
1 changed files with 71 additions and 54 deletions
  1. 71 54
      scripts/buildhistory-diff

+ 71 - 54
scripts/buildhistory-diff

@@ -7,7 +7,7 @@
 
 import sys
 import os
-import optparse
+import argparse
 from distutils.version import LooseVersion
 
 # Ensure PythonGit is installed (buildhistory_analysis needs it)
@@ -17,49 +17,70 @@ except ImportError:
     print("Please install GitPython (python3-git) 0.3.4 or later in order to use this script")
     sys.exit(1)
 
+def get_args_parser():
+    description = "Reports significant differences in the buildhistory repository."
+
+    parser = argparse.ArgumentParser(description=description,
+                                     usage="""
+    %(prog)s [options] [from-revision [to-revision]]
+    (if not specified, from-revision defaults to build-minus-1, and to-revision defaults to HEAD)""")
+
+    parser.add_argument('-p', '--buildhistory-dir',
+                        action='store',
+                        dest='buildhistory_dir',
+                        default='buildhistory/',
+                        help="Specify path to buildhistory directory (defaults to buildhistory/ under cwd)")
+    parser.add_argument('-v', '--report-version',
+                        action='store_true',
+                        dest='report_ver',
+                        default=False,
+                        help="Report changes in PKGE/PKGV/PKGR even when the values are still the default (PE/PV/PR)")
+    parser.add_argument('-a', '--report-all',
+                        action='store_true',
+                        dest='report_all',
+                        default='False',
+                        help="Report all changes, not just the default significant ones")
+    parser.add_argument('-s', '---signatures',
+                        action='store_true',
+                        dest='sigs',
+                        default=False,
+                        help="Report list of signatures differing instead of output")
+    parser.add_argument('-S', '--signatures-with-diff',
+                        action='store_true',
+                        dest='sigsdiff',
+                        default=False,
+                        help="Report on actual signature differences instead of output (requires signature data to have been generated, either by running the actual tasks or using bitbake -S)")
+    parser.add_argument('-e', '--exclude-path',
+                        action='append',
+                        help="Exclude path from the output")
+    parser.add_argument('revisions',
+                        default = ['build-minus-1', 'HEAD'],
+                        nargs='*',
+                        help=argparse.SUPPRESS)
+    return parser
+
 def main():
-    parser = optparse.OptionParser(
-        description = "Reports significant differences in the buildhistory repository.",
-        usage = """
-    %prog [options] [from-revision [to-revision]]
-(if not specified, from-revision defaults to build-minus-1, and to-revision defaults to HEAD)""")
-
-    parser.add_option("-p", "--buildhistory-dir",
-            help = "Specify path to buildhistory directory (defaults to buildhistory/ under cwd)",
-            action="store", dest="buildhistory_dir", default='buildhistory/')
-    parser.add_option("-v", "--report-version",
-            help = "Report changes in PKGE/PKGV/PKGR even when the values are still the default (PE/PV/PR)",
-            action="store_true", dest="report_ver", default=False)
-    parser.add_option("-a", "--report-all",
-            help = "Report all changes, not just the default significant ones",
-            action="store_true", dest="report_all", default=False)
-    parser.add_option("-s", "--signatures",
-            help = "Report list of signatures differing instead of output",
-            action="store_true", dest="sigs", default=False)
-    parser.add_option("-S", "--signatures-with-diff",
-            help = "Report on actual signature differences instead of output (requires signature data to have been generated, either by running the actual tasks or using bitbake -S)",
-            action="store_true", dest="sigsdiff", default=False)
-    parser.add_option("-e", "--exclude-path", action="append",
-            help = "exclude path from the output")
-
-    options, args = parser.parse_args(sys.argv)
-
-    if len(args) > 3:
-        sys.stderr.write('Invalid argument(s) specified: %s\n\n' % ' '.join(args[3:]))
-        parser.print_help()
-        sys.exit(1)
+
+    parser = get_args_parser()
+    args = parser.parse_args()
 
     if LooseVersion(git.__version__) < '0.3.1':
         sys.stderr.write("Version of GitPython is too old, please install GitPython (python-git) 0.3.1 or later in order to use this script\n")
         sys.exit(1)
 
-    if not os.path.exists(options.buildhistory_dir):
-        if options.buildhistory_dir == 'buildhistory/':
+    if len(args.revisions) > 2:
+        sys.stderr.write('Invalid argument(s) specified: %s\n\n' % ' '.join(args.revisions[2:]))
+        parser.print_help()
+
+        sys.exit(1)
+    if not os.path.exists(args.buildhistory_dir):
+        if args.buildhistory_dir == 'buildhistory/':
             cwd = os.getcwd()
             if os.path.basename(cwd) == 'buildhistory':
-                options.buildhistory_dir = cwd
-    if not os.path.exists(options.buildhistory_dir):
-        sys.stderr.write('Buildhistory directory "%s" does not exist\n\n' % options.buildhistory_dir)
+                args.buildhistory_dir = cwd
+
+    if not os.path.exists(args.buildhistory_dir):
+        sys.stderr.write('Buildhistory directory "%s" does not exist\n\n' % args.buildhistory_dir)
         parser.print_help()
         sys.exit(1)
 
@@ -73,32 +94,29 @@ def main():
     scriptpath.add_oe_lib_path()
     # Set path to bitbake lib dir so the buildhistory_analysis module can load bb.utils
     bitbakepath = scriptpath.add_bitbake_lib_path()
+
     if not bitbakepath:
         sys.stderr.write("Unable to find bitbake by searching parent directory of this script or PATH\n")
         sys.exit(1)
 
-    from oe.buildhistory_analysis import process_changes
-
-    fromrev = 'build-minus-1'
-    torev = 'HEAD'
-    if len(args) > 1:
-        if len(args) == 2 and '..' in args[1]:
-            revs = args[1].split('..')
-            fromrev = revs[0]
-            if revs[1]:
-                torev = revs[1]
+    if len(args.revisions) == 1:
+        if '..'  in args.revisions[0]:
+            fromrev, torev = args.revisions[0].split('..')
         else:
-            fromrev = args[1]
-    if len(args) > 2:
-        torev = args[2]
+            fromrev, torev = args.revisions[0], 'HEAD'
+    elif len(args.revisions) == 2:
+        fromrev, torev = args.revisions
+
+    from oe.buildhistory_analysis import process_changes
 
     import gitdb
+
     try:
-        changes = process_changes(options.buildhistory_dir, fromrev, torev,
-                                  options.report_all, options.report_ver, options.sigs,
-                                  options.sigsdiff, options.exclude_path)
+        changes = process_changes(args.buildhistory_dir, fromrev, torev,
+                                  args.report_all, args.report_ver, args.sigs,
+                                  args.sigsdiff, args.exclude_path)
     except gitdb.exc.BadObject as e:
-        if len(args) == 1:
+        if not args.revisions:
             sys.stderr.write("Unable to find previous build revision in buildhistory repository\n\n")
             parser.print_help()
         else:
@@ -112,6 +130,5 @@ def main():
 
     sys.exit(0)
 
-
 if __name__ == "__main__":
     main()