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

@ -24,7 +24,7 @@ import os
import paramiko
import pwd
import subprocess
from optparse import OptionParser
from argparse import ArgumentParser
import logging
import common
@ -195,28 +195,25 @@ def main():
global config, options
# Parse command line...
parser = OptionParser()
parser.add_option("-i", "--identity-file", default=None,
help="Specify an identity file to provide to SSH for rsyncing")
parser.add_option("--local-copy-dir", default=None,
help="Specify a local folder to sync the repo to")
parser.add_option("--sync-from-local-copy-dir", action="store_true", default=False,
help="Before uploading to servers, sync from local copy dir")
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("--no-checksum", action="store_true", default=False,
help="Don't use rsync checksums")
(options, args) = parser.parse_args()
parser = ArgumentParser()
parser.add_argument("command", help="command to execute, either 'init' or 'update'")
parser.add_argument("-i", "--identity-file", default=None,
help="Specify an identity file to provide to SSH for rsyncing")
parser.add_argument("--local-copy-dir", default=None,
help="Specify a local folder to sync the repo to")
parser.add_argument("--sync-from-local-copy-dir", action="store_true", default=False,
help="Before uploading to servers, sync from local copy dir")
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("--no-checksum", action="store_true", default=False,
help="Don't use rsync checksums")
options = parser.parse_args()
config = common.read_config(options)
if len(args) != 1:
logging.critical("Specify a single command")
sys.exit(1)
if args[0] != 'init' and args[0] != 'update':
if options.command != 'init' and options.command != 'update':
logging.critical("The only commands currently supported are 'init' and 'update'")
sys.exit(1)
@ -288,7 +285,7 @@ def main():
if config['per_app_repos']:
repo_sections += common.get_per_app_repos()
if args[0] == 'init':
if options.command == 'init':
ssh = paramiko.SSHClient()
ssh.load_system_host_keys()
for serverwebroot in config.get('serverwebroot', []):
@ -310,7 +307,7 @@ def main():
sftp.mkdir(repo_path, mode=0755)
sftp.close()
ssh.close()
elif args[0] == 'update':
elif options.command == 'update':
for repo_section in repo_sections:
if local_copy_dir is not None:
if config['sync_from_local_copy_dir'] and os.path.exists(repo_section):