public inbox for gentoo-commits@lists.gentoo.org
 help / color / mirror / Atom feed
From: "Brian Dolbec" <brian.dolbec@gmail.com>
To: gentoo-commits@lists.gentoo.org
Subject: [gentoo-commits] proj/mirrorselect:master commit in: mirrorselect/
Date: Mon, 12 Nov 2012 20:37:51 +0000 (UTC)	[thread overview]
Message-ID: <1352752540.eacec616dd653bb01a14a09b4bac832aa287f84b.dol-sen@gentoo> (raw)

commit:     eacec616dd653bb01a14a09b4bac832aa287f84b
Author:     Brian Dolbec <dolsen <AT> gentoo <DOT> org>
AuthorDate: Mon Nov 12 19:25:56 2012 +0000
Commit:     Brian Dolbec <brian.dolbec <AT> gmail <DOT> com>
CommitDate: Mon Nov 12 20:35:40 2012 +0000
URL:        http://git.overlays.gentoo.org/gitweb/?p=proj/mirrorselect.git;a=commit;h=eacec616

add py3 compatability fixes, update mirrorparser3.py header to a docstring

---
 mirrorselect/main.py          |   11 +++++
 mirrorselect/mirrorparser3.py |   70 ++++++++++++++++++++-------------
 mirrorselect/selectors.py     |   85 ++++++++++++++++++++++++++---------------
 3 files changed, 107 insertions(+), 59 deletions(-)

diff --git a/mirrorselect/main.py b/mirrorselect/main.py
index 528f383..55c1cb0 100755
--- a/mirrorselect/main.py
+++ b/mirrorselect/main.py
@@ -56,6 +56,12 @@ if "GENTOO_PORTAGE_EPREFIX" in EPREFIX:
     EPREFIX = ''
 
 
+if sys.hexversion >= 0x3000000:
+    _unicode = str
+else:
+    _unicode = unicode
+
+
 class MirrorSelect(object):
 	'''Main operational class'''
 
@@ -100,6 +106,9 @@ class MirrorSelect(object):
 		else:
 			var = 'GENTOO_MIRRORS'
 
+		if hasattr(hosts[0], 'decode'):
+			hosts = [x.decode('utf-8') for x in hosts]
+
 		mirror_string = '%s="%s"' % (var, ' '.join(hosts))
 
 		if out:
@@ -127,7 +136,9 @@ class MirrorSelect(object):
 		lines.append(mirror_string)
 
 		self.output.write('\tWriting new %s\n' % config_path)
+
 		config = open(config_path, 'w')
+
 		for line in lines:
 			config.write(line)
 		config.write('\n')

diff --git a/mirrorselect/mirrorparser3.py b/mirrorselect/mirrorparser3.py
index e5df3fc..e34f9c2 100644
--- a/mirrorselect/mirrorparser3.py
+++ b/mirrorselect/mirrorparser3.py
@@ -1,24 +1,33 @@
 #!/usr/bin/python
+# coding: utf-8
 
-# Mirrorselect 1.x
-# Tool for selecting Gentoo source and rsync mirrors.
-#
-# Copyright (C) 2009 Sebastian Pipping <sebastian@pipping.org>
-# Copyright (C) 2009 Christian Ruppert <idl0r@gentoo.org>
-#
-# This program is free software; you can redistribute it and/or modify
-# it under the terms of the GNU General Public License as published by
-# the Free Software Foundation; either version 2 of the License, or
-# (at your option) any later version.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with this program; if not, write to the Free Software
-# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
+"""Mirrorselect 2.x
+ Tool for selecting Gentoo source and rsync mirrors.
+
+Copyright 2009-2012 Gentoo Foundation
+
+	Copyright (C) 2009 Sebastian Pipping <sebastian@pipping.org>
+	Copyright (C) 2009 Christian Ruppert <idl0r@gentoo.org>
+	Copyright (C) 2012 Brian Dolbec <dolsen@gentoo.org>
+
+Distributed under the terms of the GNU General Public License v2
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, version 2 of the License.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
+
+"""
+
+
+from __future__ import print_function
 
 from xml.etree import ElementTree as ET
 
@@ -52,16 +61,21 @@ class MirrorParser3:
 							}
 
 	def tuples(self):
-		return [(url, args) for url, args in self._dict.items()]
+		return [(url, args) for url, args in list(self._dict.items())]
 
 	def uris(self):
-		return [url for url, args in self._dict.items()]
+		return [url for url, args in list(self._dict.items())]
 
 if __name__ == '__main__':
-	import urllib
-	parser = MirrorParser3()
-	parser.parse(urllib.urlopen(MIRRORS_3_XML).read())
-	print '===== tuples'
-	print parser.tuples()
-	print '===== uris'
-	print parser.uris()
+	if int(sys.version[0]) == 3:
+		import urllib.request, urllib.parse, urllib.error
+		parser = MirrorParser3()
+		parser.parse(urllib.request.urlopen(MIRRORS_3_XML).read())
+	else:
+		import urllib
+		parser = MirrorParser3()
+		parser.parse(urllib.urlopen(MIRRORS_3_XML).read())
+	print('===== tuples')
+	print(parser.tuples())
+	print('===== uris')
+	print(parser.uris())

diff --git a/mirrorselect/selectors.py b/mirrorselect/selectors.py
index a8f8e24..98bd698 100644
--- a/mirrorselect/selectors.py
+++ b/mirrorselect/selectors.py
@@ -29,18 +29,30 @@ Distributed under the terms of the GNU General Public License v2
 """
 
 import math
-import os
 import signal
 import socket
 import subprocess
 import sys
 import time
-import urllib
-import urlparse
 import hashlib
+
+if int(sys.version[0]) == 3:
+	import urllib.request, urllib.parse, urllib.error
+	url_parse = urllib.parse
+	url_open = urllib.request.urlopen
+else:
+	import urllib
+	import urlparse
+	url_parse = urlparse.urlparse
+	url_open = urllib.urlopen
+
+
 from mirrorselect.mirrorparser3 import MirrorParser3
 
-import codecs
+if sys.hexversion >= 0x3000000:
+    _unicode = str
+else:
+    _unicode = unicode
 
 
 class Extractor(object):
@@ -95,7 +107,7 @@ class Extractor(object):
 		self.output.print_info('Downloading a list of mirrors...')
 
 		try:
-			parser.parse(urllib.urlopen(url).read())
+			parser.parse(url_open(url).read())
 		except EnvironmentError:
 			pass
 
@@ -198,7 +210,7 @@ class Shallow(object):
 			self.output.write('ran netselect(%s, %s), and got %s\n' % (block, len(block),
 				host_dict), 2)
 
-			for key in host_dict.keys():
+			for key in list(host_dict.keys()):
 				ret_hosts[key] = host_dict[key]
 			block_index += 1
 
@@ -206,7 +218,7 @@ class Shallow(object):
 		'%d hosts, in blocks of %s. %s of %s blocks complete.\n'
 		% (number, block_size, block_index, len(host_blocks)))
 
-		host_ranking_keys = ret_hosts.keys()
+		host_ranking_keys = list(ret_hosts.keys())
 		host_ranking_keys.sort()
 
 		for rank in host_ranking_keys[:number]:
@@ -291,12 +303,12 @@ class Deep(object):
 				continue
 
 		self.output.write('deeptest(): got %s hosts, and returned %s\n' % (num_hosts, \
-			str(top_hosts.values())), 2)
+			str(list(top_hosts.values()))), 2)
 
 		self.output.write('\n')	#this just makes output nicer
 
 		#can't just return the dict.valuse, because we want the fastest mirror first...
-		keys = top_hosts.keys()
+		keys = list(top_hosts.keys())
 		keys.sort()
 
 		rethosts = []
@@ -319,7 +331,7 @@ class Deep(object):
 		else:
 			url = url + '/distfiles/mirrorselect-test'
 
-		url_parts = urlparse.urlparse(url)
+		url_parts = url_parse(url)
 
 		class TimeoutException(Exception):
 			pass
@@ -344,7 +356,7 @@ class Deep(object):
 						ips.append(ip)
 				finally:
 					signal.alarm(0)
-			except socket.error, e:
+			except socket.error as e:
 				self.output.write('deeptime(): dns error for host %s: %s\n' % \
 					(url_parts.hostname, e), 2)
 			except TimeoutException:
@@ -363,11 +375,11 @@ class Deep(object):
 			try:
 				try:
 					signal.alarm(self._connect_timeout)
-					f = urllib.urlopen(url)
+					f = url_open(url)
 					break
 				finally:
 					signal.alarm(0)
-			except EnvironmentError, e:
+			except EnvironmentError as e:
 				self.output.write(('deeptime(): connection to host %s ' + \
 					'failed for ip %s: %s\n') % \
 					(url_parts.hostname, ip, e), 2)
@@ -389,7 +401,7 @@ class Deep(object):
 				f.close()
 			finally:
 				signal.alarm(0)
-		except EnvironmentError, e:
+		except EnvironmentError as e:
 			self.output.write(('deeptime(): close connection to host %s ' + \
 				'failed for ip %s: %s\n') % \
 				(url_parts.hostname, ip, e), 2)
@@ -405,7 +417,7 @@ class Deep(object):
 			try:
 				signal.alarm(int(math.ceil(maxtime)))
 				stime = time.time()
-				f = urllib.urlopen(url)
+				f = url_open(url)
 
 				if hashlib.md5(f.read()).hexdigest() != "bdf077b2e683c506bf9e8f2494eeb044":
 					return (None, True)
@@ -415,7 +427,7 @@ class Deep(object):
 			finally:
 				signal.alarm(0)
 
-		except EnvironmentError, e:
+		except EnvironmentError as e:
 			self.output.write(('deeptime(): download from host %s ' + \
 				'failed for ip %s: %s\n') % \
 				(url_parts.hostname, ip, e), 2)
@@ -445,20 +457,20 @@ class Deep(object):
 					(time_host[1], time_host[0]), 2)
 
 			host_dict.update(dict([time_host]))
-			times = host_dict.keys()
+			times = list(host_dict.keys())
 			times.sort()
 
 		else: #We need to make room in the dict before we add. Kill the slowest.
 			self.output.write('_list_add(): Adding host %s with a time of %s\n' %
 					(time_host[1], time_host[0]), 2)
-			times = host_dict.keys()
+			times = list(host_dict.keys())
 			times.sort()
 			self.output.write('_list_add(): removing %s\n' % host_dict[times[-1]],
 					2)
 			del host_dict[times[-1]]
 			host_dict.update(dict([time_host]))
 			#done adding. now return the appropriate time
-			times = host_dict.keys()
+			times = list(host_dict.keys())
 			times.sort()
 
 		if len(host_dict) < maxlen:	#check again to choose new timeout
@@ -498,16 +510,17 @@ class Interactive(object):
 		Some sort of interactive menu thingy.
 		"""
 		if options.rsync:
-			dialog = 'dialog --stdout --title "Gentoo RSYNC Mirrors"'\
-				' --radiolist "Please select your desired mirror:" 20 110 14'
+			dialog = ['dialog', '--stdout', '--title', '"Gentoo RSYNC Mirrors"',
+				'--radiolist', '"Please select your desired mirror:"',
+				'20', '110', '14']
 		else:
-			dialog = 'dialog --separate-output --stdout --title'\
-				' "Gentoo Download Mirrors" --checklist "Please'\
-				' select your desired mirrors:'
+			dialog = ['dialog', '--separate-output', '--stdout', '--title',
+				'"Gentoo Download Mirrors"', '--checklist',
+				'"Please select your desired mirrors:']
 			if not options.ipv4 and not options.ipv6:
-				dialog += '\n* = supports ipv6'
+				dialog[-1] += '\n* = supports ipv6'
 
-			dialog += '" 20 110 14'
+			dialog.extend(['20', '110', '14'])
 
 		for (url, args) in sorted(hosts, key = lambda x: (x[1]['country'].lower(), x[1]['name'].lower()) ):
 			marker = ""
@@ -518,10 +531,20 @@ class Interactive(object):
 			if options.ipv6 and ( args['ipv6'] == 'n' ): continue
 			if options.ipv4 and ( args['ipv4'] == 'n' ): continue
 
-			dialog += ' ' + '"%s" "%s%s: %s" "OFF"' % ( url, marker, args['country'], args['name'] )
+			#dialog.append('"%s" "%s%s: %s" "OFF"' % ( url, marker, args['country'], args['name']))
+			dialog.extend(["%s" %url,
+				"%s%s: %s" %(marker, args['country'], args['name']),
+				 "OFF"])
+		dialog = [_unicode(x) for x in dialog]
+		proc = subprocess.Popen( dialog,
+			stdout=subprocess.PIPE, stderr=subprocess.PIPE)
+
+		out, err = proc.communicate()
+
+		self.urls = out.splitlines()
 
-		mirror_fd = os.popen('%s' % codecs.encode(dialog, 'utf8'))
-		mirrors = mirror_fd.read()
-		mirror_fd.close()
+		if hasattr(self.urls[0], 'decode'):
+			self.urls = [x.decode('utf-8').rstrip() for x in self.urls]
+		else:
+			self.urls = [x.rstrip() for x in self.urls]
 
-		self.urls = mirrors.rstrip().split('\n')


             reply	other threads:[~2012-11-12 20:38 UTC|newest]

Thread overview: 86+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-11-12 20:37 Brian Dolbec [this message]
  -- strict thread matches above, loose matches on Subject: below --
2024-05-28  3:48 [gentoo-commits] proj/mirrorselect:master commit in: mirrorselect/ Sam James
2023-08-07  0:23 Sam James
2023-08-07  0:14 Sam James
2023-08-07  0:14 Sam James
2023-08-07  0:14 Sam James
2023-08-06 23:30 Sam James
2023-08-06 23:30 Sam James
2023-07-06  8:05 Sam James
2022-05-31 18:39 Brian Dolbec
2022-05-31 18:39 Brian Dolbec
2022-05-31 18:39 Brian Dolbec
2022-05-31 18:39 Brian Dolbec
2022-05-31  4:08 Brian Dolbec
2022-05-31  4:08 Brian Dolbec
2022-05-31  2:22 Brian Dolbec
2022-05-31  2:22 Brian Dolbec
2022-05-30 23:12 Brian Dolbec
2022-05-30 23:12 Brian Dolbec
2020-06-03 19:05 Brian Dolbec
2019-07-17  5:06 Zac Medico
2019-05-27 17:52 Zac Medico
2019-05-27 17:22 Zac Medico
2019-02-13  8:22 Zac Medico
2019-02-13  8:09 Zac Medico
2019-02-13  5:51 Zac Medico
2018-05-26 15:43 Brian Dolbec
2017-02-21  4:44 Zac Medico
2017-02-21  3:19 Brian Dolbec
2017-02-21  3:19 Brian Dolbec
2016-11-15  1:16 Zac Medico
2015-01-27 18:20 Brian Dolbec
2015-01-27  4:38 Brian Dolbec
2015-01-27  4:38 Brian Dolbec
2014-05-28 21:08 Brian Dolbec
2014-05-28 19:44 Brian Dolbec
2014-05-05  2:04 Brian Dolbec
2014-05-05  2:04 Brian Dolbec
2014-05-05  2:04 Brian Dolbec
2014-03-02  7:44 [gentoo-commits] proj/mirrorselect:ssl " Brian Dolbec
2014-03-02  7:44 ` [gentoo-commits] proj/mirrorselect:master " Brian Dolbec
2014-03-02  7:44 Brian Dolbec
2014-03-02  7:44 Brian Dolbec
2014-03-02  7:44 Brian Dolbec
2014-03-02  7:44 Brian Dolbec
2014-03-02  7:44 Brian Dolbec
2014-03-02  7:44 Brian Dolbec
2014-03-02  7:44 Brian Dolbec
2014-01-31 15:44 [gentoo-commits] proj/mirrorselect:ssl " Brian Dolbec
2014-03-02  7:44 ` [gentoo-commits] proj/mirrorselect:master " Brian Dolbec
2013-10-20 18:19 [gentoo-commits] proj/mirrorselect:ssl " Brian Dolbec
2014-03-02  7:44 ` [gentoo-commits] proj/mirrorselect:master " Brian Dolbec
2013-10-19  9:06 Brian Dolbec
2013-10-19  9:06 Brian Dolbec
2013-10-19  9:06 Brian Dolbec
2013-10-18 14:26 Brian Dolbec
2013-10-18  6:46 Brian Dolbec
2013-10-18  6:46 Brian Dolbec
2013-10-18  6:46 Brian Dolbec
2013-10-18  6:46 Brian Dolbec
2013-10-17 14:26 Brian Dolbec
2013-10-17  6:57 Brian Dolbec
2013-10-17  3:16 Brian Dolbec
2013-10-17  3:16 Brian Dolbec
2013-10-17  3:16 Brian Dolbec
2013-10-16  9:17 Brian Dolbec
2013-10-16  8:36 Brian Dolbec
2013-10-16  8:36 Brian Dolbec
2013-10-16  8:36 Brian Dolbec
2013-10-16  8:36 Brian Dolbec
2013-10-15 22:43 Brian Dolbec
2013-10-15 14:39 Brian Dolbec
2013-10-15 14:39 Brian Dolbec
2013-10-15 14:39 Brian Dolbec
2013-10-15  7:27 Brian Dolbec
2013-10-15  3:51 Brian Dolbec
2013-10-15  3:51 Brian Dolbec
2013-03-10 13:07 Brian Dolbec
2012-12-16  2:38 Brian Dolbec
2012-12-15 21:25 Brian Dolbec
2012-11-15  3:53 Brian Dolbec
2012-11-15  3:44 Brian Dolbec
2012-11-14 20:28 Paul Varner
2012-11-12 21:41 Brian Dolbec
2012-11-12 15:56 Brian Dolbec
2012-11-12  7:46 Brian Dolbec
2012-11-12  7:46 Brian Dolbec
2012-11-12  7:46 Brian Dolbec
2012-11-12  7:46 Brian Dolbec

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1352752540.eacec616dd653bb01a14a09b4bac832aa287f84b.dol-sen@gentoo \
    --to=brian.dolbec@gmail.com \
    --cc=gentoo-commits@lists.gentoo.org \
    --cc=gentoo-dev@lists.gentoo.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox