1 |
The current code implements a lot of ad-hoc argument parsing when it |
2 |
could simply let the argparse module do it all for it. This makes the |
3 |
code easier to understand and extend in the process. |
4 |
--- |
5 |
bin/xpak-helper.py | 68 ++++++++++++++++++++---------------------------------- |
6 |
1 file changed, 25 insertions(+), 43 deletions(-) |
7 |
|
8 |
diff --git a/bin/xpak-helper.py b/bin/xpak-helper.py |
9 |
index 8c965ec..1b2883d 100755 |
10 |
--- a/bin/xpak-helper.py |
11 |
+++ b/bin/xpak-helper.py |
12 |
@@ -2,68 +2,50 @@ |
13 |
# Copyright 2009-2014 Gentoo Foundation |
14 |
# Distributed under the terms of the GNU General Public License v2 |
15 |
|
16 |
+"""Helper to manage xpak archives""" |
17 |
+ |
18 |
import argparse |
19 |
import sys |
20 |
import portage |
21 |
portage._internal_caller = True |
22 |
from portage import os |
23 |
|
24 |
-def command_recompose(args): |
25 |
- |
26 |
- usage = "usage: recompose <binpkg_path> <metadata_dir>\n" |
27 |
|
28 |
- if len(args) != 2: |
29 |
- sys.stderr.write(usage) |
30 |
- sys.stderr.write("2 arguments are required, got %s\n" % len(args)) |
31 |
- return 1 |
32 |
+def command_recompose(parser, opts): |
33 |
+ """Create an xpak archive from a directory""" |
34 |
+ binpkg_path = opts.binpkg_path |
35 |
+ metadata_dir = opts.metadata_dir |
36 |
|
37 |
- binpkg_path, metadata_dir = args |
38 |
- |
39 |
- if not os.path.isfile(binpkg_path): |
40 |
- sys.stderr.write(usage) |
41 |
- sys.stderr.write("Argument 1 is not a regular file: '%s'\n" % \ |
42 |
- binpkg_path) |
43 |
- return 1 |
44 |
+ if os.path.exists(binpkg_path) and not os.path.isfile(binpkg_path): |
45 |
+ parser.error('binpkg_path is not a regular file: %s' % binpkg_path) |
46 |
|
47 |
if not os.path.isdir(metadata_dir): |
48 |
- sys.stderr.write(usage) |
49 |
- sys.stderr.write("Argument 2 is not a directory: '%s'\n" % \ |
50 |
- metadata_dir) |
51 |
- return 1 |
52 |
+ parser.error('metadata_dir is not a directory: %s' % metadata_dir) |
53 |
|
54 |
t = portage.xpak.tbz2(binpkg_path) |
55 |
t.recompose(metadata_dir) |
56 |
return os.EX_OK |
57 |
|
58 |
-def main(argv): |
59 |
|
60 |
- if argv and isinstance(argv[0], bytes): |
61 |
- for i, x in enumerate(argv): |
62 |
- argv[i] = portage._unicode_decode(x, errors='strict') |
63 |
+def get_parser(): |
64 |
+ """Return the command line parser""" |
65 |
+ parser = argparse.ArgumentParser(description=__doc__) |
66 |
+ subparsers = parser.add_subparsers() |
67 |
|
68 |
- valid_commands = ('recompose',) |
69 |
- description = "Perform metadata operations on a binary package." |
70 |
- usage = "usage: %s COMMAND [args]" % \ |
71 |
- os.path.basename(argv[0]) |
72 |
+ subparser = subparsers.add_parser('recompose', |
73 |
+ help=command_recompose.__doc__) |
74 |
+ subparser.set_defaults(func=command_recompose) |
75 |
+ subparser.add_argument('binpkg_path', help='Path to output xpak') |
76 |
+ subparser.add_argument('metadata_dir', help='Path to input dir') |
77 |
|
78 |
- parser = argparse.ArgumentParser(description=description, usage=usage) |
79 |
- options, args = parser.parse_known_args(argv[1:]) |
80 |
+ return parser |
81 |
|
82 |
- if not args: |
83 |
- parser.error("missing command argument") |
84 |
|
85 |
- command = args[0] |
86 |
- |
87 |
- if command not in valid_commands: |
88 |
- parser.error("invalid command: '%s'" % command) |
89 |
- |
90 |
- if command == 'recompose': |
91 |
- rval = command_recompose(args[1:]) |
92 |
- else: |
93 |
- raise AssertionError("invalid command: '%s'" % command) |
94 |
+def main(argv): |
95 |
+ parser = get_parser() |
96 |
+ opts = parser.parse_args(argv) |
97 |
+ opts.func(parser, opts) |
98 |
|
99 |
- return rval |
100 |
|
101 |
-if __name__ == "__main__": |
102 |
- rval = main(sys.argv[:]) |
103 |
- sys.exit(rval) |
104 |
+if __name__ == '__main__': |
105 |
+ sys.exit(main(sys.argv[1:])) |
106 |
-- |
107 |
2.5.2 |