home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / python2.6 / dist-packages / aptsources / distinfo.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  10.3 KB  |  307 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import os
  5. import gettext
  6. from os import getenv
  7. import ConfigParser
  8. import re
  9. import apt_pkg
  10.  
  11. def _(s):
  12.     return gettext.dgettext('python-apt', s)
  13.  
  14.  
  15. class Template:
  16.     
  17.     def __init__(self):
  18.         self.name = None
  19.         self.child = False
  20.         self.parents = []
  21.         self.match_name = None
  22.         self.description = None
  23.         self.base_uri = None
  24.         self.type = None
  25.         self.components = []
  26.         self.children = []
  27.         self.match_uri = None
  28.         self.mirror_set = { }
  29.         self.distribution = None
  30.         self.available = True
  31.  
  32.     
  33.     def has_component(self, comp):
  34.         ''' Check if the distribution provides the given component '''
  35.         return comp in map((lambda c: c.name), self.components)
  36.  
  37.     
  38.     def is_mirror(self, url):
  39.         ''' Check if a given url of a repository is a valid mirror '''
  40.         (proto, hostname, dir) = split_url(url)
  41.         if hostname in self.mirror_set:
  42.             return self.mirror_set[hostname].has_repository(proto, dir)
  43.         return False
  44.  
  45.  
  46.  
  47. class Component:
  48.     
  49.     def __init__(self, name, desc = None, long_desc = None):
  50.         self.name = name
  51.         self.description = desc
  52.         self.description_long = long_desc
  53.  
  54.     
  55.     def get_description(self):
  56.         if self.description_long is not None:
  57.             return self.description_long
  58.         if self.description is not None:
  59.             return self.description
  60.         return None
  61.  
  62.     
  63.     def set_description(self, desc):
  64.         self.description = desc
  65.  
  66.     
  67.     def set_description_long(self, desc):
  68.         self.description_long = desc
  69.  
  70.     
  71.     def get_description_long(self):
  72.         return self.description_long
  73.  
  74.  
  75.  
  76. class Mirror:
  77.     ''' Storage for mirror related information '''
  78.     
  79.     def __init__(self, proto, hostname, dir, location = None):
  80.         self.hostname = hostname
  81.         self.repositories = []
  82.         self.add_repository(proto, dir)
  83.         self.location = location
  84.  
  85.     
  86.     def add_repository(self, proto, dir):
  87.         self.repositories.append(Repository(proto, dir))
  88.  
  89.     
  90.     def get_repositories_for_proto(self, proto):
  91.         return (filter,)((lambda r: r.proto == proto), self.repositories)
  92.  
  93.     
  94.     def has_repository(self, proto, dir):
  95.         if dir is None:
  96.             return False
  97.         for r in self.repositories:
  98.             if r.proto == proto and dir in r.dir:
  99.                 return True
  100.         
  101.         return False
  102.  
  103.     
  104.     def get_repo_urls(self):
  105.         return (map,)((lambda r: r.get_url(self.hostname)), self.repositories)
  106.  
  107.     
  108.     def get_location(self):
  109.         return self.location
  110.  
  111.     
  112.     def set_location(self, location):
  113.         self.location = location
  114.  
  115.  
  116.  
  117. class Repository:
  118.     
  119.     def __init__(self, proto, dir):
  120.         self.proto = proto
  121.         self.dir = dir
  122.  
  123.     
  124.     def get_info(self):
  125.         return (self.proto, self.dir)
  126.  
  127.     
  128.     def get_url(self, hostname):
  129.         return '%s://%s/%s' % (self.proto, hostname, self.dir)
  130.  
  131.  
  132.  
  133. def split_url(url):
  134.     ''' split a given URL into the protocoll, the hostname and the dir part '''
  135.     return map((lambda a, b: a), re.split(':*\\/+', url, maxsplit = 2), [
  136.         None,
  137.         None,
  138.         None])
  139.  
  140.  
  141. class DistInfo:
  142.     
  143.     def __init__(self, dist = None, base_dir = '/usr/share/python-apt/templates'):
  144.         self.metarelease_uri = ''
  145.         self.templates = []
  146.         self.arch = apt_pkg.Config.Find('APT::Architecture')
  147.         location = None
  148.         match_loc = re.compile('^#LOC:(.+)$')
  149.         match_mirror_line = re.compile('^(#LOC:.+)|(((http)|(ftp)|(rsync)|(file)|(https))://[A-Za-z0-9/\\.:\\-_@]+)$')
  150.         if not dist:
  151.             pipe = os.popen('lsb_release -i -s')
  152.             dist = pipe.read().strip()
  153.             pipe.close()
  154.             del pipe
  155.         
  156.         self.dist = dist
  157.         map_mirror_sets = { }
  158.         dist_fname = '%s/%s.info' % (base_dir, dist)
  159.         dist_file = open(dist_fname)
  160.         if not dist_file:
  161.             return None
  162.         template = None
  163.         component = None
  164.         for line in dist_file:
  165.             tokens = line.split(':', 1)
  166.             if len(tokens) < 2:
  167.                 continue
  168.             
  169.             field = tokens[0].strip()
  170.             value = tokens[1].strip()
  171.             if field == 'ChangelogURI':
  172.                 self.changelogs_uri = _(value)
  173.                 continue
  174.             if field == 'MetaReleaseURI':
  175.                 self.metarelease_uri = value
  176.                 continue
  177.             if field == 'Suite':
  178.                 self.finish_template(template, component)
  179.                 component = None
  180.                 template = Template()
  181.                 template.name = value
  182.                 template.distribution = dist
  183.                 template.match_name = '^%s$' % value
  184.                 continue
  185.             if field == 'MatchName':
  186.                 template.match_name = value
  187.                 continue
  188.             if field == 'ParentSuite':
  189.                 template.child = True
  190.                 for nanny in self.templates:
  191.                     if nanny.name == value:
  192.                         template.parents.append(nanny)
  193.                         nanny.children.append(template)
  194.                         continue
  195.                 
  196.             if field == 'Available':
  197.                 template.available = value
  198.                 continue
  199.             if field == 'RepositoryType':
  200.                 template.type = value
  201.                 continue
  202.             if field == 'BaseURI' and not (template.base_uri):
  203.                 template.base_uri = value
  204.                 continue
  205.             if field == 'BaseURI-%s' % self.arch:
  206.                 template.base_uri = value
  207.                 continue
  208.             if field == 'MatchURI' and not (template.match_uri):
  209.                 template.match_uri = value
  210.                 continue
  211.             if field == 'MatchURI-%s' % self.arch:
  212.                 template.match_uri = value
  213.                 continue
  214.             if field == 'MirrorsFile' or field == 'MirrorsFile-%s' % self.arch:
  215.                 if value not in map_mirror_sets:
  216.                     mirror_set = { }
  217.                     
  218.                     try:
  219.                         mirror_data = []([], [ x.strip() for x in open(value) ])
  220.                     except Exception:
  221.                         print 'WARNING: Failed to read mirror file'
  222.                         mirror_data = []
  223.  
  224.                     for line in mirror_data:
  225.                         if line.startswith('#LOC:'):
  226.                             location = match_loc.sub('\\1', line)
  227.                             continue
  228.                         
  229.                         (proto, hostname, dir) = split_url(line)
  230.                         if hostname in mirror_set:
  231.                             mirror_set[hostname].add_repository(proto, dir)
  232.                             continue
  233.                         mirror_set[hostname] = Mirror(proto, hostname, dir, location)
  234.                     
  235.                     map_mirror_sets[value] = mirror_set
  236.                 
  237.                 template.mirror_set = map_mirror_sets[value]
  238.                 continue
  239.             if field == 'Description':
  240.                 template.description = _(value)
  241.                 continue
  242.             if field == 'Component':
  243.                 if component and not template.has_component(component.name):
  244.                     template.components.append(component)
  245.                 
  246.                 component = Component(value)
  247.                 continue
  248.             if field == 'CompDescription':
  249.                 component.set_description(_(value))
  250.                 continue
  251.             if field == 'CompDescriptionLong':
  252.                 component.set_description_long(_(value))
  253.                 continue
  254.         
  255.         self.finish_template(template, component)
  256.         template = None
  257.         component = None
  258.  
  259.     
  260.     def finish_template(self, template, component):
  261.         ''' finish the current tempalte '''
  262.         if not template:
  263.             return None
  264.         if template.match_uri is None and template.child:
  265.             for t in template.parents:
  266.                 if t.match_uri:
  267.                     template.match_uri = t.match_uri
  268.                     break
  269.                     continue
  270.                 template
  271.             
  272.         
  273.         if template.mirror_set == { } and template.child:
  274.             for t in template.parents:
  275.                 if t.match_uri:
  276.                     template.mirror_set = t.mirror_set
  277.                     break
  278.                     continue
  279.             
  280.         
  281.         if component and not template.has_component(component.name):
  282.             template.components.append(component)
  283.             component = None
  284.         
  285.         self.templates.append(template)
  286.  
  287.  
  288. if __name__ == '__main__':
  289.     d = DistInfo('Ubuntu', '/usr/share/python-apt/templates')
  290.     print d.changelogs_uri
  291.     for template in d.templates:
  292.         print '\nSuite: %s' % template.name
  293.         print 'Desc: %s' % template.description
  294.         print 'BaseURI: %s' % template.base_uri
  295.         print 'MatchURI: %s' % template.match_uri
  296.         if template.mirror_set != { }:
  297.             print 'Mirrors: %s' % template.mirror_set.keys()
  298.         
  299.         for comp in template.components:
  300.             print ' %s -%s -%s' % (comp.name, comp.description, comp.description_long)
  301.         
  302.         for child in template.children:
  303.             print '  %s' % child.description
  304.         
  305.     
  306.  
  307.