本文整理匯總了Python中filecmp.dircmp方法的典型用法代碼示例。如果您正苦於以下問題:Python filecmp.dircmp方法的具體用法?Python filecmp.dircmp怎麽用?Python filecmp.dircmp使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類filecmp
的用法示例。
在下文中一共展示了filecmp.dircmp方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: same_dirs
# 需要導入模塊: import filecmp [as 別名]
# 或者: from filecmp import dircmp [as 別名]
def same_dirs(a, b):
"""Check that structure and files are the same for directories a and b
Args:
a (str): The path to the first directory
b (str): The path to the second directory
"""
comp = filecmp.dircmp(a, b)
common = sorted(comp.common)
left = sorted(comp.left_list)
right = sorted(comp.right_list)
if left != common or right != common:
return False
if len(comp.diff_files):
return False
for subdir in comp.common_dirs:
left_subdir = os.path.join(a, subdir)
right_subdir = os.path.join(b, subdir)
return same_dirs(left_subdir, right_subdir)
return True
示例2: copyCustomLDAPSchema
# 需要導入模塊: import filecmp [as 別名]
# 或者: from filecmp import dircmp [as 別名]
def copyCustomLDAPSchema(backup):
logging.info("Copying the Custom LDAP Schema")
backup_schema_dir = os.path.join(backup, 'opt/opendj/config/schema/')
present_schema_dir = '/opt/opendj/config/schema/'
custom_files = ['100-user.ldif', '99-user.ldif']
for cf in custom_files:
if os.path.isfile(os.path.join(backup_schema_dir, cf)):
shutil.copyfile(
os.path.join(backup_schema_dir, cf),
os.path.join(present_schema_dir, cf)
)
# Copy the extra files like user created custom schema files
diff = filecmp.dircmp(backup_schema_dir, present_schema_dir)
for ldif_file in diff.left_only:
shutil.copyfile(
os.path.join(backup_schema_dir, ldif_file),
os.path.join(present_schema_dir, ldif_file)
)
示例3: test_veros_copy_setup
# 需要導入模塊: import filecmp [as 別名]
# 或者: from filecmp import dircmp [as 別名]
def test_veros_copy_setup(runner):
with tempfile.TemporaryDirectory() as tempdir:
for setup in ('acc', 'acc_sector', 'global_4deg', 'global_1deg',
'global_flexible', 'north_atlantic', 'wave_propagation'):
result = runner.invoke(veros.cli.veros_copy_setup.cli, [setup, '--to', os.path.join(tempdir, setup)])
assert result.exit_code == 0, setup
assert not result.output
outpath = os.path.join(tempdir, setup)
srcpath = pkg_resources.resource_filename('veros', 'setup/%s' % setup)
ignore = [f for f in os.listdir(srcpath) if any(
fnmatch.fnmatch(f, pattern) for pattern in veros.cli.veros_copy_setup.IGNORE_PATTERNS
)]
ignore.append('version.txt')
comparer = filecmp.dircmp(outpath, srcpath, ignore=ignore)
assert not comparer.left_only and not comparer.right_only and not comparer.diff_files
示例4: _is_same_helper
# 需要導入模塊: import filecmp [as 別名]
# 或者: from filecmp import dircmp [as 別名]
def _is_same_helper(dircmp):
assert not dircmp.funny_files
if dircmp.left_only or dircmp.right_only or dircmp.diff_files or dircmp.funny_files:
for name in dircmp.diff_files:
left_file = os.path.join(dircmp.left, name)
right_file = os.path.join(dircmp.right, name)
print('>>> Two files are different:', left_file, 'AND', right_file)
diff = list(difflib.unified_diff(open(left_file).readlines(),
open(right_file).readlines()))
for line in diff[:20]:
print(line, end='')
if len(diff) > 20:
print('-- diff too long, it was truncated')
print()
else:
print('>>> Full file-tree diff:')
dircmp.report_full_closure()
return False
for sub_dircmp in dircmp.subdirs.values():
if not _is_same_helper(sub_dircmp):
return False
return True
示例5: test_example
# 需要導入模塊: import filecmp [as 別名]
# 或者: from filecmp import dircmp [as 別名]
def test_example(example):
cwd = os.path.join(directory, example)
gen_dir = os.path.join(directory, example, gtd)
ex_dir = os.path.join(gen_dir, exd)
ac_dir = os.path.join(gen_dir, acd)
py_file = os.path.join(gen_dir, pyf)
assert os.path.exists(py_file), "No test file found: %s" % py_file
if os.path.exists(ac_dir):
shutil.rmtree(ac_dir, ignore_errors=True)
os.mkdir(ac_dir)
scope = run_python(py_file, example, cwd, ac_dir)
gen_docs(scope, gen_dir, cwd)
if os.path.exists(ex_dir):
diff = filecmp.dircmp(ex_dir, ac_dir)
check_diff(diff)
示例6: test_compress_files_2
# 需要導入模塊: import filecmp [as 別名]
# 或者: from filecmp import dircmp [as 別名]
def test_compress_files_2(tmp_path):
tmp_path.joinpath('src').mkdir()
tmp_path.joinpath('tgt').mkdir()
py7zr.unpack_7zarchive(os.path.join(testdata_path, 'test_2.7z'), path=tmp_path.joinpath('src'))
target = tmp_path.joinpath('target.7z')
os.chdir(str(tmp_path.joinpath('src')))
archive = py7zr.SevenZipFile(target, 'w')
archive.set_encoded_header_mode(False)
archive.writeall('.')
archive.close()
reader = py7zr.SevenZipFile(target, 'r')
reader.extractall(path=tmp_path.joinpath('tgt'))
reader.close()
dc = filecmp.dircmp(tmp_path.joinpath('src'), tmp_path.joinpath('tgt'))
assert dc.diff_files == []
#
if shutil.which('7z'):
result = subprocess.run(['7z', 't', (tmp_path / 'target.7z').as_posix()], stdout=subprocess.PIPE)
if result.returncode != 0:
print(result.stdout)
pytest.fail('7z command report error')
示例7: test_compress_files_3
# 需要導入模塊: import filecmp [as 別名]
# 或者: from filecmp import dircmp [as 別名]
def test_compress_files_3(tmp_path):
tmp_path.joinpath('src').mkdir()
tmp_path.joinpath('tgt').mkdir()
py7zr.unpack_7zarchive(os.path.join(testdata_path, 'test_3.7z'), path=tmp_path.joinpath('src'))
target = tmp_path.joinpath('target.7z')
os.chdir(tmp_path.joinpath('src'))
archive = py7zr.SevenZipFile(target, 'w')
archive.set_encoded_header_mode(False)
archive.writeall('.')
archive.close()
reader = py7zr.SevenZipFile(target, 'r')
reader.extractall(path=tmp_path.joinpath('tgt'))
reader.close()
dc = filecmp.dircmp(tmp_path.joinpath('src'), tmp_path.joinpath('tgt'))
assert dc.diff_files == []
#
if shutil.which('7z'):
result = subprocess.run(['7z', 't', (tmp_path / 'target.7z').as_posix()], stdout=subprocess.PIPE)
if result.returncode != 0:
print(result.stdout)
pytest.fail('7z command report error')
示例8: needs_update
# 需要導入模塊: import filecmp [as 別名]
# 或者: from filecmp import dircmp [as 別名]
def needs_update(source, dest):
"""Check if a file to be linked/copied needs to be updated."""
if os.path.islink(dest):
# No need to delete a link and relink -> skip this
return False
elif os.path.isdir(dest):
diffs = filecmp.dircmp(source, dest)
ignored = get_ignored_files(source, diffs.left_only)
has_new_files = set(ignored) != set(diffs.left_only)
return (has_new_files or diffs.right_only or diffs.common_funny or
diffs.diff_files or diffs.funny_files)
else:
return not filecmp.cmp(source, dest)
示例9: _assert_dir
# 需要導入模塊: import filecmp [as 別名]
# 或者: from filecmp import dircmp [as 別名]
def _assert_dir(self, dir_path, fn_correct, fail_on_funny=True):
"""Compare directory `dir_path` to compressed directory `fn_correct`."""
correct_path = os.path.join(self.files_path, fn_correct)
if not os.path.isfile(correct_path):
with tarfile.open(correct_path, "w:gz") as f:
for content in os.listdir(dir_path):
f.add(os.path.join(dir_path, content), arcname=content)
self.fail(
msg="Compressed output directory {} missing so it was created.".format(
fn_correct
)
)
if not tarfile.is_tarfile(correct_path):
self.fail(msg="{} is not a tar file.".format(fn_correct))
with tempfile.TemporaryDirectory() as temp_dir:
with tarfile.open(correct_path) as tar:
tar.extractall(temp_dir)
cmp = filecmp.dircmp(dir_path, temp_dir)
if (
cmp.left_only
or cmp.right_only
or cmp.diff_files
or (fail_on_funny and cmp.funny_files)
):
self.fail(
msg="Directory {} content mismatch: {}.".format(
fn_correct, cmp.report()
)
)
示例10: test1
# 需要導入模塊: import filecmp [as 別名]
# 或者: from filecmp import dircmp [as 別名]
def test1(self):
expected_dir = os.path.join(DATA_DIR, "setup_reusable_addons")
generated_dir = os.path.join(DATA_DIR, "setup")
make_default_setup.main(["--addons-dir", DATA_DIR, "-f"])
dc = filecmp.dircmp(expected_dir, generated_dir)
try:
self._assert_no_diff(dc)
finally:
shutil.rmtree(generated_dir)
示例11: compare_dirs
# 需要導入模塊: import filecmp [as 別名]
# 或者: from filecmp import dircmp [as 別名]
def compare_dirs(dir1, dir2):
"Tests whether two folders, including all their contents and subdirectories are identical or not (True == they're the same, False == they are not the same)"
dc = filecmp.dircmp(dir1,dir2)
if len(dc.funny_files) > 0 or len(dc.left_only) > 0 or len(dc.right_only) > 0 or len(dc.diff_files) > 0:
dc.report()
return False
else:
for subdir in dc.common_dirs:
if not compare_dirs(dir1 + "\\" + subdir, dir2 + "\\" + subdir):
return False
return True
示例12: main
# 需要導入模塊: import filecmp [as 別名]
# 或者: from filecmp import dircmp [as 別名]
def main():
if len(sys.argv)!=3:
print 'Usage: CompareDirs <dir1> <dir2>'
sys.exit(-1)
if compare_dirs(sys.argv[1],sys.argv[2]):
print "The directories are identical"
sys.exit(0)
else: #the part that differed is explained via dircmp.report() above
print "The directories differ"
sys.exit(1)
示例13: test_dircmp
# 需要導入模塊: import filecmp [as 別名]
# 或者: from filecmp import dircmp [as 別名]
def test_dircmp(self):
# Check attributes for comparison of two identical directories
d = filecmp.dircmp(self.dir, self.dir_same)
if self.caseinsensitive:
self.assertEqual([d.left_list, d.right_list],[['file'], ['FiLe']])
else:
self.assertEqual([d.left_list, d.right_list],[['file'], ['file']])
self.assertTrue(d.common == ['file'])
self.assertTrue(d.left_only == d.right_only == [])
self.assertTrue(d.same_files == ['file'])
self.assertTrue(d.diff_files == [])
# Check attributes for comparison of two different directories
d = filecmp.dircmp(self.dir, self.dir_diff)
self.assertTrue(d.left_list == ['file'])
self.assertTrue(d.right_list == ['file', 'file2'])
self.assertTrue(d.common == ['file'])
self.assertTrue(d.left_only == [])
self.assertTrue(d.right_only == ['file2'])
self.assertTrue(d.same_files == ['file'])
self.assertTrue(d.diff_files == [])
# Add different file2
output = open(os.path.join(self.dir, 'file2'), 'w')
output.write('Different contents.\n')
output.close()
d = filecmp.dircmp(self.dir, self.dir_diff)
self.assertTrue(d.same_files == ['file'])
self.assertTrue(d.diff_files == ['file2'])
示例14: _dirs_equal
# 需要導入模塊: import filecmp [as 別名]
# 或者: from filecmp import dircmp [as 別名]
def _dirs_equal(dircmp):
if dircmp.left_only or dircmp.right_only or dircmp.diff_files:
return False
return all(
_dirs_equal(sub_dircmp) for sub_dircmp in six.itervalues(dircmp.subdirs))
示例15: assert_dirs_equal
# 需要導入模塊: import filecmp [as 別名]
# 或者: from filecmp import dircmp [as 別名]
def assert_dirs_equal(self, dir1, dir2):
"""Assert that 2 dirs are equal."""
self.assertTrue(_dirs_equal(filecmp.dircmp(dir1, dir2)))