home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / lib / python2.4 / site-packages / UpdateManager / Common / DistInfo.py < prev    next >
Encoding:
Python Source  |  2006-08-24  |  5.2 KB  |  150 lines

  1. #!/usr/bin/env python
  2. # DistInfo.py - simple parser for a xml-based metainfo file
  3. #  
  4. #  Copyright (c) 2005 Gustavo Noronha Silva
  5. #  
  6. #  Author: Gustavo Noronha Silva <kov@debian.org>
  7. #  This program is free software; you can redistribute it and/or 
  8. #  modify it under the terms of the GNU General Public License as 
  9. #  published by the Free Software Foundation; either version 2 of the
  10. #  License, or (at your option) any later version.
  11. #  This program is distributed in the hope that it will be useful,
  12. #  but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. #  GNU General Public License for more details.
  15. #  You should have received a copy of the GNU General Public License
  16. #  along with this program; if not, write to the Free Software
  17. #  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  18. #  USA
  19.  
  20. import os
  21. import gettext
  22. from os import getenv
  23. import ConfigParser
  24.  
  25. _ = gettext.gettext
  26.  
  27. class Suite:
  28.     def __init__(self):
  29.         self.name = None
  30.         self.child = False
  31.         self.match_name = None
  32.         self.description = None
  33.         self.base_uri = None
  34.         self.type = None
  35.         self.components = {}
  36.         self.children = []
  37.         self.match_uri = None
  38.         self.distribution = None
  39.         self.available = True
  40.  
  41. class Component:
  42.     def __init__(self):
  43.         self.name = ""
  44.         self.description = ""
  45.         self.description_long = ""
  46.         self.enabled = None
  47.  
  48. class DistInfo:
  49.     def __init__(self,
  50.                  dist = None,
  51.                  base_dir = "/usr/share/update-manager/channels"):
  52.         self.metarelease_uri = ''
  53.         self.suites = []
  54.  
  55.         if not dist:
  56.             pipe = os.popen("lsb_release -i -s")
  57.             dist = pipe.read().strip()
  58.             pipe.close()
  59.             del pipe
  60.  
  61.         self.dist = dist
  62.  
  63.         dist_fname = "%s/%s.info" % (base_dir, dist)
  64.         dist_file = open (dist_fname)
  65.         if not dist_file:
  66.             return
  67.         suite = None
  68.         component = None
  69.         for line in dist_file:
  70.             tokens = line.split (':', 1)
  71.             if len (tokens) < 2:
  72.                 continue
  73.             field = tokens[0].strip ()
  74.             value = tokens[1].strip ()
  75.             if field == 'ChangelogURI':
  76.                 self.changelogs_uri = _(value)
  77.             elif field == 'MetaReleaseURI':
  78.                 self.metarelease_uri = value
  79.             elif field == 'Suite':
  80.                 if suite:
  81.                     if component:
  82.                         suite.components["%s" % component.name] = \
  83.                             (component.description, component.enabled,
  84.                              component.description_long)
  85.                         component = None
  86.                     self.suites.append (suite)
  87.                 suite = Suite ()
  88.                 suite.name = value
  89.                 suite.distribution = dist
  90.                 suite.match_name = "^%s$" % value
  91.             elif field == 'MatchName':
  92.                 suite.match_name = value
  93.             elif field == 'ParentSuite':
  94.                 suite.child = True
  95.                 for nanny in self.suites:
  96.                     if nanny.name == value:
  97.                         nanny.children.append(suite)
  98.             elif field == 'Available':
  99.                 suite.available = value
  100.             elif field == 'RepositoryType':
  101.                 suite.type = value
  102.             elif field == 'BaseURI':
  103.                 suite.base_uri = value
  104.                 suite.match_uri = value
  105.             elif field == 'MatchURI':
  106.                 suite.match_uri = value
  107.             elif field == 'Description':
  108.                 suite.description = _(value)
  109.             elif field == 'Component':
  110.                 if component:
  111.                     suite.components["%s" % component.name] = \
  112.                         (component.description, component.enabled,
  113.                          component.description_long)
  114.                 component = Component ()
  115.                 component.name = value
  116.             elif field == 'Enabled':
  117.                 component.enabled = bool(int(value))
  118.             elif field == 'CompDescription':
  119.                 component.description = _(value)
  120.             elif field == 'CompDescriptionLong':
  121.                 component.description_long = _(value)
  122.         if suite:
  123.             if component:
  124.                 suite.components["%s" % component.name] = \
  125.                     (component.description, component.enabled,
  126.                      component.description_long)
  127.                 component = None
  128.             self.suites.append (suite)
  129.             suite = None
  130.  
  131.  
  132. if __name__ == "__main__":
  133.     d = DistInfo ("Ubuntu", "../../channels")
  134.     print d.changelogs_uri
  135.     for suite in d.suites:
  136.         print "\nSuite: %s" % suite.name
  137.         print "Desc: %s" % suite.description
  138.         print "BaseURI: %s" % suite.base_uri
  139.         print "MatchURI: %s" % suite.match_uri
  140.         for component in suite.components:
  141.             print "  %s - %s - %s - %s" % (component, 
  142.                                        suite.components[component][0],
  143.                                        suite.components[component][1],
  144.                                        suite.components[component][2])
  145.         for child in suite.children:
  146.             print "  %s" % child.description
  147.