commit:     f1c58b74df5eab2dac1e8e5810d9a042785f60cc
Author:     Brian Harring <ferringb <AT> gmail <DOT> com>
AuthorDate: Wed Jan 11 05:52:01 2023 +0000
Commit:     Arthur Zamarin <arthurzam <AT> gentoo <DOT> org>
CommitDate: Tue Jan 17 20:49:46 2023 +0000
URL:        
https://gitweb.gentoo.org/proj/pkgcore/pkgcore.git/commit/?id=f1c58b74

refactor(portage_conf): Push fallback (and path awareness) logic of repos.conf 
into methods.

The previous code had the exception + fallback handling in the init; this is 
already
a long init, and there are changes needed for repos conf, thus introduce a 
method to
do the fallback logic.

Said method will be extended to essentially bury the complexity of repos.conf- 
that problem-
into an encapsulated method to try and reduce the cognitive burden of dealing 
with it.

Why this matters: this work is prep for extension of repos.conf to add in 
various syncing
features portage grew over the last decade+.

Signed-off-by: Brian Harring <ferringb <AT> gmail.com>
Signed-off-by: Arthur Zamarin <arthurzam <AT> gentoo.org>

 src/pkgcore/ebuild/portage_conf.py | 56 ++++++++++++++++++++------------------
 tests/ebuild/test_portage_conf.py  |  2 +-
 2 files changed, 31 insertions(+), 27 deletions(-)

diff --git a/src/pkgcore/ebuild/portage_conf.py 
b/src/pkgcore/ebuild/portage_conf.py
index 815d563d4..9e5fffac7 100644
--- a/src/pkgcore/ebuild/portage_conf.py
+++ b/src/pkgcore/ebuild/portage_conf.py
@@ -172,24 +172,7 @@ class PortageConfig(DictMixin):
             }
         )
 
-        try:
-            repos_conf_defaults, repos_conf = self.load_repos_conf(
-                pjoin(self.dir, "repos.conf")
-            )
-        except config_errors.ParsingError as e:
-            if not getattr(getattr(e, "exc", None), "errno", None) == 
errno.ENOENT:
-                raise
-            try:
-                # fallback to defaults provided by pkgcore
-                repos_conf_defaults, repos_conf = self.load_repos_conf(
-                    pjoin(const.CONFIG_PATH, "repos.conf")
-                )
-            except IGNORED_EXCEPTIONS:
-                raise
-            except Exception as e:
-                raise config_errors.ParsingError(
-                    "failed to find a usable repos.conf"
-                ) from e
+        repos_conf_defaults, repos_conf = self.load_repos_conf()
 
         self["ebuild-repo-common"] = basics.AutoConfigSection(
             {
@@ -332,9 +315,31 @@ class PortageConfig(DictMixin):
             # quirk of read_bash_dict; it returns only what was mutated.
             vars_dict.update(new_vars)
 
+    def load_repos_conf(self) -> tuple[dict, dict]:
+        """parse and return repos.conf content, tracing the default and the 
fallback location"""
+        try:
+            repos_conf_defaults, repos_conf = self.parse_repos_conf_path(
+                pjoin(self.dir, "repos.conf")
+            )
+        except config_errors.ParsingError as e:
+            if not getattr(getattr(e, "exc", None), "errno", None) == 
errno.ENOENT:
+                raise
+            try:
+                # fallback to defaults provided by pkgcore
+                repos_conf_defaults, repos_conf = self.parse_repos_conf_path(
+                    pjoin(const.CONFIG_PATH, "repos.conf")
+                )
+            except IGNORED_EXCEPTIONS:
+                raise
+            except Exception as e:
+                raise config_errors.ParsingError(
+                    "failed to find a usable repos.conf"
+                ) from e
+        return repos_conf_defaults, repos_conf
+
     @classmethod
-    def load_repos_conf(cls, path):
-        """parse repos.conf files
+    def parse_repos_conf_path(cls, path: str):
+        """parse repos.conf files from a given entrypoint
 
         Args:
             path (str): path to the repos.conf which can be a regular file or
@@ -372,9 +377,7 @@ class PortageConfig(DictMixin):
                 ) from e
 
             if defaults and main_defaults:
-                logger.warning(
-                    f"repos.conf: parsing {fp!r}: overriding DEFAULT section"
-                )
+                logger.warning("repos.conf: parsing %r: overriding DEFAULT 
section", fp)
             main_defaults.update(defaults)
 
             if not had_repo_conf and not repo_confs:
@@ -385,15 +388,16 @@ class PortageConfig(DictMixin):
             for name, repo_conf in repo_confs.items():
                 if name in repos:
                     logger.warning(
-                        f"repos.conf: parsing {fp!r}: overriding {name!r} repo"
+                        "repos.conf: parsing %r: overriding %r repo", fp, name
                     )
 
                 # ignore repo if location is unset
                 location = repo_conf.get("location", None)
                 if location is None:
                     logger.warning(
-                        f"repos.conf: parsing {fp!r}: "
-                        f"{name!r} repo missing location setting, ignoring 
repo"
+                        "repos.conf: parsing %r: %r repo missing location 
setting, ignoring repo",
+                        fp,
+                        name,
                     )
                     continue
                 location = os.path.expanduser(location)

diff --git a/tests/ebuild/test_portage_conf.py 
b/tests/ebuild/test_portage_conf.py
index edb3d89b6..b939ee09e 100644
--- a/tests/ebuild/test_portage_conf.py
+++ b/tests/ebuild/test_portage_conf.py
@@ -13,7 +13,7 @@ from pkgcore.config import errors as config_errors
 from pkgcore.ebuild.portage_conf import PortageConfig
 
 load_make_conf = PortageConfig.load_make_conf
-load_repos_conf = PortageConfig.load_repos_conf
+load_repos_conf = PortageConfig.parse_repos_conf_path
 
 
 class TestMakeConf:

Reply via email to