commit:     70da309dbd45011c4947546ea592d2b7e6a5505b
Author:     Brian Harring <ferringb <AT> gmail <DOT> com>
AuthorDate: Mon Nov 24 08:36:45 2025 +0000
Commit:     Brian Harring <ferringb <AT> gmail <DOT> com>
CommitDate: Mon Nov 24 08:39:01 2025 +0000
URL:        
https://gitweb.gentoo.org/proj/pkgcore/pkgcheck.git/commit/?id=70da309d

chore: cleanup generated files to be readable

Break this out to use full import paths (eliminating the
possibility of collision of names- checks.attrs and reporters.attrs for
example).

Additionally write this in a way a human can read it, rather than
single giant lines.  For example, it now writes this:

```
REPORTERS = (
  ('CsvReporter', pkgcheck.reporters.CsvReporter),
  ('FancyReporter', pkgcheck.reporters.FancyReporter),
  ('FlycheckReporter', pkgcheck.reporters.FlycheckReporter),
  ('FormatReporter', pkgcheck.reporters.FormatReporter),
  ('JsonReporter', pkgcheck.reporters.JsonReporter),
  ('JsonStream', pkgcheck.reporters.JsonStream),
  ('StrReporter', pkgcheck.reporters.StrReporter),
  ('XmlReporter', pkgcheck.reporters.XmlReporter),
)
```

rather than this:
```
REPORTERS = (('CsvReporter', reporters.CsvReporter), ('FancyReporter', 
reporters.FancyReporter), ('FlycheckReporter', reporters.FlycheckReporter), 
('FormatReporter', reporters.FormatReporter), ('JsonReporter', 
reporters.JsonReporter), ('JsonStream', reporters.JsonStream), ('StrReporter', 
reporters.StrReporter), ('XmlReporter', reporters.XmlReporter))
```

Finally, use fchmod since it's the correct 'secure' approach,
and since it's my muscle memory now.

Signed-off-by: Brian Harring <ferringb <AT> gmail.com>

 py_build.py             | 52 ++++++++++++++++++-------------------------------
 src/pkgcheck/objects.py |  6 +++---
 2 files changed, 22 insertions(+), 36 deletions(-)

diff --git a/py_build.py b/py_build.py
index dd3d8f91..f211eb45 100644
--- a/py_build.py
+++ b/py_build.py
@@ -1,5 +1,5 @@
+import os
 import sys
-from collections import defaultdict
 from contextlib import contextmanager
 from pathlib import Path
 from textwrap import dedent
@@ -30,7 +30,7 @@ def write_const(cleanup_files):
     cleanup_files.append(path := Path.cwd() / "src/pkgcheck/_const.py")
     print(f"writing path constants to {path}")
     with path.open("w") as f:
-        path.chmod(0o644)
+        os.fchmod(f.fileno(), 0o644)
         f.write(
             dedent(
                 """\
@@ -49,41 +49,27 @@ def write_objects(cleanup_files):
     cleanup_files.append(path := Path.cwd() / "src/pkgcheck/_objects.py")
     print(f"writing objects to {path}")
 
-    class _kls:
-        def __init__(self, module):
-            self.module = module
-
-        def __repr__(self):
-            return self.module
-
     with sys_path():
         from pkgcheck import objects
 
-    modules = defaultdict(set)
-    objs = defaultdict(list)
-    for obj in ("KEYWORDS", "CHECKS", "REPORTERS"):
-        for name, cls in getattr(objects, obj).items():
-            parent, module = cls.__module__.rsplit(".", 1)
-            modules[parent].add(module)
-            objs[obj].append((name, _kls(f"{module}.{name}")))
+    targets = ["CHECKS", "KEYWORDS", "REPORTERS"]
+    with path.open("w") as f:
+        os.fchmod(f.fileno(), 0o644)
+        modules = set()
+        for cls_type in targets:
+            modules.update(cls.__module__ for cls in getattr(objects, 
cls_type).values())
+        for module in sorted(modules):
+            f.write(f"import {module}\n")
 
-    keywords = tuple(objs["KEYWORDS"])
-    checks = tuple(objs["CHECKS"])
-    reporters = tuple(objs["REPORTERS"])
+        for cls_type in targets:
+            f.write("\n")
 
-    with path.open("w") as f:
-        path.chmod(0o644)
-        for k, v in sorted(modules.items()):
-            f.write(f"from {k} import {', '.join(sorted(v))}\n")
-        f.write(
-            dedent(
-                f"""\
-                    KEYWORDS = {keywords}
-                    CHECKS = {checks}
-                    REPORTERS = {reporters}
-                """
-            )
-        )
+            registry = getattr(objects, cls_type)
+
+            f.write(f"{cls_type} = (\n")
+            for name, cls in sorted(registry.items(), key=lambda x: x[0]):
+                f.write(f"  ({name!r}, {cls.__module__}.{cls.__name__}),\n")
+            f.write(")\n")
 
 
 def write_files(cleanup_files):
@@ -105,7 +91,7 @@ def write_files(cleanup_files):
     for obj in ("KEYWORDS", "CHECKS", "REPORTERS"):
         print(f"Generating {obj.lower()} list")
         cleanup_files.append(path := dst / obj.lower())
-        path.write_text("\n".join(getattr(objects, obj)) + "\n")
+        path.write_text("\n".join(sorted(getattr(objects, obj))) + "\n")
 
 
 @contextmanager

diff --git a/src/pkgcheck/objects.py b/src/pkgcheck/objects.py
index 51f2bed2..0c2a03d2 100644
--- a/src/pkgcheck/objects.py
+++ b/src/pkgcheck/objects.py
@@ -99,13 +99,13 @@ class _LazyDict(Mapping):
         return self._dict[key]
 
     def keys(self):
-        return iter(self._dict.keys())
+        return self._dict.keys()
 
     def values(self):
-        return iter(self._dict.values())
+        return self._dict.values()
 
     def items(self):
-        return iter(self._dict.items())
+        return self._dict.items()
 
     def select(self, cls):
         """Return mapping of object classes inheriting a given class."""

Reply via email to