]> git.donarmstrong.com Git - dak.git/blob - daklib/packagelist.py
Add parser for Package-List field.
[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.package_type = package_type
31         self.section = section
32         self.component = component
33         self.priority = priority
34         self.other = other
35     @property
36     def architectures(self):
37         archs = self.other.get("arch", None)
38         if archs is None:
39             return None
40         return archs.split(',')
41     def built_on_architecture(self, architecture):
42         archs = self.architectures
43         if archs is None:
44             return None
45         for arch in archs:
46             if match_architecture(architecture, arch):
47                 return True
48         return False
49     def built_in_suite(self, suite):
50         built = False
51         for arch in suite.architectures:
52             built_on_arch = self.built_on_architecture(arch.arch_string)
53             if built_on_arch:
54                 return True
55             if built_on_arch is None:
56                 built = None
57         return built
58
59 class PackageList(object):
60     def __init__(self, source):
61         self._source = source
62         if 'Package-List' in self._source:
63             self._parse()
64         elif 'Binary' in self._source:
65             self._parse_fallback()
66         else:
67             raise InvalidSource('Source package has neither Package-List nor Binary field.')
68     def _parse(self):
69         self.package_list = {}
70
71         for line in self._source['Package-List'].split("\n"):
72             if not line:
73                 continue
74             fields = line.split()
75             if len(fields) < 4:
76                 raise InvalidSource("Package-List entry has less than four fields.")
77
78             # <name> <type> <component/section> <priority> [arch=<arch>[,<arch>]...]
79             name = fields[0]
80             package_type = fields[1]
81             component, section = extract_component_from_section(fields[2])
82             priority = fields[3]
83             other = dict(kv.split('=', 1) for kv in fields[4:])
84
85             entry = PackageListEntry(name, package_type, section, component, priority, **other)
86             self.package_list[name] = entry
87
88     def _parse_fallback(self):
89         self.package_list = {}
90
91         for binary in self._source['Binary'].split():
92             name = binary
93             package_type = None
94             component = None
95             section = None
96             priority = None
97             other = dict()
98
99             entry = PackageListEntry(name, package_type, section, component, priority, **other)
100             self.package_list[name] = entry
101
102     def packages_for_suite(self, suite):
103         packages = []
104         for entry in self.package_list.values():
105             built = entry.built_in_suite(suite)
106             if built or built is None:
107                 packages.append(entry)
108         return packages
109
110     def has_arch_indep_packages(self):
111         has_arch_indep = False
112         for entry in self.package_list.values():
113             built = entry.built_on_architecture('all')
114             if built:
115                 return True
116             if built is None:
117                 has_arch_indep = None
118         return has_arch_indep
119
120     def has_arch_dep_packages(self):
121         has_arch_dep = False
122         for entry in self.package_list.values():
123             built_on_all = entry.built_on_architecture('all')
124             if built_on_all == False:
125                 return True
126             if built_on_all is None:
127                 has_arch_dep = None
128         return has_arch_dep