Gentoo Archives: gentoo-commits

From: Mike Frysinger <vapier@g.o>
To: gentoo-commits@l.g.o
Subject: [gentoo-commits] proj/catalyst:master commit in: catalyst/
Date: Tue, 06 Oct 2015 17:03:15
Message-Id: 1444149230.5d42738fb733ecb3fb92b69bb85f17a36bef60b7.vapier@gentoo
1 commit: 5d42738fb733ecb3fb92b69bb85f17a36bef60b7
2 Author: Mike Frysinger <vapier <AT> gentoo <DOT> org>
3 AuthorDate: Tue Oct 6 16:33:50 2015 +0000
4 Commit: Mike Frysinger <vapier <AT> gentoo <DOT> org>
5 CommitDate: Tue Oct 6 16:33:50 2015 +0000
6 URL: https://gitweb.gentoo.org/proj/catalyst.git/commit/?id=5d42738f
7
8 main: switch to argparse
9
10 Switch from ad-hoc getopt parsing to the more powerful/standard argparse.
11
12 catalyst/main.py | 191 +++++++++++++++++++++++++------------------------------
13 1 file changed, 85 insertions(+), 106 deletions(-)
14
15 diff --git a/catalyst/main.py b/catalyst/main.py
16 index 604c6ab..2a25df4 100644
17 --- a/catalyst/main.py
18 +++ b/catalyst/main.py
19 @@ -6,9 +6,9 @@
20 # Chris Gianelloni <wolf31o2@××××××××.org>
21 # $Id$
22
23 +import argparse
24 import os
25 import sys
26 -import getopt
27 import os.path
28
29 __selfpath__ = os.path.abspath(os.path.dirname(__file__))
30 @@ -28,35 +28,6 @@ from catalyst.version import get_version
31 conf_values={}
32
33
34 -def usage():
35 - print """Usage catalyst [options] [-C variable=value...] [ -s identifier]
36 - -a --clear-autoresume clear autoresume flags
37 - -c --config use specified configuration file
38 - -C --cli catalyst commandline (MUST BE LAST OPTION)
39 - -d --debug enable debugging
40 - -f --file read specfile
41 - -F --fetchonly fetch files only
42 - -h --help print this help message
43 - -p --purge clear tmp dirs,package cache, autoresume flags
44 - -P --purgeonly clear tmp dirs,package cache, autoresume flags and exit
45 - -T --purgetmponly clear tmp dirs and autoresume flags and exit
46 - -s --snapshot generate a release snapshot
47 - -V --version display version information
48 - -v --verbose verbose output
49 -
50 -Usage examples:
51 -
52 -Using the commandline option (-C, --cli) to build a Portage snapshot:
53 -catalyst -C target=snapshot version_stamp=my_date
54 -
55 -Using the snapshot option (-s, --snapshot) to build a release snapshot:
56 -catalyst -s 20071121"
57 -
58 -Using the specfile option (-f, --file) to build a stage target:
59 -catalyst -f stage1-specfile.spec
60 -"""
61 -
62 -
63 def version():
64 print get_version()
65 print "Copyright 2003-2008 Gentoo Foundation"
66 @@ -167,96 +138,104 @@ def build_target(addlargs):
67 return target.run()
68
69
70 -def main():
71 - # we need some options in order to work correctly
72 - if len(sys.argv) < 2:
73 - usage()
74 - sys.exit(2)
75 -
76 - # parse out the command line arguments
77 - try:
78 - opts, _args = getopt.getopt(sys.argv[1:], "apPThvdc:C:f:FVs:", ["purge", "purgeonly", "purgetmponly", "help", "version", "debug",
79 - "clear-autoresume", "config=", "cli=", "file=", "fetch", "verbose","snapshot="])
80 -
81 - except getopt.GetoptError:
82 - usage()
83 - sys.exit(2)
84 -
85 - myconfig=""
86 - myspecfile=""
87 - mycmdline=[]
88 -
89 - # check preconditions
90 - if len(opts) == 0:
91 - print "!!! catalyst: please specify one of either -f or -C\n"
92 - usage()
93 - sys.exit(2)
94 -
95 - options = set()
96 -
97 - run = False
98 - for o, a in opts:
99 - if o in ("-h", "--help"):
100 - version()
101 - usage()
102 - sys.exit(1)
103 +class FilePath(object):
104 + """Argparse type for getting a path to a file."""
105
106 - if o in ("-V", "--version"):
107 - print get_version()
108 - sys.exit(1)
109 + def __init__(self, exists=True):
110 + self.exists = exists
111
112 - if o in ("-d", "--debug"):
113 - conf_values["DEBUG"] = True
114 - conf_values["VERBOSE"] = True
115 + def __call__(self, string):
116 + if not os.path.exists(string):
117 + raise argparse.ArgumentTypeError('file does not exist: %s' % string)
118 + return string
119
120 - if o in ("-c", "--config"):
121 - myconfig=a
122 + def __repr__(self):
123 + return '%s(exists=%s)' % (type(self).__name__, self.exists)
124
125 - if o in ("-C", "--cli"):
126 - run = True
127 - x=sys.argv.index(o)+1
128 - while x < len(sys.argv):
129 - mycmdline.append(sys.argv[x])
130 - x=x+1
131
132 - if o in ("-f", "--file"):
133 - run = True
134 - myspecfile=a
135 +def get_parser():
136 + """Return an argument parser"""
137 + epilog = """Usage examples:
138
139 - if o in ("-F", "--fetchonly"):
140 - options.add("fetch")
141 +Using the commandline option (-C, --cli) to build a Portage snapshot:
142 +$ catalyst -C target=snapshot version_stamp=my_date
143
144 - if o in ("-v", "--verbose"):
145 - conf_values["VERBOSE"]="1"
146 +Using the snapshot option (-s, --snapshot) to build a release snapshot:
147 +$ catalyst -s 20071121
148
149 - if o in ("-s", "--snapshot"):
150 - if len(sys.argv) < 3:
151 - print "!!! catalyst: missing snapshot identifier\n"
152 - usage()
153 - sys.exit(2)
154 - else:
155 - run = True
156 - mycmdline.append("target=snapshot")
157 - mycmdline.append("version_stamp="+a)
158 +Using the specfile option (-f, --file) to build a stage target:
159 +$ catalyst -f stage1-specfile.spec"""
160 +
161 + parser = argparse.ArgumentParser(epilog=epilog, formatter_class=argparse.RawDescriptionHelpFormatter)
162 + parser.add_argument('-d', '--debug',
163 + default=False, action='store_true',
164 + help='enable debugging')
165 + parser.add_argument('-v', '--verbose',
166 + default=False, action='store_true',
167 + help='verbose output')
168 + parser.add_argument('-c', '--config',
169 + type=FilePath(),
170 + help='use specified configuration file')
171 + parser.add_argument('-f', '--file',
172 + type=FilePath(),
173 + help='read specfile')
174 + parser.add_argument('-F', '--fetchonly',
175 + default=False, action='store_true',
176 + help='fetch files only')
177 + parser.add_argument('-a', '--clear-autoresume',
178 + default=False, action='store_true',
179 + help='clear autoresume flags')
180 + parser.add_argument('-p', '--purge',
181 + default=False, action='store_true',
182 + help='clear tmp dirs, package cache, autoresume flags')
183 + parser.add_argument('-P', '--purgeonly',
184 + default=False, action='store_true',
185 + help='clear tmp dirs, package cache, autoresume flags and exit')
186 + parser.add_argument('-T', '--purgetmponly',
187 + default=False, action='store_true',
188 + help='clear tmp dirs and autoresume flags and exit')
189 + parser.add_argument('-s', '--snapshot',
190 + help='generate a release snapshot')
191 + parser.add_argument('-V', '--version',
192 + action='version', version=get_version(),
193 + help='display version information')
194 + parser.add_argument('-C', '--cli',
195 + default=[], nargs=argparse.REMAINDER,
196 + help='catalyst commandline (MUST BE LAST OPTION)')
197 + return parser
198
199 - if o in ("-p", "--purge"):
200 - options.add("purge")
201
202 - if o in ("-P", "--purgeonly"):
203 - options.add("purgeonly")
204 +def main():
205 + parser = get_parser()
206 + opts = parser.parse_args(sys.argv[1:])
207
208 - if o in ("-T", "--purgetmponly"):
209 - options.add("purgetmponly")
210 + # Parse the command line options.
211 + myconfig = opts.config
212 + myspecfile = opts.file
213 + mycmdline = opts.cli[:]
214
215 - if o in ("-a", "--clear-autoresume"):
216 - options.add("clear-autoresume")
217 + if opts.snapshot:
218 + mycmdline.append('target=snapshot')
219 + mycmdline.append('version_stamp=' + opts.snapshot)
220
221 - #print "MAIN: cli options =", options
222 + conf_values['DEBUG'] = opts.debug
223 + conf_values['VERBOSE'] = opts.debug or opts.verbose
224
225 - if not run:
226 - print "!!! catalyst: please specify one of either -f or -C\n"
227 - usage()
228 - sys.exit(2)
229 + options = set()
230 + if opts.fetchonly:
231 + options.add('fetch')
232 + if opts.purge:
233 + options.add('purge')
234 + if opts.purgeonly:
235 + options.add('purgeonly')
236 + if opts.purgetmponly:
237 + options.add('purgetmponly')
238 + if opts.clear_autoresume:
239 + options.add('clear-autoresume')
240 +
241 + # Make sure we have some work before moving further.
242 + if not myspecfile and not mycmdline:
243 + parser.error('please specify one of either -f or -C or -s')
244
245 # made it this far so start by outputting our version info
246 version()