Gentoo Archives: gentoo-portage-dev

From: Tom Wijsman <tomwij@g.o>
To: gentoo-portage-dev@l.g.o
Subject: [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 02:22:14
Message-Id: 1388283708-11119-1-git-send-email-tomwij@gentoo.org
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 ---
2 bin/chpathtool.py | 45 ++++++++++++++++++++++++++++++++++++---------
3 1 file changed, 36 insertions(+), 9 deletions(-)
4
5 diff --git a/bin/chpathtool.py b/bin/chpathtool.py
6 index aa3b7d4..0cb5d64 100755
7 --- a/bin/chpathtool.py
8 +++ b/bin/chpathtool.py
9 @@ -13,6 +13,12 @@ import sys
10
11 from portage.util._argparse import ArgumentParser
12
13 +# Argument parsing compatibility for Python 2.6 using optparse.
14 +if sys.hexversion < 0x2070000:
15 + from optparse import OptionParser
16 +
17 +from optparse import OptionError
18 +
19 CONTENT_ENCODING = 'utf_8'
20 FS_ENCODING = 'utf_8'
21
22 @@ -147,15 +153,36 @@ def chpath_inplace_symlink(filename, st, old, new):
23 def main(argv):
24
25 parser = ArgumentParser(description=__doc__)
26 - parser.add_argument('location', default=None,
27 - help='root directory (e.g. $D)')
28 - parser.add_argument('old', default=None,
29 - help='original build prefix (e.g. /)')
30 - parser.add_argument('new', default=None,
31 - help='new install prefix (e.g. $EPREFIX)')
32 - opts = parser.parse_args(argv)
33 -
34 - location, old, new = opts.location, opts.old, opts.new
35 + try:
36 + parser.add_argument('location', default=None,
37 + help='root directory (e.g. $D)')
38 + parser.add_argument('old', default=None,
39 + help='original build prefix (e.g. /)')
40 + parser.add_argument('new', default=None,
41 + help='new install prefix (e.g. $EPREFIX)')
42 + opts = parser.parse_args(argv)
43 +
44 + location, old, new = opts.location, opts.old, opts.new
45 + except OptionError:
46 + # Argument parsing compatibility for Python 2.6 using optparse.
47 + if sys.hexversion < 0x2070000:
48 + parser = OptionParser(description=__doc__,
49 + usage="usage: %prog [-h] location old new\n\n" + \
50 + " location: root directory (e.g. $D)\n" + \
51 + " old: original build prefix (e.g. /)\n" + \
52 + " new: new install prefix (e.g. $EPREFIX)")
53 +
54 + (opts, args) = parser.parse_args()
55 +
56 + if len(args) != 3:
57 + parser.print_usage()
58 + print("%s: error: expected 3 arguments, got %i"
59 + % (__file__, len(args)))
60 + return
61 +
62 + location, old, new = args[0:3]
63 + else:
64 + raise
65
66 is_text_file = IsTextFile()
67
68 --
69 1.8.5.2

Replies