gh-57141: Add dircmp shallow option (GH-109499)

Co-authored-by: Steve Ward <planet36@gmail.com>
Co-authored-by: Sanyam Khurana <8039608+CuriousLearner@users.noreply.github.com>
This commit is contained in:
Tobias Rautenkranz 2024-03-04 18:27:43 +01:00 committed by GitHub
parent ea1b1c579f
commit 60743a9a7e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 120 additions and 31 deletions

View file

@ -88,12 +88,15 @@ def _do_cmp(f1, f2):
class dircmp:
"""A class that manages the comparison of 2 directories.
dircmp(a, b, ignore=None, hide=None)
dircmp(a, b, ignore=None, hide=None, shallow=True)
A and B are directories.
IGNORE is a list of names to ignore,
defaults to DEFAULT_IGNORES.
HIDE is a list of names to hide,
defaults to [os.curdir, os.pardir].
SHALLOW specifies whether to just check the stat signature (do not read
the files).
defaults to True.
High level usage:
x = dircmp(dir1, dir2)
@ -121,7 +124,7 @@ class dircmp:
in common_dirs.
"""
def __init__(self, a, b, ignore=None, hide=None): # Initialize
def __init__(self, a, b, ignore=None, hide=None, shallow=True): # Initialize
self.left = a
self.right = b
if hide is None:
@ -132,6 +135,7 @@ class dircmp:
self.ignore = DEFAULT_IGNORES
else:
self.ignore = ignore
self.shallow = shallow
def phase0(self): # Compare everything except common subdirectories
self.left_list = _filter(os.listdir(self.left),
@ -184,7 +188,7 @@ class dircmp:
self.common_funny.append(x)
def phase3(self): # Find out differences between common files
xx = cmpfiles(self.left, self.right, self.common_files)
xx = cmpfiles(self.left, self.right, self.common_files, self.shallow)
self.same_files, self.diff_files, self.funny_files = xx
def phase4(self): # Find out differences between common subdirectories
@ -196,7 +200,8 @@ class dircmp:
for x in self.common_dirs:
a_x = os.path.join(self.left, x)
b_x = os.path.join(self.right, x)
self.subdirs[x] = self.__class__(a_x, b_x, self.ignore, self.hide)
self.subdirs[x] = self.__class__(a_x, b_x, self.ignore, self.hide,
self.shallow)
def phase4_closure(self): # Recursively call phase4() on subdirectories
self.phase4()