public inbox for gentoo-portage-dev@lists.gentoo.org
 help / color / mirror / Atom feed
* [gentoo-portage-dev] [PATCH] Make use of optparse to fix argument parsing for Python 2.6 in bin/chpathtool.py.
@ 2013-12-29  1:54 Tom Wijsman
  2013-12-29  2:21 ` Tom Wijsman
  0 siblings, 1 reply; 4+ messages in thread
From: Tom Wijsman @ 2013-12-29  1:54 UTC (permalink / raw
  To: gentoo-portage-dev

---
 bin/chpathtool.py | 43 ++++++++++++++++++++++++++++++++-----------
 1 file changed, 32 insertions(+), 11 deletions(-)

diff --git a/bin/chpathtool.py b/bin/chpathtool.py
index aa3b7d4..692a338 100755
--- a/bin/chpathtool.py
+++ b/bin/chpathtool.py
@@ -11,7 +11,11 @@ import os
 import stat
 import sys
 
-from portage.util._argparse import ArgumentParser
+if sys.hexversion < 0x2070000:
+	# Argument parsing compatibility for Python 2.6 using optparse.
+	from optparse import OptionParser
+else:
+	from portage.util._argparse import ArgumentParser
 
 CONTENT_ENCODING = 'utf_8'
 FS_ENCODING = 'utf_8'
@@ -145,17 +149,34 @@ def chpath_inplace_symlink(filename, st, old, new):
 		os.lchown(filename, st.st_uid, st.st_gid)
 
 def main(argv):
+	if sys.hexversion < 0x2070000:
+		# Argument parsing compatibility for Python 2.6 using optparse.
+		parser = OptionParser(description=__doc__,
+			usage="usage: %prog [-h] location old new\n\n" + \
+			"  location: root directory (e.g. $D)\n" + \
+			"  old:      original build prefix (e.g. /)\n" + \
+			"  new:      new install prefix (e.g. $EPREFIX)")
+
+		(opts, args) = parser.parse_args()
+
+		if len(args) != 3:
+			parser.print_usage()
+			print("%s: error: expected 3 arguments, got %i"
+				% (__file__, len(args)))
+			return
 
-	parser = ArgumentParser(description=__doc__)
-	parser.add_argument('location', default=None,
-		help='root directory (e.g. $D)')
-	parser.add_argument('old', default=None,
-		help='original build prefix (e.g. /)')
-	parser.add_argument('new', default=None,
-		help='new install prefix (e.g. $EPREFIX)')
-	opts = parser.parse_args(argv)
-
-	location, old, new = opts.location, opts.old, opts.new
+		location, old, new = args[0:3]
+	else:
+		parser = ArgumentParser(description=__doc__)
+		parser.add_argument('location', default=None,
+			help='root directory (e.g. $D)')
+		parser.add_argument('old', default=None,
+			help='original build prefix (e.g. /)')
+		parser.add_argument('new', default=None,
+			help='new install prefix (e.g. $EPREFIX)')
+		opts = parser.parse_args(argv)
+
+		location, old, new = opts.location, opts.old, opts.new
 
 	is_text_file = IsTextFile()
 
-- 
1.8.5.2



^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [gentoo-portage-dev] [PATCH] Make use of optparse to fix argument parsing for Python 2.6 in bin/chpathtool.py.
  2013-12-29  1:54 [gentoo-portage-dev] [PATCH] Make use of optparse to fix argument parsing for Python 2.6 in bin/chpathtool.py Tom Wijsman
@ 2013-12-29  2:21 ` Tom Wijsman
  2013-12-29 15:34   ` Sebastian Luther
  0 siblings, 1 reply; 4+ messages in thread
From: Tom Wijsman @ 2013-12-29  2:21 UTC (permalink / raw
  To: gentoo-portage-dev

---
 bin/chpathtool.py | 45 ++++++++++++++++++++++++++++++++++++---------
 1 file changed, 36 insertions(+), 9 deletions(-)

diff --git a/bin/chpathtool.py b/bin/chpathtool.py
index aa3b7d4..0cb5d64 100755
--- a/bin/chpathtool.py
+++ b/bin/chpathtool.py
@@ -13,6 +13,12 @@ import sys
 
 from portage.util._argparse import ArgumentParser
 
+# Argument parsing compatibility for Python 2.6 using optparse.
+if sys.hexversion < 0x2070000:
+	from optparse import OptionParser
+
+from optparse import OptionError
+
 CONTENT_ENCODING = 'utf_8'
 FS_ENCODING = 'utf_8'
 
@@ -147,15 +153,36 @@ def chpath_inplace_symlink(filename, st, old, new):
 def main(argv):
 
 	parser = ArgumentParser(description=__doc__)
-	parser.add_argument('location', default=None,
-		help='root directory (e.g. $D)')
-	parser.add_argument('old', default=None,
-		help='original build prefix (e.g. /)')
-	parser.add_argument('new', default=None,
-		help='new install prefix (e.g. $EPREFIX)')
-	opts = parser.parse_args(argv)
-
-	location, old, new = opts.location, opts.old, opts.new
+	try:
+		parser.add_argument('location', default=None,
+			help='root directory (e.g. $D)')
+		parser.add_argument('old', default=None,
+			help='original build prefix (e.g. /)')
+		parser.add_argument('new', default=None,
+			help='new install prefix (e.g. $EPREFIX)')
+		opts = parser.parse_args(argv)
+
+		location, old, new = opts.location, opts.old, opts.new
+	except OptionError:
+		# Argument parsing compatibility for Python 2.6 using optparse.
+		if sys.hexversion < 0x2070000:
+			parser = OptionParser(description=__doc__,
+				usage="usage: %prog [-h] location old new\n\n" + \
+				"  location: root directory (e.g. $D)\n" + \
+				"  old:      original build prefix (e.g. /)\n" + \
+				"  new:      new install prefix (e.g. $EPREFIX)")
+
+			(opts, args) = parser.parse_args()
+
+			if len(args) != 3:
+				parser.print_usage()
+				print("%s: error: expected 3 arguments, got %i"
+					% (__file__, len(args)))
+				return
+
+			location, old, new = args[0:3]
+		else:
+			raise
 
 	is_text_file = IsTextFile()
 
-- 
1.8.5.2



^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [gentoo-portage-dev] [PATCH] Make use of optparse to fix argument parsing for Python 2.6 in bin/chpathtool.py.
  2013-12-29  2:21 ` Tom Wijsman
@ 2013-12-29 15:34   ` Sebastian Luther
  2013-12-29 15:51     ` Tom Wijsman
  0 siblings, 1 reply; 4+ messages in thread
From: Sebastian Luther @ 2013-12-29 15:34 UTC (permalink / raw
  To: gentoo-portage-dev

Am 29.12.2013 03:21, schrieb Tom Wijsman:
> ---
>  bin/chpathtool.py | 45 ++++++++++++++++++++++++++++++++++++---------
>  1 file changed, 36 insertions(+), 9 deletions(-)
> 
> diff --git a/bin/chpathtool.py b/bin/chpathtool.py
> index aa3b7d4..0cb5d64 100755
> --- a/bin/chpathtool.py
> +++ b/bin/chpathtool.py
> @@ -13,6 +13,12 @@ import sys
>  
>  from portage.util._argparse import ArgumentParser
>  
> +# Argument parsing compatibility for Python 2.6 using optparse.
> +if sys.hexversion < 0x2070000:
> +	from optparse import OptionParser
> +
> +from optparse import OptionError
> +
>  CONTENT_ENCODING = 'utf_8'
>  FS_ENCODING = 'utf_8'
>  
> @@ -147,15 +153,36 @@ def chpath_inplace_symlink(filename, st, old, new):
>  def main(argv):
>  
>  	parser = ArgumentParser(description=__doc__)
> -	parser.add_argument('location', default=None,
> -		help='root directory (e.g. $D)')
> -	parser.add_argument('old', default=None,
> -		help='original build prefix (e.g. /)')
> -	parser.add_argument('new', default=None,
> -		help='new install prefix (e.g. $EPREFIX)')
> -	opts = parser.parse_args(argv)
> -
> -	location, old, new = opts.location, opts.old, opts.new
> +	try:
> +		parser.add_argument('location', default=None,
> +			help='root directory (e.g. $D)')
> +		parser.add_argument('old', default=None,
> +			help='original build prefix (e.g. /)')
> +		parser.add_argument('new', default=None,
> +			help='new install prefix (e.g. $EPREFIX)')
> +		opts = parser.parse_args(argv)
> +
> +		location, old, new = opts.location, opts.old, opts.new
> +	except OptionError:
> +		# Argument parsing compatibility for Python 2.6 using optparse.
> +		if sys.hexversion < 0x2070000:
> +			parser = OptionParser(description=__doc__,
> +				usage="usage: %prog [-h] location old new\n\n" + \
> +				"  location: root directory (e.g. $D)\n" + \
> +				"  old:      original build prefix (e.g. /)\n" + \
> +				"  new:      new install prefix (e.g. $EPREFIX)")
> +
> +			(opts, args) = parser.parse_args()
> +
> +			if len(args) != 3:
> +				parser.print_usage()
> +				print("%s: error: expected 3 arguments, got %i"
> +					% (__file__, len(args)))
> +				return
> +
> +			location, old, new = args[0:3]
> +		else:
> +			raise
>  
>  	is_text_file = IsTextFile()
>  
> 

Patch looks good.

While I do not really like the approach, I didn't see a better way.
Tweaking portage.util._argparse.ArgumentParser would just mean to
reinvent argparse.

Sebastian






^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [gentoo-portage-dev] [PATCH] Make use of optparse to fix argument parsing for Python 2.6 in bin/chpathtool.py.
  2013-12-29 15:34   ` Sebastian Luther
@ 2013-12-29 15:51     ` Tom Wijsman
  0 siblings, 0 replies; 4+ messages in thread
From: Tom Wijsman @ 2013-12-29 15:51 UTC (permalink / raw
  To: SebastianLuther; +Cc: gentoo-portage-dev

[-- Attachment #1: Type: text/plain, Size: 814 bytes --]

On Sun, 29 Dec 2013 16:34:35 +0100
Sebastian Luther <SebastianLuther@gmx.de> wrote:

> While I do not really like the approach, I didn't see a better way.
> Tweaking portage.util._argparse.ArgumentParser would just mean to
> reinvent argparse.

Yes, another downside of tweaking the ArgumentParser class is that you
need to change its interface and thus need to rewrite its consumers;
given that this is a temporary compatibility fix, it's not worth it to
spend more effort on this than what's needed to make it work until
that we drop support for Python 2.6. I heard "in 1 - 2 years" mentioned.

-- 
With kind regards,

Tom Wijsman (TomWij)
Gentoo Developer

E-mail address  : TomWij@gentoo.org
GPG Public Key  : 6D34E57D
GPG Fingerprint : C165 AF18 AB4C 400B C3D2  ABF0 95B2 1FCD 6D34 E57D

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 490 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2013-12-29 15:51 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-12-29  1:54 [gentoo-portage-dev] [PATCH] Make use of optparse to fix argument parsing for Python 2.6 in bin/chpathtool.py Tom Wijsman
2013-12-29  2:21 ` Tom Wijsman
2013-12-29 15:34   ` Sebastian Luther
2013-12-29 15:51     ` Tom Wijsman

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox