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