aboutsummaryrefslogtreecommitdiff
blob: 1e0291e21cb20c6dd9302c3bb72480a88fc45b46 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#
# mirrorselect_gui.py: gui mirrorselect.
#
# Copyright (C) 2011 wiktor w brodlo
# Copyright (C) 2011 Gentoo Foundation
#
# 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, see <http://www.gnu.org/licenses/>.
#

import string
import gtk
import gtk.glade
import gtk.gdk
import gobject
import pygtk
import pango
import sys
import gui
import os
from mirrorselect.mirrorparser3 import MirrorParser3

from iw_gui import *

from constants import *
import gettext
_ = lambda x: gettext.ldgettext("anaconda", x)

class MirrorselectWindow(InstallWindow):
	buttons = []
	
	def getNext(self):
		for button in self.buttons:
			if button.get_property("active"):
				# For while the treeview is broken
				#self.anaconda.mirrors.append(button.get_property("label"))
				self.anaconda.mirrors.append("http://mirrors.kernel.org/gentoo/")
		return None

	def getScreen(self, anaconda):
		self.anaconda = anaconda
		self.intf = anaconda.intf        
		
		(self.xml, self.align) = gui.getGladeWidget("mirrorselect.glade", "mirrorselect_align")
		
		mirrorsf = None
		
		while mirrorsf == None:
			# Try downloading the mirrorlist
			mirrorsf = self.downloadMirrorlist()
			# if failed, 
			if mirrorsf == None:
				md = gtk.MessageDialog(buttons=gtk.BUTTONS_YES_NO)
				md.set_property("title", _("Failed to download mirrorlist"))
				md.set_property("text", _("Failed to download the mirror list. Would you like to try again?\nPressing 'No' will abort the installation."))
				if md.run() == gtk.RESPONSE_NO:
					md.destroy()
					InstallControlWindow._doExit([])
					
		mirrors_parsed = self.parseMirrors(mirrorsf)
		self.mirrors = self.mirrorList(mirrors_parsed, "http+ftp")
		
		self.addMirrors(self.mirrors, mirrors_parsed, self.xml)
	
		return self.align
	
	def addMirrors(self, mirror_list, mirror_data, xml):
		(regions, countries, mirrors) = mirror_list
		self.treestore = gtk.TreeStore(bool, str, str, str, str)
		for region in regions:
			region_ts = self.treestore.append(False, [region, "", "", ""])
			for country in countries[region]:
				country_ts = self.treestore.append(region_ts, [country, "", "", ""])
				for mirror in mirrors[country]:
					self.addMirrorRow(self.treestore, country_ts, region, country, mirror, mirror_data[region][country][mirror])
		treeview = gtk.TreeView(self.treestore)
		url_column = gtk.TreeViewColumn(_("Mirror URL"))
		treeview.append_column(url_column)
		name_column = gtk.TreeViewColumn(_("Mirror Name"))
		treeview.append_column(name_column)
		ipv4_column = gtk.TreeViewColumn("IPv4?")
		treeview.append_column(ipv4_column)
		ipv6_column = gtk.TreeViewColumn("IPv6?")
		treeview.append_column(ipv6_column)
		url_cell = gtk.CellRendererToggle()
		text_cell = gtk.CellRendererText()
		url_column.pack_start(url_cell, True)
		name_column.pack_start(text_cell, True)
		ipv4_column.pack_start(text_cell, True)
		ipv6_column.pack_start(text_cell, True)
		url_column.add_attribute(url_cell, "active", 0)
		name_column.add_attribute(text_cell, "text", 1)
		ipv4_column.add_attribute(text_cell, "text", 2)
		ipv6_column.add_attribute(text_cell, "text", 3)
		treeview.set_search_column(1)
		url_cell.connect("toggled", self.toggleCB, self.treestore)
		
		xml.get_widget("mirrors_viewport").add(treeview)
		
	def toggleCB(self, widget, path, model):
		model[path][0] = not model[path][0]
	
	def addMirrorRow(self, ts, country_ts, region, country, mirror, data):
		ipv4 = ""
		ipv6 = ""
		if data["ipv4"] == "y":
			ipv4 = "ipv4"
		if data["ipv6"] == "y":
			ipv6 = " ipv6"
		
		ts.append(country_ts, [False, mirror, ipv4, ipv6])
	
	def downloadMirrorlist(self):
		try:
			os.system("wget -nc -O /tmp/mirror3.xml %s" % os.environ["PORTAGE_MIRRORLIST_URL"])
			f = open("/tmp/mirror3.xml")
		except:
			return None
		return f
	
	def parseMirrors(self, mirrorlist):
		xml = mirrorlist.read()
		parser = MirrorParser3()
		parser.parse(xml)
		tuples = parser.tuples()
			
		mirrors = {}
		for mirror in tuples:
			if mirror[1]["region"] not in mirrors:
				mirrors[mirror[1]["region"]] = {}
			if mirror[1]["country"] not in mirrors[mirror[1]["region"]]:
				mirrors[mirror[1]["region"]][mirror[1]["country"]] = {}
			mirrors[mirror[1]["region"]][mirror[1]["country"]]["%s (%s)" % (mirror[1]["name"], mirror[1]["proto"])] = \
				{"url": mirror[0],
				 "name": mirror[1]["name"],
				 "proto": mirror[1]["proto"],
				 "ipv4": mirror[1]["ipv4"],
				 "ipv6": mirror[1]["ipv6"]}
			
		return mirrors
	
	def mirrorList(self, mirrors_parsed, proto):
		region_list = mirrors_parsed.keys()
		region_list.sort()
		countries = {}
		mirrors = {}
		mirrors_proto = {}
		for region in region_list:
			countries[region] = mirrors_parsed[region].keys()
			countries[region].sort()
			for country in countries[region]:
				mirrors[country] = mirrors_parsed[region][country].keys()
				mirrors[country].sort()
				mirrors_proto[country] = []
				for mirror in mirrors[country]:
					if proto == "http+ftp":
						if mirrors_parsed[region][country][mirror]["proto"] == "http":
							mirrors_proto[country].append(mirror)
						if mirrors_parsed[region][country][mirror]["proto"] == "ftp":
							mirrors_proto[country].append(mirror)
					if mirrors_parsed[region][country][mirror]["proto"] == proto:
						mirrors_proto[country].append(mirror)
				mirrors_proto[country].sort()
				
		return (region_list, countries, mirrors_proto)