replace deprecated optparse with argparse

following guidelines from:
https://docs.python.org/2/library/argparse.html#upgrading-optparse-code
except, still using option = parse.parse_args() instead of args = ...

- using the following script in folder fdroidserver:
	for i in *.py; do
		sed -i -e 's/optparse/argparse/' \
			-e 's/OptionParser/ArgumentParser/' \
			-e 's/OptionError/ArgumentError/' \
			-e 's/add_option/add_argument/' \
			-e 's/(options, args) = parser/options = parser/' \
			-e 's/options, args = parser/options = parser/' \
			-e 's/Usage: %prog/%(prog)s/' $i;
	done
- use ArgumentParser argument to replace (option, args) = parser.parse()
  call
- use parser.error(msg) instead of raise ArgumentException as suggested
  in https://docs.python.org/2/library/argparse.html#exiting-methods
- in fdroid catch ArgumentError instead of OptionError
This commit is contained in:
nero-tux 2015-09-04 11:37:05 +02:00 committed by NeroBurner
parent 41443edd55
commit d23ecf1b35
17 changed files with 232 additions and 227 deletions

View file

@ -29,7 +29,7 @@ import traceback
import time
import json
from ConfigParser import ConfigParser
from optparse import OptionParser, OptionError
from argparse import ArgumentParser
from distutils.version import LooseVersion
import logging
@ -930,47 +930,48 @@ def trybuild(app, thisbuild, build_dir, output_dir, also_check_dir, srclib_dir,
def parse_commandline():
"""Parse the command line. Returns options, args."""
"""Parse the command line. Returns options, parser."""
parser = OptionParser(usage="Usage: %prog [options] [APPID[:VERCODE] [APPID[:VERCODE] ...]]")
parser.add_option("-v", "--verbose", action="store_true", default=False,
help="Spew out even more information than normal")
parser.add_option("-q", "--quiet", action="store_true", default=False,
help="Restrict output to warnings and errors")
parser.add_option("-l", "--latest", action="store_true", default=False,
help="Build only the latest version of each package")
parser.add_option("-s", "--stop", action="store_true", default=False,
help="Make the build stop on exceptions")
parser.add_option("-t", "--test", action="store_true", default=False,
help="Test mode - put output in the tmp directory only, and always build, even if the output already exists.")
parser.add_option("--server", action="store_true", default=False,
help="Use build server")
parser.add_option("--resetserver", action="store_true", default=False,
help="Reset and create a brand new build server, even if the existing one appears to be ok.")
parser.add_option("--on-server", dest="onserver", action="store_true", default=False,
help="Specify that we're running on the build server")
parser.add_option("--skip-scan", dest="skipscan", action="store_true", default=False,
help="Skip scanning the source code for binaries and other problems")
parser.add_option("--no-tarball", dest="notarball", action="store_true", default=False,
help="Don't create a source tarball, useful when testing a build")
parser.add_option("--no-refresh", dest="refresh", action="store_false", default=True,
help="Don't refresh the repository, useful when testing a build with no internet connection")
parser.add_option("-f", "--force", action="store_true", default=False,
help="Force build of disabled apps, and carries on regardless of scan problems. Only allowed in test mode.")
parser.add_option("-a", "--all", action="store_true", default=False,
help="Build all applications available")
parser.add_option("-w", "--wiki", default=False, action="store_true",
help="Update the wiki")
options, args = parser.parse_args()
parser = ArgumentParser(usage="%(prog)s [options] [APPID[:VERCODE] [APPID[:VERCODE] ...]]")
parser.add_argument("appid", nargs='*', help="app-id with optional versioncode in the form APPID[:VERCODE]")
parser.add_argument("-v", "--verbose", action="store_true", default=False,
help="Spew out even more information than normal")
parser.add_argument("-q", "--quiet", action="store_true", default=False,
help="Restrict output to warnings and errors")
parser.add_argument("-l", "--latest", action="store_true", default=False,
help="Build only the latest version of each package")
parser.add_argument("-s", "--stop", action="store_true", default=False,
help="Make the build stop on exceptions")
parser.add_argument("-t", "--test", action="store_true", default=False,
help="Test mode - put output in the tmp directory only, and always build, even if the output already exists.")
parser.add_argument("--server", action="store_true", default=False,
help="Use build server")
parser.add_argument("--resetserver", action="store_true", default=False,
help="Reset and create a brand new build server, even if the existing one appears to be ok.")
parser.add_argument("--on-server", dest="onserver", action="store_true", default=False,
help="Specify that we're running on the build server")
parser.add_argument("--skip-scan", dest="skipscan", action="store_true", default=False,
help="Skip scanning the source code for binaries and other problems")
parser.add_argument("--no-tarball", dest="notarball", action="store_true", default=False,
help="Don't create a source tarball, useful when testing a build")
parser.add_argument("--no-refresh", dest="refresh", action="store_false", default=True,
help="Don't refresh the repository, useful when testing a build with no internet connection")
parser.add_argument("-f", "--force", action="store_true", default=False,
help="Force build of disabled apps, and carries on regardless of scan problems. Only allowed in test mode.")
parser.add_argument("-a", "--all", action="store_true", default=False,
help="Build all applications available")
parser.add_argument("-w", "--wiki", default=False, action="store_true",
help="Update the wiki")
options = parser.parse_args()
# Force --stop with --on-server to get correct exit code
if options.onserver:
options.stop = True
if options.force and not options.test:
raise OptionError("Force is only allowed in test mode", "force")
parser.error("option %s: Force is only allowed in test mode" % "force")
return options, args
return options, parser
options = None
config = None
@ -980,16 +981,16 @@ def main():
global options, config
options, args = parse_commandline()
if not args and not options.all:
raise OptionError("If you really want to build all the apps, use --all", "all")
options, parser = parse_commandline()
if not options.appid and not options.all:
parser.error("option %s: If you really want to build all the apps, use --all" % "all")
config = common.read_config(options)
if config['build_server_always']:
options.server = True
if options.resetserver and not options.server:
raise OptionError("Using --resetserver without --server makes no sense", "resetserver")
parser.error("option %s: Using --resetserver without --server makes no sense" % "resetserver")
log_dir = 'logs'
if not os.path.isdir(log_dir):
@ -1026,7 +1027,7 @@ def main():
# Read all app and srclib metadata
allapps = metadata.read_metadata(xref=not options.onserver)
apps = common.read_app_args(args, allapps, True)
apps = common.read_app_args(options.appid, allapps, True)
for appid, app in apps.items():
if (app['Disabled'] and not options.force) or not app['Repo Type'] or not app['builds']:
del apps[appid]