1 |
On 09/04/2016 10:04 AM, Mike Gilbert wrote: |
2 |
> Validate Display-If-Installed with EAPI 0 or 5. |
3 |
> Add support for trailing wildcard matching for Display-If-Profile. |
4 |
> |
5 |
> Bug: https://bugs.gentoo.org/577372 |
6 |
> --- |
7 |
> pym/portage/news.py | 42 ++++++++++++++++++++++++++++++------------ |
8 |
> 1 file changed, 30 insertions(+), 12 deletions(-) |
9 |
> |
10 |
> diff --git a/pym/portage/news.py b/pym/portage/news.py |
11 |
> index 177f9db..fa6fb00 100644 |
12 |
> --- a/pym/portage/news.py |
13 |
> +++ b/pym/portage/news.py |
14 |
> @@ -266,14 +266,24 @@ class NewsItem(object): |
15 |
> f.close() |
16 |
> self.restrictions = {} |
17 |
> invalids = [] |
18 |
> + news_format = None |
19 |
> + |
20 |
> + # Look for News-Item-Format |
21 |
> for i, line in enumerate(lines): |
22 |
> - # Optimization to ignore regex matchines on lines that |
23 |
> - # will never match |
24 |
> format_match = _formatRE.match(line) |
25 |
> - if (format_match is not None and |
26 |
> - not fnmatch.fnmatch(format_match.group(1), '1.*')): |
27 |
> + if format_match is not None: |
28 |
> + news_format = format_match.group(1) |
29 |
> + if fnmatch.fnmatch(news_format, '[12].*'): |
30 |
> + break |
31 |
> invalids.append((i + 1, line.rstrip('\n'))) |
32 |
> - break |
33 |
> + |
34 |
> + if news_format is None: |
35 |
> + invalids.append((0, 'News-Item-Format unspecified')) |
36 |
> + |
37 |
> + # Parse the rest |
38 |
> + for i, line in enumerate(lines): |
39 |
> + # Optimization to ignore regex matchines on lines that |
40 |
|
41 |
s/matchines/matches/ |
42 |
|
43 |
> + # will never match |
44 |
> if not line.startswith('D'): |
45 |
> continue |
46 |
> restricts = { _installedRE : DisplayInstalledRestriction, |
47 |
> @@ -282,13 +292,14 @@ class NewsItem(object): |
48 |
> for regex, restriction in restricts.items(): |
49 |
> match = regex.match(line) |
50 |
> if match: |
51 |
> - restrict = restriction(match.groups()[0].strip()) |
52 |
> + restrict = restriction(match.groups()[0].strip(), news_format) |
53 |
> if not restrict.isValid(): |
54 |
> invalids.append((i + 1, line.rstrip("\n"))) |
55 |
> else: |
56 |
> self.restrictions.setdefault( |
57 |
> id(restriction), []).append(restrict) |
58 |
> continue |
59 |
> + |
60 |
> if invalids: |
61 |
> self._valid = False |
62 |
> msg = [] |
63 |
> @@ -321,13 +332,14 @@ class DisplayProfileRestriction(DisplayRestriction): |
64 |
> if the user is running a specific profile. |
65 |
> """ |
66 |
> |
67 |
> - def __init__(self, profile): |
68 |
> + def __init__(self, profile, news_format): |
69 |
> self.profile = profile |
70 |
> + self.format = news_format |
71 |
> |
72 |
> def checkRestriction(self, **kwargs): |
73 |
> - if self.profile == kwargs['profile']: |
74 |
> - return True |
75 |
> - return False |
76 |
> + if fnmatch.fnmatch(self.format, '2.*') and self.profile.endswith('/*'): |
77 |
> + return (kwargs['profile'].startswith(self.profile[:-1])) |
78 |
|
79 |
Maybe we should raise an exception if a wildcard is used but the format |
80 |
doesn't allow it? We could also raise an exception for any unsupported |
81 |
wildcards that do not occur at the end of the string. |
82 |
|
83 |
> + return (kwargs['profile'] == self.profile) |
84 |
> |
85 |
> class DisplayKeywordRestriction(DisplayRestriction): |
86 |
> """ |
87 |
> @@ -335,8 +347,9 @@ class DisplayKeywordRestriction(DisplayRestriction): |
88 |
> if the user is running a specific keyword. |
89 |
> """ |
90 |
> |
91 |
> - def __init__(self, keyword): |
92 |
> + def __init__(self, keyword, news_format): |
93 |
> self.keyword = keyword |
94 |
> + self.format = news_format |
95 |
> |
96 |
> def checkRestriction(self, **kwargs): |
97 |
> if kwargs['config'].get('ARCH', '') == self.keyword: |
98 |
> @@ -349,10 +362,15 @@ class DisplayInstalledRestriction(DisplayRestriction): |
99 |
> if the user has that item installed. |
100 |
> """ |
101 |
> |
102 |
> - def __init__(self, atom): |
103 |
> + def __init__(self, atom, news_format): |
104 |
> self.atom = atom |
105 |
> + self.format = news_format |
106 |
> |
107 |
> def isValid(self): |
108 |
> + if fnmatch.fnmatch(self.format, '1.*'): |
109 |
> + return isvalidatom(self.atom, eapi='0') |
110 |
|
111 |
We might want to check the existing news items to make sure that they |
112 |
are all conformant here. |
113 |
|
114 |
> + if fnmatch.fnmatch(self.format, '2.*'): |
115 |
> + return isvalidatom(self.atom, eapi='5') |
116 |
> return isvalidatom(self.atom) |
117 |
> |
118 |
> def checkRestriction(self, **kwargs): |
119 |
> |
120 |
|
121 |
|
122 |
-- |
123 |
Thanks, |
124 |
Zac |