Close #15442: Expand the list of default directories ignored by filecmp.dircmp and expose it as a module attribute

This commit is contained in:
Eli Bendersky 2013-01-12 06:13:32 -08:00
parent 60a0c71031
commit eb2884a875
4 changed files with 32 additions and 12 deletions

View File

@ -55,10 +55,10 @@ The :class:`dircmp` class
.. class:: dircmp(a, b, ignore=None, hide=None) .. class:: dircmp(a, b, ignore=None, hide=None)
Construct a new directory comparison object, to compare the directories *a* and Construct a new directory comparison object, to compare the directories *a*
*b*. *ignore* is a list of names to ignore, and defaults to ``['RCS', 'CVS', and *b*. *ignore* is a list of names to ignore, and defaults to
'tags']``. *hide* is a list of names to hide, and defaults to ``[os.curdir, :attr:`filecmp.DEFAULT_IGNORES`. *hide* is a list of names to hide, and
os.pardir]``. defaults to ``[os.curdir, os.pardir]``.
The :class:`dircmp` class compares files by doing *shallow* comparisons The :class:`dircmp` class compares files by doing *shallow* comparisons
as described for :func:`filecmp.cmp`. as described for :func:`filecmp.cmp`.
@ -164,6 +164,12 @@ The :class:`dircmp` class
A dictionary mapping names in :attr:`common_dirs` to :class:`dircmp` A dictionary mapping names in :attr:`common_dirs` to :class:`dircmp`
objects. objects.
.. attribute:: DEFAULT_IGNORES
.. versionadded:: 3.3
List of directories ignored by :class:`dircmp` by default.
Here is a simplified example of using the ``subdirs`` attribute to search Here is a simplified example of using the ``subdirs`` attribute to search
recursively through two directories to show common different files:: recursively through two directories to show common different files::

View File

@ -13,11 +13,15 @@ import os
import stat import stat
from itertools import filterfalse from itertools import filterfalse
__all__ = ["cmp", "dircmp", "cmpfiles"] __all__ = ['cmp', 'dircmp', 'cmpfiles', 'DEFAULT_IGNORES']
_cache = {} _cache = {}
BUFSIZE = 8*1024 BUFSIZE = 8*1024
DEFAULT_IGNORES = [
'RCS', 'CVS', 'tags', '.git', '.hg', '.bzr', '_darcs', '__pycache__']
def cmp(f1, f2, shallow=True): def cmp(f1, f2, shallow=True):
"""Compare two files. """Compare two files.
@ -80,7 +84,7 @@ class dircmp:
dircmp(a, b, ignore=None, hide=None) dircmp(a, b, ignore=None, hide=None)
A and B are directories. A and B are directories.
IGNORE is a list of names to ignore, IGNORE is a list of names to ignore,
defaults to ['RCS', 'CVS', 'tags']. defaults to DEFAULT_IGNORES.
HIDE is a list of names to hide, HIDE is a list of names to hide,
defaults to [os.curdir, os.pardir]. defaults to [os.curdir, os.pardir].
@ -116,7 +120,7 @@ class dircmp:
else: else:
self.hide = hide self.hide = hide
if ignore is None: if ignore is None:
self.ignore = ['RCS', 'CVS', 'tags'] # Names ignored in comparison self.ignore = DEFAULT_IGNORES
else: else:
self.ignore = ignore self.ignore = ignore

View File

@ -1,4 +1,3 @@
import os, filecmp, shutil, tempfile import os, filecmp, shutil, tempfile
import unittest import unittest
from test import support from test import support
@ -46,9 +45,14 @@ class DirCompareTestCase(unittest.TestCase):
self.dir = os.path.join(tmpdir, 'dir') self.dir = os.path.join(tmpdir, 'dir')
self.dir_same = os.path.join(tmpdir, 'dir-same') self.dir_same = os.path.join(tmpdir, 'dir-same')
self.dir_diff = os.path.join(tmpdir, 'dir-diff') self.dir_diff = os.path.join(tmpdir, 'dir-diff')
# Another dir is created under dir_same, but it has a name from the
# ignored list so it should not affect testing results.
self.dir_ignored = os.path.join(self.dir_same, '.hg')
self.caseinsensitive = os.path.normcase('A') == os.path.normcase('a') self.caseinsensitive = os.path.normcase('A') == os.path.normcase('a')
data = 'Contents of file go here.\n' data = 'Contents of file go here.\n'
for dir in [self.dir, self.dir_same, self.dir_diff]: for dir in (self.dir, self.dir_same, self.dir_diff, self.dir_ignored):
shutil.rmtree(dir, True) shutil.rmtree(dir, True)
os.mkdir(dir) os.mkdir(dir)
if self.caseinsensitive and dir is self.dir_same: if self.caseinsensitive and dir is self.dir_same:
@ -64,9 +68,11 @@ class DirCompareTestCase(unittest.TestCase):
output.close() output.close()
def tearDown(self): def tearDown(self):
shutil.rmtree(self.dir) for dir in (self.dir, self.dir_same, self.dir_diff):
shutil.rmtree(self.dir_same) shutil.rmtree(dir)
shutil.rmtree(self.dir_diff)
def test_default_ignores(self):
self.assertIn('.hg', filecmp.DEFAULT_IGNORES)
def test_cmpfiles(self): def test_cmpfiles(self):
self.assertTrue(filecmp.cmpfiles(self.dir, self.dir, ['file']) == self.assertTrue(filecmp.cmpfiles(self.dir, self.dir, ['file']) ==

View File

@ -1387,6 +1387,10 @@ Documentation
- Issue #15250: Document that `filecmp.dircmp()` compares files shallowly. Patch - Issue #15250: Document that `filecmp.dircmp()` compares files shallowly. Patch
contributed by Chris Jerdonek. contributed by Chris Jerdonek.
- Issue #15442: Expose the default list of directories ignored by
`filecmp.dircmp()` as a module attribute, and expand the list to more modern
values.
Tests Tests
----- -----