我们从Python开源项目中,提取了以下48个代码示例,用于说明如何使用filecmp.dircmp()。
def test_both_dirs_exist(self): self.source_tree = { "file1": "stuff", "file2": "stuff", "dir1": { "file3": "stuff" } } self.dest_tree = { "file4": "stuff", "file5": "stuff", "dir2": { "file6": "stuff" } } create_files_from_tree(self.source_tree, parent=self.source_dir) create_files_from_tree(self.dest_tree, parent=self.dest_dir) self.link_and_print() result = dircmp(self.source_dir, self.dest_dir) self.recursive_dircmp(result)
def test_extract_paths(tmpdir, randomized_dir, vms, vm_name, mode): if mode == 'dead': pytest.skip('extract_paths_dead is not stable in CI') vm = vms[vm_name] dst = '/root/extract-{vm}-{mode}'.format(vm=vm_name, mode=mode) vm.copy_to(randomized_dir.path, dst, recursive=True) res = vm.ssh(['sync']) assert res.code == 0 if mode == 'normal': extract = getattr(vm, 'extract_paths') elif mode == 'dead': extract = getattr(vm, 'extract_paths_dead') extracted_path = str(tmpdir) extract([(dst, extracted_path)], ignore_nopath=False) cmp_res = filecmp.dircmp( os.path.join(extracted_path, os.path.basename(dst)), randomized_dir.path ) assert sorted(cmp_res.left_list) == sorted(cmp_res.right_list) # todo: Test extract_paths_dead once we figure why it's unstable in CI
def stdout_file(self,path1,path2): oldStdout = None logfile = None x = filecmp.dircmp(path1,path2) path1 = os.path.abspath(sys.argv[0]) filepath = os.path.dirname(path1) self.find_file = filepath + "\\find_file.log" try: logfile = open( self.find_file,'w+') oldStdout = sys.stdout sys.stdout = logfile x.report_full_closure() finally: if logfile: logfile.close() if oldStdout: sys.stdout = oldStdout result = self.find_str(self.find_file) return result
def phase0(self): return super(dircmp, self).phase0()
def phase1(self): return super(dircmp, self).phase1()
def phase2(self): return super(dircmp, self).phase2()
def phase4(self): # Find out differences between common subdirectories # A new dircmp object is created for each common subdirectory, # these are stored in a dictionary indexed by filename. # The hide and ignore properties are inherited from the parent self.subdirs = {} 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)
def _dirs_are_unequal(dir1, dir2): """Returns True if dir1 and dir2 are unequal""" dircmps = [filecmp.dircmp(dir1, dir2)] while len(dircmps): dircmp = dircmps.pop() if dircmp.left_only or dircmp.right_only: logger.error("The following files and directories are only " "present in one directory") if dircmp.left_only: logger.error(dircmp.left_only) else: logger.error(dircmp.right_only) return True elif dircmp.common_funny or dircmp.funny_files: logger.error("The following files and directories could not be " "compared:") if dircmp.common_funny: logger.error(dircmp.common_funny) else: logger.error(dircmp.funny_files) return True elif dircmp.diff_files: logger.error("The following files differ:") logger.error(dircmp.diff_files) return True for subdir in dircmp.subdirs.itervalues(): dircmps.append(subdir) return False
def check_core_alteration(self, dir_path, core_url): alterations = [] ignored = [".git", "cache", "plugins", "themes", "images", "license.txt", "readme.html", "version.php"] temp_directory = uCMS.TempDir.create() log.print_cms("info", "[+] Checking core alteration", "", 0) try: response = requests.get(core_url) response.raise_for_status() if response.status_code == 200: zip_file = zipfile.ZipFile(io.BytesIO(response.content), 'r') zip_file.extractall(temp_directory) zip_file.close() except requests.exceptions.HTTPError as e: msg = "[-] The original WordPress archive has not been found. Search manually ! " log.print_cms("alert", msg, "", 0) return msg, e clean_core_path = os.path.join(temp_directory, "wordpress") dcmp = dircmp(clean_core_path, dir_path, ignored) uCMS.diff_files(dcmp, alterations, dir_path) if alterations is not None: msg = "[+] For further analysis, archive downloaded here : " + clean_core_path log.print_cms("info", msg, "", 1) return alterations, None
def check_core_alteration(self, dir_path, version_core, core_url): alterations = [] ignored = ["modules", "CHANGELOG.txt", "COPYRIGHT.txt", "LICENSE.txt", "MAINTAINERS.txt", "INSTALL.txt", "README.txt"] temp_directory = uCMS.TempDir.create() log.print_cms("info", "[+] Checking core alteration", "", 0) try: response = requests.get(core_url) response.raise_for_status() if response.status_code == 200: zip_file = zipfile.ZipFile(io.BytesIO(response.content), 'r') zip_file.extractall(temp_directory) zip_file.close() except requests.exceptions.HTTPError as e: msg = "[-] The original drupal archive has not been found. Search " \ "manually ! " log.print_cms("alert", msg, "", 0) return msg, e clean_core_path = os.path.join(temp_directory, "drupal-" + version_core) dcmp = dircmp(clean_core_path, dir_path, ignored) uCMS.diff_files(dcmp, alterations, dir_path) return alterations, None
def build_common_files(dcmp): # listing common files in dir commonFiles = [] for filename in dcmp.common_files: commonFiles.append(dcmp.left + "/" + filename) # listing in sub-dirs for subdir in dcmp.common_dirs: subCommonFiles = build_common_files(dircmp(dcmp.left + "/" + subdir, dcmp.right + "/" + subdir)) for filename in subCommonFiles: commonFiles.append(filename) commonFiles.sort() return commonFiles # HASHING A FILE, READ BY 16M CHUNKS NOT TO OVERLOAD MEMORY
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.assertEqual(d.common, ['file']) self.assertTrue(d.left_only == d.right_only == []) self.assertEqual(d.same_files, ['file']) self.assertEqual(d.diff_files, []) # Check attributes for comparison of two different directories d = filecmp.dircmp(self.dir, self.dir_diff) self.assertEqual(d.left_list, ['file']) self.assertTrue(d.right_list == ['file', 'file2']) self.assertEqual(d.common, ['file']) self.assertEqual(d.left_only, []) self.assertEqual(d.right_only, ['file2']) self.assertEqual(d.same_files, ['file']) self.assertEqual(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.assertEqual(d.same_files, ['file']) self.assertEqual(d.diff_files, ['file2'])
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)
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'])
def report_difference(casedirpath): # get the directories to be compared refpath = os.path.join(casedirpath, "ref") outpath = os.path.join(casedirpath, "out") if not os.path.isdir(refpath): print "Test case has no reference directory" return if not os.path.isdir(refpath): print "Test case has no output directory" return # check for recursive subdirectories if len(filter(lambda fn: os.path.isdir(fn), os.listdir(refpath))) > 0: print "Reference directory contains a subdirectory" return if len(filter(lambda fn: os.path.isdir(fn), os.listdir(outpath))) > 0: print "Output directory contains a sub directory" return # verify list of filenames dircomp = filecmp.dircmp(outpath, refpath, ignore=['.DS_Store']) if (len(dircomp.left_only) > 0): print "Output contains " + str(len(dircomp.left_only)) + " extra file(s)" if (len(dircomp.right_only) > 0): print "Output misses " + str(len(dircomp.right_only)) + " file(s)" # compare common files matches, mismatches, errors = filecmp.cmpfiles(outpath, refpath, dircomp.common, shallow=False) for filename in matches: print "Output file matches: " + filename for filename in mismatches + errors: if equalfiles(os.path.join(outpath, filename), os.path.join(refpath, filename)): print "Output file matches: " + filename else: print "Output file differs: " + filename + " <-------" # ----------------------------------------------------------------- # get the command-line argument specifying the target directory, if any
def test_dircmp(self): # Check attributes for comparison of two identical directories left_dir, right_dir = self.dir, self.dir_same d = filecmp.dircmp(left_dir, right_dir) self.assertEqual(d.left, left_dir) self.assertEqual(d.right, right_dir) 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.assertEqual(d.common, ['file']) self.assertTrue(d.left_only == d.right_only == []) self.assertEqual(d.same_files, ['file']) self.assertEqual(d.diff_files, []) # Check attributes for comparison of two different directories left_dir, right_dir = self.dir, self.dir_diff d = filecmp.dircmp(left_dir, right_dir) self.assertEqual(d.left, left_dir) self.assertEqual(d.right, right_dir) self.assertEqual(d.left_list, ['file']) self.assertTrue(d.right_list == ['file', 'file2']) self.assertEqual(d.common, ['file']) self.assertEqual(d.left_only, []) self.assertEqual(d.right_only, ['file2']) self.assertEqual(d.same_files, ['file']) self.assertEqual(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.assertEqual(d.same_files, ['file']) self.assertEqual(d.diff_files, ['file2'])
def check_full_vs_incr(self, seq, sha, stdout=sys.stdout): full = self.saved_build_state_path(seq, 'full', sha) incr = self.saved_build_state_path(seq, 'incr', sha) common.debug_print("Comparing dirs %s vs. %s" % (os.path.relpath(full), os.path.basename(incr)), stderr=stdout) d = filecmp.dircmp(full, incr, self.ignored_differences()) if not have_same_trees(full, incr, d): message = ("Dirs differ: %s vs. %s" % (os.path.relpath(full), os.path.basename(incr))) if self.expect_determinism(): raise EarlyExit(ActionResult(Result.FAIL, message)) else: common.debug_print(message, stderr=stdout)
def manpath_select(select: bool=True) -> Union[str, List[str]]: """ Parses the output of the 'manpath' program and returns one of its non-empty results (non-empty directory) if 'select' is set to True; otherwise returns all the results un-altered. NOTE: A platform-dependent path separator will be appended to the result. """ paths = None result_manpath = None with os.popen("manpath") as proc, TemporaryDirectory() as tmpdir: paths = proc.read().strip().split(os.pathsep) if select: for candidate in paths: # "Elect" the candidate directory with "rich" non-empty status if dircmp(candidate, tmpdir).left_only: result_manpath = candidate break if not paths: raise RuntimeError("Output of the 'manpath' program cannot be parsed") if select and not result_manpath: raise RuntimeError("All the directories in 'manpath' is empty") if select: if result_manpath.endswith(os.sep): return result_manpath else: return result_manpath + os.sep else: return [path + os.sep for path in paths if not path.endswith(os.sep)] # -------------------------------- FUNCTIONS ----------------------------------
def diffdir(d1, d2): result = dircmp(d1, d2) return find_diff_files(result)
def find_diff_files(dcmp): '''Return list of differing or missing relative filenames. Args ---- dcmp: class filecmp.dircmp ''' # print('left ', dcmp.left_only) # print('right ', dcmp.right_only) # print('diff ', dcmp.diff_files) left_only = filter_files(dcmp.left_only, dcmp.left) right_only = filter_files(dcmp.right_only, dcmp.right) diff_files = filter_files(dcmp.diff_files, dcmp.right) result = left_only + right_only + diff_files for sub in dcmp.subdirs.values(): result += find_diff_files(sub) return result # copyright - Add or replace license boilerplate. # Copyright (C) 2016 Remik Ziemlinski # # copyright is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # copyright is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>.
def test_report_partial_closure(self): left_dir, right_dir = self.dir, self.dir_same d = filecmp.dircmp(left_dir, right_dir) expected_report = [ "diff {} {}".format(self.dir, self.dir_same), "Identical files : ['file']", ] self._assert_report(d.report_partial_closure, expected_report)
def test_report_full_closure(self): left_dir, right_dir = self.dir, self.dir_same d = filecmp.dircmp(left_dir, right_dir) expected_report = [ "diff {} {}".format(self.dir, self.dir_same), "Identical files : ['file']", ] self._assert_report(d.report_full_closure, expected_report)
def assertDirsIdentical(self, dir1, dir2): result = filecmp.dircmp(dir1, dir2) if result.diff_files or result.left_only or result.right_only: raise AssertionError( "'%s' and '%s' is not exactly identical.\n" "Diff files: %r\n" "Left only: %r\n" "Right only: %r\n" % ( dir1, dir2, result.diff_files, result.left_only, result.right_only))
def test_dircmp_diff_files_accuracy(self): """Different files are identified as such using filecmp.dircmp()""" file1 = os.path.join(self.folder1, 'hello_world.txt') with open(file1, 'w') as file: file.write('foo') file2 = os.path.join(self.folder2, 'hello_world.txt') with open(file2, 'w') as file: file.write('bar') comparison = filecmp.dircmp(self.folder1, self.folder2) self.assertTrue(comparison.diff_files == ['hello_world.txt']) self.assertTrue(comparison.same_files == [])
def is_same(dir1, dir2, **kwargs): """ Compare two directory trees content. Return False if they differ, True is they are the same. """ compared = dircmp(dir1, dir2, **kwargs) if (compared.left_only or compared.right_only or compared.diff_files or compared.funny_files): return False for subdir in compared.common_dirs: if not is_same(os.path.join(dir1, subdir), os.path.join(dir2, subdir), **kwargs): return False return True
def is_same2(dir1, dir2, raise_exc=True, **kwargs): """ Compare two directory trees content. Only compare files in both directories. Return False if they differ, True is they are the same. """ cmp = filecmp.dircmp(dir1, dir2, **kwargs) return _dir_cmp(cmp, raise_exc)
def test_destination_exists(self): self.dest_tree = { "file1": "stuff", "file2": "stuff", "dir1": { "file3": "stuff" } } create_files_from_tree(self.dest_tree, parent=self.dest_dir) self.link_and_print() dircmp(self.source_dir, self.dest_dir) result = dircmp(self.source_dir, self.dest_dir) self.recursive_dircmp(result)
def test_source_exists(self): self.source_tree = { "file1": "stuff", "file2": "stuff", "dir1": { "file3": "stuff" } } create_files_from_tree(self.source_tree, parent=self.source_dir) self.link_and_print() dircmp(self.source_dir, self.dest_dir) result = dircmp(self.source_dir, self.dest_dir) self.recursive_dircmp(result)
def assertEqualPaths(self, x, y): dircmp = filecmp.dircmp(x, y) self.assertEmpty(dircmp.left_only) self.assertEmpty(dircmp.right_only) self.assertEmpty(dircmp.diff_files) # # def tearDown(self): # for path in self.temp_data: # if os.path.isdir(path): # shutil.rmtree(path) # elif os.path.exists(path): # os.remove(path)
def _diff(self, baselinedir, outputdir, dc=None): if dc is None: dc = filecmp.dircmp(baselinedir, outputdir, ['.svn']) if dc.left_only: self.fail("Files or subdirectories missing from output: " +str(dc.left_only)) if dc.right_only: self.fail("Files or subdirectories missing from baseline: " +str(dc.right_only)) for name in dc.diff_files: fromfile = join(dc.left, name) tofile = join(dc.right, name) with open(fromfile, 'r') as f_from: fromlines = f_from.readlines() with open(tofile, 'r') as f_to: tolines = f_to.readlines() diff = difflib.context_diff(fromlines, tolines, fromfile+" (baseline)", tofile+" (output)") diff = list(diff) # The filecmp.dircmp function does a weaker # comparison that can sometimes lead to false # positives. Make sure the true diff is not empty # before we call this a failure. if len(diff) > 0: out = StringIO() out.write("Output file does not match baseline:\n") for line in diff: out.write(line) self.fail(out.getvalue()) for subdir in dc.subdirs: self._diff(join(baselinedir, subdir), join(outputdir, subdir), dc=dc.subdirs[subdir]) shutil.rmtree(outputdir, ignore_errors=True)
def path_cmp(self,PATH1,PATH2): self.File1 = PATH1 self.File2 = PATH2 if self.path_exist(self.File1)==False: return False if self.path_exist(self.File2)==False: return False #filecmp.dircmp(self.File1,self.File2) result = self.stdout_file(self.File1,self.File2) return result
def analyze(dcres, wpPath): """Get extra, changed and missing files from a dircmp results object. From dircmp results find: 1. Any files that differ. 2. Extra files in left that are not in right. 3. Missing files that are in right but not in left. Recursively enter sub directories to continue search. When searching for extra files, certain files and directories that are user modifiable should be ignored. For example, 'wp-content/themes' will likely contain unique files that will not appear in a new copy of the same WordPress version. These should be ignored to avoid giving false positives. """ diff = set() extra = set() missing = set() # 1. Get modified files [diff.add(os.path.join(dcres.left, f)) for f in dcres.diff_files] # 2. Get extra files for name in dcres.left_only: path = os.path.join(dcres.left, name) if not ignored_file(path, wpPath): # ignore user modified if not os.path.isdir(path): # do not add directories extra.add(path) # 3. Get missing files [missing.add(os.path.join(dcres.right, f)) for f in dcres.right_only] # Recurse into each sub dir for sub_dcres in dcres.subdirs.values(): newDiff, newExtra, newMissing = analyze(sub_dcres, wpPath) diff = diff.union(newDiff) extra = extra.union(newExtra) missing = missing.union(newMissing) return diff, extra, missing
def reorder(miz_file_path, target_dir, skip_options_file): LOGGER.debug('re-ordering miz file: {}'.format(miz_file_path)) LOGGER.debug('destination folder: {}'.format(target_dir)) LOGGER.debug('{}option file'.format('skipping' if skip_options_file else 'including')) if not Path(target_dir).exists(): LOGGER.debug(f'creating directory {target_dir}') Path(target_dir).makedirs() with Miz(miz_file_path, overwrite=True) as m: def mirror_dir(src, dst): LOGGER.debug('{} -> {}'.format(src, dst)) diff_ = dircmp(src, dst, ignore) diff_list = diff_.left_only + diff_.diff_files LOGGER.debug('differences: {}'.format(diff_list)) for x in diff_list: source = Path(diff_.left).joinpath(x) target = Path(diff_.right).joinpath(x) LOGGER.debug('looking at: {}'.format(x)) if source.isdir(): LOGGER.debug('isdir: {}'.format(x)) if not target.exists(): LOGGER.debug('creating: {}'.format(x)) target.mkdir() mirror_dir(source, target) else: LOGGER.debug('copying: {}'.format(x)) source.copy2(diff_.right) for sub in diff_.subdirs.values(): assert isinstance(sub, dircmp) mirror_dir(sub.left, sub.right) m._encode() if skip_options_file: ignore = ['options'] else: ignore = [] mirror_dir(m.tmpdir, target_dir)
def check_addon_alteration(self, addon, dir_path, temp_directory): addon_url = "{}{}.{}.zip".format(self.download_addon_url, addon["name"], addon["version"]) if addon["version"] == "trunk": addon_url = "{}{}.zip".format(self.download_addon_url, addon["name"]) log.print_cms("default", "To download the addon: " + addon_url, "", 1) try: response = requests.get(addon_url) response.raise_for_status() if response.status_code == 200: zip_file = zipfile.ZipFile(io.BytesIO(response.content), 'r') zip_file.extractall(temp_directory) zip_file.close() project_dir = os.path.join(dir_path, self.wp_content, "plugins", addon["name"]) project_dir_hash = dirhash(project_dir, 'sha1') ref_dir = os.path.join(temp_directory, addon["name"]) ref_dir_hash = dirhash(ref_dir, 'sha1') if project_dir_hash == ref_dir_hash: altered = "NO" log.print_cms("good", "Different from sources : " + altered, "", 1) else: altered = "YES" log.print_cms("alert", "Different from sources : " + altered, "", 1) ignored = ["css", "img", "js", "fonts", "images"] dcmp = dircmp(project_dir, ref_dir, ignored) uCMS.diff_files(dcmp, addon["alterations"], project_dir) addon["edited"] = altered if addon["alterations"] is not None: msg = "[+] For further analysis, archive downloaded here : " + ref_dir log.print_cms("info", msg, "", 1) except requests.exceptions.HTTPError as e: msg = "The download link is not standard. Search manually !" log.print_cms("alert", msg, "", 1) addon["notes"] = msg return msg, e return altered, None
def check_addon_alteration(self, addon, addon_path, temp_directory): addon_url = "{}{}-{}.zip".format(self.download_addon_url, addon["name"], addon["version"]) if addon["version"] == "VERSION": # TODO return None, None log.print_cms("default", "To download the addon : " + addon_url, "", 1) altered = "" try: response = requests.get(addon_url) response.raise_for_status() if response.status_code == 200: zip_file = zipfile.ZipFile(io.BytesIO(response.content), 'r') zip_file.extractall(temp_directory) zip_file.close() project_dir_hash = dirhash(addon_path, 'sha1') ref_dir = os.path.join(temp_directory, addon["name"]) ref_dir_hash = dirhash(ref_dir, 'sha1') if project_dir_hash == ref_dir_hash: altered = "NO" log.print_cms("good", "Different from sources : " + altered, "", 1) else: altered = "YES" log.print_cms("alert", "Different from sources : " + altered, "", 1) ignored = ["tests"] dcmp = dircmp(addon_path, ref_dir, ignored) uCMS.diff_files(dcmp, addon["alterations"], addon_path) addon["edited"] = altered except requests.exceptions.HTTPError as e: msg = "The download link is not standard. Search manually !" log.print_cms("alert", msg, "", 1) addon["notes"] = msg return msg, e return altered, None
def _finddifference(self, casedirpath): # Get the full output and reference paths outpath = os.path.join(casedirpath, "out") refpath = os.path.join(casedirpath, "ref") # Check for the presence of the reference directory if not os.path.isdir(refpath): return "Test case has no reference directory" # Initialize lists to contain the extra files, missing files and differing files extra = [] missing = [] differ = [] # Verify list of filenames if len(filter(lambda fn: os.path.isdir(fn), os.listdir(outpath))) > 0: return "Output contains a directory" dircomp = filecmp.dircmp(outpath, refpath, ignore=['.DS_Store']) # Create a list of files that were not present in the reference directory for file in dircomp.left_only: extra.append(file) # Create a list of files that are missing from the output directory for file in dircomp.right_only: missing.append(file) # Compare files, focusing on those that aren't trivially equal. matches, mismatches, errors = filecmp.cmpfiles(outpath, refpath, dircomp.common, shallow=False) for filename in mismatches + errors: reffile = os.path.join(refpath, filename) outfile = os.path.join(outpath, filename) # For a soft comparison between files, check if both files are similar enough if self._parallel: if not similarfiles(reffile, outfile): differ.append(filename) # In the other case, check if both files are equal (except for potential timestamps) else: if not equalfiles(reffile, outfile): differ.append(filename) # Return the list of extra files, the list of missing files and the list of differing files return extra, missing, differ # ----------------------------------------------------------------- # This functions searches for directories within a certain parent directories that have a specific name
def _recursive_dircmp(folder1, folder2, prefix='.'): """Return a recursive dircmp comparison report as a dictionary.""" comparison = filecmp.dircmp(folder1, folder2) data = { 'left': [r'{}/{}'.format(prefix, i) for i in comparison.left_only], 'right': [r'{}/{}'.format(prefix, i) for i in comparison.right_only], 'both': [r'{}/{}'.format(prefix, i) for i in comparison.common_files], } for datalist in data.values(): datalist.sort() if comparison.common_dirs: for folder in comparison.common_dirs: # Update prefix to include new sub_folder prefix += '/' + folder # Compare common folder and add results to the report sub_folder1 = os.path.join(folder1, folder) sub_folder2 = os.path.join(folder2, folder) sub_report = _recursive_dircmp(sub_folder1, sub_folder2, prefix) # Add results from sub_report to main report for key, value in sub_report.items(): data[key] += value return data
def main(): parser = create_args() args = parser.parse_args() global verbose verbose = args.verbose msg(""" ___ _ _ _ / __|_) | | | _ _ _ ____ | |__ _| | ____ ____ ____ ____| |_ _ ___ ____ ____ | | | | _ \ | __) | |/ _ ) / _ | _ \ / _ | | | | |/___)/ _ )/ ___) | | | | | | | | | | | ( (/ / ( ( | | | | ( ( | | | |_| |___ ( (/ /| | \____| ||_/ |_| |_|_|\____) \_||_|_| |_|\_||_|_|\__ (___/ \____)_| |_| (____/ """) msg('Setting up:') wpPath, otherWpPath = process_wp_dirs(args) if (wpPath is False) or (otherWpPath is False): msg('ERROR: could not get WordPress directories for comparison', True) sys.exit() # Get plugins and themes, but only when a new WordPress directory has # been created. if not args.other_wordpress_path: msg('Getting plugins:') plugins = find_plugins(wpPath) for plugin in plugins: name = plugin['name'] version = plugin['version'] res = get_plugin(name, version, otherWpPath) if not res: msg("ERROR: Could not download %s %s" % (name, version), True) msg('Getting themes:') themes = find_themes(wpPath) for theme in themes: name = theme['name'] version = theme['version'] res = get_theme(name, version, otherWpPath) if not res: msg("ERROR: Could not download %s %s" % (name, version), True) msg('Comparing %s with %s' % (wpPath, otherWpPath)) msg('Starting Analysis:') dcres = dircmp(wpPath, otherWpPath) diff, extra, missing = analyze(dcres, wpPath) uploadsPath = os.path.join(wpPath, 'wp-content', 'uploads') phpFiles = search_dir_for_exts(uploadsPath, PHP_FILE_EXTENSIONS) print_analysis(diff, extra, missing, phpFiles) if args.remove_temporary_files: if os.path.exists(TEMP_DIR): msg("Removing %s " % TEMP_DIR) shutil.rmtree(TEMP_DIR)