lorax: Improve argument parsing and help
Make the metavars useful, not STRING. Simplify some of the error checking, let the parser handle it. Add type=os.path.abspath to several path arguments so that relative paths will be converted to absolute paths when they are processed.
This commit is contained in:
		
							parent
							
								
									77a7eb266d
								
							
						
					
					
						commit
						a252baed18
					
				| @ -57,9 +57,9 @@ def main(args): | |||||||
| 
 | 
 | ||||||
|     # required arguments for image creation |     # required arguments for image creation | ||||||
|     required = parser.add_argument_group("required arguments") |     required = parser.add_argument_group("required arguments") | ||||||
|     required.add_argument("-p", "--product", help="product name", required=True, metavar="STRING") |     required.add_argument("-p", "--product", help="product name", required=True, metavar="PRODUCT") | ||||||
|     required.add_argument("-v", "--version", help="version identifier", required=True, metavar="STRING") |     required.add_argument("-v", "--version", help="version identifier", required=True, metavar="VERSION") | ||||||
|     required.add_argument("-r", "--release", help="release information", required=True, metavar="STRING") |     required.add_argument("-r", "--release", help="release information", required=True, metavar="RELEASE") | ||||||
|     required.add_argument("-s", "--source", help="source repository (may be listed multiple times)", |     required.add_argument("-s", "--source", help="source repository (may be listed multiple times)", | ||||||
|                         metavar="REPOSITORY", action="append", default=[], required=True) |                         metavar="REPOSITORY", action="append", default=[], required=True) | ||||||
| 
 | 
 | ||||||
| @ -69,36 +69,36 @@ def main(args): | |||||||
|                         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_argument("-t", "--variant", |     optional.add_argument("-t", "--variant", | ||||||
|                         help="variant name", metavar="STRING") |                         help="variant name", metavar="VARIANT") | ||||||
|     optional.add_argument("-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_argument("--isfinal", help="", |     optional.add_argument("--isfinal", help="", | ||||||
|                         action="store_true", default=False, dest="isfinal") |                         action="store_true", default=False, dest="isfinal") | ||||||
|     optional.add_argument("-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="CONFIGFILE") | ||||||
|     optional.add_argument("--proxy", default=None, |     optional.add_argument("--proxy", default=None, | ||||||
|                         help="repo proxy url:port", metavar="STRING") |                           help="repo proxy url:port", metavar="HOST") | ||||||
|     optional.add_argument("-i", "--installpkgs", default=[], |     optional.add_argument("-i", "--installpkgs", default=[], | ||||||
|                         action="append", metavar="STRING", |                         action="append", metavar="PACKAGE", | ||||||
|                         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_argument("--buildarch", default=None, |     optional.add_argument("--buildarch", default=None, | ||||||
|                         help="build architecture", metavar="STRING") |                         help="build architecture", metavar="ARCH") | ||||||
|     optional.add_argument("--volid", default=None, |     optional.add_argument("--volid", default=None, | ||||||
|                         help="volume id", metavar="STRING") |                         help="volume id", metavar="VOLID") | ||||||
|     optional.add_argument("--macboot", help="", |     optional.add_argument("--macboot", help="", | ||||||
|                         action="store_true", default=True, dest="domacboot") |                         action="store_true", default=True, dest="domacboot") | ||||||
|     optional.add_argument("--nomacboot", help="", |     optional.add_argument("--nomacboot", help="", | ||||||
|                         action="store_false", dest="domacboot") |                         action="store_false", dest="domacboot") | ||||||
|     optional.add_argument("--noupgrade", help="", |     optional.add_argument("--noupgrade", help="", | ||||||
|                         action="store_false", default=True, dest="doupgrade") |                         action="store_false", default=True, dest="doupgrade") | ||||||
|     optional.add_argument("--logfile", default="./lorax.log", |     optional.add_argument("--logfile", default="./lorax.log", type=os.path.abspath, | ||||||
|                         help="Path to logfile") |                         help="Path to logfile") | ||||||
|     optional.add_argument("--tmp", default="/var/tmp", |     optional.add_argument("--tmp", default="/var/tmp", | ||||||
|                         help="Top level temporary directory" ) |                         help="Top level temporary directory" ) | ||||||
|     optional.add_argument("--cachedir", default=None, |     optional.add_argument("--cachedir", default=None, type=os.path.abspath, | ||||||
|                         help="DNF cache directory. Default is a temporary dir.") |                         help="DNF cache directory. Default is a temporary dir.") | ||||||
|     optional.add_argument("--workdir", default=None, |     optional.add_argument("--workdir", default=None, type=os.path.abspath, | ||||||
|                         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_argument("--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") | ||||||
| @ -123,31 +123,16 @@ def main(args): | |||||||
|     parser.add_argument("-V", help="show program's version number and exit", |     parser.add_argument("-V", help="show program's version number and exit", | ||||||
|                       action="version", version=version) |                       action="version", version=version) | ||||||
| 
 | 
 | ||||||
|     parser.add_argument("outputdir", help="Output directory", metavar="STRING") |     parser.add_argument("outputdir", help="Output directory", metavar="OUTPUTDIR", type=os.path.abspath) | ||||||
| 
 | 
 | ||||||
|     # parse the arguments |     # parse the arguments | ||||||
|     opts = parser.parse_args() |     opts = parser.parse_args() | ||||||
| 
 | 
 | ||||||
|     try: |     if not opts.force and os.path.exists(opts.outputdir): | ||||||
|         outputdir = os.path.abspath(opts.outputdir) |         parser.error("output directory %s should not exist." % opts.outputdir) | ||||||
|     except IndexError: |  | ||||||
|         parser.error("missing one or more required arguments") |  | ||||||
| 
 | 
 | ||||||
|     # check for the required arguments |  | ||||||
|     if not opts.product or not opts.version or not opts.release \ |  | ||||||
|             or not opts.source or not outputdir: |  | ||||||
|         parser.error("missing one or more required arguments") |  | ||||||
| 
 |  | ||||||
|     if not opts.force and os.path.exists(outputdir): |  | ||||||
|         parser.error("output directory %s should not exist." % outputdir) |  | ||||||
| 
 |  | ||||||
|     opts.logfile = os.path.abspath(opts.logfile) |  | ||||||
|     if not os.path.exists(os.path.dirname(opts.logfile)): |     if not os.path.exists(os.path.dirname(opts.logfile)): | ||||||
|         os.makedirs(os.path.dirname(opts.logfile)) |         os.makedirs(os.path.dirname(opts.logfile)) | ||||||
|     if opts.cachedir: |  | ||||||
|         opts.cachedir = os.path.abspath(opts.cachedir) |  | ||||||
|     if opts.workdir: |  | ||||||
|         opts.workdir = os.path.abspath(opts.workdir) |  | ||||||
|     if opts.sharedir and not os.path.isdir(opts.sharedir): |     if opts.sharedir and not os.path.isdir(opts.sharedir): | ||||||
|         parser.error("sharedir %s doesn't exist." % opts.sharedir) |         parser.error("sharedir %s doesn't exist." % opts.sharedir) | ||||||
| 
 | 
 | ||||||
| @ -204,7 +189,7 @@ def main(args): | |||||||
| 
 | 
 | ||||||
|     lorax.run(dnfbase, opts.product, opts.version, opts.release, |     lorax.run(dnfbase, opts.product, opts.version, opts.release, | ||||||
|               opts.variant, opts.bugurl, opts.isfinal, |               opts.variant, opts.bugurl, opts.isfinal, | ||||||
|               workdir=tempdir, outputdir=outputdir, buildarch=opts.buildarch, |               workdir=tempdir, outputdir=opts.outputdir, buildarch=opts.buildarch, | ||||||
|               volid=opts.volid, domacboot=opts.domacboot, doupgrade=opts.doupgrade, |               volid=opts.volid, domacboot=opts.domacboot, doupgrade=opts.doupgrade, | ||||||
|               installpkgs=opts.installpkgs, |               installpkgs=opts.installpkgs, | ||||||
|               add_templates=opts.add_templates, |               add_templates=opts.add_templates, | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user