home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / checkbox / registries / packages.py < prev    next >
Encoding:
Python Source  |  2009-04-27  |  2.4 KB  |  82 lines

  1. #
  2. # This file is part of Checkbox.
  3. #
  4. # Copyright 2008 Canonical Ltd.
  5. #
  6. # Checkbox is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # Checkbox 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. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with Checkbox.  If not, see <http://www.gnu.org/licenses/>.
  18. #
  19. from checkbox.lib.cache import cache
  20.  
  21. from checkbox.properties import String
  22. from checkbox.registry import Registry
  23. from checkbox.registries.command import CommandRegistry
  24. from checkbox.registries.link import LinkRegistry
  25.  
  26.  
  27. COLUMNS = ["name", "version"]
  28.  
  29.  
  30. class PackageRegistry(Registry):
  31.  
  32.     def __init__(self, package):
  33.         super(PackageRegistry, self).__init__()
  34.         self._package = package
  35.  
  36.     def __str__(self):
  37.         strings = ["%s: %s" % (k, v) for k, v in self._package.items()]
  38.  
  39.         return "\n".join(strings)
  40.  
  41.     def items(self):
  42.         items = [(k, v) for k, v in self._package.items()]
  43.         items.append(("package", LinkRegistry(self)))
  44.  
  45.         return items
  46.  
  47.  
  48. class PackagesRegistry(CommandRegistry):
  49.     """Registry for packages."""
  50.  
  51.     # Command to retrieve packages.
  52.     command = String(default="COLUMNS=200 dpkg -l")
  53.  
  54.     @cache
  55.     def items(self):
  56.         items = []
  57.         for line in [l for l in self.split("\n") if l]:
  58.             # Determine the lengths of dpkg columns and
  59.             # strip status column.
  60.             if line.startswith("+++"):
  61.                 lengths = [len(i) for i in line.split("-")]
  62.                 lengths[0] += 1
  63.                 for i in range(1, len(lengths)):
  64.                     lengths[i] += lengths[i - 1] + 1
  65.  
  66.             # Parse information from installed packages.
  67.             if line.startswith("ii"):
  68.                 package = {}
  69.                 for i in range(len(COLUMNS)):
  70.                     key = COLUMNS[i]
  71.                     value = line[lengths[i]:lengths[i+1]-1].strip()
  72.                     package[key] = value
  73.  
  74.                 key = package["name"]
  75.                 value = PackageRegistry(package)
  76.                 items.append((key, value))
  77.  
  78.         return items
  79.  
  80.  
  81. factory = PackagesRegistry
  82.