1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
|
# Copyright 2010 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
__all__ = (
'LicenseManager',
)
from copy import deepcopy
from portage import os
from portage.dep import ExtendedAtomDict, use_reduce
from portage.exception import InvalidDependString
from portage.localization import _
from portage.util import grabdict, grabdict_package, writemsg
from portage.versions import cpv_getkey
from portage.package.ebuild._config.helper import ordered_by_atom_specificity
class LicenseManager(object):
def __init__(self, _copy=False):
if _copy:
return
self._accept_license_str = None
self._accept_license = None
self._license_groups = {}
self._plicensedict = ExtendedAtomDict(dict)
self._undef_lic_groups = set()
def __deepcopy__(self, memo=None):
if memo is None:
memo = {}
result = LicenseManager(_copy=True)
memo[id(self)] = result
# immutable attributes
result._accept_license_str = self._accept_license_str
result._accept_license = self._accept_license
# mutable attributes
result._plicensedict = deepcopy(self._plicensedict, memo)
result._license_groups = deepcopy(self._license_groups, memo)
result._undef_lic_groups = deepcopy(self._undef_lic_groups, memo)
return result
def read_config_files(self, abs_user_config):
licdict = grabdict_package(os.path.join(
abs_user_config, "package.license"), recursive=1, allow_wildcard=True)
for k, v in licdict.items():
self._plicensedict.setdefault(k.cp, {})[k] = \
self.expandLicenseTokens(v)
def parse_license_groups(self, locations):
for loc in locations:
for k, v in grabdict(
os.path.join(loc, "license_groups")).items():
self._license_groups.setdefault(k, []).extend(v)
def extract_global_changes(self, old=""):
ret = old
atom_license_map = self._plicensedict.get("*/*")
if atom_license_map is not None:
v = atom_license_map.pop("*/*", None)
if v is not None:
ret = " ".join(v)
if old:
ret = old + " " + ret
if not atom_license_map:
#No tokens left in atom_license_map, remove it.
del self._plicensedict["*/*"]
return ret
def expandLicenseTokens(self, tokens):
""" Take a token from ACCEPT_LICENSE or package.license and expand it
if it's a group token (indicated by @) or just return it if it's not a
group. If a group is negated then negate all group elements."""
expanded_tokens = []
for x in tokens:
expanded_tokens.extend(self._expandLicenseToken(x, None))
return expanded_tokens
def _expandLicenseToken(self, token, traversed_groups):
negate = False
rValue = []
if token.startswith("-"):
negate = True
license_name = token[1:]
else:
license_name = token
if not license_name.startswith("@"):
rValue.append(token)
return rValue
group_name = license_name[1:]
if traversed_groups is None:
traversed_groups = set()
license_group = self._license_groups.get(group_name)
if group_name in traversed_groups:
writemsg(_("Circular license group reference"
" detected in '%s'\n") % group_name, noiselevel=-1)
rValue.append("@"+group_name)
elif license_group:
traversed_groups.add(group_name)
for l in license_group:
if l.startswith("-"):
writemsg(_("Skipping invalid element %s"
" in license group '%s'\n") % (l, group_name),
noiselevel=-1)
else:
rValue.extend(self._expandLicenseToken(l, traversed_groups))
else:
if self._license_groups and \
group_name not in self._undef_lic_groups:
self._undef_lic_groups.add(group_name)
writemsg(_("Undefined license group '%s'\n") % group_name,
noiselevel=-1)
rValue.append("@"+group_name)
if negate:
rValue = ["-" + token for token in rValue]
return rValue
def _getPkgAcceptLicense(self, cpv, slot):
"""
Get an ACCEPT_LICENSE list, accounting for package.license.
"""
accept_license = self._accept_license
cp = cpv_getkey(cpv)
cpdict = self._plicensedict.get(cp)
if cpdict:
cpv_slot = "%s:%s" % (cpv, slot)
plicence_list = ordered_by_atom_specificity(cpdict, cpv_slot)
if plicence_list:
accept_license = list(self._accept_license)
for x in plicence_list:
accept_license.extend(x)
return accept_license
def get_prunned_accept_license(self, cpv, use, lic, slot):
"""
Generate a pruned version of ACCEPT_LICENSE, by intersection with
LICENSE. This is required since otherwise ACCEPT_LICENSE might be
too big (bigger than ARG_MAX), causing execve() calls to fail with
E2BIG errors as in bug #262647.
"""
try:
licenses = set(use_reduce(lic, uselist=use, flat=True))
except InvalidDependString:
licenses = set()
licenses.discard('||')
accept_license = self._getPkgAcceptLicense(cpv, slot)
if accept_license:
acceptable_licenses = set()
for x in accept_license:
if x == '*':
acceptable_licenses.update(licenses)
elif x == '-*':
acceptable_licenses.clear()
elif x[:1] == '-':
acceptable_licenses.discard(x[1:])
elif x in licenses:
acceptable_licenses.add(x)
licenses = acceptable_licenses
return ' '.join(sorted(licenses))
def getMissingLicenses(self, cpv, use, lic, slot):
"""
Take a LICENSE string and return a list any licenses that the user may
may need to accept for the given package. The returned list will not
contain any licenses that have already been accepted. This method
can throw an InvalidDependString exception.
@param cpv: The package name (for package.license support)
@type cpv: String
@param use: "USE" from the cpv's metadata
@type use: String
@param lic: "LICENSE" from the cpv's metadata
@type lic: String
@param slot: "SLOT" from the cpv's metadata
@type slot: String
@rtype: List
@return: A list of licenses that have not been accepted.
"""
licenses = set(use_reduce(lic, matchall=1, flat=True))
licenses.discard('||')
acceptable_licenses = set()
for x in self._getPkgAcceptLicense(cpv, slot):
if x == '*':
acceptable_licenses.update(licenses)
elif x == '-*':
acceptable_licenses.clear()
elif x[:1] == '-':
acceptable_licenses.discard(x[1:])
else:
acceptable_licenses.add(x)
license_str = lic
if "?" in license_str:
use = use.split()
else:
use = []
license_struct = use_reduce(license_str, uselist=use, opconvert=True)
return self._getMaskedLicenses(license_struct, acceptable_licenses)
def _getMaskedLicenses(self, license_struct, acceptable_licenses):
if not license_struct:
return []
if license_struct[0] == "||":
ret = []
for element in license_struct[1:]:
if isinstance(element, list):
if element:
tmp = self._getMaskedLicenses(element, acceptable_licenses)
if not tmp:
return []
ret.extend(tmp)
else:
if element in acceptable_licenses:
return []
ret.append(element)
# Return all masked licenses, since we don't know which combination
# (if any) the user will decide to unmask.
return ret
ret = []
for element in license_struct:
if isinstance(element, list):
if element:
ret.extend(self._getMaskedLicenses(element,
acceptable_licenses))
else:
if element not in acceptable_licenses:
ret.append(element)
return ret
def set_accept_license_str(self, accept_license_str):
if accept_license_str != self._accept_license_str:
self._accept_license_str = accept_license_str
self._accept_license = tuple(self.expandLicenseTokens(accept_license_str.split()))
|