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