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

login
register
mail settings
Submitter phabricator
Date Dec. 12, 2019, 11:44 p.m.
Message ID <differential-rev-PHID-DREV-y7qvpi7t65mx6kmxwt2a-req@mercurial-scm.org>
Download mbox | patch
Permalink /patch/43768/
State Superseded
Headers show

Comments

phabricator - Dec. 12, 2019, 11:44 p.m.
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




To: martinvonz, #hg-reviewers
Cc: mercurial-devel
phabricator - Jan. 23, 2020, 10:03 p.m.
martinvonz added a comment.
martinvonz abandoned this revision.


  I think this was obsoleted by D7776 <https://phab.mercurial-scm.org/D7776>

REPOSITORY
  rHG Mercurial

CHANGES SINCE LAST ACTION
  https://phab.mercurial-scm.org/D7629/new/

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

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

Patch

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: [