[SPARK-26822] Upgrade the deprecated module 'optparse'

Follow the [official document](https://docs.python.org/2/library/argparse.html#upgrading-optparse-code)  to upgrade the deprecated module 'optparse' to  'argparse'.

## What changes were proposed in this pull request?

This PR proposes to replace 'optparse' module with 'argparse' module.

## How was this patch tested?

Follow the [previous testing](7e3eb3cd20), manually tested and negative tests were also done. My [test results](https://gist.github.com/cchung100m/1661e7df6e8b66940a6e52a20861f61d)

Closes #23730 from cchung100m/solve_deprecated_module_optparse.

Authored-by: cchung100m <cchung100m@cs.ccu.edu.tw>
Signed-off-by: Sean Owen <sean.owen@databricks.com>
This commit is contained in:
cchung100m 2019-02-10 00:36:22 -06:00 committed by Sean Owen
parent b8d666940b
commit dc46fb77ba
2 changed files with 31 additions and 31 deletions

View file

@ -19,7 +19,7 @@
from __future__ import print_function
import itertools
from optparse import OptionParser
from argparse import ArgumentParser
import os
import random
import re
@ -484,20 +484,20 @@ def run_sparkr_tests():
def parse_opts():
parser = OptionParser(
parser = ArgumentParser(
prog="run-tests"
)
parser.add_option(
"-p", "--parallelism", type="int", default=8,
help="The number of suites to test in parallel (default %default)"
parser.add_argument(
"-p", "--parallelism", type=int, default=8,
help="The number of suites to test in parallel (default %(default)d)"
)
(opts, args) = parser.parse_args()
if args:
parser.error("Unsupported arguments: %s" % ' '.join(args))
if opts.parallelism < 1:
args, unknown = parser.parse_known_args()
if unknown:
parser.error("Unsupported arguments: %s" % ' '.join(unknown))
if args.parallelism < 1:
parser.error("Parallelism cannot be less than 1")
return opts
return args
def main():
@ -636,6 +636,7 @@ def _test():
if failure_count:
sys.exit(-1)
if __name__ == "__main__":
_test()
main()

View file

@ -19,7 +19,7 @@
from __future__ import print_function
import logging
from optparse import OptionParser, OptionGroup
from argparse import ArgumentParser
import os
import re
import shutil
@ -168,30 +168,30 @@ def get_default_python_executables():
def parse_opts():
parser = OptionParser(
parser = ArgumentParser(
prog="run-tests"
)
parser.add_option(
"--python-executables", type="string", default=','.join(get_default_python_executables()),
help="A comma-separated list of Python executables to test against (default: %default)"
parser.add_argument(
"--python-executables", type=str, default=','.join(get_default_python_executables()),
help="A comma-separated list of Python executables to test against (default: %(default)s)"
)
parser.add_option(
"--modules", type="string",
parser.add_argument(
"--modules", type=str,
default=",".join(sorted(python_modules.keys())),
help="A comma-separated list of Python modules to test (default: %default)"
help="A comma-separated list of Python modules to test (default: %(default)s)"
)
parser.add_option(
"-p", "--parallelism", type="int", default=4,
help="The number of suites to test in parallel (default %default)"
parser.add_argument(
"-p", "--parallelism", type=int, default=4,
help="The number of suites to test in parallel (default %(default)d)"
)
parser.add_option(
parser.add_argument(
"--verbose", action="store_true",
help="Enable additional debug logging"
)
group = OptionGroup(parser, "Developer Options")
group.add_option(
"--testnames", type="string",
group = parser.add_argument_group("Developer Options")
group.add_argument(
"--testnames", type=str,
default=None,
help=(
"A comma-separated list of specific modules, classes and functions of doctest "
@ -201,14 +201,13 @@ def parse_opts():
"'pyspark.sql.tests FooTests.test_foo' to run the specific unittest in the class. "
"'--modules' option is ignored if they are given.")
)
parser.add_option_group(group)
(opts, args) = parser.parse_args()
if args:
parser.error("Unsupported arguments: %s" % ' '.join(args))
if opts.parallelism < 1:
args, unknown = parser.parse_known_args()
if unknown:
parser.error("Unsupported arguments: %s" % ' '.join(unknown))
if args.parallelism < 1:
parser.error("Parallelism cannot be less than 1")
return opts
return args
def _check_coverage(python_exec):