~mvo/update-manager/pae-kernel-transtion

« back to all changes in this revision

Viewing changes to src/aptsources.py.in

  • Committer: Michael Vogt
  • Date: 2005-11-15 13:18:07 UTC
  • Revision ID: egon@top-20051115131807-12fada324eb74180
* initial revision (after accidently killing it)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# aptsource.py.in - parse sources.list
 
2
#  
 
3
#  Copyright (c) 2004 Canonical
 
4
#                2004 Michiel Sikkes
 
5
#  
 
6
#  Author: Michiel Sikkes <michiel@eyesopened.nl>
 
7
#          Michael Vogt <mvo@debian.org>
 
8
 
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.
 
13
 
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.
 
18
 
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
 
22
#  USA
 
23
 
 
24
import string
 
25
import gettext
 
26
import re
 
27
 
 
28
# actual source.list entries
 
29
class SourceEntry:
 
30
 
 
31
  # works mostely like split but takes [] into account
 
32
  def mysplit(self, line):
 
33
    line = string.strip(line)
 
34
    pieces = []
 
35
    tmp = ""
 
36
    # we are inside a [..] block
 
37
    p_found = False
 
38
    space_found = False
 
39
    for i in range(len(line)):
 
40
      if line[i] == "[":
 
41
        p_found=True
 
42
        tmp += line[i]
 
43
      elif line[i] == "]":
 
44
        p_found=False
 
45
        tmp += line[i]
 
46
      elif space_found and not line[i].isspace(): # we skip one or more space
 
47
        space_found = False
 
48
        pieces.append(tmp)
 
49
        tmp = line[i]
 
50
      elif line[i].isspace() and not p_found:     # found a whitespace
 
51
        space_found = True
 
52
      else:
 
53
        tmp += line[i]
 
54
    # append last piece
 
55
    if len(tmp) > 0:
 
56
      pieces.append(tmp)
 
57
    return pieces
 
58
 
 
59
 
 
60
  # parse a given source line and split it into the fields we need
 
61
  def parse(self,line):
 
62
    line  = string.strip(self.line)
 
63
    #print line
 
64
    # check if the source is enabled/disabled
 
65
    if line == "" or line == "#":
 
66
      self.invalid = True
 
67
      return
 
68
    if line[0] == "#":
 
69
      self.disabled = True
 
70
      pieces = string.split(line[1:])
 
71
      # if it looks not like a disabled deb line return 
 
72
      if not (pieces[0] == "deb" or pieces[0] == "deb-src"):
 
73
        self.invalid = True
 
74
        return
 
75
      else:
 
76
        line = line[1:]
 
77
    # check for another "#" in the line (this is treated as a comment)
 
78
    i = line.find("#")
 
79
    if i > 0:
 
80
      self.comment = line[i+1:]
 
81
      line = line[:i]
 
82
    # source is ok, split it and see what we have
 
83
    pieces = self.mysplit(line)
 
84
    # Type, deb or deb-src
 
85
    self.type = string.strip(pieces[0])
 
86
    # URI
 
87
    self.uri = string.strip(pieces[1])
 
88
    # distro and components (optional)
 
89
    # Directory or distro
 
90
    self.dist = string.strip(pieces[2])
 
91
    if len(pieces) > 3:
 
92
      # List of components
 
93
      self.comps = pieces[3:]
 
94
    else:
 
95
      self.comps = []
 
96
 
 
97
    #print self.__dict__
 
98
 
 
99
 
 
100
  # set enabled/disabled
 
101
  def set_enabled(self, new_value):
 
102
    self.disabled = not new_value
 
103
    # enable, remove all "#" from the start of the line
 
104
    if new_value == True:
 
105
      i=0
 
106
      self.line = string.lstrip(self.line)
 
107
      while self.line[i] == "#":
 
108
        i += 1
 
109
      self.line = self.line[i:]
 
110
    else:
 
111
      # disabled, add a "#" 
 
112
      if string.strip(self.line)[0] != "#":
 
113
        self.line = "#" + self.line
 
114
 
 
115
  
 
116
  def __init__(self, line):
 
117
    self.invalid = False
 
118
    self.disabled = False
 
119
    self.type = ""
 
120
    self.uri = ""
 
121
    self.dist = ""
 
122
    self.comps = []
 
123
    self.comment = ""
 
124
    self.line = line
 
125
    self.parse(line)
 
126
 
 
127
 
 
128
  def str(self):
 
129
    return self.line
 
130
 
 
131
 
 
132
def uniq(s):
 
133
  """ simple (and not efficient) way to return uniq list """
 
134
  u = []
 
135
  for x in s:
 
136
    if x not in u:
 
137
      u.append(x)
 
138
  return u 
 
139
  
 
140
 
 
141
# the SourceList file as a class
 
142
class SourcesList:
 
143
  def __init__(self, file):
 
144
    self.list = []      # of Type SourceEntries
 
145
    self.load(file)
 
146
 
 
147
  def is_mirror(self, add_uri, orig_uri):
 
148
    """check if the given add_url is idential or a mirror of orig_uri
 
149
       e.g. add_uri = archive.ubuntu.com
 
150
            orig_uri = de.archive.ubuntu.com
 
151
            -> True
 
152
    """
 
153
    # remove traling spaces and "/"
 
154
    add_uri = add_uri.rstrip("/ ")
 
155
    orig_uri = orig_uri.rstrip("/ ")
 
156
    # uri is identical
 
157
    if add_uri == orig_uri:
 
158
      #print "Identical"
 
159
      return True
 
160
    # add uri is a master site and orig_uri has the from "XX.mastersite"
 
161
    # (e.g. de.archive.ubuntu.com)
 
162
    try:
 
163
      add_srv = add_uri.split("//")[1]
 
164
      orig_srv = orig_uri.split("//")[1]
 
165
      #print "%s == %s " % (add_srv, orig_srv)
 
166
    except IndexError: # ok, somethings wrong here
 
167
      #print "IndexError"
 
168
      return False
 
169
    if add_srv == orig_srv[3:]:
 
170
      #print "Mirror"
 
171
      return True
 
172
    return False
 
173
 
 
174
  def add(self, type, uri, dist, comps, comment="", pos=-1):
 
175
    # if there is a repo with the same (type, uri, dist) just add the
 
176
    # components
 
177
    for i in self.list:
 
178
      if i.type == type and self.is_mirror(uri,i.uri) and i.dist == dist:
 
179
        comps = uniq(i.comps + comps)
 
180
        # preserver mirror
 
181
        uri = i.uri
 
182
        # set to the old position and preserve comment
 
183
        comment = i.comment
 
184
        pos = self.list.index(i)
 
185
        self.list.remove(i)
 
186
    line = "%s %s %s" % (type,uri,dist)
 
187
    for c in comps:
 
188
      line = line + " " + c;
 
189
    if comment != "":
 
190
      line = "%s #%s\n" %(line,comment)
 
191
    line = line + "\n"
 
192
    self.list.insert(pos, SourceEntry(line))
 
193
 
 
194
  def remove(self, source_entry):
 
195
    self.list.remove(source_entry)
 
196
 
 
197
  def load(self,file):
 
198
    f = open(file, "r")
 
199
    lines = f.readlines()
 
200
    for line in lines:
 
201
      source = SourceEntry(line)
 
202
      self.list.append(source)
 
203
    f.close()
 
204
 
 
205
  def save(self,file):
 
206
    f=open(file,"w")
 
207
    for source in self.list:
 
208
      f.write(source.str())
 
209
    f.close()
 
210
 
 
211
 
 
212
# templates for the add dialog
 
213
class SourceEntryTemplate(SourceEntry):
 
214
  def __init__(self,a_type,uri,dist,description,comps):
 
215
    self.comps = []
 
216
    self.comps_descriptions = []
 
217
    self.type = a_type
 
218
    self.uri = uri
 
219
    self.dist = dist
 
220
    self.description = description
 
221
    self.comps = comps
 
222
 
 
223
class SourceCompTemplate:
 
224
  def __init__(self, name, description, on_by_default):
 
225
    self.name = name
 
226
    self.description = description
 
227
    self.on_by_default = on_by_default
 
228
 
 
229
class SourceEntryTemplates:
 
230
  def __init__(self):
 
231
    _ = gettext.gettext
 
232
    self.templates = []
 
233
 
 
234
    # ubuntu components templates
 
235
    ubuntu_comps = []
 
236
    ubuntu_comps.append(SourceCompTemplate("main",_("Officially supported"),True))
 
237
    ubuntu_comps.append(SourceCompTemplate("restricted",_("Restricted copyright"),True))
 
238
    ubuntu_comps.append(SourceCompTemplate("universe",_("Community maintained (Universe)"),False))
 
239
    ubuntu_comps.append(SourceCompTemplate("multiverse",_("Non-free (Multiverse)"),False))
 
240
    
 
241
    # ubuntu distro
 
242
    self.templates.append(SourceEntryTemplate("deb",
 
243
                                         "http://archive.ubuntu.com/ubuntu/",
 
244
                                         "breezy",
 
245
                                         "Ubuntu 5.10 \"Breezy Badger\"",
 
246
                                         ubuntu_comps ))
 
247
    self.templates.append(SourceEntryTemplate("deb",
 
248
                                         "http://security.ubuntu.com/ubuntu/",
 
249
                                         "breezy-security",
 
250
                                         _("Ubuntu 5.10 Security Updates"),
 
251
                                         ubuntu_comps))
 
252
    self.templates.append(SourceEntryTemplate("deb",
 
253
                                         "http://archive.ubuntu.com/ubuntu/",
 
254
                                          "breezy-updates",
 
255
                                          _("Ubuntu 5.10 Updates"),
 
256
                                          ubuntu_comps))
 
257
                                             
 
258
 
 
259
# matcher class to make a source entry look nice
 
260
# lots of predefined matchers to make it i18n/gettext friendly
 
261
class SourceEntryMatcher:
 
262
  class MatchType:
 
263
    def __init__(self, a_type,a_descr):
 
264
      self.type = a_type
 
265
      self.description = a_descr
 
266
  
 
267
  class MatchDist:
 
268
    def __init__(self,a_uri,a_dist, a_descr,l_comps, l_comps_descr):
 
269
      self.uri = a_uri
 
270
      self.dist = a_dist
 
271
      self.description = a_descr
 
272
      self.comps = l_comps
 
273
      self.comps_descriptions = l_comps_descr
 
274
 
 
275
  def __init__(self):
 
276
    _ = gettext.gettext
 
277
    self.type_list = []
 
278
    self.type_list.append(self.MatchType("^deb$",_("Binary")))
 
279
    self.type_list.append(self.MatchType("^deb-src$",_("Source")))
 
280
 
 
281
    self.dist_list = []
 
282
 
 
283
    ubuntu_comps = ["^main$","^restricted$","^universe$","^multiverse$"]
 
284
    ubuntu_comps_descr = [_("Officially supported"),
 
285
                          _("Restricted copyright"),
 
286
                          _("Community maintained (Universe)"),
 
287
                          _("Non-free (Multiverse)")]
 
288
    # CDs
 
289
    self.dist_list.append(self.MatchDist("cdrom:\[Ubuntu.*4.10",
 
290
                                         ".*",
 
291
                                        _("CD") +
 
292
                                         " Ubuntu 4.10 \"Warty Warthog\"",
 
293
                                         ubuntu_comps, ubuntu_comps_descr))
 
294
    self.dist_list.append(self.MatchDist("cdrom:\[Ubuntu.*5.04",
 
295
                                         ".*",
 
296
                                         _("CD") +
 
297
                                         " Ubuntu 5.04 \"Hoary Hedgehog\"",
 
298
                                         ubuntu_comps, ubuntu_comps_descr))
 
299
    self.dist_list.append(self.MatchDist("cdrom:\[Ubuntu.*5.10",
 
300
                                         ".*",
 
301
                                         _("CD") +
 
302
                                         " Ubuntu 5.10 \"Breezy Badger\"",
 
303
                                         ubuntu_comps, ubuntu_comps_descr))
 
304
 
 
305
    # URIs
 
306
    # normal archive
 
307
    self.dist_list.append(self.MatchDist(".*archive.ubuntu.com/ubuntu",
 
308
                                         "^warty$",
 
309
                                         "Ubuntu 4.10 \"Warty Warthog\"",
 
310
                                         ubuntu_comps, ubuntu_comps_descr))
 
311
    self.dist_list.append(self.MatchDist(".*archive.ubuntu.com/ubuntu",
 
312
                                         "^hoary$",
 
313
                                         "Ubuntu 5.04 \"Hoary Hedgehog\"",
 
314
                                         ubuntu_comps, ubuntu_comps_descr))
 
315
    self.dist_list.append(self.MatchDist(".*archive.ubuntu.com/ubuntu",
 
316
                                         "^breezy$",
 
317
                                         "Ubuntu 5.10 \"Breezy Badger\"",
 
318
                                         ubuntu_comps, ubuntu_comps_descr))
 
319
    # updates
 
320
    self.dist_list.append(self.MatchDist(".*archive.ubuntu.com/ubuntu",
 
321
                                         "^hoary-updates$",
 
322
                                         _("Ubuntu 5.04 Updates"),
 
323
                                         ubuntu_comps, ubuntu_comps_descr))
 
324
    self.dist_list.append(self.MatchDist(".*archive.ubuntu.com/ubuntu",
 
325
                                         "^breezy-updates$",
 
326
                                         _("Ubuntu 5.10 Updates"),
 
327
                                         ubuntu_comps, ubuntu_comps_descr))
 
328
 
 
329
    # security
 
330
    self.dist_list.append(self.MatchDist(".*security.ubuntu.com/ubuntu",
 
331
                                         "^warty-security$",
 
332
                                         _("Ubuntu 4.10 Security Updates"),
 
333
                                         ubuntu_comps, ubuntu_comps_descr))
 
334
    self.dist_list.append(self.MatchDist(".*security.ubuntu.com/ubuntu",
 
335
                                         "^hoary-security$",
 
336
                                         _("Ubuntu 5.04 Security Updates"),
 
337
                                         ubuntu_comps, ubuntu_comps_descr))
 
338
    self.dist_list.append(self.MatchDist(".*security.ubuntu.com/ubuntu",
 
339
                                         "^breezy-security$",
 
340
                                         _("Ubuntu 5.10 Security Updates"),
 
341
                                         ubuntu_comps, ubuntu_comps_descr))
 
342
    # security (normal archive uri)
 
343
    self.dist_list.append(self.MatchDist(".*archive.ubuntu.com/ubuntu",
 
344
                                         "^warty-security$",
 
345
                                         _("Ubuntu 4.10 Security Updates"),
 
346
                                         ubuntu_comps, ubuntu_comps_descr))
 
347
    self.dist_list.append(self.MatchDist(".*archive.ubuntu.com/ubuntu",
 
348
                                         "^hoary-security$",
 
349
                                         _("Ubuntu 5.04 Security Updates"),
 
350
                                         ubuntu_comps, ubuntu_comps_descr))
 
351
    self.dist_list.append(self.MatchDist(".*archive.ubuntu.com/ubuntu",
 
352
                                         "^breezy-security$",
 
353
                                         _("Ubuntu 5.10 Security Updates"),
 
354
                                         ubuntu_comps, ubuntu_comps_descr))
 
355
 
 
356
 
 
357
    # DEBIAN
 
358
    debian_comps =  ["^main$","^contrib$","^non-free$","^non-US$"]
 
359
    debian_comps_descr = [_("Officially supported"),
 
360
                          _("Contributed software"),
 
361
                          _("Non-free software"),
 
362
                          _("US export restricted software")
 
363
                          ]
 
364
 
 
365
    # dists by name
 
366
    self.dist_list.append(self.MatchDist(".*debian.org/debian",
 
367
                                         "^sarge$",
 
368
                                         "Debian 3.1 \"Sarge\"",
 
369
                                         debian_comps, debian_comps_descr))
 
370
    self.dist_list.append(self.MatchDist(".*debian.org/debian",
 
371
                                         "^woody$",
 
372
                                         "Debian 3.0 \"Woody\"",
 
373
                                         debian_comps, debian_comps_descr))
 
374
    # securtiy
 
375
    self.dist_list.append(self.MatchDist(".*security.debian.org",
 
376
                                         "^stable.*$",
 
377
                                         _("Debian Stable Security Updates"),
 
378
                                         debian_comps, debian_comps_descr))
 
379
    # dists by status
 
380
    self.dist_list.append(self.MatchDist(".*debian.org/debian",
 
381
                                         "^stable$",
 
382
                                         "Debian Stable",
 
383
                                         debian_comps, debian_comps_descr))
 
384
    self.dist_list.append(self.MatchDist(".*debian.org/debian",
 
385
                                         "^testing$",
 
386
                                         "Debian Testing",
 
387
                                         debian_comps, debian_comps_descr))
 
388
    self.dist_list.append(self.MatchDist(".*debian.org/debian",
 
389
                                         "^unstable$",
 
390
                                         "Debian Unstable \"Sid\"",
 
391
                                         debian_comps, debian_comps_descr))
 
392
 
 
393
    # non-us
 
394
    self.dist_list.append(self.MatchDist(".*debian.org/debian-non-US",
 
395
                                         "^stable.*$",
 
396
                                         "Debian Non-US (Stable)",
 
397
                                         debian_comps, debian_comps_descr))
 
398
    self.dist_list.append(self.MatchDist(".*debian.org/debian-non-US",
 
399
                                         "^testing.*$",
 
400
                                         "Debian Non-US (Testing)",
 
401
                                         debian_comps, debian_comps_descr))
 
402
    self.dist_list.append(self.MatchDist(".*debian.org/debian-non-US",
 
403
                                         "^unstable.*$",
 
404
                                         "Debian Non-US (Unstable)",
 
405
                                         debian_comps, debian_comps_descr))
 
406
 
 
407
 
 
408
 
 
409
  
 
410
  def match(self,source):
 
411
    _ = gettext.gettext
 
412
    # some sane defaults first
 
413
    type_description = source.type
 
414
    dist_description = source.uri + " " + source.dist
 
415
    comp_description = ""
 
416
    for c in source.comps:
 
417
      comp_description = comp_description + " " + c 
 
418
    
 
419
    for t in self.type_list:
 
420
      if re.match(t.type, source.type):
 
421
        type_description = _(t.description)
 
422
        break
 
423
 
 
424
    for d in self.dist_list:
 
425
      #print "'%s'" %source.uri
 
426
      if re.match(d.uri, source.uri) and re.match(d.dist,source.dist):
 
427
        dist_description = d.description
 
428
        comp_description = ""
 
429
        for c in source.comps:
 
430
          found = False
 
431
          for i in range(len(d.comps)):
 
432
            if re.match(d.comps[i], c):
 
433
              comp_description = comp_description+"\n"+d.comps_descriptions[i]
 
434
              found = True
 
435
          if found == False:
 
436
            comp_description = comp_description+" "+c
 
437
        break
 
438
      
 
439
      
 
440
    return (type_description,dist_description,comp_description)
 
441
    
 
442