1 |
commit: 1a18160c8200fb444878538b127b30bb461e4c42 |
2 |
Author: Michał Górny <mgorny <AT> gentoo <DOT> org> |
3 |
AuthorDate: Fri Apr 24 07:42:44 2020 +0000 |
4 |
Commit: Michał Górny <mgorny <AT> gentoo <DOT> org> |
5 |
CommitDate: Fri Apr 24 07:42:44 2020 +0000 |
6 |
URL: https://gitweb.gentoo.org/proj/gentoolkit.git/commit/?id=1a18160c |
7 |
|
8 |
Extract profile reading code from eshowkw, with tests from ekeyword |
9 |
|
10 |
Signed-off-by: Michał Górny <mgorny <AT> gentoo.org> |
11 |
|
12 |
pym/gentoolkit/eshowkw/keywords_header.py | 78 +------------------------- |
13 |
pym/gentoolkit/profile.py | 92 +++++++++++++++++++++++++++++++ |
14 |
pym/gentoolkit/test/test_profile.py | 49 ++++++++++++++++ |
15 |
3 files changed, 142 insertions(+), 77 deletions(-) |
16 |
|
17 |
diff --git a/pym/gentoolkit/eshowkw/keywords_header.py b/pym/gentoolkit/eshowkw/keywords_header.py |
18 |
index f98f11a..31261dc 100644 |
19 |
--- a/pym/gentoolkit/eshowkw/keywords_header.py |
20 |
+++ b/pym/gentoolkit/eshowkw/keywords_header.py |
21 |
@@ -6,87 +6,11 @@ from __future__ import print_function |
22 |
|
23 |
__all__ = ['keywords_header'] |
24 |
|
25 |
-import portage |
26 |
-import os |
27 |
-import sys |
28 |
-if sys.hexversion < 0x3000000: |
29 |
- from io import open |
30 |
-from portage import _encodings, _unicode_encode |
31 |
from portage import settings as ports |
32 |
from gentoolkit.eshowkw.display_pretty import colorize_string |
33 |
from gentoolkit.eshowkw.display_pretty import align_string |
34 |
+from gentoolkit.profile import load_profile_data |
35 |
|
36 |
-# Copied from ekeyword |
37 |
-def warning(msg): |
38 |
- """Write |msg| as a warning to stderr""" |
39 |
- print('warning: %s' % msg, file=sys.stderr) |
40 |
- |
41 |
- |
42 |
-# Copied from ekeyword, modified to support arch vs ~arch status |
43 |
-def load_profile_data(portdir=None, repo='gentoo'): |
44 |
- """Load the list of known arches from the tree |
45 |
- |
46 |
- Args: |
47 |
- portdir: The repository to load all data from (and ignore |repo|) |
48 |
- repo: Look up this repository by name to locate profile data |
49 |
- |
50 |
- Returns: |
51 |
- A dict mapping the keyword to its preferred state: |
52 |
- {'x86': ('stable', 'arch'), 'mips': ('dev', '~arch'), ...} |
53 |
- """ |
54 |
- if portdir is None: |
55 |
- portdir = portage.db[portage.root]['vartree'].settings.repositories[repo].location |
56 |
- |
57 |
- arch_status = {} |
58 |
- |
59 |
- try: |
60 |
- arch_list = os.path.join(portdir, 'profiles', 'arch.list') |
61 |
- with open(_unicode_encode(arch_list, encoding=_encodings['fs']), |
62 |
- encoding=_encodings['content']) as f: |
63 |
- for line in f: |
64 |
- line = line.split('#', 1)[0].strip() |
65 |
- if line: |
66 |
- arch_status[line] = None |
67 |
- except IOError: |
68 |
- pass |
69 |
- |
70 |
- try: |
71 |
- profile_status = { |
72 |
- 'stable': 0, |
73 |
- 'dev': 1, |
74 |
- 'exp': 2, |
75 |
- None: 3, |
76 |
- } |
77 |
- profiles_list = os.path.join(portdir, 'profiles', 'profiles.desc') |
78 |
- with open(_unicode_encode(profiles_list, encoding=_encodings['fs']), |
79 |
- encoding=_encodings['content']) as f: |
80 |
- for line in f: |
81 |
- line = line.split('#', 1)[0].split() |
82 |
- if line: |
83 |
- arch, _profile, status = line |
84 |
- arch_status.setdefault(arch, status) |
85 |
- curr_status = profile_status[arch_status[arch]] |
86 |
- new_status = profile_status[status] |
87 |
- if new_status < curr_status: |
88 |
- arch_status[arch] = status |
89 |
- except IOError: |
90 |
- pass |
91 |
- |
92 |
- if arch_status: |
93 |
- arch_status['all'] = None |
94 |
- else: |
95 |
- warning('could not read profile files: %s' % arch_list) |
96 |
- warning('will not be able to verify args are correct') |
97 |
- |
98 |
- # TODO: support arches.desc once the GLEP is finalized |
99 |
- # for now, we just hardcode ~mips + *-* (fbsd, prefix) |
100 |
- for k, v in arch_status.items(): |
101 |
- if k in ('alpha', 'mips', 'riscv') or '-' in k: |
102 |
- arch_status[k] = (v, '~arch') |
103 |
- else: |
104 |
- arch_status[k] = (v, 'arch') |
105 |
- |
106 |
- return arch_status |
107 |
|
108 |
def gen_arch_list(status): |
109 |
_arch_status = load_profile_data() |
110 |
|
111 |
diff --git a/pym/gentoolkit/profile.py b/pym/gentoolkit/profile.py |
112 |
new file mode 100644 |
113 |
index 0000000..945362d |
114 |
--- /dev/null |
115 |
+++ b/pym/gentoolkit/profile.py |
116 |
@@ -0,0 +1,92 @@ |
117 |
+#!/usr/bin/python |
118 |
+# Copyright 2020 Gentoo Authors |
119 |
+# Distributed under the terms of the GNU General Public License v2 |
120 |
+# |
121 |
+# Licensed under the GNU General Public License, v2 |
122 |
+ |
123 |
+"""Routines to load profile information for ekeyword/eshowkw""" |
124 |
+ |
125 |
+__all__ = ( |
126 |
+ 'load_profile_data', |
127 |
+) |
128 |
+ |
129 |
+ |
130 |
+import os.path |
131 |
+import portage |
132 |
+import sys |
133 |
+ |
134 |
+if sys.hexversion < 0x3000000: |
135 |
+ from io import open |
136 |
+ |
137 |
+from portage import _encodings, _unicode_encode |
138 |
+ |
139 |
+ |
140 |
+def warning(msg): |
141 |
+ """Write |msg| as a warning to stderr""" |
142 |
+ print('warning: %s' % msg, file=sys.stderr) |
143 |
+ |
144 |
+ |
145 |
+def load_profile_data(portdir=None, repo='gentoo'): |
146 |
+ """Load the list of known arches from the tree |
147 |
+ |
148 |
+ Args: |
149 |
+ portdir: The repository to load all data from (and ignore |repo|) |
150 |
+ repo: Look up this repository by name to locate profile data |
151 |
+ |
152 |
+ Returns: |
153 |
+ A dict mapping the keyword to its preferred state: |
154 |
+ {'x86': ('stable', 'arch'), 'mips': ('dev', '~arch'), ...} |
155 |
+ """ |
156 |
+ if portdir is None: |
157 |
+ portdir = portage.db[portage.root]['vartree'].settings.repositories[repo].location |
158 |
+ |
159 |
+ arch_status = {} |
160 |
+ |
161 |
+ try: |
162 |
+ arch_list = os.path.join(portdir, 'profiles', 'arch.list') |
163 |
+ with open(_unicode_encode(arch_list, encoding=_encodings['fs']), |
164 |
+ encoding=_encodings['content']) as f: |
165 |
+ for line in f: |
166 |
+ line = line.split('#', 1)[0].strip() |
167 |
+ if line: |
168 |
+ arch_status[line] = None |
169 |
+ except IOError: |
170 |
+ pass |
171 |
+ |
172 |
+ try: |
173 |
+ profile_status = { |
174 |
+ 'stable': 0, |
175 |
+ 'dev': 1, |
176 |
+ 'exp': 2, |
177 |
+ None: 3, |
178 |
+ } |
179 |
+ profiles_list = os.path.join(portdir, 'profiles', 'profiles.desc') |
180 |
+ with open(_unicode_encode(profiles_list, encoding=_encodings['fs']), |
181 |
+ encoding=_encodings['content']) as f: |
182 |
+ for line in f: |
183 |
+ line = line.split('#', 1)[0].split() |
184 |
+ if line: |
185 |
+ arch, _profile, status = line |
186 |
+ arch_status.setdefault(arch, status) |
187 |
+ curr_status = profile_status[arch_status[arch]] |
188 |
+ new_status = profile_status[status] |
189 |
+ if new_status < curr_status: |
190 |
+ arch_status[arch] = status |
191 |
+ except IOError: |
192 |
+ pass |
193 |
+ |
194 |
+ if arch_status: |
195 |
+ arch_status['all'] = None |
196 |
+ else: |
197 |
+ warning('could not read profile files: %s' % arch_list) |
198 |
+ warning('will not be able to verify args are correct') |
199 |
+ |
200 |
+ # TODO: support arches.desc once the GLEP is finalized |
201 |
+ # for now, we just hardcode ~mips + *-* (fbsd, prefix) |
202 |
+ for k, v in arch_status.items(): |
203 |
+ if k in ('alpha', 'mips', 'riscv') or '-' in k: |
204 |
+ arch_status[k] = (v, '~arch') |
205 |
+ else: |
206 |
+ arch_status[k] = (v, 'arch') |
207 |
+ |
208 |
+ return arch_status |
209 |
|
210 |
diff --git a/pym/gentoolkit/test/test_profile.py b/pym/gentoolkit/test/test_profile.py |
211 |
new file mode 100644 |
212 |
index 0000000..038525d |
213 |
--- /dev/null |
214 |
+++ b/pym/gentoolkit/test/test_profile.py |
215 |
@@ -0,0 +1,49 @@ |
216 |
+#!/usr/bin/python |
217 |
+# Copyright 2020 Gentoo Authors |
218 |
+# Distributed under the terms of the GNU General Public License v2 |
219 |
+# |
220 |
+# Licensed under the GNU General Public License, v2 |
221 |
+ |
222 |
+import os.path |
223 |
+import unittest |
224 |
+ |
225 |
+from gentoolkit.profile import load_profile_data |
226 |
+ |
227 |
+ |
228 |
+TESTDIR = os.path.join(os.path.dirname(__file__), '../ekeyword/tests') |
229 |
+ |
230 |
+ |
231 |
+class TestLoadProfileData(unittest.TestCase): |
232 |
+ """Tests for load_profile_data""" |
233 |
+ |
234 |
+ def _test(self, subdir): |
235 |
+ portdir = os.path.join(TESTDIR, 'profiles', subdir) |
236 |
+ return load_profile_data(portdir=portdir) |
237 |
+ |
238 |
+ def testLoadBoth(self): |
239 |
+ """Test loading both arch.list and profiles.desc""" |
240 |
+ ret = self._test('both') |
241 |
+ self.assertIn('arm', ret) |
242 |
+ self.assertEqual(ret['arm'], ('stable', 'arch')) |
243 |
+ self.assertIn('arm64', ret) |
244 |
+ self.assertEqual(ret['arm64'], ('exp', 'arch')) |
245 |
+ |
246 |
+ def testLoadArchOnly(self): |
247 |
+ """Test loading only arch.list""" |
248 |
+ ret = self._test('arch-only') |
249 |
+ self.assertIn('arm', ret) |
250 |
+ self.assertEqual(ret['arm'], (None, 'arch')) |
251 |
+ self.assertIn('x86-solaris', ret) |
252 |
+ |
253 |
+ def testLoadProfilesOnly(self): |
254 |
+ """Test loading only profiles.desc""" |
255 |
+ ret = self._test('profiles-only') |
256 |
+ self.assertIn('arm', ret) |
257 |
+ self.assertEqual(ret['arm'], ('stable', 'arch')) |
258 |
+ self.assertIn('arm64', ret) |
259 |
+ self.assertEqual(ret['arm64'], ('exp', 'arch')) |
260 |
+ |
261 |
+ def testLoadNone(self): |
262 |
+ """Test running when neither files exists""" |
263 |
+ ret = self._test('none') |
264 |
+ self.assertEqual(ret, {}) |