commit: 1a18160c8200fb444878538b127b30bb461e4c42
Author: Michał Górny <mgorny <AT> gentoo <DOT> org>
AuthorDate: Fri Apr 24 07:42:44 2020 +0000
Commit: Michał Górny <mgorny <AT> gentoo <DOT> org>
CommitDate: Fri Apr 24 07:42:44 2020 +0000
URL: https://gitweb.gentoo.org/proj/gentoolkit.git/commit/?id=1a18160c
Extract profile reading code from eshowkw, with tests from ekeyword
Signed-off-by: Michał Górny <mgorny <AT> gentoo.org>
pym/gentoolkit/eshowkw/keywords_header.py | 78 +-------------------------
pym/gentoolkit/profile.py | 92 +++++++++++++++++++++++++++++++
pym/gentoolkit/test/test_profile.py | 49 ++++++++++++++++
3 files changed, 142 insertions(+), 77 deletions(-)
diff --git a/pym/gentoolkit/eshowkw/keywords_header.py
b/pym/gentoolkit/eshowkw/keywords_header.py
index f98f11a..31261dc 100644
--- a/pym/gentoolkit/eshowkw/keywords_header.py
+++ b/pym/gentoolkit/eshowkw/keywords_header.py
@@ -6,87 +6,11 @@ from __future__ import print_function
__all__ = ['keywords_header']
-import portage
-import os
-import sys
-if sys.hexversion < 0x3000000:
- from io import open
-from portage import _encodings, _unicode_encode
from portage import settings as ports
from gentoolkit.eshowkw.display_pretty import colorize_string
from gentoolkit.eshowkw.display_pretty import align_string
+from gentoolkit.profile import load_profile_data
-# Copied from ekeyword
-def warning(msg):
- """Write |msg| as a warning to stderr"""
- print('warning: %s' % msg, file=sys.stderr)
-
-
-# Copied from ekeyword, modified to support arch vs ~arch status
-def load_profile_data(portdir=None, repo='gentoo'):
- """Load the list of known arches from the tree
-
- Args:
- portdir: The repository to load all data from (and ignore |repo|)
- repo: Look up this repository by name to locate profile data
-
- Returns:
- A dict mapping the keyword to its preferred state:
- {'x86': ('stable', 'arch'), 'mips': ('dev', '~arch'), ...}
- """
- if portdir is None:
- portdir =
portage.db[portage.root]['vartree'].settings.repositories[repo].location
-
- arch_status = {}
-
- try:
- arch_list = os.path.join(portdir, 'profiles', 'arch.list')
- with open(_unicode_encode(arch_list, encoding=_encodings['fs']),
- encoding=_encodings['content']) as f:
- for line in f:
- line = line.split('#', 1)[0].strip()
- if line:
- arch_status[line] = None
- except IOError:
- pass
-
- try:
- profile_status = {
- 'stable': 0,
- 'dev': 1,
- 'exp': 2,
- None: 3,
- }
- profiles_list = os.path.join(portdir, 'profiles',
'profiles.desc')
- with open(_unicode_encode(profiles_list,
encoding=_encodings['fs']),
- encoding=_encodings['content']) as f:
- for line in f:
- line = line.split('#', 1)[0].split()
- if line:
- arch, _profile, status = line
- arch_status.setdefault(arch, status)
- curr_status =
profile_status[arch_status[arch]]
- new_status = profile_status[status]
- if new_status < curr_status:
- arch_status[arch] = status
- except IOError:
- pass
-
- if arch_status:
- arch_status['all'] = None
- else:
- warning('could not read profile files: %s' % arch_list)
- warning('will not be able to verify args are correct')
-
- # TODO: support arches.desc once the GLEP is finalized
- # for now, we just hardcode ~mips + *-* (fbsd, prefix)
- for k, v in arch_status.items():
- if k in ('alpha', 'mips', 'riscv') or '-' in k:
- arch_status[k] = (v, '~arch')
- else:
- arch_status[k] = (v, 'arch')
-
- return arch_status
def gen_arch_list(status):
_arch_status = load_profile_data()
diff --git a/pym/gentoolkit/profile.py b/pym/gentoolkit/profile.py
new file mode 100644
index 0000000..945362d
--- /dev/null
+++ b/pym/gentoolkit/profile.py
@@ -0,0 +1,92 @@
+#!/usr/bin/python
+# Copyright 2020 Gentoo Authors
+# Distributed under the terms of the GNU General Public License v2
+#
+# Licensed under the GNU General Public License, v2
+
+"""Routines to load profile information for ekeyword/eshowkw"""
+
+__all__ = (
+ 'load_profile_data',
+)
+
+
+import os.path
+import portage
+import sys
+
+if sys.hexversion < 0x3000000:
+ from io import open
+
+from portage import _encodings, _unicode_encode
+
+
+def warning(msg):
+ """Write |msg| as a warning to stderr"""
+ print('warning: %s' % msg, file=sys.stderr)
+
+
+def load_profile_data(portdir=None, repo='gentoo'):
+ """Load the list of known arches from the tree
+
+ Args:
+ portdir: The repository to load all data from (and ignore |repo|)
+ repo: Look up this repository by name to locate profile data
+
+ Returns:
+ A dict mapping the keyword to its preferred state:
+ {'x86': ('stable', 'arch'), 'mips': ('dev', '~arch'), ...}
+ """
+ if portdir is None:
+ portdir =
portage.db[portage.root]['vartree'].settings.repositories[repo].location
+
+ arch_status = {}
+
+ try:
+ arch_list = os.path.join(portdir, 'profiles', 'arch.list')
+ with open(_unicode_encode(arch_list, encoding=_encodings['fs']),
+ encoding=_encodings['content']) as f:
+ for line in f:
+ line = line.split('#', 1)[0].strip()
+ if line:
+ arch_status[line] = None
+ except IOError:
+ pass
+
+ try:
+ profile_status = {
+ 'stable': 0,
+ 'dev': 1,
+ 'exp': 2,
+ None: 3,
+ }
+ profiles_list = os.path.join(portdir, 'profiles',
'profiles.desc')
+ with open(_unicode_encode(profiles_list,
encoding=_encodings['fs']),
+ encoding=_encodings['content']) as f:
+ for line in f:
+ line = line.split('#', 1)[0].split()
+ if line:
+ arch, _profile, status = line
+ arch_status.setdefault(arch, status)
+ curr_status =
profile_status[arch_status[arch]]
+ new_status = profile_status[status]
+ if new_status < curr_status:
+ arch_status[arch] = status
+ except IOError:
+ pass
+
+ if arch_status:
+ arch_status['all'] = None
+ else:
+ warning('could not read profile files: %s' % arch_list)
+ warning('will not be able to verify args are correct')
+
+ # TODO: support arches.desc once the GLEP is finalized
+ # for now, we just hardcode ~mips + *-* (fbsd, prefix)
+ for k, v in arch_status.items():
+ if k in ('alpha', 'mips', 'riscv') or '-' in k:
+ arch_status[k] = (v, '~arch')
+ else:
+ arch_status[k] = (v, 'arch')
+
+ return arch_status
diff --git a/pym/gentoolkit/test/test_profile.py
b/pym/gentoolkit/test/test_profile.py
new file mode 100644
index 0000000..038525d
--- /dev/null
+++ b/pym/gentoolkit/test/test_profile.py
@@ -0,0 +1,49 @@
+#!/usr/bin/python
+# Copyright 2020 Gentoo Authors
+# Distributed under the terms of the GNU General Public License v2
+#
+# Licensed under the GNU General Public License, v2
+
+import os.path
+import unittest
+
+from gentoolkit.profile import load_profile_data
+
+
+TESTDIR = os.path.join(os.path.dirname(__file__), '../ekeyword/tests')
+
+
+class TestLoadProfileData(unittest.TestCase):
+ """Tests for load_profile_data"""
+
+ def _test(self, subdir):
+ portdir = os.path.join(TESTDIR, 'profiles', subdir)
+ return load_profile_data(portdir=portdir)
+
+ def testLoadBoth(self):
+ """Test loading both arch.list and profiles.desc"""
+ ret = self._test('both')
+ self.assertIn('arm', ret)
+ self.assertEqual(ret['arm'], ('stable', 'arch'))
+ self.assertIn('arm64', ret)
+ self.assertEqual(ret['arm64'], ('exp', 'arch'))
+
+ def testLoadArchOnly(self):
+ """Test loading only arch.list"""
+ ret = self._test('arch-only')
+ self.assertIn('arm', ret)
+ self.assertEqual(ret['arm'], (None, 'arch'))
+ self.assertIn('x86-solaris', ret)
+
+ def testLoadProfilesOnly(self):
+ """Test loading only profiles.desc"""
+ ret = self._test('profiles-only')
+ self.assertIn('arm', ret)
+ self.assertEqual(ret['arm'], ('stable', 'arch'))
+ self.assertIn('arm64', ret)
+ self.assertEqual(ret['arm64'], ('exp', 'arch'))
+
+ def testLoadNone(self):
+ """Test running when neither files exists"""
+ ret = self._test('none')
+ self.assertEqual(ret, {})