From f262d686486e0358a36ec4372b560ef93ded8147 Mon Sep 17 00:00:00 2001
From: Paladox none <thomasmulhall410@yahoo.com>
Date: Sun, 2 Feb 2020 01:40:48 +0000
Subject: [PATCH] Replace deprecated OptionParser with ArgumentParser

Change-Id: I8dbee8967edd9cfe1e2f9e2df97d786a2bd1ca94
---
 Documentation/replace_macros.py  | 30 ++++++++++-----------
 contrib/populate-fixture-data.py | 18 ++++++-------
 tools/download_file.py           | 18 ++++++-------
 tools/js/bower2bazel.py          | 26 +++++++++---------
 tools/js/download_bower.py       | 46 ++++++++++++++++----------------
 tools/maven/mvn.py               | 18 ++++++-------
 tools/version.py                 | 15 ++++-------
 7 files changed, 83 insertions(+), 88 deletions(-)

diff --git a/Documentation/replace_macros.py b/Documentation/replace_macros.py
index 309a135b7f..d1b50d31ce 100755
--- a/Documentation/replace_macros.py
+++ b/Documentation/replace_macros.py
@@ -14,7 +14,7 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-from optparse import OptionParser
+import argparse
 import re
 import sys
 
@@ -219,23 +219,23 @@ LINK_SCRIPT = """
 
 """
 
-opts = OptionParser()
-opts.add_option('-o', '--out', help='output file')
-opts.add_option('-s', '--src', help='source file')
-opts.add_option('-x', '--suffix', help='suffix for included filenames')
-opts.add_option('-b', '--searchbox', action="store_true", default=True,
+parser = argparse.ArgumentParser()
+parser.add_argument('-o', '--out', help='output file')
+parser.add_argument('-s', '--src', help='source file')
+parser.add_argument('-x', '--suffix', help='suffix for included filenames')
+parser.add_argument('-b', '--searchbox', action="store_true", default=True,
                 help="generate the search boxes")
-opts.add_option('--no-searchbox', action="store_false", dest='searchbox',
+parser.add_argument('--no-searchbox', action="store_false", dest='searchbox',
                 help="don't generate the search boxes")
-options, _ = opts.parse_args()
+args = parser.parse_args()
 
 try:
     try:
-        out_file = open(options.out, 'w', errors='ignore')
-        src_file = open(options.src, 'r', errors='ignore')
+        out_file = open(args.out, 'w', errors='ignore')
+        src_file = open(args.src, 'r', errors='ignore')
     except TypeError:
-        out_file = open(options.out, 'w')
-        src_file = open(options.src, 'r')
+        out_file = open(args.out, 'w')
+        src_file = open(args.src, 'r')
     last_line = ''
     ignore_next_line = False
     last_title = ''
@@ -246,14 +246,14 @@ try:
             last_line = ''
         elif PAT_SEARCHBOX.match(last_line):
             # Case of 'SEARCHBOX\n---------'
-            if options.searchbox:
+            if args.searchbox:
                 out_file.write(SEARCH_BOX)
             last_line = ''
         elif PAT_INCLUDE.match(line):
             # Case of 'include::<filename>'
             match = PAT_INCLUDE.match(line)
             out_file.write(last_line)
-            last_line = match.group(1) + options.suffix + match.group(2) + '\n'
+            last_line = match.group(1) + args.suffix + match.group(2) + '\n'
         elif PAT_STARS.match(line):
             if PAT_TITLE.match(last_line):
                 # Case of the title in '.<title>\n****\nget::<url>\n****'
@@ -279,5 +279,5 @@ try:
     out_file.close()
 except IOError as err:
     sys.stderr.write(
-        "error while expanding %s to %s: %s" % (options.src, options.out, err))
+        "error while expanding %s to %s: %s" % (args.src, args.out, err))
     exit(1)
diff --git a/contrib/populate-fixture-data.py b/contrib/populate-fixture-data.py
index 22e0c1b71e..4c6769cf3e 100755
--- a/contrib/populate-fixture-data.py
+++ b/contrib/populate-fixture-data.py
@@ -22,9 +22,9 @@ TODO(hiesel): Add comments
 """
 
 from __future__ import print_function
+import argparse
 import atexit
 import json
-import optparse
 import os
 import random
 import shutil
@@ -275,22 +275,22 @@ def clean_up():
 
 
 def main():
-    p = optparse.OptionParser()
-    p.add_option("-u", "--user_count", action="store",
+    p = argparse.ArgumentParser()
+    p.add_argument("-u", "--user_count", action="store",
                  default=100,
-                 type='int',
+                 type=int,
                  help="number of users to generate")
-    p.add_option("-p", "--port", action="store",
+    p.add_argument("-p", "--port", action="store",
                  default=8080,
-                 type='int',
+                 type=int,
                  help="port of server")
-    (options, _) = p.parse_args()
+    args = p.parse_args()
     global BASE_URL
-    BASE_URL = BASE_URL % options.port
+    BASE_URL = BASE_URL % args.port
     print(BASE_URL)
 
     set_up()
-    gerrit_users = get_random_users(options.user_count)
+    gerrit_users = get_random_users(args.user_count)
 
     group_names = create_gerrit_groups()
     for idx, u in enumerate(gerrit_users):
diff --git a/tools/download_file.py b/tools/download_file.py
index 29398e6082..7b3bf95c3d 100755
--- a/tools/download_file.py
+++ b/tools/download_file.py
@@ -15,8 +15,8 @@
 
 from __future__ import print_function
 
+import argparse
 from hashlib import sha1
-from optparse import OptionParser
 from os import link, makedirs, path, remove
 import shutil
 from subprocess import check_call, CalledProcessError
@@ -75,14 +75,14 @@ def cache_entry(args):
     return path.join(CACHE_DIR, name)
 
 
-opts = OptionParser()
-opts.add_option('-o', help='local output file')
-opts.add_option('-u', help='URL to download')
-opts.add_option('-v', help='expected content SHA-1')
-opts.add_option('-x', action='append', help='file to delete from ZIP')
-opts.add_option('--exclude_java_sources', action='store_true')
-opts.add_option('--unsign', action='store_true')
-args, _ = opts.parse_args()
+parser = argparse.ArgumentParser()
+parser.add_argument('-o', help='local output file')
+parser.add_argument('-u', help='URL to download')
+parser.add_argument('-v', help='expected content SHA-1')
+parser.add_argument('-x', action='append', help='file to delete from ZIP')
+parser.add_argument('--exclude_java_sources', action='store_true')
+parser.add_argument('--unsign', action='store_true')
+args = parser.parse_args()
 
 root_dir = args.o
 while root_dir and path.dirname(root_dir) != root_dir:
diff --git a/tools/js/bower2bazel.py b/tools/js/bower2bazel.py
index 7d00c0dba5..ec1ead5fc6 100755
--- a/tools/js/bower2bazel.py
+++ b/tools/js/bower2bazel.py
@@ -22,10 +22,10 @@ python tools/js/bower2bazel.py -w lib/js/bower_archives.bzl \
 
 from __future__ import print_function
 
+import argparse
 import collections
 import json
 import hashlib
-import optparse
 import os
 import subprocess
 import sys
@@ -141,11 +141,11 @@ def bower_command(args):
             os.getcwd() + "/tools/js/run_npm_binary.py", sorted(fs)[0]] + args
 
 
-def main(args):
-    opts = optparse.OptionParser()
-    opts.add_option('-w', help='.bzl output for WORKSPACE')
-    opts.add_option('-b', help='.bzl output for //lib:BUILD')
-    opts, args = opts.parse_args()
+def main():
+    parser = argparse.ArgumentParser()
+    parser.add_argument('-w', help='.bzl output for WORKSPACE')
+    parser.add_argument('-b', help='.bzl output for //lib:BUILD')
+    args = parser.parse_args()
 
     target_str = subprocess.check_output([
         "bazel", "query", "kind(bower_component_bundle, //polygerrit-ui/...)"])
@@ -166,12 +166,12 @@ def main(args):
     cmd = bower_command(["install"])
 
     build_out = sys.stdout
-    if opts.b:
-        build_out = open(opts.b + ".tmp", 'w')
+    if args.b:
+        build_out = open(args.b + ".tmp", 'w')
 
     ws_out = sys.stdout
-    if opts.b:
-        ws_out = open(opts.w + ".tmp", 'w')
+    if args.b:
+        ws_out = open(args.w + ".tmp", 'w')
 
     header = """# DO NOT EDIT
 # generated with the following command:
@@ -193,8 +193,8 @@ def main(args):
     build_out.close()
 
     os.chdir(oldwd)
-    os.rename(opts.w + ".tmp", opts.w)
-    os.rename(opts.b + ".tmp", opts.b)
+    os.rename(args.w + ".tmp", args.w)
+    os.rename(args.b + ".tmp", args.b)
 
 
 def dump_workspace(data, seeds, out):
@@ -258,4 +258,4 @@ def interpret_bower_json(seeds, ws_out, build_out):
 
 
 if __name__ == '__main__':
-    main(sys.argv[1:])
+    main()
diff --git a/tools/js/download_bower.py b/tools/js/download_bower.py
index c1d7d00ce2..1df4b826bc 100755
--- a/tools/js/download_bower.py
+++ b/tools/js/download_bower.py
@@ -15,9 +15,9 @@
 
 from __future__ import print_function
 
+import argparse
 import hashlib
 import json
-import optparse
 import os
 import shutil
 import subprocess
@@ -80,44 +80,44 @@ def cache_entry(name, package, version, sha1):
     return os.path.join(CACHE_DIR, '%s-%s.zip-%s' % (name, version, sha1))
 
 
-def main(args):
-    opts = optparse.OptionParser()
-    opts.add_option('-n', help='short name of component')
-    opts.add_option('-b', help='bower command')
-    opts.add_option('-p', help='full package name of component')
-    opts.add_option('-v', help='version number')
-    opts.add_option('-s', help='expected content sha1')
-    opts.add_option('-o', help='output file location')
-    opts, args_ = opts.parse_args(args)
+def main():
+    parser = argparse.ArgumentParser()
+    parser.add_argument('-n', help='short name of component')
+    parser.add_argument('-b', help='bower command')
+    parser.add_argument('-p', help='full package name of component')
+    parser.add_argument('-v', help='version number')
+    parser.add_argument('-s', help='expected content sha1')
+    parser.add_argument('-o', help='output file location')
+    args = parser.parse_args()
 
-    assert opts.p
-    assert opts.v
-    assert opts.n
+    assert args.p
+    assert args.v
+    assert args.n
 
     cwd = os.getcwd()
-    outzip = os.path.join(cwd, opts.o)
-    cached = cache_entry(opts.n, opts.p, opts.v, opts.s)
+    outzip = os.path.join(cwd, args.o)
+    cached = cache_entry(args.n, args.p, args.v, args.s)
 
     if not os.path.exists(cached):
-        info = bower_info(opts.b, opts.n, opts.p, opts.v)
+        info = bower_info(args.b, args.n, args.p, args.v)
         ignore_deps(info)
         subprocess.check_call(
             bower_cmd(
-                opts.b, '--quiet', 'install', '%s#%s' % (opts.p, opts.v)))
+                args.b, '--quiet', 'install', '%s#%s' % (args.p, args.v)))
         bc = os.path.join(cwd, 'bower_components')
         subprocess.check_call(
-            ['zip', '-q', '--exclude', '.bower.json', '-r', cached, opts.n],
+            ['zip', '-q', '--exclude', '.bower.json', '-r', cached, args.n],
             cwd=bc)
 
-        if opts.s:
-            path = os.path.join(bc, opts.n)
+        if args.s:
+            path = os.path.join(bc, args.n)
             sha1 = bowerutil.hash_bower_component(
                 hashlib.sha1(), path).hexdigest()
-            if opts.s != sha1:
+            if args.s != sha1:
                 print((
                     '%s#%s:\n'
                     'expected %s\n'
-                    'received %s\n') % (opts.p, opts.v, opts.s, sha1),
+                    'received %s\n') % (args.p, args.v, args.s, sha1),
                     file=sys.stderr)
                 try:
                     os.remove(cached)
@@ -132,4 +132,4 @@ def main(args):
 
 
 if __name__ == '__main__':
-    sys.exit(main(sys.argv[1:]))
+    sys.exit(main())
diff --git a/tools/maven/mvn.py b/tools/maven/mvn.py
index 59a5efbb80..60e9f15608 100755
--- a/tools/maven/mvn.py
+++ b/tools/maven/mvn.py
@@ -14,20 +14,20 @@
 # limitations under the License.
 
 from __future__ import print_function
-from optparse import OptionParser
+import argparse
 from os import path, environ
 from subprocess import check_output, CalledProcessError
 from sys import stderr
 
-opts = OptionParser()
-opts.add_option('--repository', help='maven repository id')
-opts.add_option('--url', help='maven repository url')
-opts.add_option('-o')
-opts.add_option('-a', help='action (valid actions are: install,deploy)')
-opts.add_option('-v', help='gerrit version')
-opts.add_option('-s', action='append', help='triplet of artifactId:type:path')
+parser = argparse.ArgumentParser()
+parser.add_argument('--repository', help='maven repository id')
+parser.add_argument('--url', help='maven repository url')
+parser.add_argument('-o')
+parser.add_argument('-a', help='action (valid actions are: install,deploy)')
+parser.add_argument('-v', help='gerrit version')
+parser.add_argument('-s', action='append', help='triplet of artifactId:type:path')
+args = parser.parse_args()
 
-args, ctx = opts.parse_args()
 if not args.v:
     print('version is empty', file=stderr)
     exit(1)
diff --git a/tools/version.py b/tools/version.py
index 4aafcb0c2b..7ea54827b9 100755
--- a/tools/version.py
+++ b/tools/version.py
@@ -14,21 +14,16 @@
 # limitations under the License.
 
 from __future__ import print_function
-from optparse import OptionParser
+import argparse
 import os.path
 import re
 import sys
 
-parser = OptionParser()
-opts, args = parser.parse_args()
-
-if not len(args):
-    parser.error('not enough arguments')
-elif len(args) > 1:
-    parser.error('too many arguments')
-
-DEST_PATTERN = r'\g<1>%s\g<3>' % args[0]
+parser = argparse.ArgumentParser()
+parser.add_argument('version')
+args = parser.parse_args()
 
+DEST_PATTERN = r'\g<1>%s\g<3>' % args.version
 
 def replace_in_file(filename, src_pattern):
     try: