1 |
Validate Display-If-Installed with EAPI 0 or 5. |
2 |
Add support for trailing wildcard matching for Display-If-Profile. |
3 |
|
4 |
Bug: https://bugs.gentoo.org/577372 |
5 |
--- |
6 |
pym/portage/news.py | 50 ++++++++++++++++++++++++++++++++++++++------------ |
7 |
1 file changed, 38 insertions(+), 12 deletions(-) |
8 |
|
9 |
diff --git a/pym/portage/news.py b/pym/portage/news.py |
10 |
index 177f9db..28faf83 100644 |
11 |
--- a/pym/portage/news.py |
12 |
+++ b/pym/portage/news.py |
13 |
@@ -197,6 +197,7 @@ _formatRE = re.compile("News-Item-Format:\s*([^\s]*)\s*$") |
14 |
_installedRE = re.compile("Display-If-Installed:(.*)\n") |
15 |
_profileRE = re.compile("Display-If-Profile:(.*)\n") |
16 |
_keywordRE = re.compile("Display-If-Keyword:(.*)\n") |
17 |
+_valid_profile_RE = re.compile(r'^[^*]+(/\*)?$') |
18 |
|
19 |
class NewsItem(object): |
20 |
""" |
21 |
@@ -266,14 +267,24 @@ class NewsItem(object): |
22 |
f.close() |
23 |
self.restrictions = {} |
24 |
invalids = [] |
25 |
+ news_format = None |
26 |
+ |
27 |
+ # Look for News-Item-Format |
28 |
for i, line in enumerate(lines): |
29 |
- # Optimization to ignore regex matchines on lines that |
30 |
- # will never match |
31 |
format_match = _formatRE.match(line) |
32 |
- if (format_match is not None and |
33 |
- not fnmatch.fnmatch(format_match.group(1), '1.*')): |
34 |
+ if format_match is not None: |
35 |
+ news_format = format_match.group(1) |
36 |
+ if fnmatch.fnmatch(news_format, '[12].*'): |
37 |
+ break |
38 |
invalids.append((i + 1, line.rstrip('\n'))) |
39 |
- break |
40 |
+ |
41 |
+ if news_format is None: |
42 |
+ invalids.append((0, 'News-Item-Format unspecified')) |
43 |
+ |
44 |
+ # Parse the rest |
45 |
+ for i, line in enumerate(lines): |
46 |
+ # Optimization to ignore regex matches on lines that |
47 |
+ # will never match |
48 |
if not line.startswith('D'): |
49 |
continue |
50 |
restricts = { _installedRE : DisplayInstalledRestriction, |
51 |
@@ -282,13 +293,14 @@ class NewsItem(object): |
52 |
for regex, restriction in restricts.items(): |
53 |
match = regex.match(line) |
54 |
if match: |
55 |
- restrict = restriction(match.groups()[0].strip()) |
56 |
+ restrict = restriction(match.groups()[0].strip(), news_format) |
57 |
if not restrict.isValid(): |
58 |
invalids.append((i + 1, line.rstrip("\n"))) |
59 |
else: |
60 |
self.restrictions.setdefault( |
61 |
id(restriction), []).append(restrict) |
62 |
continue |
63 |
+ |
64 |
if invalids: |
65 |
self._valid = False |
66 |
msg = [] |
67 |
@@ -321,13 +333,21 @@ class DisplayProfileRestriction(DisplayRestriction): |
68 |
if the user is running a specific profile. |
69 |
""" |
70 |
|
71 |
- def __init__(self, profile): |
72 |
+ def __init__(self, profile, news_format): |
73 |
self.profile = profile |
74 |
+ self.format = news_format |
75 |
+ |
76 |
+ def isValid(self): |
77 |
+ if fnmatch.fnmatch(self.format, '1.*') and '*' in self.profile: |
78 |
+ return False |
79 |
+ if fnmatch.fnmatch(self.format, '2.*') and not _valid_profile_RE.match(self.profile): |
80 |
+ return False |
81 |
+ return True |
82 |
|
83 |
def checkRestriction(self, **kwargs): |
84 |
- if self.profile == kwargs['profile']: |
85 |
- return True |
86 |
- return False |
87 |
+ if fnmatch.fnmatch(self.format, '2.*') and self.profile.endswith('/*'): |
88 |
+ return (kwargs['profile'].startswith(self.profile[:-1])) |
89 |
+ return (kwargs['profile'] == self.profile) |
90 |
|
91 |
class DisplayKeywordRestriction(DisplayRestriction): |
92 |
""" |
93 |
@@ -335,8 +355,9 @@ class DisplayKeywordRestriction(DisplayRestriction): |
94 |
if the user is running a specific keyword. |
95 |
""" |
96 |
|
97 |
- def __init__(self, keyword): |
98 |
+ def __init__(self, keyword, news_format): |
99 |
self.keyword = keyword |
100 |
+ self.format = news_format |
101 |
|
102 |
def checkRestriction(self, **kwargs): |
103 |
if kwargs['config'].get('ARCH', '') == self.keyword: |
104 |
@@ -349,10 +370,15 @@ class DisplayInstalledRestriction(DisplayRestriction): |
105 |
if the user has that item installed. |
106 |
""" |
107 |
|
108 |
- def __init__(self, atom): |
109 |
+ def __init__(self, atom, news_format): |
110 |
self.atom = atom |
111 |
+ self.format = news_format |
112 |
|
113 |
def isValid(self): |
114 |
+ if fnmatch.fnmatch(self.format, '1.*'): |
115 |
+ return isvalidatom(self.atom, eapi='0') |
116 |
+ if fnmatch.fnmatch(self.format, '2.*'): |
117 |
+ return isvalidatom(self.atom, eapi='5') |
118 |
return isvalidatom(self.atom) |
119 |
|
120 |
def checkRestriction(self, **kwargs): |
121 |
-- |
122 |
2.10.0 |