pungi-koji: Port to argparse

Optparse is deprecated since Python 2.7, long live argparse.

This also allows us to remove some of the manual error checking and make
the library check required or conflicting options.

Signed-off-by: Lubomír Sedlář <lsedlar@redhat.com>
This commit is contained in:
Lubomír Sedlář 2017-06-22 16:29:45 +02:00
parent 68098bec37
commit 01026858f7

View File

@ -4,7 +4,7 @@
import os import os
import sys import sys
import optparse import argparse
import logging import logging
import locale import locale
import datetime import datetime
@ -31,29 +31,35 @@ COMPOSE = None
def main(): def main():
global COMPOSE global COMPOSE
parser = optparse.OptionParser() parser = argparse.ArgumentParser()
parser.add_option( group = parser.add_mutually_exclusive_group(required=True)
group.add_argument(
"--target-dir", "--target-dir",
metavar="PATH", metavar="PATH",
help="a compose is created under this directory", help="a compose is created under this directory",
) )
parser.add_option( group.add_argument(
"--compose-dir",
metavar="PATH",
help="reuse an existing compose directory (DANGEROUS!)",
)
parser.add_argument(
"--label", "--label",
help="specify compose label (example: Snapshot-1.0); required for production composes" help="specify compose label (example: Snapshot-1.0); required for production composes"
) )
parser.add_option( parser.add_argument(
"--no-label", "--no-label",
action="store_true", action="store_true",
default=False, default=False,
help="make a production compose without label" help="make a production compose without label"
) )
parser.add_option( parser.add_argument(
"--supported", "--supported",
action="store_true", action="store_true",
default=False, default=False,
help="set supported flag on media (automatically on for 'RC-x.y' labels)" help="set supported flag on media (automatically on for 'RC-x.y' labels)"
) )
parser.add_option( parser.add_argument(
"--old-composes", "--old-composes",
metavar="PATH", metavar="PATH",
dest="old_composes", dest="old_composes",
@ -61,93 +67,89 @@ def main():
action="append", action="append",
help="Path to directory with old composes. Reuse an existing repodata from the most recent compose.", help="Path to directory with old composes. Reuse an existing repodata from the most recent compose.",
) )
parser.add_option( parser.add_argument(
"--compose-dir",
metavar="PATH",
help="reuse an existing compose directory (DANGEROUS!)",
)
parser.add_option(
"--debug-mode", "--debug-mode",
action="store_true", action="store_true",
default=False, default=False,
help="run pungi in DEBUG mode (DANGEROUS!)", help="run pungi in DEBUG mode (DANGEROUS!)",
) )
parser.add_option( parser.add_argument(
"--config", "--config",
help="Config file" help="Config file",
required=True
) )
parser.add_option( parser.add_argument(
"--skip-phase", "--skip-phase",
metavar="PHASE", metavar="PHASE",
action="append", action="append",
default=[], default=[],
help="skip a compose phase", help="skip a compose phase",
) )
parser.add_option( parser.add_argument(
"--just-phase", "--just-phase",
metavar="PHASE", metavar="PHASE",
action="append", action="append",
default=[], default=[],
help="run only a specified compose phase", help="run only a specified compose phase",
) )
parser.add_option( parser.add_argument(
"--nightly", "--nightly",
action="store_const", action="store_const",
const="nightly", const="nightly",
dest="compose_type", dest="compose_type",
help="make a nightly compose", help="make a nightly compose",
) )
parser.add_option( parser.add_argument(
"--test", "--test",
action="store_const", action="store_const",
const="test", const="test",
dest="compose_type", dest="compose_type",
help="make a test compose", help="make a test compose",
) )
parser.add_option( parser.add_argument(
"--ci", "--ci",
action="store_const", action="store_const",
const="ci", const="ci",
dest="compose_type", dest="compose_type",
help="make a CI compose", help="make a CI compose",
) )
parser.add_option( parser.add_argument(
"--koji-event", "--koji-event",
metavar="ID", metavar="ID",
type="int", type=int,
help="specify a koji event for populating package set", help="specify a koji event for populating package set",
) )
parser.add_option( parser.add_argument(
"--version", "--version",
action="store_true", action="store_true",
help="output version information and exit", help="output version information and exit",
) )
parser.add_option( parser.add_argument(
"--notification-script", "--notification-script",
help="script for sending progress notification messages" help="script for sending progress notification messages"
) )
parser.add_option( parser.add_argument(
"--no-latest-link", "--no-latest-link",
action="store_true", action="store_true",
default=False, default=False,
dest="no_latest_link", dest="no_latest_link",
help="don't create latest symbol link to this compose" help="don't create latest symbol link to this compose"
) )
parser.add_option( parser.add_argument(
"--latest-link-status", "--latest-link-status",
metavar="STATUS", metavar="STATUS",
action="append", action="append",
default=[], default=[],
help="only create latest symbol link to this compose when compose status matches specified status", help="only create latest symbol link to this compose when compose status matches specified status",
) )
parser.add_option( parser.add_argument(
"--quiet", "--quiet",
action="store_true", action="store_true",
default=False, default=False,
help="quiet mode, don't print log on screen" help="quiet mode, don't print log on screen"
) )
opts, args = parser.parse_args() opts = parser.parse_args()
import pungi.notifier import pungi.notifier
notifier = pungi.notifier.PungiNotifier(opts.notification_script) notifier = pungi.notifier.PungiNotifier(opts.notification_script)
@ -164,12 +166,6 @@ def main():
print("pungi %s" % get_full_version()) print("pungi %s" % get_full_version())
sys.exit(0) sys.exit(0)
if opts.target_dir and opts.compose_dir:
abort("cannot specify --target-dir and --compose-dir at once")
if not opts.target_dir and not opts.compose_dir:
abort("please specify a target directory")
if opts.target_dir and not opts.compose_dir: if opts.target_dir and not opts.compose_dir:
opts.target_dir = os.path.abspath(opts.target_dir) opts.target_dir = os.path.abspath(opts.target_dir)
if not os.path.isdir(opts.target_dir): if not os.path.isdir(opts.target_dir):
@ -183,8 +179,6 @@ def main():
if compose_type == "production" and not opts.label and not opts.no_label: if compose_type == "production" and not opts.label and not opts.no_label:
abort("must specify label for a production compose") abort("must specify label for a production compose")
if not opts.config:
abort("please specify a config")
opts.config = os.path.abspath(opts.config) opts.config = os.path.abspath(opts.config)
create_latest_link = not opts.no_latest_link create_latest_link = not opts.no_latest_link