packagelist.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. from collections.abc import Mapping
  21. from typing import Optional
  22. class InvalidSource(Exception):
  23. pass
  24. class PackageListEntry:
  25. def __init__(self, name, package_type, section, component, priority, **other):
  26. self.name = name
  27. self.type = package_type
  28. self.section = section
  29. self.component = component
  30. self.priority = priority
  31. self.other = other
  32. self.architectures = self._architectures()
  33. def _architectures(self) -> Optional[list[str]]:
  34. archs = self.other.get("arch", None)
  35. if archs is None:
  36. return None
  37. return archs.split(',')
  38. def built_on_architecture(self, architecture: str) -> Optional[bool]:
  39. archs = self.architectures
  40. if archs is None:
  41. return None
  42. for arch in archs:
  43. if match_architecture(architecture, arch):
  44. return True
  45. return False
  46. def built_in_suite(self, suite) -> Optional[bool]:
  47. built: Optional[bool] = False
  48. for arch in suite.architectures:
  49. if arch.arch_string == 'source':
  50. continue
  51. built_on_arch = self.built_on_architecture(arch.arch_string)
  52. if built_on_arch:
  53. return True
  54. if built_on_arch is None:
  55. built = None
  56. return built
  57. def built_in_default_profile(self) -> bool:
  58. # See man:dsc(5) and https://bugs.debian.org/913965#77
  59. profiles_and = self.other.get('profile')
  60. if profiles_and is None:
  61. return True
  62. return all(
  63. any(profile.startswith("!") for profile in profiles_or.split("+"))
  64. for profiles_or in profiles_and.split(",")
  65. )
  66. class PackageList:
  67. def __init__(self, source: Mapping[str, str]):
  68. if 'Package-List' in source:
  69. self.package_list = self._parse(source)
  70. elif 'Binary' in source:
  71. self.package_list = self._parse_fallback(source)
  72. else:
  73. raise InvalidSource('Source package has neither Package-List nor Binary field.')
  74. self.fallback = any(entry.architectures is None for entry in self.package_list)
  75. def _binaries(self, source: Mapping[str, str]) -> set[str]:
  76. return set(name.strip() for name in source['Binary'].split(","))
  77. def _parse(self, source: Mapping[str, str]) -> list[PackageListEntry]:
  78. package_list = []
  79. binaries_binary = self._binaries(source)
  80. binaries_package_list = set()
  81. for line in source['Package-List'].split("\n"):
  82. if not line:
  83. continue
  84. fields = line.split()
  85. if len(fields) < 4:
  86. raise InvalidSource("Package-List entry has less than four fields.")
  87. # <name> <type> <component/section> <priority> [arch=<arch>[,<arch>]...]
  88. name = fields[0]
  89. package_type = fields[1]
  90. section, component = extract_component_from_section(fields[2])
  91. priority = fields[3]
  92. other = dict(kv.split('=', 1) for kv in fields[4:])
  93. if name in binaries_package_list:
  94. raise InvalidSource("Package-List has two entries for '{0}'.".format(name))
  95. if name not in binaries_binary:
  96. raise InvalidSource("Package-List lists {0} which is not listed in Binary.".format(name))
  97. binaries_package_list.add(name)
  98. entry = PackageListEntry(name, package_type, section, component, priority, **other)
  99. package_list.append(entry)
  100. if len(binaries_binary) != len(binaries_package_list):
  101. raise InvalidSource("Package-List and Binaries fields have a different number of entries.")
  102. return package_list
  103. def _parse_fallback(self, source: Mapping[str, str]) -> list[PackageListEntry]:
  104. package_list = []
  105. for binary in self._binaries(source):
  106. name = binary
  107. package_type = None
  108. component = None
  109. section = None
  110. priority = None
  111. other = dict()
  112. entry = PackageListEntry(name, package_type, section, component, priority, **other)
  113. package_list.append(entry)
  114. return package_list
  115. def packages_for_suite(self, suite, only_default_profile=True) -> list[PackageListEntry]:
  116. packages = []
  117. for entry in self.package_list:
  118. if only_default_profile and not entry.built_in_default_profile():
  119. continue
  120. built = entry.built_in_suite(suite)
  121. if built or built is None:
  122. packages.append(entry)
  123. return packages
  124. def has_arch_indep_packages(self) -> Optional[bool]:
  125. has_arch_indep: Optional[bool] = False
  126. for entry in self.package_list:
  127. built = entry.built_on_architecture('all')
  128. if built:
  129. return True
  130. if built is None:
  131. has_arch_indep = None
  132. return has_arch_indep
  133. def has_arch_dep_packages(self) -> Optional[bool]:
  134. has_arch_dep: Optional[bool] = False
  135. for entry in self.package_list:
  136. built_on_all = entry.built_on_architecture('all')
  137. if built_on_all is False:
  138. return True
  139. if built_on_all is None:
  140. has_arch_dep = None
  141. return has_arch_dep