public inbox for gentoo-portage-dev@lists.gentoo.org
 help / color / mirror / Atom feed
* [gentoo-portage-dev] [PATCH] xpak-helper: rewrite to rely more on argparse
@ 2015-11-01  5:23 Mike Frysinger
  2015-11-01 17:36 ` Zac Medico
  0 siblings, 1 reply; 5+ messages in thread
From: Mike Frysinger @ 2015-11-01  5:23 UTC (permalink / raw
  To: gentoo-portage-dev

The current code implements a lot of ad-hoc argument parsing when it
could simply let the argparse module do it all for it.  This makes the
code easier to understand and extend in the process.
---
 bin/xpak-helper.py | 68 ++++++++++++++++++++----------------------------------
 1 file changed, 25 insertions(+), 43 deletions(-)

diff --git a/bin/xpak-helper.py b/bin/xpak-helper.py
index 8c965ec..1b2883d 100755
--- a/bin/xpak-helper.py
+++ b/bin/xpak-helper.py
@@ -2,68 +2,50 @@
 # Copyright 2009-2014 Gentoo Foundation
 # Distributed under the terms of the GNU General Public License v2
 
+"""Helper to manage xpak archives"""
+
 import argparse
 import sys
 import portage
 portage._internal_caller = True
 from portage import os
 
-def command_recompose(args):
-
-	usage = "usage: recompose <binpkg_path> <metadata_dir>\n"
 
-	if len(args) != 2:
-		sys.stderr.write(usage)
-		sys.stderr.write("2 arguments are required, got %s\n" % len(args))
-		return 1
+def command_recompose(parser, opts):
+	"""Create an xpak archive from a directory"""
+	binpkg_path = opts.binpkg_path
+	metadata_dir = opts.metadata_dir
 
-	binpkg_path, metadata_dir = args
-
-	if not os.path.isfile(binpkg_path):
-		sys.stderr.write(usage)
-		sys.stderr.write("Argument 1 is not a regular file: '%s'\n" % \
-			binpkg_path)
-		return 1
+	if os.path.exists(binpkg_path) and not os.path.isfile(binpkg_path):
+		parser.error('binpkg_path is not a regular file: %s' % binpkg_path)
 
 	if not os.path.isdir(metadata_dir):
-		sys.stderr.write(usage)
-		sys.stderr.write("Argument 2 is not a directory: '%s'\n" % \
-			metadata_dir)
-		return 1
+		parser.error('metadata_dir is not a directory: %s' % metadata_dir)
 
 	t = portage.xpak.tbz2(binpkg_path)
 	t.recompose(metadata_dir)
 	return os.EX_OK
 
-def main(argv):
 
-	if argv and isinstance(argv[0], bytes):
-		for i, x in enumerate(argv):
-			argv[i] = portage._unicode_decode(x, errors='strict')
+def get_parser():
+	"""Return the command line parser"""
+	parser = argparse.ArgumentParser(description=__doc__)
+	subparsers = parser.add_subparsers()
 
-	valid_commands = ('recompose',)
-	description = "Perform metadata operations on a binary package."
-	usage = "usage: %s COMMAND [args]" % \
-		os.path.basename(argv[0])
+	subparser = subparsers.add_parser('recompose',
+		help=command_recompose.__doc__)
+	subparser.set_defaults(func=command_recompose)
+	subparser.add_argument('binpkg_path', help='Path to output xpak')
+	subparser.add_argument('metadata_dir', help='Path to input dir')
 
-	parser = argparse.ArgumentParser(description=description, usage=usage)
-	options, args = parser.parse_known_args(argv[1:])
+	return parser
 
-	if not args:
-		parser.error("missing command argument")
 
-	command = args[0]
-
-	if command not in valid_commands:
-		parser.error("invalid command: '%s'" % command)
-
-	if command == 'recompose':
-		rval = command_recompose(args[1:])
-	else:
-		raise AssertionError("invalid command: '%s'" % command)
+def main(argv):
+	parser = get_parser()
+	opts = parser.parse_args(argv)
+	opts.func(parser, opts)
 
-	return rval
 
-if __name__ == "__main__":
-	rval = main(sys.argv[:])
-	sys.exit(rval)
+if __name__ == '__main__':
+	sys.exit(main(sys.argv[1:]))
-- 
2.5.2



^ permalink raw reply related	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2015-11-02 17:10 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-11-01  5:23 [gentoo-portage-dev] [PATCH] xpak-helper: rewrite to rely more on argparse Mike Frysinger
2015-11-01 17:36 ` Zac Medico
2015-11-02 16:52   ` Mike Frysinger
2015-11-02 17:06     ` Zac Medico
2015-11-02 17:10       ` Zac Medico

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox