pungi: Port to argparse

And fix PEP8 violations while modifying the file.

Signed-off-by: Lubomír Sedlář <lsedlar@redhat.com>
This commit is contained in:
Lubomír Sedlář 2017-06-27 09:35:21 +02:00
parent 87c485f2d0
commit 9193a6902e
1 changed files with 124 additions and 109 deletions

233
bin/pungi
View File

@ -29,15 +29,15 @@ import pungi.ks
def main(): def main():
config = pungi.config.Config() config = pungi.config.Config()
(opts, args) = get_arguments(config) opts = get_arguments(config)
# Read the config to create "new" defaults # Read the config to create "new" defaults
# reparse command line options so they take precedence # reparse command line options so they take precedence
config = pungi.config.Config(pungirc=opts.pungirc) 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 # 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" print >> sys.stderr, "You must run pungi as root"
return 1 return 1
@ -60,7 +60,7 @@ def main():
for part in ksparser.handler.partition.partitions: for part in ksparser.handler.partition.partitions:
if part.mountpoint == 'iso': if part.mountpoint == 'iso':
config.set('pungi', 'cdsize', str(part.size)) config.set('pungi', 'cdsize', str(part.size))
config.set('pungi', 'force', str(opts.force)) config.set('pungi', 'force', str(opts.force))
if config.get('pungi', 'workdirbase') == '/work': if config.get('pungi', 'workdirbase') == '/work':
@ -69,7 +69,7 @@ def main():
if not os.path.exists(config.get('pungi', 'destdir')): if not os.path.exists(config.get('pungi', 'destdir')):
try: try:
os.makedirs(config.get('pungi', 'destdir')) os.makedirs(config.get('pungi', 'destdir'))
except OSError, e: except OSError:
print >> sys.stderr, "Error: Cannot create destination dir %s" % config.get('pungi', 'destdir') print >> sys.stderr, "Error: Cannot create destination dir %s" % config.get('pungi', 'destdir')
sys.exit(1) sys.exit(1)
else: else:
@ -78,7 +78,7 @@ def main():
if not os.path.exists(config.get('pungi', 'workdirbase')): if not os.path.exists(config.get('pungi', 'workdirbase')):
try: try:
os.makedirs(config.get('pungi', 'workdirbase')) 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') print >> sys.stderr, "Error: Cannot create working base dir %s" % config.get('pungi', 'workdirbase')
sys.exit(1) sys.exit(1)
else: else:
@ -89,7 +89,7 @@ def main():
if not os.path.exists(cachedir): if not os.path.exists(cachedir):
try: try:
os.makedirs(cachedir) os.makedirs(cachedir)
except OSError, e: except OSError:
print >> sys.stderr, "Error: Cannot create cache dir %s" % cachedir print >> sys.stderr, "Error: Cannot create cache dir %s" % cachedir
sys.exit(1) sys.exit(1)
@ -137,7 +137,7 @@ def main():
with mypungi.yumlock: with mypungi.yumlock:
if not opts.sourceisos: if not opts.sourceisos:
if opts.do_all or opts.do_gather or opts.do_buildinstall: 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: if opts.do_all or opts.do_gather:
mypungi.gather() mypungi.gather()
if opts.nodownload: if opts.nodownload:
@ -205,118 +205,133 @@ def main():
print "All done!" print "All done!"
if __name__ == '__main__': if __name__ == '__main__':
from optparse import OptionParser from argparse import ArgumentParser, Action
import sys import sys
import time import time
today = time.strftime('%Y%m%d', time.localtime()) today = time.strftime('%Y%m%d', time.localtime())
def get_arguments(config): 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): class SetConfig(Action):
config.set('pungi', option.dest, value) 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 # Pulled in from config file to be cli options as part of pykickstart conversion
parser.add_option("--name", dest="family", type="string", parser.add_argument(
action="callback", callback=set_config, callback_args=(config, ), "--name", dest="family", type=str, action=SetConfig,
help='the name for your distribution (defaults to "Fedora"), DEPRECATED') help='the name for your distribution (defaults to "Fedora"), DEPRECATED')
parser.add_option("--family", dest="family", type="string", parser.add_argument(
action="callback", callback=set_config, callback_args=(config, ), "--family", dest="family", action=SetConfig,
help='the family name for your distribution (defaults to "Fedora")') help='the family name for your distribution (defaults to "Fedora")')
parser.add_option("--ver", dest="version", type="string", parser.add_argument(
action="callback", callback=set_config, callback_args=(config, ), "--ver", dest="version", action=SetConfig,
help='the version of your distribution (defaults to datestamp)') help='the version of your distribution (defaults to datestamp)')
parser.add_option("--flavor", dest="variant", type="string", parser.add_argument(
action="callback", callback=set_config, callback_args=(config, ), "--flavor", dest="variant", action=SetConfig,
help='the flavor of your distribution spin (optional), DEPRECATED') help='the flavor of your distribution spin (optional), DEPRECATED')
parser.add_option("--variant", dest="variant", type="string", parser.add_argument(
action="callback", callback=set_config, callback_args=(config, ), "--variant", dest="variant", action=SetConfig,
help='the variant of your distribution spin (optional)') help='the variant of your distribution spin (optional)')
parser.add_option("--destdir", dest="destdir", type="string", parser.add_argument(
action="callback", callback=set_config, callback_args=(config, ), "--destdir", dest="destdir", action=SetConfig,
help='destination directory (defaults to current directory)') help='destination directory (defaults to current directory)')
parser.add_option("--cachedir", dest="cachedir", type="string", parser.add_argument(
action="callback", callback=set_config, callback_args=(config, ), "--cachedir", dest="cachedir", action=SetConfig,
help='package cache directory (defaults to /var/cache/pungi)') help='package cache directory (defaults to /var/cache/pungi)')
parser.add_option("--bugurl", dest="bugurl", type="string", parser.add_argument(
action="callback", callback=set_config, callback_args=(config, ), "--bugurl", dest="bugurl", action=SetConfig,
help='the url for your bug system (defaults to http://bugzilla.redhat.com)') help='the url for your bug system (defaults to http://bugzilla.redhat.com)')
parser.add_option("--selfhosting", action="store_true", dest="selfhosting", parser.add_argument(
help='build a self-hosting tree by following build dependencies (optional)') "--selfhosting", action="store_true", dest="selfhosting",
parser.add_option("--fulltree", action="store_true", dest="fulltree", help='build a self-hosting tree by following build dependencies (optional)')
help='build a tree that includes all packages built from corresponding source rpms (optional)') parser.add_argument(
parser.add_option("--nosource", action="store_true", dest="nosource", "--fulltree", action="store_true", dest="fulltree",
help='disable gathering of source packages (optional)') help='build a tree that includes all packages built from corresponding source rpms (optional)')
parser.add_option("--nodebuginfo", action="store_true", dest="nodebuginfo", parser.add_argument(
help='disable gathering of debuginfo packages (optional)') "--nosource", action="store_true", dest="nosource",
parser.add_option("--nodownload", action="store_true", dest="nodownload", help='disable gathering of source packages (optional)')
help='disable downloading of packages. instead, print the package URLs (optional)') parser.add_argument(
parser.add_option("--norelnotes", action="store_true", dest="norelnotes", "--nodebuginfo", action="store_true", dest="nodebuginfo",
help='disable gathering of release notes (optional); DEPRECATED') help='disable gathering of debuginfo packages (optional)')
parser.add_option("--nogreedy", action="store_true", dest="nogreedy", parser.add_argument(
help='disable pulling of all providers of package dependencies (optional)') "--nodownload", action="store_true", dest="nodownload",
parser.add_option("--nodeps", action="store_false", dest="resolve_deps", default=True, help='disable downloading of packages. instead, print the package URLs (optional)')
help='disable resolving dependencies') parser.add_argument(
parser.add_option("--sourceisos", default=False, action="store_true", dest="sourceisos", "--norelnotes", action="store_true", dest="norelnotes",
help='Create the source isos (other arch runs must be done)') help='disable gathering of release notes (optional); DEPRECATED')
parser.add_option("--force", default=False, action="store_true", parser.add_argument(
help='Force reuse of an existing destination directory (will overwrite files)') "--nogreedy", action="store_true", dest="nogreedy",
parser.add_option("--isfinal", default=False, action="store_true", help='disable pulling of all providers of package dependencies (optional)')
help='Specify this is a GA tree, which causes betanag to be turned off during install') parser.add_argument(
parser.add_option("--nohash", default=False, action="store_true", "--nodeps", action="store_false", dest="resolve_deps", default=True,
help='disable hashing the Packages trees') help='disable resolving dependencies')
parser.add_option("--full-archlist", action="store_true", parser.add_argument(
help='Use the full arch list for x86_64 (include i686, i386, etc.)') "--sourceisos", default=False, action="store_true", dest="sourceisos",
parser.add_option("--arch", help='Create the source isos (other arch runs must be done)')
help='Override default (uname based) arch') parser.add_argument(
parser.add_option("--greedy", metavar="METHOD", "--force", default=False, action="store_true",
help='Greedy method; none, all, build') help='Force reuse of an existing destination directory (will overwrite files)')
parser.add_option("--multilib", action="append", metavar="METHOD", parser.add_argument(
help='Multilib method; can be specified multiple times; recommended: devel, runtime') "--isfinal", default=False, action="store_true",
parser.add_option("--lookaside-repo", action="append", dest="lookaside_repos", metavar="NAME", help='Specify this is a GA tree, which causes betanag to be turned off during install')
help='Specify lookaside repo name(s) (packages will used for depsolving but not be included in the output)') parser.add_argument(
parser.add_option("--workdirbase", dest="workdirbase", type="string", "--nohash", default=False, action="store_true",
action="callback", callback=set_config, callback_args=(config, ), help='disable hashing the Packages trees')
help='base working directory (defaults to destdir + /work)') parser.add_argument(
parser.add_option("--no-dvd", default=False, action="store_true", dest="no_dvd", "--full-archlist", action="store_true",
help='Do not make a install DVD/CD only the netinstall image and the tree') help='Use the full arch list for x86_64 (include i686, i386, etc.)')
parser.add_option("--lorax-conf", type="string", parser.add_argument("--arch", help='Override default (uname based) arch')
help='Path to lorax.conf file (optional)') parser.add_argument(
parser.add_option("-i", "--installpkgs", default=[], "--greedy", metavar="METHOD",
action="append", metavar="STRING", 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)") 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", parser.add_argument(
action="callback", callback=set_config, callback_args=(config, ), "--multilibconf", default=None, action=SetConfig,
help="Path to multilib conf files. Default is /usr/share/pungi/multilib/") help="Path to multilib conf files. Default is /usr/share/pungi/multilib/")
parser.add_option("-c", "--config", dest="config", parser.add_argument("-c", "--config", dest="config", required=True,
help='Path to kickstart config file') help='Path to kickstart config file')
parser.add_option("--all-stages", action="store_true", default=True, dest="do_all", parser.add_argument("--all-stages", action="store_true", default=True, dest="do_all",
help="Enable ALL stages") help="Enable ALL stages")
parser.add_option("-G", action="store_true", default=False, dest="do_gather", parser.add_argument("-G", action="store_true", default=False, dest="do_gather",
help="Flag to enable processing the Gather stage") help="Flag to enable processing the Gather stage")
parser.add_option("-C", action="store_true", default=False, dest="do_createrepo", parser.add_argument("-C", action="store_true", default=False, dest="do_createrepo",
help="Flag to enable processing the Createrepo stage") help="Flag to enable processing the Createrepo stage")
parser.add_option("-B", action="store_true", default=False, dest="do_buildinstall", parser.add_argument("-B", action="store_true", default=False, dest="do_buildinstall",
help="Flag to enable processing the BuildInstall stage") help="Flag to enable processing the BuildInstall stage")
parser.add_option("-I", action="store_true", default=False, dest="do_createiso", parser.add_argument("-I", action="store_true", default=False, dest="do_createiso",
help="Flag to enable processing the CreateISO stage") help="Flag to enable processing the CreateISO stage")
parser.add_option("--relnotepkgs", dest="relnotepkgs", type="string", parser.add_argument("--relnotepkgs", dest="relnotepkgs", action=SetConfig,
action="callback", callback=set_config, callback_args=(config, ), help='Rpms which contain the release notes')
help='Rpms which contain the release notes') parser.add_argument(
parser.add_option("--relnotefilere", dest="relnotefilere", type="string", "--relnotefilere", dest="relnotefilere", action=SetConfig,
action="callback", callback=set_config, callback_args=(config, ), help='Which files are the release notes -- GPL EULA')
help='Which files are the release notes -- GPL EULA') parser.add_argument("--nomacboot", action="store_true", dest="nomacboot",
parser.add_option("--nomacboot", action="store_true", dest="nomacboot", help='disable setting up macboot as no hfs support ') help='disable setting up macboot as no hfs support ')
parser.add_option("--pungirc", dest="pungirc", default='~/.pungirc', parser.add_argument(
action="callback", callback=set_config, callback_args=(config, ), "--pungirc", dest="pungirc", default='~/.pungirc', action=SetConfig,
help='Read pungi options from config file ') help='Read pungi options from config file ')
(opts, args) = parser.parse_args() opts = parser.parse_args()
if not opts.config:
parser.error("Please specify a config file")
if not config.get('pungi', 'variant').isalnum() and not config.get('pungi', 'variant') == '': if not config.get('pungi', 'variant').isalnum() and not config.get('pungi', 'variant') == '':
parser.error("Variant must be alphanumeric") parser.error("Variant must be alphanumeric")
@ -333,6 +348,6 @@ if __name__ == '__main__':
else: else:
config.set('pungi', 'iso_basename', config.get('pungi', 'family')) config.set('pungi', 'iso_basename', config.get('pungi', 'family'))
return (opts, args) return opts
main() main()