From 9193a6902e0f2d56559378df245cb72860e76a70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lubom=C3=ADr=20Sedl=C3=A1=C5=99?= Date: Tue, 27 Jun 2017 09:35:21 +0200 Subject: [PATCH] pungi: Port to argparse MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit And fix PEP8 violations while modifying the file. Signed-off-by: Lubomír Sedlář --- bin/pungi | 233 +++++++++++++++++++++++++++++------------------------- 1 file changed, 124 insertions(+), 109 deletions(-) diff --git a/bin/pungi b/bin/pungi index d1100265..d5cd6bf0 100755 --- a/bin/pungi +++ b/bin/pungi @@ -29,15 +29,15 @@ import pungi.ks def main(): config = pungi.config.Config() - (opts, args) = get_arguments(config) + opts = get_arguments(config) # Read the config to create "new" defaults # reparse command line options so they take precedence config = pungi.config.Config(pungirc=opts.pungirc) - (opts, args) = get_arguments(config) + opts = get_arguments(config) # You must be this high to ride if you're going to do root tasks - if os.geteuid () != 0 and (opts.do_all or opts.do_buildinstall): + if os.geteuid() != 0 and (opts.do_all or opts.do_buildinstall): print >> sys.stderr, "You must run pungi as root" return 1 @@ -60,7 +60,7 @@ def main(): for part in ksparser.handler.partition.partitions: if part.mountpoint == 'iso': config.set('pungi', 'cdsize', str(part.size)) - + config.set('pungi', 'force', str(opts.force)) if config.get('pungi', 'workdirbase') == '/work': @@ -69,7 +69,7 @@ def main(): if not os.path.exists(config.get('pungi', 'destdir')): try: os.makedirs(config.get('pungi', 'destdir')) - except OSError, e: + except OSError: print >> sys.stderr, "Error: Cannot create destination dir %s" % config.get('pungi', 'destdir') sys.exit(1) else: @@ -78,7 +78,7 @@ def main(): if not os.path.exists(config.get('pungi', 'workdirbase')): try: os.makedirs(config.get('pungi', 'workdirbase')) - except OSError, e: + except OSError: print >> sys.stderr, "Error: Cannot create working base dir %s" % config.get('pungi', 'workdirbase') sys.exit(1) else: @@ -89,7 +89,7 @@ def main(): if not os.path.exists(cachedir): try: os.makedirs(cachedir) - except OSError, e: + except OSError: print >> sys.stderr, "Error: Cannot create cache dir %s" % cachedir sys.exit(1) @@ -137,7 +137,7 @@ def main(): with mypungi.yumlock: if not opts.sourceisos: if opts.do_all or opts.do_gather or opts.do_buildinstall: - mypungi._inityum() # initialize the yum object for things that need it + mypungi._inityum() # initialize the yum object for things that need it if opts.do_all or opts.do_gather: mypungi.gather() if opts.nodownload: @@ -205,118 +205,133 @@ def main(): print "All done!" if __name__ == '__main__': - from optparse import OptionParser + from argparse import ArgumentParser, Action import sys import time today = time.strftime('%Y%m%d', time.localtime()) def get_arguments(config): - parser = OptionParser("%prog [--help] [options]", version="%%prog %s" % get_full_version()) + parser = ArgumentParser() - def set_config(option, opt_str, value, parser, config): - config.set('pungi', option.dest, value) + class SetConfig(Action): + def __call__(self, parser, namespace, value, option_string=None): + config.set('pungi', self.dest, value) + + parser.add_argument('--version', action='version', version=get_full_version()) # Pulled in from config file to be cli options as part of pykickstart conversion - parser.add_option("--name", dest="family", type="string", - action="callback", callback=set_config, callback_args=(config, ), - help='the name for your distribution (defaults to "Fedora"), DEPRECATED') - parser.add_option("--family", dest="family", type="string", - action="callback", callback=set_config, callback_args=(config, ), - help='the family name for your distribution (defaults to "Fedora")') - parser.add_option("--ver", dest="version", type="string", - action="callback", callback=set_config, callback_args=(config, ), - help='the version of your distribution (defaults to datestamp)') - parser.add_option("--flavor", dest="variant", type="string", - action="callback", callback=set_config, callback_args=(config, ), - help='the flavor of your distribution spin (optional), DEPRECATED') - parser.add_option("--variant", dest="variant", type="string", - action="callback", callback=set_config, callback_args=(config, ), - help='the variant of your distribution spin (optional)') - parser.add_option("--destdir", dest="destdir", type="string", - action="callback", callback=set_config, callback_args=(config, ), - help='destination directory (defaults to current directory)') - parser.add_option("--cachedir", dest="cachedir", type="string", - action="callback", callback=set_config, callback_args=(config, ), - help='package cache directory (defaults to /var/cache/pungi)') - parser.add_option("--bugurl", dest="bugurl", type="string", - action="callback", callback=set_config, callback_args=(config, ), - help='the url for your bug system (defaults to http://bugzilla.redhat.com)') - parser.add_option("--selfhosting", action="store_true", dest="selfhosting", - help='build a self-hosting tree by following build dependencies (optional)') - parser.add_option("--fulltree", action="store_true", dest="fulltree", - help='build a tree that includes all packages built from corresponding source rpms (optional)') - parser.add_option("--nosource", action="store_true", dest="nosource", - help='disable gathering of source packages (optional)') - parser.add_option("--nodebuginfo", action="store_true", dest="nodebuginfo", - help='disable gathering of debuginfo packages (optional)') - parser.add_option("--nodownload", action="store_true", dest="nodownload", - help='disable downloading of packages. instead, print the package URLs (optional)') - parser.add_option("--norelnotes", action="store_true", dest="norelnotes", - help='disable gathering of release notes (optional); DEPRECATED') - parser.add_option("--nogreedy", action="store_true", dest="nogreedy", - help='disable pulling of all providers of package dependencies (optional)') - parser.add_option("--nodeps", action="store_false", dest="resolve_deps", default=True, - help='disable resolving dependencies') - parser.add_option("--sourceisos", default=False, action="store_true", dest="sourceisos", - help='Create the source isos (other arch runs must be done)') - parser.add_option("--force", default=False, action="store_true", - help='Force reuse of an existing destination directory (will overwrite files)') - parser.add_option("--isfinal", default=False, action="store_true", - help='Specify this is a GA tree, which causes betanag to be turned off during install') - parser.add_option("--nohash", default=False, action="store_true", - help='disable hashing the Packages trees') - parser.add_option("--full-archlist", action="store_true", - help='Use the full arch list for x86_64 (include i686, i386, etc.)') - parser.add_option("--arch", - help='Override default (uname based) arch') - parser.add_option("--greedy", metavar="METHOD", - help='Greedy method; none, all, build') - parser.add_option("--multilib", action="append", metavar="METHOD", - help='Multilib method; can be specified multiple times; recommended: devel, runtime') - parser.add_option("--lookaside-repo", action="append", dest="lookaside_repos", metavar="NAME", - help='Specify lookaside repo name(s) (packages will used for depsolving but not be included in the output)') - parser.add_option("--workdirbase", dest="workdirbase", type="string", - action="callback", callback=set_config, callback_args=(config, ), - help='base working directory (defaults to destdir + /work)') - parser.add_option("--no-dvd", default=False, action="store_true", dest="no_dvd", - help='Do not make a install DVD/CD only the netinstall image and the tree') - parser.add_option("--lorax-conf", type="string", - help='Path to lorax.conf file (optional)') - parser.add_option("-i", "--installpkgs", default=[], - action="append", metavar="STRING", + parser.add_argument( + "--name", dest="family", type=str, action=SetConfig, + help='the name for your distribution (defaults to "Fedora"), DEPRECATED') + parser.add_argument( + "--family", dest="family", action=SetConfig, + help='the family name for your distribution (defaults to "Fedora")') + parser.add_argument( + "--ver", dest="version", action=SetConfig, + help='the version of your distribution (defaults to datestamp)') + parser.add_argument( + "--flavor", dest="variant", action=SetConfig, + help='the flavor of your distribution spin (optional), DEPRECATED') + parser.add_argument( + "--variant", dest="variant", action=SetConfig, + help='the variant of your distribution spin (optional)') + parser.add_argument( + "--destdir", dest="destdir", action=SetConfig, + help='destination directory (defaults to current directory)') + parser.add_argument( + "--cachedir", dest="cachedir", action=SetConfig, + help='package cache directory (defaults to /var/cache/pungi)') + parser.add_argument( + "--bugurl", dest="bugurl", action=SetConfig, + help='the url for your bug system (defaults to http://bugzilla.redhat.com)') + parser.add_argument( + "--selfhosting", action="store_true", dest="selfhosting", + help='build a self-hosting tree by following build dependencies (optional)') + parser.add_argument( + "--fulltree", action="store_true", dest="fulltree", + help='build a tree that includes all packages built from corresponding source rpms (optional)') + parser.add_argument( + "--nosource", action="store_true", dest="nosource", + help='disable gathering of source packages (optional)') + parser.add_argument( + "--nodebuginfo", action="store_true", dest="nodebuginfo", + help='disable gathering of debuginfo packages (optional)') + parser.add_argument( + "--nodownload", action="store_true", dest="nodownload", + help='disable downloading of packages. instead, print the package URLs (optional)') + parser.add_argument( + "--norelnotes", action="store_true", dest="norelnotes", + help='disable gathering of release notes (optional); DEPRECATED') + parser.add_argument( + "--nogreedy", action="store_true", dest="nogreedy", + help='disable pulling of all providers of package dependencies (optional)') + parser.add_argument( + "--nodeps", action="store_false", dest="resolve_deps", default=True, + help='disable resolving dependencies') + parser.add_argument( + "--sourceisos", default=False, action="store_true", dest="sourceisos", + help='Create the source isos (other arch runs must be done)') + parser.add_argument( + "--force", default=False, action="store_true", + help='Force reuse of an existing destination directory (will overwrite files)') + parser.add_argument( + "--isfinal", default=False, action="store_true", + help='Specify this is a GA tree, which causes betanag to be turned off during install') + parser.add_argument( + "--nohash", default=False, action="store_true", + help='disable hashing the Packages trees') + parser.add_argument( + "--full-archlist", action="store_true", + help='Use the full arch list for x86_64 (include i686, i386, etc.)') + parser.add_argument("--arch", help='Override default (uname based) arch') + parser.add_argument( + "--greedy", metavar="METHOD", + help='Greedy method; none, all, build') + parser.add_argument( + "--multilib", action="append", metavar="METHOD", + help='Multilib method; can be specified multiple times; recommended: devel, runtime') + parser.add_argument( + "--lookaside-repo", action="append", dest="lookaside_repos", metavar="NAME", + help='Specify lookaside repo name(s) (packages will used for depsolving but not be included in the output)') + parser.add_argument( + "--workdirbase", dest="workdirbase", action=SetConfig, + help='base working directory (defaults to destdir + /work)') + parser.add_argument("--no-dvd", default=False, action="store_true", dest="no_dvd", + help='Do not make a install DVD/CD only the netinstall image and the tree') + parser.add_argument("--lorax-conf", + help='Path to lorax.conf file (optional)') + parser.add_argument( + "-i", "--installpkgs", default=[], action="append", metavar="STRING", help="Package glob for lorax to install before runtime-install.tmpl runs. (may be listed multiple times)") - parser.add_option("--multilibconf", default=None, type="string", - action="callback", callback=set_config, callback_args=(config, ), - help="Path to multilib conf files. Default is /usr/share/pungi/multilib/") + parser.add_argument( + "--multilibconf", default=None, action=SetConfig, + help="Path to multilib conf files. Default is /usr/share/pungi/multilib/") - parser.add_option("-c", "--config", dest="config", - help='Path to kickstart config file') - parser.add_option("--all-stages", action="store_true", default=True, dest="do_all", - help="Enable ALL stages") - parser.add_option("-G", action="store_true", default=False, dest="do_gather", - help="Flag to enable processing the Gather stage") - parser.add_option("-C", action="store_true", default=False, dest="do_createrepo", - help="Flag to enable processing the Createrepo stage") - parser.add_option("-B", action="store_true", default=False, dest="do_buildinstall", - help="Flag to enable processing the BuildInstall stage") - parser.add_option("-I", action="store_true", default=False, dest="do_createiso", - help="Flag to enable processing the CreateISO stage") - parser.add_option("--relnotepkgs", dest="relnotepkgs", type="string", - action="callback", callback=set_config, callback_args=(config, ), - help='Rpms which contain the release notes') - parser.add_option("--relnotefilere", dest="relnotefilere", type="string", - action="callback", callback=set_config, callback_args=(config, ), - help='Which files are the release notes -- GPL EULA') - parser.add_option("--nomacboot", action="store_true", dest="nomacboot", help='disable setting up macboot as no hfs support ') - parser.add_option("--pungirc", dest="pungirc", default='~/.pungirc', - action="callback", callback=set_config, callback_args=(config, ), - help='Read pungi options from config file ') + parser.add_argument("-c", "--config", dest="config", required=True, + help='Path to kickstart config file') + parser.add_argument("--all-stages", action="store_true", default=True, dest="do_all", + help="Enable ALL stages") + parser.add_argument("-G", action="store_true", default=False, dest="do_gather", + help="Flag to enable processing the Gather stage") + parser.add_argument("-C", action="store_true", default=False, dest="do_createrepo", + help="Flag to enable processing the Createrepo stage") + parser.add_argument("-B", action="store_true", default=False, dest="do_buildinstall", + help="Flag to enable processing the BuildInstall stage") + parser.add_argument("-I", action="store_true", default=False, dest="do_createiso", + help="Flag to enable processing the CreateISO stage") + parser.add_argument("--relnotepkgs", dest="relnotepkgs", action=SetConfig, + help='Rpms which contain the release notes') + parser.add_argument( + "--relnotefilere", dest="relnotefilere", action=SetConfig, + help='Which files are the release notes -- GPL EULA') + parser.add_argument("--nomacboot", action="store_true", dest="nomacboot", + help='disable setting up macboot as no hfs support ') + parser.add_argument( + "--pungirc", dest="pungirc", default='~/.pungirc', action=SetConfig, + help='Read pungi options from config file ') - (opts, args) = parser.parse_args() - - if not opts.config: - parser.error("Please specify a config file") + opts = parser.parse_args() if not config.get('pungi', 'variant').isalnum() and not config.get('pungi', 'variant') == '': parser.error("Variant must be alphanumeric") @@ -333,6 +348,6 @@ if __name__ == '__main__': else: config.set('pungi', 'iso_basename', config.get('pungi', 'family')) - return (opts, args) + return opts main()