Gentoo Archives: gentoo-portage-dev

From: Sebastian Luther <SebastianLuther@×××.de>
To: gentoo-portage-dev@l.g.o
Subject: Re: [gentoo-portage-dev] [PATCH] Make use of optparse to fix argument parsing for Python 2.6 in bin/chpathtool.py.
Date: Sun, 29 Dec 2013 15:34:41
Message-Id: 52C0410B.3050006@gmx.de
In Reply to: [gentoo-portage-dev] [PATCH] Make use of optparse to fix argument parsing for Python 2.6 in bin/chpathtool.py. by Tom Wijsman
1 Am 29.12.2013 03:21, schrieb Tom Wijsman:
2 > ---
3 > bin/chpathtool.py | 45 ++++++++++++++++++++++++++++++++++++---------
4 > 1 file changed, 36 insertions(+), 9 deletions(-)
5 >
6 > diff --git a/bin/chpathtool.py b/bin/chpathtool.py
7 > index aa3b7d4..0cb5d64 100755
8 > --- a/bin/chpathtool.py
9 > +++ b/bin/chpathtool.py
10 > @@ -13,6 +13,12 @@ import sys
11 >
12 > from portage.util._argparse import ArgumentParser
13 >
14 > +# Argument parsing compatibility for Python 2.6 using optparse.
15 > +if sys.hexversion < 0x2070000:
16 > + from optparse import OptionParser
17 > +
18 > +from optparse import OptionError
19 > +
20 > CONTENT_ENCODING = 'utf_8'
21 > FS_ENCODING = 'utf_8'
22 >
23 > @@ -147,15 +153,36 @@ def chpath_inplace_symlink(filename, st, old, new):
24 > def main(argv):
25 >
26 > parser = ArgumentParser(description=__doc__)
27 > - parser.add_argument('location', default=None,
28 > - help='root directory (e.g. $D)')
29 > - parser.add_argument('old', default=None,
30 > - help='original build prefix (e.g. /)')
31 > - parser.add_argument('new', default=None,
32 > - help='new install prefix (e.g. $EPREFIX)')
33 > - opts = parser.parse_args(argv)
34 > -
35 > - location, old, new = opts.location, opts.old, opts.new
36 > + try:
37 > + parser.add_argument('location', default=None,
38 > + help='root directory (e.g. $D)')
39 > + parser.add_argument('old', default=None,
40 > + help='original build prefix (e.g. /)')
41 > + parser.add_argument('new', default=None,
42 > + help='new install prefix (e.g. $EPREFIX)')
43 > + opts = parser.parse_args(argv)
44 > +
45 > + location, old, new = opts.location, opts.old, opts.new
46 > + except OptionError:
47 > + # Argument parsing compatibility for Python 2.6 using optparse.
48 > + if sys.hexversion < 0x2070000:
49 > + parser = OptionParser(description=__doc__,
50 > + usage="usage: %prog [-h] location old new\n\n" + \
51 > + " location: root directory (e.g. $D)\n" + \
52 > + " old: original build prefix (e.g. /)\n" + \
53 > + " new: new install prefix (e.g. $EPREFIX)")
54 > +
55 > + (opts, args) = parser.parse_args()
56 > +
57 > + if len(args) != 3:
58 > + parser.print_usage()
59 > + print("%s: error: expected 3 arguments, got %i"
60 > + % (__file__, len(args)))
61 > + return
62 > +
63 > + location, old, new = args[0:3]
64 > + else:
65 > + raise
66 >
67 > is_text_file = IsTextFile()
68 >
69 >
70
71 Patch looks good.
72
73 While I do not really like the approach, I didn't see a better way.
74 Tweaking portage.util._argparse.ArgumentParser would just mean to
75 reinvent argparse.
76
77 Sebastian

Replies