Update lorax for python3 and argparse

This commit is contained in:
Brian C. Lane 2015-05-06 16:09:35 -07:00
parent e1a05d962c
commit 2f927118a7

View File

@ -1,8 +1,8 @@
#!/usr/bin/python2 #!/usr/bin/python3
# #
# lorax # lorax
# #
# Copyright (C) 2009-2014 Red Hat, Inc. # Copyright (C) 2009-2015 Red Hat, Inc.
# #
# This program is free software; you can redistribute it and/or modify # This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by # it under the terms of the GNU General Public License as published by
@ -19,8 +19,6 @@
# #
# Red Hat Author(s): Martin Gracik <mgracik@redhat.com> # Red Hat Author(s): Martin Gracik <mgracik@redhat.com>
# #
from __future__ import print_function
import logging import logging
log = logging.getLogger("lorax") log = logging.getLogger("lorax")
program_log = logging.getLogger("program") program_log = logging.getLogger("program")
@ -31,7 +29,7 @@ dnf_log = logging.getLogger("dnf")
import sys import sys
import os import os
import tempfile import tempfile
from optparse import OptionParser, OptionGroup import argparse
import shutil import shutil
import dnf import dnf
@ -80,91 +78,79 @@ def main(args):
vernum = "devel" vernum = "devel"
version = "{0}-{1}".format(os.path.basename(args[0]), vernum) version = "{0}-{1}".format(os.path.basename(args[0]), vernum)
usage = "%prog -p PRODUCT -v VERSION -r RELEASE -s REPOSITORY OUTPUTDIR" parser = argparse.ArgumentParser(description="Create the Anaconda boot.iso")
parser = OptionParser(usage=usage)
# required arguments for image creation # required arguments for image creation
required = OptionGroup(parser, "required arguments") required = parser.add_argument_group("required arguments")
required.add_option("-p", "--product", help="product name", required.add_argument("-p", "--product", help="product name", required=True, metavar="STRING")
metavar="STRING") required.add_argument("-v", "--version", help="version identifier", required=True, metavar="STRING")
required.add_option("-v", "--version", help="version identifier", required.add_argument("-r", "--release", help="release information", required=True, metavar="STRING")
metavar="STRING") required.add_argument("-s", "--source", help="source repository (may be listed multiple times)",
required.add_option("-r", "--release", help="release information", metavar="REPOSITORY", action="append", default=[], required=True)
metavar="STRING")
required.add_option("-s", "--source",
help="source repository (may be listed multiple times)",
metavar="REPOSITORY", action="append", default=[])
# optional arguments # optional arguments
optional = OptionGroup(parser, "optional arguments") optional = parser.add_argument_group("optional arguments")
optional.add_option("-m", "--mirrorlist", optional.add_argument("-m", "--mirrorlist",
help="mirrorlist repository (may be listed multiple times)", help="mirrorlist repository (may be listed multiple times)",
metavar="REPOSITORY", action="append", default=[]) metavar="REPOSITORY", action="append", default=[])
optional.add_option("-t", "--variant", optional.add_argument("-t", "--variant",
help="variant name", metavar="STRING") help="variant name", metavar="STRING")
optional.add_option("-b", "--bugurl", optional.add_argument("-b", "--bugurl",
help="bug reporting URL for the product", metavar="URL", help="bug reporting URL for the product", metavar="URL",
default="your distribution provided bug reporting tool") default="your distribution provided bug reporting tool")
optional.add_option("--isfinal", help="", optional.add_argument("--isfinal", help="",
action="store_true", default=False, dest="isfinal") action="store_true", default=False, dest="isfinal")
optional.add_option("-c", "--config", default="/etc/lorax/lorax.conf", optional.add_argument("-c", "--config", default="/etc/lorax/lorax.conf",
help="config file", metavar="STRING") help="config file", metavar="STRING")
optional.add_option("--proxy", default=None, optional.add_argument("--proxy", default=None,
help="repo proxy url:port", metavar="STRING") help="repo proxy url:port", metavar="STRING")
optional.add_option("-i", "--installpkgs", default=[], optional.add_argument("-i", "--installpkgs", default=[],
action="append", metavar="STRING", action="append", metavar="STRING",
help="package glob to install before runtime-install.tmpl runs. (may be listed multiple times)") help="package glob to install before runtime-install.tmpl runs. (may be listed multiple times)")
optional.add_option("--buildarch", default=None, optional.add_argument("--buildarch", default=None,
help="build architecture", metavar="STRING") help="build architecture", metavar="STRING")
optional.add_option("--volid", default=None, optional.add_argument("--volid", default=None,
help="volume id", metavar="STRING") help="volume id", metavar="STRING")
optional.add_option("--macboot", help="", optional.add_argument("--macboot", help="",
action="store_true", default=True, dest="domacboot") action="store_true", default=True, dest="domacboot")
optional.add_option("--nomacboot", help="", optional.add_argument("--nomacboot", help="",
action="store_false", dest="domacboot") action="store_false", dest="domacboot")
optional.add_option("--noupgrade", help="", optional.add_argument("--noupgrade", help="",
action="store_false", default=True, dest="doupgrade") action="store_false", default=True, dest="doupgrade")
optional.add_option("--logfile", default="./lorax.log", optional.add_argument("--logfile", default="./lorax.log",
help="Path to logfile") help="Path to logfile")
optional.add_option("--tmp", default="/var/tmp", optional.add_argument("--tmp", default="/var/tmp",
help="Top level temporary directory" ) help="Top level temporary directory" )
optional.add_option("--cachedir", default=None, optional.add_argument("--cachedir", default=None,
help="DNF cache directory. Default is a temporary dir.") help="DNF cache directory. Default is a temporary dir.")
optional.add_option("--workdir", default=None, optional.add_argument("--workdir", default=None,
help="Work directory, overrides --tmp. Default is a temporary dir under /var/tmp") help="Work directory, overrides --tmp. Default is a temporary dir under /var/tmp")
optional.add_option("--force", default=False, action="store_true", optional.add_argument("--force", default=False, action="store_true",
help="Run even when the destination directory exists") help="Run even when the destination directory exists")
optional.add_option("--add-template", dest="add_templates", optional.add_argument("--add-template", dest="add_templates",
action="append", help="Additional template for runtime image", action="append", help="Additional template for runtime image",
default=[]) default=[])
optional.add_option("--add-template-var", dest="add_template_vars", optional.add_argument("--add-template-var", dest="add_template_vars",
action="append", help="Set variable for runtime image template", action="append", help="Set variable for runtime image template",
default=[]) default=[])
optional.add_option("--add-arch-template", dest="add_arch_templates", optional.add_argument("--add-arch-template", dest="add_arch_templates",
action="append", help="Additional template for architecture-specific image", action="append", help="Additional template for architecture-specific image",
default=[]) default=[])
optional.add_option("--add-arch-template-var", dest="add_arch_template_vars", optional.add_argument("--add-arch-template-var", dest="add_arch_template_vars",
action="append", help="Set variable for architecture-specific image", action="append", help="Set variable for architecture-specific image",
default=[]) default=[])
# add the option groups to the parser
parser.add_option_group(required)
parser.add_option_group(optional)
# add the show version option # add the show version option
parser.add_option("-V", help="show program's version number and exit", parser.add_argument("-V", help="show program's version number and exit",
action="store_true", default=False, dest="showver") action="version", version=version)
parser.add_argument("outputdir", help="Output directory", metavar="STRING")
# parse the arguments # parse the arguments
opts, args = parser.parse_args() opts = parser.parse_args()
if opts.showver:
print(version)
sys.exit(0)
try: try:
outputdir = os.path.abspath(args[0]) outputdir = os.path.abspath(opts.outputdir)
except IndexError: except IndexError:
parser.error("missing one or more required arguments") parser.error("missing one or more required arguments")
@ -270,12 +256,12 @@ def get_dnf_base_object(installroot, repositories, mirrorlists=None,
mirrorlists = mirrorlists or [] mirrorlists = mirrorlists or []
# sanitize the repositories # sanitize the repositories
repositories = map(sanitize_repo, repositories) repositories = list(sanitize_repo(r) for r in repositories)
mirrorlists = map(sanitize_repo, mirrorlists) mirrorlists = list(sanitize_repo(r) for r in mirrorlists)
# remove invalid repositories # remove invalid repositories
repositories = filter(bool, repositories) repositories = list(r for r in repositories if r)
mirrorlists = filter(bool, mirrorlists) mirrorlists = list(r for r in mirrorlists if r)
if not cachedir: if not cachedir:
cachedir = os.path.join(tempdir, "dnf.cache") cachedir = os.path.join(tempdir, "dnf.cache")