aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSebastian Pipping <sebastian@pipping.org>2009-07-07 23:01:08 +0200
committerSebastian Pipping <sebastian@pipping.org>2009-07-07 23:27:04 +0200
commita2ebb10225b9cc00a66a0b0c7c72725e12281da2 (patch)
tree9c692afed3203a893a35c8639a24e51e3d98ac69
parentMove class MirrorParser to extra file (diff)
downloadmirrorselect-a2ebb10225b9cc00a66a0b0c7c72725e12281da2.tar.gz
mirrorselect-a2ebb10225b9cc00a66a0b0c7c72725e12281da2.tar.bz2
mirrorselect-a2ebb10225b9cc00a66a0b0c7c72725e12281da2.zip
Add parser for mirrors3.xml and migrate to it
-rw-r--r--mirrorparser3.py58
-rwxr-xr-xmirrorselect18
2 files changed, 69 insertions, 7 deletions
diff --git a/mirrorparser3.py b/mirrorparser3.py
new file mode 100644
index 0000000..6efcc3d
--- /dev/null
+++ b/mirrorparser3.py
@@ -0,0 +1,58 @@
+#!/usr/bin/python
+
+# Mirrorselect 1.x
+# Tool for selecting Gentoo source and rsync mirrors.
+#
+# Copyright (C) 2009 Sebastian Pipping <sebastian@pipping.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.
+
+from xml.etree import ElementTree as ET
+
+MIRRORS_3_XML = 'http://www.gentoo.org/main/en/mirrors3.xml'
+
+class MirrorParser3:
+ def __init__(self):
+ self._reset()
+
+ def _reset(self):
+ self._dict = {}
+
+ def parse(self, text):
+ self._reset()
+ for mirrorgroup in ET.XML(text):
+ for mirror in mirrorgroup:
+ name = ''
+ for e in mirror:
+ if e.tag == 'name':
+ name = e.text
+ if e.tag == 'uri':
+ uri = e.text
+ self._dict[uri] = name
+
+ def tuples(self):
+ return [(url, name) for url, name in self._dict.items()]
+
+ def uris(self):
+ return [url for url, name in 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()
diff --git a/mirrorselect b/mirrorselect
index 74b94aa..41dcf1f 100755
--- a/mirrorselect
+++ b/mirrorselect
@@ -36,7 +36,8 @@ import time
import urllib
import urlparse
from optparse import IndentedHelpFormatter, OptionParser
-from mirrorparser import MirrorParser, MIRRORS_XML
+from mirrorparser3 import MirrorParser3, MIRRORS_3_XML
+import codecs
class Output(object):
"""Handles text output. Only prints messages with level <= verbosity.
@@ -139,12 +140,12 @@ class ColoredFormatter(IndentedHelpFormatter):
class Extractor(object):
- """The Extractor employs a MirrorParser object to get a list of valid
+ """The Extractor employs a MirrorParser3 object to get a list of valid
mirrors, and then filters them. Only the mirrors that should be tested, based on
user input are saved. They will be in the hosts attribute."""
def __init__(self, list_url, options):
- parser = MirrorParser()
+ parser = MirrorParser3()
self.hosts = []
if options.rsync:
@@ -202,12 +203,15 @@ class Extractor(object):
output.print_info('Downloading a list of mirrors...')
try:
- parser.feed(urllib.urlopen(url).read())
+ parser.parse(urllib.urlopen(url).read())
except EnvironmentError:
pass
- parser.close()
- tuples = parser.tuples()
+ def fix_tuple(tuple):
+ url, name = tuple
+ return (url, codecs.encode(name, 'utf8'))
+
+ tuples = [fix_tuple(t) for t in parser.tuples()]
if len(tuples) == 0:
output.print_err('Could not get mirror list. Check your internet'
' connection.')
@@ -872,7 +876,7 @@ def main(argv):
output.verbosity = options.verbosity
fsmirrors = get_filesystem_mirrors(options.output, config_path, options.rsync)
- hosts = Extractor(MIRRORS_XML, options).hosts
+ hosts = Extractor(MIRRORS_3_XML, options).hosts
if options.interactive:
selector = Interactive(hosts, options.rsync)