D7629: config: read defaultrc/ using `resources` module if available

martinvonz (Martin von Zweigbergk) phabricator at mercurial-scm.org
Thu Dec 12 18:44:48 EST 2019


martinvonz created this revision.
Herald added a subscriber: mercurial-devel.
Herald added a reviewer: hg-reviewers.

REVISION SUMMARY
  Similar to the an earlier patch for `hg help`. Here we also needed a
  way of listing resources/files in a package/directory, so I added that
  to the resourceutil module.
  
  Tested by running `$PYTHON ./hg config --debug` for each $PYTHON in
  {python2, python3.6, python3.7}.

REPOSITORY
  rHG Mercurial

BRANCH
  default

REVISION DETAIL
  https://phab.mercurial-scm.org/D7629

AFFECTED FILES
  mercurial/rcutil.py
  mercurial/utils/resourceutil.py
  tests/test-config-env.py

CHANGE DETAILS

diff --git a/tests/test-config-env.py b/tests/test-config-env.py
--- a/tests/test-config-env.py
+++ b/tests/test-config-env.py
@@ -13,6 +13,7 @@
 )
 
 from mercurial.utils import procutil
+from mercurial.utils import resourceutil
 
 testtmp = encoding.environ[b'TESTTMP']
 
@@ -36,7 +37,13 @@
     return [join(b'userrc')]
 
 
-extensions.wrapfunction(rcutil, 'defaultrcpath', lambda orig: [])
+def list_resources(orig, package):
+    if package != b'defaultrc':
+        for name in orig(package):
+            yield name
+
+
+extensions.wrapfunction(resourceutil, 'list_resources', list_resources)
 
 rcutil.systemrcpath = systemrcpath
 rcutil.userrcpath = userrcpath
diff --git a/mercurial/utils/resourceutil.py b/mercurial/utils/resourceutil.py
--- a/mercurial/utils/resourceutil.py
+++ b/mercurial/utils/resourceutil.py
@@ -11,9 +11,15 @@
 
 import imp
 import os
+import stat as statmod
 import sys
 
-from .. import pycompat
+from .. import (
+    policy,
+    pycompat,
+)
+
+osutil = policy.importmod('osutil')
 
 
 def mainfrozen():
@@ -48,6 +54,12 @@
             pycompat.sysstr(package), pycompat.sysstr(name)
         )
 
+    def list_resources(package):
+        package = b'mercurial.' + package
+        for name in importlib.resources.contents(pycompat.sysstr(package)):
+            if importlib.resources.is_resource(pycompat.sysstr(package), name):
+                yield pycompat.sysbytes(name)
+
 
 except AttributeError:
 
@@ -57,3 +69,8 @@
     def open_resouce(package, name):
         path = os.path.join(_package_path(package), name)
         return open(path, 'rb')
+
+    def list_resources(package):
+        for name, kind in osutil.listdir(_package_path(package)):
+            if kind == statmod.S_IFREG:
+                yield name
diff --git a/mercurial/rcutil.py b/mercurial/rcutil.py
--- a/mercurial/rcutil.py
+++ b/mercurial/rcutil.py
@@ -62,12 +62,6 @@
     return result
 
 
-def defaultrcpath():
-    '''return rc paths in defaultrc'''
-    defaultpath = os.path.join(resourceutil.datapath, b'defaultrc')
-    return _expandrcpath(defaultpath)
-
-
 def rccomponents():
     '''return an ordered [(type, obj)] about where to load configs.
 
@@ -92,13 +86,16 @@
             _rccomponents.extend((b'path', p) for p in _expandrcpath(p))
     else:
         default_entries = []
-        for path in defaultrcpath():
-            cfg = config.config()
-            cfg.read(path)
-            for section in cfg:
-                for name, value in cfg.items(section):
-                    source = cfg.source(section, name)
-                    default_entries.append((section, name, value, source))
+        for name in resourceutil.list_resources(b'defaultrc'):
+            if not name.endswith(b'.rc'):
+                continue
+            with resourceutil.open_resouce(b'defaultrc', name) as fp:
+                cfg = config.config()
+                cfg.read(pycompat.sysbytes(fp.name), fp=fp)
+                for section in cfg:
+                    for name, value in cfg.items(section):
+                        source = cfg.source(section, name)
+                        default_entries.append((section, name, value, source))
         _rccomponents = [(b'items', default_entries)]
 
         normpaths = lambda paths: [



To: martinvonz, #hg-reviewers
Cc: mercurial-devel


More information about the Mercurial-devel mailing list