Gentoo Archives: gentoo-commits

From: "André Erdmann" <dywi@×××××××.de>
To: gentoo-commits@l.g.o
Subject: [gentoo-commits] proj/R_overlay:master commit in: roverlay/
Date: Tue, 03 Jul 2012 17:48:47
Message-Id: 1341337555.6485e56521930bb59e4fdcdddc015286aa6c874b.dywi@gentoo
1 commit: 6485e56521930bb59e4fdcdddc015286aa6c874b
2 Author: André Erdmann <dywi <AT> mailerd <DOT> de>
3 AuthorDate: Tue Jul 3 17:45:55 2012 +0000
4 Commit: André Erdmann <dywi <AT> mailerd <DOT> de>
5 CommitDate: Tue Jul 3 17:45:55 2012 +0000
6 URL: http://git.overlays.gentoo.org/gitweb/?p=proj/R_overlay.git;a=commit;h=6485e565
7
8 roverlay/argutil (using argparse)
9
10 this sets up an ArgParser suitable for roverlay scripts
11
12 new file: roverlay/argutil.py
13
14 ---
15 roverlay/argutil.py | 211 +++++++++++++++++++++++++++++++++++++++++++++++++++
16 1 files changed, 211 insertions(+), 0 deletions(-)
17
18 diff --git a/roverlay/argutil.py b/roverlay/argutil.py
19 new file mode 100644
20 index 0000000..d1640ac
21 --- /dev/null
22 +++ b/roverlay/argutil.py
23 @@ -0,0 +1,211 @@
24 +
25 +import os.path
26 +import argparse
27 +import roverlay
28 +
29 +def get_parser ( CMD_DESC, DEFAULT_CONFIG ):
30 +
31 + def is_fs_file ( value ):
32 + f = os.path.abspath ( value )
33 + if not os.path.isfile ( f ):
34 + raise argparse.ArgumentTypeError (
35 + "%r is not a file." % value
36 + )
37 + return f
38 +
39 + def is_fs_dir ( value ):
40 + d = os.path.abspath ( value )
41 + if not os.path.isdir ( d ):
42 + raise argparse.ArgumentTypeError (
43 + "%r is not a directory." % value
44 + )
45 + return d
46 +
47 + def couldbe_fs_dir ( value ):
48 + d = os.path.abspath ( value )
49 + if os.path.exists ( d ) and not os.path.isdir ( d ):
50 + raise argparse.ArgumentTypeError (
51 + "%r cannot be a directory." % value
52 + )
53 + return d
54 +
55 + parser = argparse.ArgumentParser (
56 + description='\n'.join ((
57 + roverlay.description_str, roverlay.license_str,
58 + )),
59 + epilog = 'Known commands:\n' + '\n'.join (
60 + ( ( '* ' + c ).ljust(17) + ' - ' + d for (c,d) in CMD_DESC.items() )
61 + ),
62 + add_help=True,
63 + formatter_class=argparse.RawDescriptionHelpFormatter,
64 + )
65 +
66 + arg = parser.add_argument
67 + opt_in = dict ( default=False, action='store_true' )
68 + opt_out = dict ( default=True, action='store_false' )
69 +
70 + fs_file = dict ( type=is_fs_file, metavar="<file>" )
71 +
72 + # adding args starts here
73 +
74 + arg (
75 + '-V', '--version', action='version', version=roverlay.version_str
76 + )
77 +
78 + arg (
79 + 'commands',
80 + # fixme: CMD_DESC is "unknown", but default is set to a specific command
81 + default='create',
82 + help="action to perform. choices are " + ', '.join (CMD_DESC.keys()) + \
83 + ". defaults to %(default)s.",
84 + nargs="*",
85 + choices=CMD_DESC.keys(),
86 + metavar="command"
87 + )
88 +
89 + arg (
90 + '-c', '--config',
91 + default=DEFAULT_CONFIG,
92 + help="config file",
93 + **fs_file
94 + )
95 + arg (
96 + '-F', '--field-definition', '--fdef', default=argparse.SUPPRESS,
97 + help="field definition file",
98 + **fs_file
99 + )
100 +
101 + arg (
102 + '-R', '--repo-config', default=argparse.SUPPRESS,
103 + action='append',
104 + help="repo config file.",
105 + **fs_file
106 + )
107 +
108 + arg (
109 + '-D', '--deprule-file', default=argparse.SUPPRESS,
110 + action='append',
111 + help="simple rule file. can be specified more than once.",
112 + **fs_file
113 + )
114 +
115 +
116 + arg (
117 + '--distdir', '--from', default=argparse.SUPPRESS,
118 + action='append',
119 + help='''
120 + use packages from %(metavar)s for ebuild creation (ignore all repos).
121 + only useful for testing 'cause SRC_URI will be invalid in the created
122 + ebuilds.
123 + ''',
124 + metavar="<DISTDIR>",
125 + type=is_fs_dir
126 + )
127 +
128 + arg (
129 + '--distroot', default=argparse.SUPPRESS,
130 + help='''
131 + use %(metavar)s as distdir root for repos
132 + that don't define their own package dir.
133 + ''',
134 + metavar="<DISTROOT>",
135 + type=couldbe_fs_dir
136 + )
137 +
138 + arg (
139 + '--show',
140 + help="print ebuilds and metadata to console",
141 + **opt_in
142 + )
143 +
144 + arg (
145 + '--write',
146 + help="write overlay to filesystem",
147 + # !! change to opt_out (FIXME)
148 + **opt_in
149 + )
150 +
151 +
152 + arg (
153 + '--nosync', '--no-sync',
154 + help="disable syncing with remotes (offline mode). TODO",
155 + **opt_in
156 + )
157 + arg (
158 + '--force-distroot',
159 + help="always use <DISTROOT>/<repo name> as repo distdir. TODO.",
160 + **opt_in
161 + )
162 +
163 + arg (
164 + '--debug',
165 + help='''
166 + Turn on debugging. This produces a lot of messages.
167 + (TODO: always on).
168 + ''',
169 + **opt_out
170 + )
171 +
172 + return parser
173 +# --- end of get_parser (...) ---
174 +
175 +def parse_argv ( *args, **kw ):
176 + """Parses sys.argv and returns the result as tuple
177 + (<commands to run>, <config file>,
178 + <dict for config>, <extra options as dict>).
179 +
180 + All args/keywords are passed to get_parser().
181 + Passes all exceptions.
182 + """
183 + def doconf ( value, path ):
184 + pos = conf
185 + if isinstance ( path, str ):
186 + path = path.split ( '.' )
187 + last = len ( path ) - 1
188 + for i, k in enumerate ( path ):
189 + if i == last:
190 + pos [k.lower()] = value
191 + else:
192 + k = k.upper()
193 + if not k in pos:
194 + pos [k] = dict()
195 +
196 + pos = pos [k]
197 +
198 +
199 + p = get_parser ( *args, **kw ).parse_args()
200 +
201 + given = lambda kw : hasattr ( p, kw )
202 +
203 +
204 + conf = dict()
205 + extra = dict (
206 + nosync = p.nosync,
207 + show = p.show,
208 + write = p.write,
209 + debug = p.debug,
210 + force_distroot = p.force_distroot,
211 + )
212 +
213 + if given ( 'field_definition' ):
214 + doconf ( p.field_definition, 'DESCRIPTION.field_definition_file' )
215 +
216 + if given ( 'repo_config' ):
217 + doconf ( p.repo_config, 'REPO.config_files' )
218 +
219 + if given ( 'distroot' ):
220 + doconf ( p.distroot, 'distfiles.root' )
221 +
222 + if given ( 'distdir' ):
223 + doconf ( (), 'REPO.config_files' )
224 + extra ['distdir'] = p.distdir
225 +
226 + if given ( 'deprule_file' ):
227 + doconf ( p.deprule_file, 'DEPRES.SIMPLE_RULES.files' )
228 +
229 +
230 + return (
231 + ( p.commands, ) if isinstance ( p.commands, str ) else p.commands,
232 + p.config, conf, extra
233 + )
234 +# --- end of parse_argv (...) ---