[PATCH 03 of 10 V4] rcutil: extra directory listing logic

Jun Wu quark at fb.com
Mon Mar 27 00:49:16 EDT 2017


# HG changeset patch
# User Jun Wu <quark at fb.com>
# Date 1490586365 25200
#      Sun Mar 26 20:46:05 2017 -0700
# Node ID 30a341f7fd233e55615cab11a64a8a521f389783
# Parent  cd0c8320d216165bd8a62acc711e174ddef7a9f9
# Available At https://bitbucket.org/quark-zju/hg-draft
#              hg pull https://bitbucket.org/quark-zju/hg-draft -r 30a341f7fd23
rcutil: extra directory listing logic

The logic of listing a ".rc" directory is duplicated in two functions,
extract it to a single function to make the code cleaner.

diff --git a/mercurial/rcutil.py b/mercurial/rcutil.py
--- a/mercurial/rcutil.py
+++ b/mercurial/rcutil.py
@@ -25,4 +25,12 @@ systemrcpath = scmplatform.systemrcpath
 userrcpath = scmplatform.userrcpath
 
+def _expandrcpath(path):
+    '''path could be a file or a directory. return a list of file paths'''
+    p = util.expandpath(path)
+    if os.path.isdir(p):
+        join = os.path.join
+        return [join(p, f) for f, k in osutil.listdir(p) if f.endswith('.rc')]
+    return [p]
+
 def defaultrcpath():
     '''return rc paths in default.d'''
@@ -30,7 +38,5 @@ def defaultrcpath():
     defaultpath = os.path.join(util.datapath, 'default.d')
     if os.path.isdir(defaultpath):
-        for f, kind in osutil.listdir(defaultpath):
-            if f.endswith('.rc'):
-                path.append(os.path.join(defaultpath, f))
+        path = _expandrcpath(defaultpath)
     return path
 
@@ -50,11 +56,5 @@ def rcpath():
                 if not p:
                     continue
-                p = util.expandpath(p)
-                if os.path.isdir(p):
-                    for f, kind in osutil.listdir(p):
-                        if f.endswith('.rc'):
-                            _rcpath.append(os.path.join(p, f))
-                else:
-                    _rcpath.append(p)
+                _rcpath.extend(_expandrcpath(p))
         else:
             paths = defaultrcpath() + systemrcpath() + userrcpath()


More information about the Mercurial-devel mailing list