packagelist.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. """parse Package-List field
  2. @copyright: 2014, Ansgar Burchardt <ansgar@debian.org>
  3. @license: GPL-2+
  4. """
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 2 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  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 USA
  18. from daklib.architecture import match_architecture
  19. from daklib.utils import extract_component_from_section
  20. class InvalidSource(Exception):
  21. pass
  22. class PackageListEntry(object):
  23. def __init__(self, name, package_type, section, component, priority, **other):
  24. self.name = name
  25. self.type = package_type
  26. self.section = section
  27. self.component = component
  28. self.priority = priority
  29. self.other = other
  30. self.architectures = self._architectures()
  31. def _architectures(self):
  32. archs = self.other.get("arch", None)
  33. if archs is None:
  34. return None
  35. return archs.split(',')
  36. def built_on_architecture(self, architecture):
  37. archs = self.architectures
  38. if archs is None:
  39. return None
  40. for arch in archs:
  41. if match_architecture(architecture, arch):
  42. return True
  43. return False
  44. def built_in_suite(self, suite):
  45. built = False
  46. for arch in suite.architectures:
  47. if arch.arch_string == 'source':
  48. continue
  49. built_on_arch = self.built_on_architecture(arch.arch_string)
  50. if built_on_arch:
  51. return True
  52. if built_on_arch is None:
  53. built = None
  54. return built
  55. class PackageList(object):
  56. def __init__(self, source):
  57. if 'Package-List' in source:
  58. self._parse(source)
  59. elif 'Binary' in source:
  60. self._parse_fallback(source)
  61. else:
  62. raise InvalidSource('Source package has neither Package-List nor Binary field.')
  63. self.fallback = any(entry.architectures is None for entry in self.package_list)
  64. def _binaries(self, source):
  65. return set(name.strip() for name in source['Binary'].split(","))
  66. def _parse(self, source):
  67. self.package_list = []
  68. binaries_binary = self._binaries(source)
  69. binaries_package_list = set()
  70. for line in source['Package-List'].split("\n"):
  71. if not line:
  72. continue
  73. fields = line.split()
  74. if len(fields) < 4:
  75. raise InvalidSource("Package-List entry has less than four fields.")
  76. # <name> <type> <component/section> <priority> [arch=<arch>[,<arch>]...]
  77. name = fields[0]
  78. package_type = fields[1]
  79. section, component = extract_component_from_section(fields[2])
  80. priority = fields[3]
  81. other = dict(kv.split('=', 1) for kv in fields[4:])
  82. if name in binaries_package_list:
  83. raise InvalidSource("Package-List has two entries for '{0}'.".format(name))
  84. if name not in binaries_binary:
  85. raise InvalidSource("Package-List lists {0} which is not listed in Binary.".format(name))
  86. binaries_package_list.add(name)
  87. entry = PackageListEntry(name, package_type, section, component, priority, **other)
  88. self.package_list.append(entry)
  89. if len(binaries_binary) != len(binaries_package_list):
  90. raise InvalidSource("Package-List and Binaries fields have a different number of entries.")
  91. def _parse_fallback(self, source):
  92. self.package_list = []
  93. for binary in self._binaries(source):
  94. name = binary
  95. package_type = None
  96. component = None
  97. section = None
  98. priority = None
  99. other = dict()
  100. entry = PackageListEntry(name, package_type, section, component, priority, **other)
  101. self.package_list.append(entry)
  102. def packages_for_suite(self, suite):
  103. packages = []
  104. for entry in self.package_list:
  105. built = entry.built_in_suite(suite)
  106. if built or built is None:
  107. packages.append(entry)
  108. return packages
  109. def has_arch_indep_packages(self):
  110. has_arch_indep = False
  111. for entry in self.package_list:
  112. built = entry.built_on_architecture('all')
  113. if built:
  114. return True
  115. if built is None:
  116. has_arch_indep = None
  117. return has_arch_indep
  118. def has_arch_dep_packages(self):
  119. has_arch_dep = False
  120. for entry in self.package_list:
  121. built_on_all = entry.built_on_architecture('all')
  122. if built_on_all is False:
  123. return True
  124. if built_on_all is None:
  125. has_arch_dep = None
  126. return has_arch_dep