packagelist.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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:
  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. def built_in_default_profile(self):
  56. # See man:dsc(5) and https://bugs.debian.org/913965#77
  57. profiles_and = self.other.get('profile')
  58. if profiles_and is None:
  59. return True
  60. return all(
  61. any(profile.startswith("!") for profile in profiles_or.split("+"))
  62. for profiles_or in profiles_and.split(",")
  63. )
  64. class PackageList:
  65. def __init__(self, source):
  66. if 'Package-List' in source:
  67. self._parse(source)
  68. elif 'Binary' in source:
  69. self._parse_fallback(source)
  70. else:
  71. raise InvalidSource('Source package has neither Package-List nor Binary field.')
  72. self.fallback = any(entry.architectures is None for entry in self.package_list)
  73. def _binaries(self, source):
  74. return set(name.strip() for name in source['Binary'].split(","))
  75. def _parse(self, source):
  76. self.package_list = []
  77. binaries_binary = self._binaries(source)
  78. binaries_package_list = set()
  79. for line in source['Package-List'].split("\n"):
  80. if not line:
  81. continue
  82. fields = line.split()
  83. if len(fields) < 4:
  84. raise InvalidSource("Package-List entry has less than four fields.")
  85. # <name> <type> <component/section> <priority> [arch=<arch>[,<arch>]...]
  86. name = fields[0]
  87. package_type = fields[1]
  88. section, component = extract_component_from_section(fields[2])
  89. priority = fields[3]
  90. other = dict(kv.split('=', 1) for kv in fields[4:])
  91. if name in binaries_package_list:
  92. raise InvalidSource("Package-List has two entries for '{0}'.".format(name))
  93. if name not in binaries_binary:
  94. raise InvalidSource("Package-List lists {0} which is not listed in Binary.".format(name))
  95. binaries_package_list.add(name)
  96. entry = PackageListEntry(name, package_type, section, component, priority, **other)
  97. self.package_list.append(entry)
  98. if len(binaries_binary) != len(binaries_package_list):
  99. raise InvalidSource("Package-List and Binaries fields have a different number of entries.")
  100. def _parse_fallback(self, source):
  101. self.package_list = []
  102. for binary in self._binaries(source):
  103. name = binary
  104. package_type = None
  105. component = None
  106. section = None
  107. priority = None
  108. other = dict()
  109. entry = PackageListEntry(name, package_type, section, component, priority, **other)
  110. self.package_list.append(entry)
  111. def packages_for_suite(self, suite, only_default_profile=True):
  112. packages = []
  113. for entry in self.package_list:
  114. if only_default_profile and not entry.built_in_default_profile():
  115. continue
  116. built = entry.built_in_suite(suite)
  117. if built or built is None:
  118. packages.append(entry)
  119. return packages
  120. def has_arch_indep_packages(self):
  121. has_arch_indep = False
  122. for entry in self.package_list:
  123. built = entry.built_on_architecture('all')
  124. if built:
  125. return True
  126. if built is None:
  127. has_arch_indep = None
  128. return has_arch_indep
  129. def has_arch_dep_packages(self):
  130. has_arch_dep = False
  131. for entry in self.package_list:
  132. built_on_all = entry.built_on_architecture('all')
  133. if built_on_all is False:
  134. return True
  135. if built_on_all is None:
  136. has_arch_dep = None
  137. return has_arch_dep