1
# aptsource.py.in - parse sources.list
3
# Copyright (c) 2004 Canonical
6
# Author: Michiel Sikkes <michiel@eyesopened.nl>
7
# Michael Vogt <mvo@debian.org>
9
# This program is free software; you can redistribute it and/or
10
# modify it under the terms of the GNU General Public License as
11
# published by the Free Software Foundation; either version 2 of the
12
# License, or (at your option) any later version.
14
# This program is distributed in the hope that it will be useful,
15
# but WITHOUT ANY WARRANTY; without even the implied warranty of
16
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
# GNU General Public License for more details.
19
# You should have received a copy of the GNU General Public License
20
# along with this program; if not, write to the Free Software
21
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
30
from DistInfo import DistInfo
32
# actual source.list entries
35
def __init__(self, line,file):
47
# works mostely like split but takes [] into account
48
def mysplit(self, line):
49
line = string.strip(line)
52
# we are inside a [..] block
55
for i in range(len(line)):
62
elif space_found and not line[i].isspace(): # we skip one or more space
66
elif line[i].isspace() and not p_found: # found a whitespace
76
# parse a given source line and split it into the fields we need
78
line = string.strip(self.line)
80
# check if the source is enabled/disabled
81
if line == "" or line == "#": # empty line
86
pieces = string.split(line[1:])
87
# if it looks not like a disabled deb line return
88
if not (pieces[0] == "deb" or pieces[0] == "deb-src"):
93
# check for another "#" in the line (this is treated as a comment)
96
self.comment = line[i+1:]
98
# source is ok, split it and see what we have
99
pieces = self.mysplit(line)
100
# Type, deb or deb-src
101
self.type = string.strip(pieces[0])
103
self.uri = string.strip(pieces[1])
104
# distro and components (optional)
105
# Directory or distro
106
self.dist = string.strip(pieces[2])
109
self.comps = pieces[3:]
116
# set enabled/disabled
117
def set_enabled(self, new_value):
118
self.disabled = not new_value
119
# enable, remove all "#" from the start of the line
120
if new_value == True:
122
self.line = string.lstrip(self.line)
123
while self.line[i] == "#":
125
self.line = self.line[i:]
127
# disabled, add a "#"
128
if string.strip(self.line)[0] != "#":
129
self.line = "#" + self.line
136
""" simple (and not efficient) way to return uniq list """
144
# the SourceList file as a class
147
self.list = [] # of Type SourceEntries
149
dir = apt_pkg.Config.FindDir("Dir::Etc")
150
file = apt_pkg.Config.Find("Dir::Etc::sourcelist")
152
# read sources.list.d
153
partsdir = apt_pkg.Config.FindDir("Dir::Etc::sourceparts")
154
for file in glob.glob("%s/*.list" % partsdir):
157
def is_mirror(self, add_uri, orig_uri):
158
"""check if the given add_url is idential or a mirror of orig_uri
159
e.g. add_uri = archive.ubuntu.com
160
orig_uri = de.archive.ubuntu.com
163
# remove traling spaces and "/"
164
add_uri = add_uri.rstrip("/ ")
165
orig_uri = orig_uri.rstrip("/ ")
167
if add_uri == orig_uri:
170
# add uri is a master site and orig_uri has the from "XX.mastersite"
171
# (e.g. de.archive.ubuntu.com)
173
add_srv = add_uri.split("//")[1]
174
orig_srv = orig_uri.split("//")[1]
175
#print "%s == %s " % (add_srv, orig_srv)
176
except IndexError: # ok, somethings wrong here
179
if add_srv == orig_srv[3:]:
184
def add(self, type, uri, dist, comps, comment="", pos=-1):
185
# if there is a repo with the same (type, uri, dist) just add the
188
if i.type == type and self.is_mirror(uri,i.uri) and i.dist == dist:
189
comps = uniq(i.comps + comps)
190
# set to the old position and preserve comment
192
pos = self.list.index(i)
194
line = "%s %s %s" % (type,uri,dist)
196
line = line + " " + c;
198
line = "%s #%s\n" %(line,comment)
200
self.list.insert(pos, SourceEntry(line))
202
def remove(self, source_entry):
203
self.list.remove(source_entry)
207
lines = f.readlines()
209
source = SourceEntry(line,file)
210
self.list.append(source)
215
for source in self.list:
216
if not files.has_key(source.file):
217
files[source.file]=open(source.file,"w")
218
files[source.file].write(source.str())
223
# templates for the add dialog
224
class SourceEntryTemplate(SourceEntry):
225
def __init__(self,a_type,uri,dist,description,comps):
227
self.comps_descriptions = []
231
self.description = description
234
class SourceCompTemplate:
235
def __init__(self, name, description, on_by_default):
237
self.description = description
238
self.on_by_default = on_by_default
240
class SourceEntryTemplates:
247
for suite in dinfo.suites:
249
for comp in suite.components:
250
comps.append(SourceCompTemplate(comp.name, _(comp.description),
252
self.templates.append (SourceEntryTemplate(suite.repository_type,
258
# matcher class to make a source entry look nice
259
# lots of predefined matchers to make it i18n/gettext friendly
260
class SourceEntryMatcher:
262
def __init__(self, a_type,a_descr):
264
self.description = a_descr
267
def __init__(self,a_uri,a_dist, a_descr,l_comps, l_comps_descr):
270
self.description = a_descr
272
self.comps_descriptions = l_comps_descr
277
self.type_list.append(self.MatchType("^deb$",_("Binary")))
278
self.type_list.append(self.MatchType("^deb-src$",_("Source")))
282
ubuntu_comps = ["^main$","^restricted$","^universe$","^multiverse$"]
283
ubuntu_comps_descr = [_("Officially supported"),
284
_("Restricted copyright"),
285
_("Community maintained (Universe)"),
286
_("Non-free (Multiverse)")]
288
self.dist_list.append(self.MatchDist("cdrom:\[Ubuntu.*5.10",
290
_("CD disk with Ubuntu 5.10 \"Breezy Badger\""),
291
ubuntu_comps, ubuntu_comps_descr))
292
self.dist_list.append(self.MatchDist("cdrom:\[Ubuntu.*5.04",
294
_("CD disk with Ubuntu 5.04 \"Hoary Hedgehog\""),
295
ubuntu_comps, ubuntu_comps_descr))
296
self.dist_list.append(self.MatchDist("cdrom:\[Ubuntu.*4.10",
298
_("CD disk with Ubuntu 4.10 \"Warty Warthog\""),
299
ubuntu_comps, ubuntu_comps_descr))
302
self.dist_list.append(self.MatchDist(".*archive.ubuntu.com/ubuntu",
304
"Ubuntu 4.10 \"Warty Warthog\"",
305
ubuntu_comps, ubuntu_comps_descr))
306
self.dist_list.append(self.MatchDist(".*security.ubuntu.com/ubuntu",
308
_("Ubuntu 4.10 Security Updates"),
309
ubuntu_comps, ubuntu_comps_descr))
310
self.dist_list.append(self.MatchDist(".*archive.ubuntu.com/ubuntu",
312
_("Ubuntu 4.10 Security Updates"),
313
ubuntu_comps, ubuntu_comps_descr))
314
self.dist_list.append(self.MatchDist(".*archive.ubuntu.com/ubuntu",
316
_("Ubuntu 4.10 Updates"),
317
ubuntu_comps, ubuntu_comps_descr))
319
self.dist_list.append(self.MatchDist(".*archive.ubuntu.com/ubuntu",
321
_("Ubuntu 5.04 Security Updates"),
322
ubuntu_comps, ubuntu_comps_descr))
323
self.dist_list.append(self.MatchDist(".*security.ubuntu.com/ubuntu",
325
_("Ubuntu 5.04 Security Updates"),
326
ubuntu_comps, ubuntu_comps_descr))
327
self.dist_list.append(self.MatchDist(".*archive.ubuntu.com/ubuntu",
329
"Ubuntu 5.04 \"Hoary Hedgehog\"",
330
ubuntu_comps, ubuntu_comps_descr))
331
self.dist_list.append(self.MatchDist(".*archive.ubuntu.com/ubuntu",
333
_("Ubuntu 5.04 Updates"),
334
ubuntu_comps, ubuntu_comps_descr))
336
self.dist_list.append(self.MatchDist(".*archive.ubuntu.com/ubuntu",
338
_("Ubuntu 5.10 Security Updates"),
339
ubuntu_comps, ubuntu_comps_descr))
340
self.dist_list.append(self.MatchDist(".*security.ubuntu.com/ubuntu",
342
_("Ubuntu 5.10 Security Updates"),
343
ubuntu_comps, ubuntu_comps_descr))
344
self.dist_list.append(self.MatchDist(".*archive.ubuntu.com/ubuntu",
346
"Ubuntu 5.10 \"Breezy Badger\"",
347
ubuntu_comps, ubuntu_comps_descr))
348
self.dist_list.append(self.MatchDist(".*archive.ubuntu.com/ubuntu",
350
_("Ubuntu 5.10 Updates"),
351
ubuntu_comps, ubuntu_comps_descr))
355
debian_comps = ["^main$","^contrib$","^non-free$","^non-US$"]
356
debian_comps_descr = [_("Officially supported"),
357
_("Contributed software"),
358
_("Non-free software"),
359
_("US export restricted software")
363
self.dist_list.append(self.MatchDist(".*debian.org/debian",
365
_("Debian 3.1 \"Sarge\""),
366
debian_comps, debian_comps_descr))
367
self.dist_list.append(self.MatchDist(".*debian.org/debian",
369
_("Debian 3.0 \"Woody\""),
370
debian_comps, debian_comps_descr))
372
self.dist_list.append(self.MatchDist(".*security.debian.org",
374
_("Debian Stable Security Updates"),
375
debian_comps, debian_comps_descr))
377
self.dist_list.append(self.MatchDist(".*debian.org/debian",
380
debian_comps, debian_comps_descr))
381
self.dist_list.append(self.MatchDist(".*debian.org/debian",
384
debian_comps, debian_comps_descr))
385
self.dist_list.append(self.MatchDist(".*debian.org/debian",
387
_("Debian Unstable \"Sid\""),
388
debian_comps, debian_comps_descr))
391
self.dist_list.append(self.MatchDist(".*debian.org/debian-non-US",
393
_("Debian Non-US (Stable)"),
394
debian_comps, debian_comps_descr))
395
self.dist_list.append(self.MatchDist(".*debian.org/debian-non-US",
397
_("Debian Non-US (Testing)"),
398
debian_comps, debian_comps_descr))
399
self.dist_list.append(self.MatchDist(".*debian.org/debian-non-US",
401
_("Debian Non-US (Unstable)"),
402
debian_comps, debian_comps_descr))
407
def match(self,source):
409
# some sane defaults first
410
type_description = source.type
411
dist_description = source.uri + " " + source.dist
412
comp_description = ""
413
for c in source.comps:
414
comp_description = comp_description + " " + c
416
for t in self.type_list:
417
if re.match(t.type, source.type):
418
type_description = _(t.description)
421
for d in self.dist_list:
422
#print "'%s'" %source.uri
423
if re.match(d.uri, source.uri) and re.match(d.dist,source.dist):
424
dist_description = d.description
425
comp_description = ""
426
for c in source.comps:
428
for i in range(len(d.comps)):
429
if re.match(d.comps[i], c):
430
comp_description = comp_description+"\n"+d.comps_descriptions[i]
433
comp_description = comp_description+" "+c
437
return (type_description,dist_description,comp_description)