Example #1
0
    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.failUnless(d.common == ['file'])
        self.failUnless(d.left_only == d.right_only == [])
        self.failUnless(d.same_files == ['file'])
        self.failUnless(d.diff_files == [])

        # Check attributes for comparison of two different directories
        d = filecmp.dircmp(self.dir, self.dir_diff)
        self.failUnless(d.left_list == ['file'])
        self.failUnless(d.right_list == ['file', 'file2'])
        self.failUnless(d.common == ['file'])
        self.failUnless(d.left_only == [])
        self.failUnless(d.right_only == ['file2'])
        self.failUnless(d.same_files == ['file'])
        self.failUnless(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.failUnless(d.same_files == ['file'])
        self.failUnless(d.diff_files == ['file2'])
Example #2
0
    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"])
Example #3
0
 def test_export_master(self):
     url = self.remote_path
     client = GitClient(self.local_path)
     subclient = GitClient(self.sublocal_path)
     subsubclient = GitClient(self.subsublocal_path)
     self.assertFalse(client.path_exists())
     self.assertFalse(client.detect_presence())
     self.assertFalse(os.path.exists(self.export_path))
     self.assertTrue(client.checkout(url))
     self.assertTrue(client.path_exists())
     self.assertTrue(subclient.path_exists())
     self.assertTrue(subsubclient.path_exists())
     tarpath = client.export_repository("master", self.export_path)
     self.assertEqual(tarpath, self.export_path + '.tar.gz')
     os.mkdir(self.export_path)
     with closing(tarfile.open(tarpath, "r:gz")) as tarf:
         tarf.extractall(self.export_path)
     subsubdirdiff = filecmp.dircmp(self.subsubexport_path, self.subsublocal_path, ignore=['.git', '.gitmodules'])
     self.assertEqual(subsubdirdiff.left_only, [])
     self.assertEqual(subsubdirdiff.right_only, [])
     self.assertEqual(subsubdirdiff.diff_files, [])
     subdirdiff = filecmp.dircmp(self.subexport_path, self.sublocal_path, ignore=['.git', '.gitmodules'])
     self.assertEqual(subdirdiff.left_only, [])
     self.assertEqual(subdirdiff.right_only, [])
     self.assertEqual(subdirdiff.diff_files, [])
     dirdiff = filecmp.dircmp(self.export_path, self.local_path, ignore=['.git', '.gitmodules'])
     self.assertEqual(dirdiff.left_only, [])
     self.assertEqual(dirdiff.right_only, [])
     self.assertEqual(dirdiff.diff_files, [])
Example #4
0
def compare(prev_course, cur_course, folder, deep=False):
    dir1 = '{}/{}'.format(prev_course.get_canvas_folder(), folder)
    dir2 = '{}/{}'.format(cur_course.get_canvas_folder(), folder)
    if deep:
        filecmp.dircmp(dir1, dir2).report_full_closure()
    else:
        filecmp.dircmp(dir1, dir2).report()
    print "\n========================================\n"
Example #5
0
 def testConflictedMerge(self):
     config.journalPath = self.localDir
     write.time = datetime.datetime.strftime(datetime.datetime.strptime("11:00:00", "%H:%M:%S"), config.timeFormat)
     write.write(name="1800-01-01", tags=self.remoteContent[0], content=self.localContent[1], quiet=True)
     merge = sync.Sync(self.remoteDir, self.localDir)
     sync.raw_input = lambda _: "2" 
     merge.merge()
     self.assertEqual(filecmp.dircmp(self.remoteDir, self.localDir).left_only, [])
     self.assertEqual(filecmp.dircmp(self.remoteDir, self.localDir).right_only, [])
     self.assertEqual(filecmp.dircmp(self.remoteDir, self.localDir).diff_files, [])
Example #6
0
def dive(remote_dir,local_dir): 
 
   #_common contains the directories which are common between the remote
   #_and the local
   for common in filecmp.dircmp(local_dir,remote_dir).common_dirs:

     #_ paths used for the walk function call.
     l_common = os.path.join(local_dir,common)
     r_common = os.path.join(remote_dir,common)
    
     print "Checking ---" + r_common + "--- "
    
     #_ walk to the child directory
     dive(r_common,l_common)

   #_the dir_kill contains the directories or files solely in the remote_dir.
   #_that will be killed/erased
   for dir_kill in filecmp.dircmp(local_dir,remote_dir).right_only:
     
     #_get the full path
     path_kill  = os.path.join(remote_dir,dir_kill) 
  
     print "Deleting " + path_kill + " --" 
  
     #_make the child process do the removal, and wait
     subprocess.Popen(["rm","-rf",path_kill]).wait() 
  
   #_the dir_copy contains the directories or files solely in the local_dir.
   #_that will be copied over to the remote directory.
   for dir_copy in filecmp.dircmp(local_dir,remote_dir).left_only:

     #_get the full path for remote and local
     l_copy  = os.path.join(local_dir,dir_copy)
     r_copy = os.path.join(remote_dir,dir_copy) 

     print "Copying " + l_copy + " to " + r_copy + " --"  
    
     #_make the child process to do the copying
     subprocess.Popen(["cp","-rf",l_copy,r_copy]).wait() 

   #_the dir_diff contains the files or directories that differ but have the 
   #_same filename.
   for dir_diff in filecmp.dircmp(local_dir,remote_dir).diff_files:

     #_get the full path for remote and local
     l_diff  = os.path.join(local_dir,dir_diff)
     r_diff = os.path.join(remote_dir,dir_diff)
      
     print "Updating " + r_diff + " --"  

     #_make the child processes to do the erasing and copying.
     subprocess.Popen(["rm","-rf",r_diff]).wait()
     subprocess.Popen(["cp","-rf",l_diff,r_diff]).wait()   
Example #7
0
    def test_export_hash(self):
        url = self.remote_path
        client = GitClient(self.local_path)
        subclient = GitClient(self.sublocal_path)
        subclient2 = GitClient(self.sublocal2_path)
        subsubclient = GitClient(self.subsublocal_path)
        subsubclient2 = GitClient(self.subsublocal2_path)
        self.assertFalse(client.path_exists())
        self.assertFalse(client.detect_presence())
        self.assertFalse(os.path.exists(self.export_path))
        self.assertTrue(client.checkout(url, version='master'))
        self.assertTrue(client.path_exists())
        self.assertTrue(subclient.path_exists())
        self.assertTrue(subsubclient.path_exists())
        self.assertFalse(subclient2.path_exists())
        self.assertFalse(subsubclient2.path_exists())
        # we need first to retrieve locally the hash we want to export
        self.assertTrue(client.update(version=self.version_test))
        self.assertTrue(client.path_exists())
        # git leaves old submodule around by default
        self.assertTrue(subclient.path_exists())
        self.assertTrue(subsubclient.path_exists())
        # new submodule should be there
        self.assertTrue(subclient2.path_exists())
        self.assertTrue(subsubclient2.path_exists())

        tarpath = client.export_repository(self.version_test, self.export_path)
        self.assertEqual(tarpath, self.export_path + '.tar.gz')
        os.mkdir(self.export_path)
        with closing(tarfile.open(tarpath, "r:gz")) as tarf:
            tarf.extractall(self.export_path)

        # Checking that we have only submodule2 in our export
        self.assertFalse(os.path.exists(self.subexport_path))
        self.assertFalse(os.path.exists(self.subsubexport_path))
        self.assertTrue(os.path.exists(self.subexport2_path))
        self.assertTrue(os.path.exists(self.subsubexport2_path))

        # comparing with version_test ( currently checked-out )
        subsubdirdiff = filecmp.dircmp(self.subsubexport2_path, self.subsublocal_path, ignore=['.git', '.gitmodules'])
        self.assertEqual(subsubdirdiff.left_only, [])  # same subsubfixed.txt in both subsubmodule/
        self.assertEqual(subsubdirdiff.right_only, [])
        self.assertEqual(subsubdirdiff.diff_files, [])
        subdirdiff = filecmp.dircmp(self.subexport2_path, self.sublocal_path, ignore=['.git', '.gitmodules'])
        self.assertEqual(subdirdiff.left_only, [])
        self.assertEqual(subdirdiff.right_only, [])
        self.assertEqual(subdirdiff.diff_files, [])
        dirdiff = filecmp.dircmp(self.export_path, self.local_path, ignore=['.git', '.gitmodules'])
        self.assertEqual(dirdiff.left_only, [])
        # submodule is still there on local_path (git default behavior)
        self.assertEqual(dirdiff.right_only, ['submodule'])
        self.assertEqual(dirdiff.diff_files, [])
Example #8
0
 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)
Example #9
0
def sync(source, dest):
    """Compare top-level contents. If unequal, carry out equalising
    shell commands; recursively synchronise subdirectories."""
    dcmp = dircmp(source, dest)
    if settings.delete:
        for f in dcmp.right_only:
            delete(f, dest)
    if not settings.delete_only:
        for f in dcmp.left_only:
            copy(f, source, dest)
    dcmp = dircmp(source, dest)
    for d in dcmp.common_dirs:
        sync(os.path.join(source, d), os.path.join(dest, d))
Example #10
0
def walk(src, dest):
    print '****************************************************************'
    print dest
    print '****************************************************************'
    dirCmp = filecmp.dircmp(src, dest, ignore=['Thumbs.db'])
    for destFile in dirCmp.right_only:
        destFilePath = dest+'/'+destFile
        if os.path.isfile(destFilePath):
            print u'删除文件\n',destFilePath
            os.remove(destFilePath)
        else:
            print u'删除文件夹\n',destFilePath
#            os.rmdir(destFilePath)
            shutil.rmtree(destFilePath)
    for srcFile in dirCmp.left_only:
        srcFilePath = src+'/'+srcFile
        destFilePath = dest+'/'+srcFile
        if os.path.isfile(srcFilePath):
            print u'复制文件\n',destFilePath
            shutil.copy2(srcFilePath, dest)
        else:
            print u'复制文件夹\n',destFilePath
            shutil.copytree(srcFilePath, destFilePath)
    for srcFile in dirCmp.diff_files:
        srcFilePath = src+'/'+srcFile
        destFilePath = dest+'/'+srcFile
        print u'同步文件\n',destFilePath
        shutil.copy2(srcFilePath, dest)
    subDirs = set(os.listdir(src))-set(dirCmp.left_only)
    targetDirs = [subDir for subDir in subDirs if os.path.isdir(src+'/'+subDir)]
    for targetDir in targetDirs:
        walk(src+'/'+targetDir, dest+'/'+targetDir)
def _compareDirectories(dir1, dir2):
    """
    From: http://stackoverflow.com/a/6681395
    Compare two directories recursively. Files in each directory are
    assumed to be equal if their names and contents are equal.
    
    @param dir1: First directory path
    @param dir2: Second directory path
    
    @return: True if the directory trees are the same and 
        there were no errors while accessing the directories or files, 
        False otherwise.
    """
    dirs_cmp = filecmp.dircmp(dir1, dir2)
    if len(dirs_cmp.left_only)>0 or len(dirs_cmp.right_only)>0 or \
        len(dirs_cmp.funny_files)>0:
        return False
    (_, mismatch, errors) =  filecmp.cmpfiles(
        dir1, dir2, dirs_cmp.common_files, shallow=False)
    if len(mismatch)>0 or len(errors)>0:
        return False
    for common_dir in dirs_cmp.common_dirs:
        new_dir1 = os.path.join(dir1, common_dir)
        new_dir2 = os.path.join(dir2, common_dir)
        if not _compareDirectories(new_dir1, new_dir2):
                return False
    return True
Example #12
0
def identical(f, dst):
    if os.path.isdir(dst):
        dircmp = filecmp.dircmp(f, dst)
        if not (len(dircmp.left_only) or len(dircmp.right_only) or
                len(dircmp.diff_files)):
            return True
    return filecmp.cmp(f, dst)
Example #13
0
 def shutdown(self):
    if (self._shutting_down):
       return
    from filecmp import dircmp
    
    self.log(20, '{0!a} shutting down.'.format(self))
    self._shutting_down = True
    for ap in self.aps:
       for name in ('stdin', 'stdout', 'stderr'):
          name += '_async'
          if not (hasattr(ap,name)):
             continue
          stream = getattr(ap,name)
          if (not stream):
             continue
          stream.close()
    for cc in self.ccs:
       cc.close()
    
    bdirn = os.path.join(self.config_argsets[0][1], self.intermediate_ddname)
    dcmps = deque()
    
    for cas in self.config_argsets[1:]:
       dn = os.path.join(cas[1], self.intermediate_ddname)
       dcmps.append(dircmp(bdirn, dn))
    
    self._dcmps = dcmps
    
    if (self._standalone):
       self.ed.shutdown()
Example #14
0
def are_dir_trees_equal(dir1, dir2, ignore=None):
	"""
	Compare two directories recursively. Files in each directory are
	assumed to be equal if their names and contents are equal.

	@param dir1: First directory path
	@param dir2: Second directory path
	@param ignore: list of names to ignore (none are ignored by default)

	@return: True if the directory trees are the same and 
		there were no errors while accessing the directories or files, 
		False otherwise.
	"""
	dirs_cmp = filecmp.dircmp(dir1, dir2, ignore=ignore)
	if len(dirs_cmp.left_only)>0 or len(dirs_cmp.right_only)>0 or \
		len(dirs_cmp.funny_files)>0:
		return False
	(_, mismatch, errors) =  filecmp.cmpfiles(
		dir1, dir2, dirs_cmp.common_files, shallow=False)
	if len(mismatch)>0 or len(errors)>0:
		return False
	for common_dir in dirs_cmp.common_dirs:
		new_dir1 = os.path.join(dir1, common_dir)
		new_dir2 = os.path.join(dir2, common_dir)
		if not are_dir_trees_equal(new_dir1, new_dir2, ignore=ignore):
			return False
	return True
Example #15
0
def test_correct_pip_version(script):
    """
    Check we are running proper version of pip in run_pip.
    """
    # output is like:
    # pip PIPVERSION from PIPDIRECTORY (python PYVERSION)
    result = script.pip('--version')

    # compare the directory tree of the invoked pip with that of this source
    # distribution
    pip_folder_outputed = re.match(
        r'pip \d+(\.[\d]+)+(\.?(rc|dev|pre|post)\d+)? from (.*) '
        r'\(python \d(.[\d])+\)$',
        result.stdout
    ).group(4)
    pip_folder = join(SRC_DIR, 'src', 'pip')

    diffs = filecmp.dircmp(pip_folder, pip_folder_outputed)

    # If any non-matching .py files exist, we have a problem: run_pip
    # is picking up some other version!  N.B. if this project acquires
    # primary resources other than .py files, this code will need
    # maintenance
    mismatch_py = [
        x for x in diffs.left_only + diffs.right_only + diffs.diff_files
        if x.endswith('.py')
    ]
    assert not mismatch_py, (
        'mismatched source files in %r and %r: %r' %
        (pip_folder, pip_folder_outputed, mismatch_py)
    )
    def __dowork(self, dir1, dir2, copyfunc = None, updatefunc = None):
        """ Private attribute for doing work """
        
        print 'Source directory: ', dir1, ':'

        self.__numdirs += 1
        self.__dcmp = filecmp.dircmp(dir1, dir2)
        
        # Files & directories only in target directory
        if self.__purge:
            for f2 in self.__dcmp.right_only:
                fullf2 = os.path.join(dir2, f2)
                print 'Deleting ',fullf2
                try:
                    if os.path.isfile(fullf2):
                        
                        try:
                            os.remove(fullf2)
                            self.__numdelfiles += 1
                        except OSError, e:
                            print e
                            self.__numdelffld += 1
                    elif os.path.isdir(fullf2):
                        try:
                            shutil.rmtree( fullf2, True )
                            self.__numdeldirs += 1
                        except shutil.Error, e:
                            print e
                            self.__numdeldfld += 1
Example #17
0
def MergeApk(args, tmp_apk, tmp_dir_32, tmp_dir_64):
  # Expected files to copy from 32- to 64-bit APK together with an extra flag
  # setting the compression level of the file
  expected_files = {'snapshot_blob_32.bin': ['-0'],
                    'natives_blob_32.bin': ['-0'],
                    args.shared_library: []}

  if args.uncompress_shared_libraries:
    expected_files[args.shared_library] += ['-0']

  shutil.copyfile(args.apk_64bit, tmp_apk)

  # need to unpack APKs to compare their contents
  UnpackApk(args.apk_64bit, tmp_dir_64)
  UnpackApk(args.apk_32bit, tmp_dir_32)

  dcmp = filecmp.dircmp(
      tmp_dir_64,
      tmp_dir_32,
      ignore=['META-INF', 'AndroidManifest.xml'])

  diff_files = GetDiffFiles(dcmp, tmp_dir_32)

  # Check that diff_files match exactly those files we want to insert into
  # the 64-bit APK.
  CheckFilesExpected(diff_files, expected_files)

  RemoveMetafiles(tmp_apk)

  AddDiffFiles(diff_files, tmp_dir_32, tmp_apk, expected_files)
Example #18
0
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 + "     <-------"
Example #19
0
    def diff_resources(self):
        if self.source.resources is None or self.destination.resources is None:
            # FIXME: warning message ?
            return
        sdir=self.source.resources.dir_
        ddir=self.destination.resources.dir_

        d=filecmp.dircmp(sdir, ddir)

        def relative_path(origin, dirname, name):
            """Return the relative path (hence id) for the resource 'name' in resource dir 'dirname',
            relative to the origin dir.
            """
            return os.path.join(dirname, name).replace(origin, '')

        def handle_dircmp(dc):
            for n in dc.left_only:
                yield ('create_resource',
                       relative_path(sdir, dc.left, n),
                       relative_path(ddir, dc.right, n), self.create_resource)
            for n in dc.diff_files:
                yield ('update_resource',
                       relative_path(sdir, dc.left, n),
                       relative_path(ddir, dc.right, n), self.update_resource)

        for t in handle_dircmp(d):
            yield t

        for sd in d.subdirs.itervalues():
            for t in handle_dircmp(sd):
                yield t
        return
Example #20
0
 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)")
                 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)
Example #21
0
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 analyzeQueryResults(self):

        outputs_dir = os.path.join(self._out_dirname, "outputs")

        failing_queries = []

        mysql_out_dir = os.path.join(outputs_dir, "mysql")
        qserv_out_dir = os.path.join(outputs_dir, "qserv")

        dcmp = dircmp(mysql_out_dir, qserv_out_dir)

        if self.dataReader.notLoadedTables:
            self.logger.info("Tables/Views not loaded: %s",
                             self.dataReader.notLoadedTables)

        if not dcmp.diff_files:
            self.logger.info("MySQL/Qserv results are identical")
        else:
            for query_name in dcmp.diff_files:
                failing_queries.append(query_name)
            self.logger.error("MySQL/Qserv differs for %s queries:",
                              len(failing_queries))
            self.logger.error("Broken queries list in %s: %s",
                              qserv_out_dir, failing_queries)

        return failing_queries
Example #23
0
def find_different_files(dir1, dir2, ignore_subtree_names=None):
  """Returns a list of any files that differ between the directory trees rooted
  at dir1 and dir2.

  Args:
    dir1: root of a directory tree; if nonexistent, will raise OSError
    dir2: root of another directory tree; if nonexistent, will raise OSError
    ignore_subtree_names: list of subtree directory names to ignore;
          defaults to ['.svn'], so all SVN files are ignores

  TODO(epoger): include the dirname within each filename (not just the
  basename), to make it easier to locate any differences
  """
  differing_files = []
  if ignore_subtree_names is None:
    ignore_subtree_names = ['.svn']
  dircmp = filecmp.dircmp(dir1, dir2, ignore=ignore_subtree_names)
  differing_files.extend(dircmp.left_only)
  differing_files.extend(dircmp.right_only)
  differing_files.extend(dircmp.common_funny)
  differing_files.extend(dircmp.diff_files)
  differing_files.extend(dircmp.funny_files)
  for common_dir in dircmp.common_dirs:
    differing_files.extend(find_different_files(
        os.path.join(dir1, common_dir), os.path.join(dir2, common_dir)))
  return differing_files
Example #24
0
def MergeApk(args, tmp_apk, tmp_dir_32, tmp_dir_64):
  # Expected files to copy from 32- to 64-bit APK together with whether to
  # compress within the .apk.
  expected_files = {'snapshot_blob_32.bin': False}
  if args.shared_library:
    expected_files[args.shared_library] = not args.uncompress_shared_libraries

  # need to unpack APKs to compare their contents
  UnpackApk(args.apk_64bit, tmp_dir_64)
  UnpackApk(args.apk_32bit, tmp_dir_32)

  ignores = ['META-INF', 'AndroidManifest.xml']
  if args.ignore_classes_dex:
    ignores += ['classes.dex', 'classes2.dex']
  if args.debug:
    # see http://crbug.com/648720
    ignores += ['webview_licenses.notice']

  dcmp = filecmp.dircmp(
      tmp_dir_64,
      tmp_dir_32,
      ignore=ignores)

  diff_files = GetDiffFiles(dcmp, tmp_dir_32)

  # Check that diff_files match exactly those files we want to insert into
  # the 64-bit APK.
  CheckFilesExpected(diff_files, expected_files, args.component_build)

  with zipfile.ZipFile(tmp_apk, 'w') as out_zip:
    build_utils.MergeZips(out_zip, [args.apk_64bit],
                          exclude_patterns=['META-INF/*'])
    AddDiffFiles(diff_files, tmp_dir_32, out_zip, expected_files,
                 args.component_build, args.uncompress_shared_libraries)
Example #25
0
def can_be_unrolled(source, dest):
    if not exists(dest):
        return True

    if islink(dest):
        if islink(source):
            return readlink(source) != readlink(dest)
        else:
            return realpath(dest) != abspath(source)
    elif isdir(dest) and isdir(source):
        dcmp = filecmp.dircmp(source, dest)
        afiles = [r for r in dcmp.right_only if not ignore_regex.match(r)]
        dfiles = dcmp.diff_files

        if afiles or dfiles:
            print 'dirs are different {}: new {}, chg {}'.format(source, afiles, dfiles)
            return False

        return True
    elif isfile(dest) and isfile(source):
        if filecmp.cmp(source, dest):
            return True

        print 'files are different {} {}'.format(source, dest)
        return False
    else:
        print 'unknown case {} {}'.format(source, dest)
Example #26
0
 def _compare_results_dirs(self, actual_dir, expected_dir):
     # First check that the two directories contain only the same
     # files and subdirectories.
     dircmp = filecmp.dircmp(actual_dir, expected_dir)
     self._check_unshared(dircmp)
     # Then check that the common files are the same.
     self._check_common(dircmp)
Example #27
0
def untar(tarfile, outdir):
  """Returns True if the untar dir differs from a pre-existing dir."""
  tmp_dir = tempfile.mkdtemp()
  try:
    with tempfile.TemporaryFile(mode='w+') as f:
      try:
        subprocess.check_call(['tar', '-xvf', tarfile], cwd=tmp_dir, stdout=f)
      except:
        f.seek(0)
        sys.stderr.write(f.read())
        raise
    untar_outdir = os.path.join(tmp_dir, os.path.basename(outdir))
    if os.path.exists(outdir):
      diff = filecmp.dircmp(untar_outdir, outdir)
      if not (diff.left_only + diff.right_only + diff.diff_files +
              diff.common_funny + diff.funny_files):
        # outdir already existed with exactly the same content.
        return False
      shutil.rmtree(outdir)
    # The untar files are different, or there was no previous outdir.
    print 'Updating', outdir
    shutil.move(untar_outdir, outdir)
    return True
  finally:
    if os.path.isdir(tmp_dir):
      shutil.rmtree(tmp_dir)
Example #28
0
 def test_custom_generation_works(self):
     # the same thing with a specified set of settings should work
     settings = read_settings(filename=SAMPLE_CONFIG, override={"PATH": INPUT_PATH, "OUTPUT_PATH": self.temp_path})
     pelican = Pelican(settings=settings)
     pelican.run()
     dcmp = dircmp(self.temp_path, os.sep.join((OUTPUT_PATH, "custom")))
     self.assertFilesEqual(recursiveDiff(dcmp))
Example #29
0
    def compute_metrics(self, commit_no):
        """Compute metrics for commmit number (ordered as from git log).

        Checks out the corresponding commit in the git repository, and
        compute the metrics for its difference with the given package.
        The returned metrics are those produced by compare_dirs plus:
         * commit: hash for the commit
         * date: commit date for the commit (as a string)

        :params commits: list of all commits
        :params commmit_no: commit number (starting in 0)
        :returns: dictionary with metrics
        """

        commit = self.commits[commit_no]
        subprocess.call(["git", "-C", self.repo, "checkout", commit[0]],
                        stdout = subprocess.DEVNULL, stderr = subprocess.DEVNULL)
        dcmp = filecmp.dircmp(self.repo, self.dir)
        m = compare_dirs(dcmp)
        logging.debug ("Commit %s. Files: %d, %d, %d, lines: %d, %d, %d, %d)"
            % (commit[0], m["left_files"], m["right_files"], m["diff_files"],
            m["left_lines"], m["right_lines"],
            m["added_lines"], m["removed_lines"]))
        m["total_files"] = m["left_files"] + m["right_files"] + m["diff_files"]
        m["total_lines"] = m["left_lines"] + m["right_lines"] \
            + m["added_lines"] + m["removed_lines"]
        m["commit_seq"] = commit_no
        m["commit"] = commit[0]
        m["date"] = commit[1]
        return m
Example #30
0
 def _compare_directories(self, left, right):
     ''' This method compares directories. If there is a common directory, the
         algorithm must compare what is inside of the directory by calling this
         recursively.
     '''
     comparison = filecmp.dircmp(left, right)
     if comparison.common_dirs:
         for d in comparison.common_dirs:
             self._compare_directories(os.path.join(left, d), os.path.join(right, d))
     if comparison.left_only:
         self._copy(comparison.left_only, left, right)
     if comparison.right_only:
         self._copy(comparison.right_only, right, left)
     left_newer = []
     right_newer = []
     if comparison.diff_files:
         for d in comparison.diff_files:
             l_modified = os.stat(os.path.join(left, d)).st_mtime
             r_modified = os.stat(os.path.join(right, d)).st_mtime
             if l_modified > r_modified:
                 left_newer.append(d)
             else:
                 right_newer.append(d)
     self._copy(left_newer, left, right)
     self._copy(right_newer, right, left)
Example #31
0
    def test_j_createnewcase_user_compset_vs_alias(self):
        """
        Create a compset using the alias and another compset using the full compset name
        and make sure they are the same by comparing the namelist files in CaseDocs.
        Ignore the modelio files and clean the directory names out first.
        """
        cls = self.__class__

        testdir1 = os.path.join(cls._testroot, "testcreatenewcase_user_compset")
        if os.path.exists(testdir1):
            shutil.rmtree(testdir1)
        cls._testdirs.append(testdir1)

        args = " --case CreateNewcaseTest --script-root {} --compset 2000_DATM%NYF_SLND_SICE_DOCN%SOMAQP_SROF_SGLC_SWAV --output-root {} --handle-preexisting-dirs u".format(
            testdir1, cls._testroot
        )
        if utils.get_cime_default_driver() == "nuopc":
            args += " --res f19_g17 "
        else:
            args += " --res f19_g16 "
        if utils.get_model() == "cesm":
            args += " --run-unsupported"
        if self.TEST_COMPILER is not None:
            args += " --compiler %s" % self.TEST_COMPILER
        if self.TEST_MPILIB is not None:
            args += " --mpilib %s" % self.TEST_MPILIB

        args += f" --machine {self.MACHINE.get_machine_name()}"

        self.run_cmd_assert_result(
            "{}/create_newcase {}".format(self.SCRIPT_DIR, args),
            from_dir=self.SCRIPT_DIR,
        )
        self.run_cmd_assert_result("./case.setup ", from_dir=testdir1)
        self.run_cmd_assert_result("./preview_namelists ", from_dir=testdir1)

        dir1 = os.path.join(testdir1, "CaseDocs")
        dir2 = os.path.join(testdir1, "CleanCaseDocs")
        os.mkdir(dir2)
        for _file in os.listdir(dir1):
            if "modelio" in _file:
                continue
            with open(os.path.join(dir1, _file), "r") as fi:
                file_text = fi.read()
                file_text = file_text.replace(os.path.basename(testdir1), "PATH")
                file_text = re.sub(r"logfile =.*", "", file_text)
            with open(os.path.join(dir2, _file), "w") as fo:
                fo.write(file_text)
        cleancasedocs1 = dir2

        testdir2 = os.path.join(cls._testroot, "testcreatenewcase_alias_compset")
        if os.path.exists(testdir2):
            shutil.rmtree(testdir2)
        cls._testdirs.append(testdir2)
        args = " --case CreateNewcaseTest --script-root {} --compset ADSOMAQP --output-root {} --handle-preexisting-dirs u".format(
            testdir2, cls._testroot
        )
        if utils.get_cime_default_driver() == "nuopc":
            args += " --res f19_g17 "
        else:
            args += " --res f19_g16 "
        if utils.get_model() == "cesm":
            args += " --run-unsupported"
        if self.TEST_COMPILER is not None:
            args += " --compiler %s" % self.TEST_COMPILER
        if self.TEST_MPILIB is not None:
            args += " --mpilib %s" % self.TEST_MPILIB

        args += f" --machine {self.MACHINE.get_machine_name()}"

        self.run_cmd_assert_result(
            "{}/create_newcase {}".format(self.SCRIPT_DIR, args),
            from_dir=self.SCRIPT_DIR,
        )
        self.run_cmd_assert_result("./case.setup ", from_dir=testdir2)
        self.run_cmd_assert_result("./preview_namelists ", from_dir=testdir2)

        dir1 = os.path.join(testdir2, "CaseDocs")
        dir2 = os.path.join(testdir2, "CleanCaseDocs")
        os.mkdir(dir2)
        for _file in os.listdir(dir1):
            if "modelio" in _file:
                continue
            with open(os.path.join(dir1, _file), "r") as fi:
                file_text = fi.read()
                file_text = file_text.replace(os.path.basename(testdir2), "PATH")
                file_text = re.sub(r"logfile =.*", "", file_text)
            with open(os.path.join(dir2, _file), "w") as fo:
                fo.write(file_text)

        cleancasedocs2 = dir2
        dcmp = filecmp.dircmp(cleancasedocs1, cleancasedocs2)
        self.assertTrue(
            len(dcmp.diff_files) == 0, "CaseDocs differ {}".format(dcmp.diff_files)
        )

        cls._do_teardown.append(testdir1)
        cls._do_teardown.append(testdir2)
def areFoldersDifferent(dir1, dir2):
    dcmp = dircmp(dir1, dir2)
    differences = len(dcmp.diff_files) + len(dcmp.left_only) + len(
        dcmp.right_only)
    return differences > 0
        # comp_files.report()  # Shows a small report with no data on sub-directory related data. So not really useful.
        print('\n')
        folder_size(comp_from_path, folder_name_from)
        folder_size(comp_to_path, folder_name_to)

        file.close()
    except Exception as e:
        return 'Error in function full_run : ' + str(e)


# All Variables
# Variable for comparing the folders and file
comp_from_path = 'E:\\'
folder_name_from = 'Internal Folder'
comp_to_path = 'H:\\Back-Up'
folder_name_to = 'External Backup'
comp_files = dircmp(comp_from_path, comp_to_path, ignore=['BIN'])

# All Function Runs
# Puts compare report in a text file
full_run()

# Generates result for Mis-Match Folder and Files
find('Only in', 'FullResult.txt', 'MissingFolderAndFiles.txt')

# Generates result for Mis-Match Files
find('Different file', 'FullResult.txt', 'MissingFiles.txt')

# Generates result for sizes
find('Size: ', 'FullResult.txt', 'SizeFiles.txt')
Example #34
0
 def __cmp(self, dir1, dir2, shallow):
     dircmp = filecmp.dircmp(dir1, dir2)
     self.__recursive_cmp(dircmp, shallow)
Example #35
0
"""
Diff tool to detect and copy differences
"""
from filecmp import dircmp


def print_diff_files(dcmp):
    """
    Print Differences in directories.
    """
    for name in dcmp.diff_files:
        print("diff_file %s found in %s and %s" %
              (name, dcmp.left, dcmp.right))
    for sub_dcmp in dcmp.subdirs.values():
        print_diff_files(sub_dcmp)


dcmp = dircmp('dir1', 'dir2')
print_diff_files(dcmp)
Example #36
0
    def testSerialization(self):
        n = Network()

        imageDims = (42, 38)
        params = dict(width=imageDims[0],
                      height=imageDims[1],
                      mode="bw",
                      background=1,
                      invertOutput=1)

        sensor = n.addRegion("sensor", "py.ImageSensor", json.dumps(params))
        sensor.setDimensions(Dimensions(imageDims[0], imageDims[1]))

        params = dict(inputShape=imageDims,
                      coincidencesShape=imageDims,
                      disableTemporal=1,
                      tpSeed=43,
                      spSeed=42,
                      nCellsPerCol=1)

        l1 = n.addRegion("l1", "py.CLARegion", json.dumps(params))

        params = dict(maxCategoryCount=48,
                      SVDSampleCount=400,
                      SVDDimCount=5,
                      distanceNorm=0.6)

        _classifier = n.addRegion("classifier", "py.KNNClassifierRegion",
                                  json.dumps(params))

        # TODO: link params should not be required. Dest region dimensions are
        # already specified as [1]
        params = dict(mapping="in", rfSize=imageDims)

        n.link("sensor", "l1", "UniformLink", json.dumps(params))
        n.link("l1", "classifier", "UniformLink", "", "bottomUpOut",
               "bottomUpIn")
        n.link("sensor", "classifier", "UniformLink", "", "categoryOut",
               "categoryIn")
        n.initialize()

        n.save("fdr.nta")

        # Make sure the network bundle has all the expected files
        self.assertTrue(os.path.exists("fdr.nta/network.yaml"))
        self.assertTrue(os.path.exists("fdr.nta/R0-pkl"))
        self.assertTrue(os.path.exists("fdr.nta/R1-pkl"))
        self.assertTrue(os.path.exists("fdr.nta/R2-pkl"))

        n2 = Network("fdr.nta")
        n2.initialize()  # should not fail

        # Make sure the network is actually the same
        sensor = n2.regions['sensor']
        self.assertEqual(sensor.type, "py.ImageSensor")
        # would like to directly compare, but can't -- NPC-6
        self.assertEqual(str(sensor.dimensions), str(Dimensions(42, 38)))
        self.assertEqual(sensor.getParameter("width"), 42)
        self.assertEqual(sensor.getParameter("height"), 38)
        self.assertEqual(sensor.getParameter("mode"), "bw")
        self.assertEqual(sensor.getParameter("background"), 1)
        self.assertEqual(sensor.getParameter("invertOutput"), 1)

        l1 = n2.regions['l1']
        self.assertEqual(l1.type, "py.CLARegion")
        self.assertEqual(str(l1.dimensions), str(Dimensions(1)))
        a = l1.getParameter("inputShape")
        self.assertEqual(len(a), 2)
        self.assertEqual(a[0], 42)
        self.assertEqual(a[1], 38)

        a = l1.getParameter("coincidencesShape")
        self.assertEqual(len(a), 2)
        self.assertEqual(a[0], 42)
        self.assertEqual(a[1], 38)

        self.assertEqual(l1.getParameter("disableTemporal"), 1)
        self.assertEqual(l1.getParameter("spSeed"), 42)
        self.assertEqual(l1.getParameter("tpSeed"), 43)

        cl = n2.regions['classifier']
        self.assertEqual(cl.type, "py.KNNClassifierRegion")
        self.assertEqual(cl.getParameter("maxCategoryCount"), 48)
        self.assertEqual(cl.getParameter("SVDSampleCount"), 400)
        self.assertEqual(cl.getParameter("SVDDimCount"), 5)
        self.assertLess((cl.getParameter("distanceNorm") - 0.6), 0.0001)
        self.assertEqual(str(cl.dimensions), str(Dimensions(1)))

        n2.save("fdr2.nta")

        # now compare the two network bundles -- should be the same
        c = filecmp.dircmp("fdr.nta", "fdr2.nta")
        self.assertEqual(len(c.left_only), 0,
                         "fdr.nta has extra files: %s" % c.left_only)

        self.assertEqual(len(c.right_only), 0,
                         "fdr2.nta has extra files: %s" % c.right_only)

        if len(c.diff_files) > 0:
            _LOGGER.warn(
                "Some bundle files differ: %s\n"
                "This is expected, as pickle.load() followed by "
                "pickle.dump() doesn't produce the same file", c.diff_files)
Example #37
0
def assert_dirs_equal(expected, actual):
    dir_comparison = filecmp.dircmp(expected, actual)
    assert len(dir_comparison.left_only) == 0
    assert len(dir_comparison.right_only) == 0
    assert len(dir_comparison.diff_files) == 0
    assert len(dir_comparison.funny_files) == 0
#!/usr/bin/env python
# encoding: utf-8
#
# Copyright (c) 2008 Doug Hellmann All rights reserved.
#
"""
"""

__version__ = "$Id$"
#end_pymotw_header

import filecmp

dc = filecmp.dircmp('example/dir1', 'example/dir2')
print 'Common     :', dc.common
print 'Directories:', dc.common_dirs
print 'Files      :', dc.common_files
print 'Funny      :', dc.common_funny
#         new_files_path = '{}/{}'.format(path_a, x)
#         old_files_path = '{}/{}'.format(path_b, x)
#         path_create1 = '{}/{}'.format(path_create, x)
#         if os.listdir(new_files_path):
#             dircomp = filecmp.dircmp(new_files_path, old_files_path)
#             for function in [dircomp.left_only, dircomp.diff_files, dircomp.right_list]:
#                 if function == dircomp.right_list:
#                     new_path(new_files_path, path_create1, x, files, function, dircomp.diff_files)
#                 new_path(new_files_path, path_create1, x, files, function)
#     print(set(files))
#     for z, g in set(files):
#         copyfile(z, g)

deleted_files = []
new_files = []
delete_modified_files = []
modified_files = []
for x in ['dokumenty', 'metadane']:
    old_files_path = '{}/{}'.format(path_a, x)
    new_files_path = '{}/{}'.format(path_b, x)
    dircomp = filecmp.dircmp(old_files_path, new_files_path)
    print('{}/{}'.format(new_files_path, ''.join(dircomp.right_only)))
    if dircomp.left_only:
        deleted_files.append('{}/{}'.format(old_files_path, ''.join(dircomp.left_only)))
    if dircomp.right_only:
        new_files.append('{}/{}'.format(new_files_path, ''.join(dircomp.right_only)))
    if dircomp.diff_files:
        delete_modified_files.append('{}/{}'.format(old_files_path, ''.join(dircomp.diff_files)))
        modified_files.append('{}/{}'.format(new_files_path, ''.join(dircomp.diff_files)))
print({'deleted_files': deleted_files, 'new_files': new_files, 'modified_files': modified_files})
def print_diff_report(different_list):
    with open(DIFF_REPORT, 'w') as f:
        for i in different_list:
            l_file = f"{i['dir1_path']}/{i['name']}"
            r_file = f"{i['dir2_path']}/{i['name']}"
            diff_cmd = f"diff {l_file} {r_file}"
            f.write(f"Diff - {l_file} vs {r_file}\n")
            f.write("="*20 + "\n")
            try:
                diff_output=subprocess.check_output(diff_cmd, stderr=subprocess.STDOUT, shell=True)
            except subprocess.CalledProcessError as e:
                f.write(e.output.decode("utf-8"))    


dcmp = dircmp(DIR_1, DIR_2) 
get_diff_files(dcmp) 

nodiffaddon={}
diffaddon={}

with open(CONSOLIDATED_REPORT, 'w') as cr:
    cr.write(f"\nAdd ons in {DIR_1} but not in {DIR_2}\n")
    counter = 1
    for i in dir1_only_list:
        dir_path=i['dir1_path'].replace(DIR_1,'.')
        file_name = i['name']
        file_path=f"{dir_path}/{file_name}" 
        add_on_name=file_path.split("/")[1]
        if check_in_excludelist(add_on_name):
            continue        
 def test_dircmp(self):
     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.assertEqual(d.left_only, [])
     self.assertEqual(d.right_only, [])
     self.assertEqual(d.same_files, ['file'])
     self.assertEqual(d.diff_files, [])
     expected_report = [
         'diff {} {}'.format(self.dir, self.dir_same),
         "Identical files : ['file']"
     ]
     self._assert_report(d.report, expected_report)
     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.assertEqual(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, [])
     expected_report = [
         'diff {} {}'.format(self.dir, self.dir_diff),
         "Only in {} : ['file2']".format(self.dir_diff),
         "Identical files : ['file']"
     ]
     self._assert_report(d.report, expected_report)
     left_dir, right_dir = self.dir, self.dir_diff
     shutil.move(os.path.join(self.dir_diff, 'file2'),
                 os.path.join(self.dir, 'file2'))
     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', 'file2'])
     self.assertEqual(d.right_list, ['file'])
     self.assertEqual(d.common, ['file'])
     self.assertEqual(d.left_only, ['file2'])
     self.assertEqual(d.right_only, [])
     self.assertEqual(d.same_files, ['file'])
     self.assertEqual(d.diff_files, [])
     expected_report = [
         'diff {} {}'.format(self.dir, self.dir_diff),
         "Only in {} : ['file2']".format(self.dir),
         "Identical files : ['file']"
     ]
     self._assert_report(d.report, expected_report)
     with open(os.path.join(self.dir_diff, 'file2'), 'w') as output:
         output.write('Different contents.\n')
     d = filecmp.dircmp(self.dir, self.dir_diff)
     self.assertEqual(d.same_files, ['file'])
     self.assertEqual(d.diff_files, ['file2'])
     expected_report = [
         'diff {} {}'.format(self.dir, self.dir_diff),
         "Identical files : ['file']", "Differing files : ['file2']"
     ]
     self._assert_report(d.report, expected_report)
def main():
    env_file, rest = sys.argv[1], sys.argv[2:]

    # Parse some argument flags.
    header_dir = None
    resource_dir = None
    input_file = None
    for i, arg in enumerate(rest):
        if arg == '-h' and len(rest) > i + 1:
            assert header_dir == None
            header_dir = rest[i + 1]
        elif arg == '-r' and len(rest) > i + 1:
            assert resource_dir == None
            resource_dir = rest[i + 1]
        elif arg.endswith('.mc') or arg.endswith('.man'):
            assert input_file == None
            input_file = arg

    # Copy checked-in outputs to final location.
    THIS_DIR = os.path.abspath(os.path.dirname(__file__))
    assert header_dir == resource_dir
    source = os.path.join(THIS_DIR, "..", "..", "third_party",
                          "win_build_output",
                          re.sub(r'^(?:[^/]+/)?gen/', 'mc/', header_dir))
    distutils.dir_util.copy_tree(source, header_dir, preserve_times=False)

    # On non-Windows, that's all we can do.
    if sys.platform != 'win32':
        return

    # On Windows, run mc.exe on the input and check that its outputs are
    # identical to the checked-in outputs.

    # Read the environment block from the file. This is stored in the format used
    # by CreateProcess. Drop last 2 NULs, one for list terminator, one for
    # trailing vs. separator.
    env_pairs = open(env_file).read()[:-2].split('\0')
    env_dict = dict([item.split('=', 1) for item in env_pairs])

    extension = os.path.splitext(input_file)[1]
    if extension in ['.man', '.mc']:
        # For .man files, mc's output changed significantly from Version 10.0.15063
        # to Version 10.0.16299.  We should always have the output of the current
        # default SDK checked in and compare to that. Early out if a different SDK
        # is active. This also happens with .mc files.
        # TODO(thakis): Check in new baselines and compare to 16299 instead once
        # we use the 2017 Fall Creator's Update by default.
        mc_help = subprocess.check_output(['mc.exe', '/?'],
                                          env=env_dict,
                                          stderr=subprocess.STDOUT,
                                          shell=True)
        version = re.search(r'Message Compiler\s+Version (\S+)',
                            mc_help).group(1)
        if version != '10.0.15063':
            return

    # mc writes to stderr, so this explicitly redirects to stdout and eats it.
    try:
        tmp_dir = tempfile.mkdtemp()
        delete_tmp_dir = True
        if header_dir:
            rest[rest.index('-h') + 1] = tmp_dir
            header_dir = tmp_dir
        if resource_dir:
            rest[rest.index('-r') + 1] = tmp_dir
            resource_dir = tmp_dir

        # This needs shell=True to search the path in env_dict for the mc
        # executable.
        subprocess.check_output(['mc.exe'] + rest,
                                env=env_dict,
                                stderr=subprocess.STDOUT,
                                shell=True)
        # We require all source code (in particular, the header generated here) to
        # be UTF-8. jinja can output the intermediate .mc file in UTF-8 or UTF-16LE.
        # However, mc.exe only supports Unicode via the -u flag, and it assumes when
        # that is specified that the input is UTF-16LE (and errors out on UTF-8
        # files, assuming they're ANSI). Even with -u specified and UTF16-LE input,
        # it generates an ANSI header, and includes broken versions of the message
        # text in the comment before the value. To work around this, for any invalid
        # // comment lines, we simply drop the line in the header after building it.
        # Also, mc.exe apparently doesn't always write #define lines in
        # deterministic order, so manually sort each block of #defines.
        if header_dir:
            header_file = os.path.join(
                header_dir,
                os.path.splitext(os.path.basename(input_file))[0] + '.h')
            header_contents = []
            with open(header_file, 'rb') as f:
                define_block = []  # The current contiguous block of #defines.
                for line in f.readlines():
                    if line.startswith('//') and '?' in line:
                        continue
                    if line.startswith('#define '):
                        define_block.append(line)
                        continue
                    # On the first non-#define line, emit the sorted preceding #define
                    # block.
                    header_contents += sorted(define_block,
                                              key=lambda s: s.split()[-1])
                    define_block = []
                    header_contents.append(line)
                # If the .h file ends with a #define block, flush the final block.
                header_contents += sorted(define_block,
                                          key=lambda s: s.split()[-1])
            with open(header_file, 'wb') as f:
                f.write(''.join(header_contents))

        # mc.exe invocation and post-processing are complete, now compare the output
        # in tmp_dir to the checked-in outputs.
        diff = filecmp.dircmp(tmp_dir, source)
        if diff.diff_files or set(diff.left_list) != set(diff.right_list):
            print 'mc.exe output different from files in %s, see %s' % (
                source, tmp_dir)
            diff.report()
            for f in diff.diff_files:
                if f.endswith('.bin'): continue
                fromfile = os.path.join(source, f)
                tofile = os.path.join(tmp_dir, f)
                print ''.join(
                    difflib.unified_diff(
                        open(fromfile, 'U').readlines(),
                        open(tofile, 'U').readlines(), fromfile, tofile))
            delete_tmp_dir = False
            sys.exit(1)
    except subprocess.CalledProcessError as e:
        print e.output
        sys.exit(e.returncode)
    finally:
        if os.path.exists(tmp_dir) and delete_tmp_dir:
            shutil.rmtree(tmp_dir)
Example #43
0
def copy_left_only(src, dest, module):
    """Copy files that exist in `src` directory only to the `dest` directory."""

    changed = False
    owner = module.params['owner']
    group = module.params['group']
    local_follow = module.params['local_follow']
    left_only = filecmp.dircmp(src, dest).left_only
    if len(left_only):
        changed = True
    if not module.check_mode:
        for item in left_only:
            src_item_path = os.path.join(src, item)
            dest_item_path = os.path.join(dest, item)
            b_src_item_path = to_bytes(src_item_path,
                                       errors='surrogate_or_strict')
            b_dest_item_path = to_bytes(dest_item_path,
                                        errors='surrogate_or_strict')

            if os.path.islink(b_src_item_path) and os.path.isdir(
                    b_src_item_path) and local_follow is True:
                shutil.copytree(b_src_item_path,
                                b_dest_item_path,
                                symlinks=not (local_follow))
                chown_recursive(b_dest_item_path, module)

            if os.path.islink(b_src_item_path) and os.path.isdir(
                    b_src_item_path) and local_follow is False:
                linkto = os.readlink(b_src_item_path)
                os.symlink(linkto, b_dest_item_path)

            if os.path.islink(b_src_item_path) and os.path.isfile(
                    b_src_item_path) and local_follow is True:
                shutil.copyfile(b_src_item_path, b_dest_item_path)
                if owner is not None:
                    module.set_owner_if_different(b_dest_item_path, owner,
                                                  False)
                if group is not None:
                    module.set_group_if_different(b_dest_item_path, group,
                                                  False)

            if os.path.islink(b_src_item_path) and os.path.isfile(
                    b_src_item_path) and local_follow is False:
                linkto = os.readlink(b_src_item_path)
                os.symlink(linkto, b_dest_item_path)

            if not os.path.islink(b_src_item_path) and os.path.isfile(
                    b_src_item_path):
                shutil.copyfile(b_src_item_path, b_dest_item_path)
                shutil.copymode(b_src_item_path, b_dest_item_path)

                if owner is not None:
                    module.set_owner_if_different(b_dest_item_path, owner,
                                                  False)
                if group is not None:
                    module.set_group_if_different(b_dest_item_path, group,
                                                  False)

            if not os.path.islink(b_src_item_path) and os.path.isdir(
                    b_src_item_path):
                shutil.copytree(b_src_item_path,
                                b_dest_item_path,
                                symlinks=not (local_follow))
                chown_recursive(b_dest_item_path, module)

            changed = True
    return changed
Example #44
0
    def __init__(self, cliargs=None):
        # config file handling

        # handle override of the default .anchore location
        if os.getenv('ANCHOREDATADIR'):
            self.DEFAULT_ANCHORE_DATA_DIR = os.getenv('ANCHOREDATADIR')
            self.DEFAULT_CONFIG_DIR = os.path.join(
                self.DEFAULT_ANCHORE_DATA_DIR, 'conf')
            self.DEFAULT_CONFIG_FILE = os.path.join(self.DEFAULT_CONFIG_DIR,
                                                    'config.yaml')
            self.DEFAULT_TMP_DIR = os.path.join(self.DEFAULT_ANCHORE_DATA_DIR,
                                                'anchoretmp')
            self.DEFAULTS['tmpdir'] = self.DEFAULT_TMP_DIR
            self.DEFAULTS['anchore_data_dir'] = self.DEFAULT_ANCHORE_DATA_DIR

        self.config_dir, self.config_file = self.find_config_file()
        try:
            self.data = load_and_merge(file_path=self.config_file,
                                       defaults=self.DEFAULTS)

            self.cliargs = {}
            if cliargs:
                # store CLI arguments
                self.cliargs = cliargs

            if 'config_overrides' in self.cliargs and self.cliargs[
                    'config_overrides']:
                for override_key in self.cliargs['config_overrides']:
                    if override_key in self.data:
                        self.data[override_key] = self.cliargs[
                            'config_overrides'][override_key]

            if not os.path.exists(self.data['anchore_data_dir']):
                os.makedirs(self.data['anchore_data_dir'])

            if not os.path.exists(self.data['tmpdir']):
                os.makedirs(self.data['tmpdir'])

            #update relative to data dir if not an absolute path
            if not os.path.isabs(self.data['image_data_store']):
                self.data['image_data_store'] = os.path.join(
                    self.data['anchore_data_dir'],
                    self.data['image_data_store'])

            if not os.path.exists(self.data['image_data_store']):
                os.makedirs(self.data['image_data_store'])

            if not os.path.isabs(self.data['feeds_dir']):
                self.data['feeds_dir'] = os.path.join(
                    self.data['anchore_data_dir'], self.data['feeds_dir'])

            if not os.path.isabs(self.data['policy_dir']):
                self.data['policy_dir'] = os.path.join(
                    self.data['anchore_data_dir'], self.data['policy_dir'])

            if not os.path.isabs(self.data['user_scripts_dir']):
                self.data['user_scripts_dir'] = os.path.join(
                    self.data['anchore_data_dir'],
                    self.data['user_scripts_dir'])

            if not os.path.exists(self.data['user_scripts_dir']):
                os.makedirs(self.data['user_scripts_dir'])

            for d in [
                    'analyzers', 'gates', 'queries', 'multi-queries',
                    'shell-utils'
            ]:
                thedir = os.path.join(self.data['user_scripts_dir'], d)
                if not os.path.exists(thedir):
                    os.makedirs(thedir)

            dc = filecmp.dircmp(
                os.path.join(self.data['scripts_dir'], 'shell-utils'),
                os.path.join(self.data['user_scripts_dir'], 'shell-utils'))
            for f in dc.left_only + dc.diff_files:
                try:
                    shutil.copy(
                        os.path.join(self.data['scripts_dir'], 'shell-utils',
                                     f),
                        os.path.join(self.data['user_scripts_dir'],
                                     'shell-utils', f))
                except Exception as err:
                    raise err

        except Exception as err:
            self.data = None
            import traceback
            traceback.print_exc()
            raise err
Example #45
0
    def difference_to_fetched_agent(cls, public_id: str,
                                    agent_name: str) -> List[str]:
        """
        Compare agent against the one fetched from public id.

        :param public_id: str public id
        :param agents_name: str agent name.

        :return: list of files differing in the projects
        """

        # for pydocstyle
        def is_allowed_diff_in_agent_config(
            path_to_fetched_aea, path_to_manually_created_aea
        ) -> Tuple[bool, Dict[str, str], Dict[str, str]]:
            with open(os.path.join(path_to_fetched_aea, "aea-config.yaml"),
                      "r") as file:
                content1 = yaml.full_load(file)
            with open(
                    os.path.join(path_to_manually_created_aea,
                                 "aea-config.yaml"), "r") as file:
                content2 = yaml.full_load(file)
            content1c = copy.deepcopy(content1)
            for key, value in content1c.items():
                if content2[key] == value:
                    content1.pop(key)
                    content2.pop(key)
            allowed_diff_keys = [
                "aea_version",
                "author",
                "description",
                "version",
                "registry_path",
            ]
            result = all([key in allowed_diff_keys for key in content1.keys()])
            result = result and all(
                [key in allowed_diff_keys for key in content2.keys()])
            if result:
                return result, {}, {}
            else:
                return result, content1, content2

        path_to_manually_created_aea = os.path.join(cls.t, agent_name)
        new_cwd = os.path.join(cls.t, "fetch_dir")
        os.mkdir(new_cwd)
        fetched_agent_name = agent_name
        path_to_fetched_aea = os.path.join(new_cwd, fetched_agent_name)
        registry_tmp_dir = os.path.join(new_cwd, cls.packages_dir_path)
        shutil.copytree(str(cls.package_registry_src), str(registry_tmp_dir))
        with cd(new_cwd):
            cls.run_cli_command("fetch", "--local", public_id, "--alias",
                                fetched_agent_name)
        comp = dircmp(path_to_manually_created_aea, path_to_fetched_aea)
        file_diff = comp.diff_files
        result, diff1, diff2 = is_allowed_diff_in_agent_config(
            path_to_fetched_aea, path_to_manually_created_aea)
        if result:
            if "aea-config.yaml" in file_diff:  # pragma: nocover
                file_diff.remove("aea-config.yaml")  # won't match!
        else:
            file_diff.append("Difference in aea-config.yaml: " + str(diff1) +
                             " vs. " + str(diff2))

        with suppress(OSError, IOError):
            shutil.rmtree(new_cwd)

        return file_diff
Example #46
0
def test_build(datadir, tmpdir):
    deliverable_model_builder = DeliverableModelBuilder(tmpdir)

    # metadata builder
    metadata_builder = MetadataBuilder()

    meta_content = MetaContent("algorithmId-corpusId-configId-runId")

    metadata_builder.set_meta_content(meta_content)

    metadata_builder.save()

    # processor builder
    processor_builder = ProcessorBuilder()

    lookup_processor = LookupProcessor()

    vocabulary_lookup_table = Lookuper({"a": 1, "b": 2, "c": 3})
    lookup_processor.add_vocabulary_lookup_table(vocabulary_lookup_table)

    tag_lookup_table = Lookuper({"tag-a": 1, "tag-b": 2, "tag-c": 3})
    lookup_processor.add_tag_lookup_table(tag_lookup_table)

    lookup_processor_handle = processor_builder.add_processor(lookup_processor)
    processor_builder.add_preprocess(lookup_processor_handle)
    processor_builder.add_postprocess(lookup_processor_handle)

    processor_builder.save()

    # model builder
    model_builder = ModelBuilder()
    model_builder.add_keras_h5_model(datadir / "fixture" / "keras_h5_model")
    model_builder.save()

    #
    deliverable_model_builder.add_processor(processor_builder)
    deliverable_model_builder.add_metadata(metadata_builder)
    deliverable_model_builder.add_model(model_builder)

    metadata = deliverable_model_builder.save()

    assert metadata == {
        "version": "1.0",
        "dependency": ["seq2annotation", "tensorflow"],
        "processor": {
            "version": "1.0",
            "instance": {
                "LookupProcessor_0": {
                    "class":
                    "deliverable_model.builtin.processor.lookup_processor.LookupProcessor",
                    "parameter": {
                        "lookup_table": ["vocabulary", "tag"],
                        "padding_parameter": {},
                    },
                }
            },
            "pipeline": {
                "pre": ["LookupProcessor_0"],
                "post": ["LookupProcessor_0"]
            },
        },
        "model": {
            "version": "1.0",
            "type": "keras_h5_model",
            "custom_object_dependency": [],
            "converter_for_request": "converter_for_request",
            "converter_for_response": "converter_for_response",
        },
        "metadata": {
            "version": "1.0",
            "id": "algorithmId-corpusId-configId-runId"
        },
    }

    #
    dircmp_obj = filecmp.dircmp(datadir / "expected", tmpdir)
    assert not dircmp_obj.diff_files
Example #47
0
    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.assertEqual(d.left_only, [])
        self.assertEqual(d.right_only, [])
        self.assertEqual(d.same_files, ['file'])
        self.assertEqual(d.diff_files, [])
        #        expected_report = [
        #            "diff {} {}".format(self.dir, self.dir_same),
        #            "Identical files : ['file']",
        #        ]
        #        self._assert_report(d.report, expected_report)

        # Check attributes for comparison of two different directories (right)
        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.assertEqual(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, [])
        #        expected_report = [
        #            "diff {} {}".format(self.dir, self.dir_diff),
        #            "Only in {} : ['file2']".format(self.dir_diff),
        #            "Identical files : ['file']",
        #        ]
        #        self._assert_report(d.report, expected_report)

        # Check attributes for comparison of two different directories (left)
        left_dir, right_dir = self.dir, self.dir_diff
        shutil.move(os.path.join(self.dir_diff, 'file2'),
                    os.path.join(self.dir, 'file2'))
        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', 'file2'])
        self.assertEqual(d.right_list, ['file'])
        self.assertEqual(d.common, ['file'])
        self.assertEqual(d.left_only, ['file2'])
        self.assertEqual(d.right_only, [])
        self.assertEqual(d.same_files, ['file'])
        self.assertEqual(d.diff_files, [])
        #        expected_report = [
        #            "diff {} {}".format(self.dir, self.dir_diff),
        #            "Only in {} : ['file2']".format(self.dir),
        #            "Identical files : ['file']",
        #        ]
        #        self._assert_report(d.report, expected_report)

        # Add different file2
        with open(os.path.join(self.dir_diff, 'file2'), 'w') as output:
            output.write('Different contents.\n')
        d = filecmp.dircmp(self.dir, self.dir_diff)
        self.assertEqual(d.same_files, ['file'])
        self.assertEqual(d.diff_files, ['file2'])
Example #48
0
def main(arch, outdir, tlb, h, dlldata, iid, proxy, idl, *flags):
    # chromoting_lib.idl uses a uuid that's hashed of chrome's version string,
    # i.e. it changes every few compiles.  So a checked-in file does not work
    # for chromoting_lib.idl.  For now, call midl.exe for remoting instead of
    # using checked-in artifacts for it.
    is_chromoting = os.path.basename(idl) == 'chromoting_lib.idl'

    # Copy checked-in outputs to final location.
    THIS_DIR = os.path.abspath(os.path.dirname(__file__))
    source = os.path.join(THIS_DIR, '..', '..', '..', 'third_party',
                          'win_build_output', outdir.replace('gen/', 'midl/'))
    if os.path.isdir(os.path.join(source, os.path.basename(idl))):
        source = os.path.join(source, os.path.basename(idl))
    source = os.path.join(source, arch.split('.')[1])  # Append 'x86' or 'x64'.
    source = os.path.normpath(source)
    if not is_chromoting:
        distutils.dir_util.copy_tree(source, outdir, preserve_times=False)

    # On non-Windows, that's all we can do.
    if sys.platform != 'win32':
        return 0 if not is_chromoting else 1

    # On Windows, run midl.exe on the input and check that its outputs are
    # identical to the checked-in outputs.
    if not is_chromoting:
        tmp_dir = tempfile.mkdtemp()
        delete_tmp_dir = True
    else:
        tmp_dir = outdir
        delete_tmp_dir = False

    # Read the environment block from the file. This is stored in the format used
    # by CreateProcess. Drop last 2 NULs, one for list terminator, one for
    # trailing vs. separator.
    env_pairs = open(arch).read()[:-2].split('\0')
    env_dict = dict([item.split('=', 1) for item in env_pairs])

    args = ['midl', '/nologo'] + list(flags) + [
        '/out', tmp_dir, '/tlb', tlb, '/h', h, '/dlldata', dlldata, '/iid',
        iid, '/proxy', proxy, idl
    ]
    try:
        popen = subprocess.Popen(args,
                                 shell=True,
                                 env=env_dict,
                                 stdout=subprocess.PIPE,
                                 stderr=subprocess.STDOUT)
        out, _ = popen.communicate()
        # Filter junk out of stdout, and write filtered versions. Output we want
        # to filter is pairs of lines that look like this:
        # Processing C:\Program Files (x86)\Microsoft SDKs\...\include\objidl.idl
        # objidl.idl
        lines = out.splitlines()
        prefixes = ('Processing ', '64 bit Processing ')
        processing = set(
            os.path.basename(x) for x in lines if x.startswith(prefixes))
        for line in lines:
            if not line.startswith(prefixes) and line not in processing:
                print line
        if popen.returncode != 0:
            return popen.returncode
        if is_chromoting:
            return 0

        for f in os.listdir(tmp_dir):
            ZapTimestamp(os.path.join(tmp_dir, f))

        # Now compare the output in tmp_dir to the checked-in outputs.
        diff = filecmp.dircmp(tmp_dir, source)
        if diff.diff_files or set(diff.left_list) != set(diff.right_list):
            print 'midl.exe output different from files in %s, see %s' \
                % (source, tmp_dir)
            diff.report()
            for f in diff.diff_files:
                if f.endswith('.tlb'): continue
                fromfile = os.path.join(source, f)
                tofile = os.path.join(tmp_dir, f)
                print ''.join(
                    difflib.unified_diff(
                        open(fromfile, 'U').readlines(),
                        open(tofile, 'U').readlines(), fromfile, tofile))
            delete_tmp_dir = False
            print 'To rebaseline:'
            print '  copy /y %s\* %s' % (tmp_dir, source)
            sys.exit(1)
        return 0
    finally:
        if os.path.exists(tmp_dir) and delete_tmp_dir:
            shutil.rmtree(tmp_dir)
Example #49
0
#!/usr/bin/env python3
import filecmp
a = 'testdir1'
b = 'testdir2'
#目录之间比较
result = filecmp.dircmp(a, b)
print(result.report())

print("=" * 10)

#第一个目录中所有的文件
print(result.left_list)  #right_list

print("=" * 10)
#第一个目录中独有的文件
print(result.left_only)

print('=' * 20)
#目录中是否有相同的目录名
print(result.common_dirs)

print('=' * 20)
#目录中是否有相同的文件名
print(result.common_files)
Example #50
0
    fp.close()

    print("json:", port_list)


def run_nmap():
    nmap_out = subprocess.run([nmap_path, "-T4", nmap_network],
                              capture_output=True)
    nmap_data = nmap_out.stdout.splitlines()
    #nmap_data = 'nmap output'
    return nmap_data


create_directory()
input_data = run_nmap()
print(input_data)
file_name = create_date_string()
write_files(input_data, file_name)

import filecmp

path = my_dir
comparison = filecmp.dircmp(path + '051220.json', path + '051220.pickle')
common_files = ','.join(comparison.common)
left_only_file = ','.join(comparison.left_only)
right_only_file = ','.join(comparison.right_only)
with open(path + 'folder_diff.txt', 'w') as folder_report:
    folder_report.write("common files: " + common_files + '\n')
    folder_report.write('\n' + "only in json file" + left_only_file + '\n')
    folder_report.write('\n' + "only in pickle file" + right_only_file + '\n')
Example #51
0
def test_generate_partitions_sources(monkeypatch, test_template_setup):
    """
    Test which calls generate_partitions_sources() with the data from
    'test_template_setup' fixture.
    Because generate_partitions_sources() is a compound of the other functions in
    the module which are tested individually, this test just do the following:
    1. Calls generate_partitions_sources() and checks that the autogen directory
       was created.
    2. Saves the modified times of the generated files.
    3. Calls generate_partitions_sources() again, checks that the autogen directory
       still exist and that modified times of the generated files didn't
       change.

    :param monkeypatch: The 'monkeypatch' fixture
           (https://docs.pytest.org/en/latest/monkeypatch.html).
    :param test_template_setup: The 'test_template_setup' fixture.
    :return:
    """
    monkeypatch.setitem(DEFAULT_FILTERS, 'basename', os.path.basename)
    monkeypatch.setitem(DEFAULT_FILTERS, 'find_priority_key',
                        find_priority_key)
    monkeypatch.setitem(DEFAULT_FILTERS, 'find_permission_key',
                        find_permission_key)

    autogen_dirs = generate_partitions_sources(
        test_template_setup['manifest_files'])
    autogen_dirs_backup = tempfile.mkdtemp()
    for directory in autogen_dirs:
        assert os.path.isdir(directory)
        shutil.copytree(
            directory,
            os.path.join(autogen_dirs_backup,
                         os.path.split(directory)[1]))

    autogen_dirs = generate_partitions_sources(
        test_template_setup['manifest_files'])
    for directory in autogen_dirs:
        assert os.path.isdir(directory)
        dcmp = filecmp.dircmp(
            directory,
            os.path.join(autogen_dirs_backup,
                         os.path.split(directory)[1]))
        assert not dcmp.diff_files

    spm_output_dir = generate_psa_setup(
        test_template_setup['manifest_files'],
        os.path.join(test_template_setup['dir'], 'SETUP'),
        weak_setup=False,
        extra_filters=test_template_setup['filters'])

    assert os.path.isdir(spm_output_dir)
    shutil.copytree(
        spm_output_dir,
        os.path.join(autogen_dirs_backup,
                     os.path.split(spm_output_dir)[1]))

    for gen_file in test_template_setup['common_files']:
        generated_file = os.path.join(spm_output_dir, gen_file)
        expected_file = os.path.join(test_template_setup['dir'], gen_file)
        assert os.path.isfile(generated_file)
        assert os.path.isfile(expected_file)

        with open(generated_file) as gfh:
            with open(expected_file) as efh:
                assert json.load(gfh) == json.load(efh)

    spm_output_dir = generate_psa_setup(
        test_template_setup['manifest_files'],
        os.path.join(test_template_setup['dir'], 'SETUP'),
        weak_setup=False,
        extra_filters=test_template_setup['filters'])

    assert os.path.isdir(spm_output_dir)
    dcmp = filecmp.dircmp(
        spm_output_dir,
        os.path.join(autogen_dirs_backup,
                     os.path.split(spm_output_dir)[1]))
    assert not dcmp.diff_files
Example #52
0
    def compare_directories(self,
                            left,
                            right,
                            left_prefix="",
                            right_prefix=""):
        """Compares two directories.

    Returns a string representing the diff between them if there is
    any difference between them.  If there is no difference between
    them, returns an empty string.
    left: Path to left side of diff
    right: Path to right side of diff
    left_prefix: String to prepend to all left-side files
    right_prefix: String to prepend to all right-side files
    """

        # Right side might not exist.
        if not os.path.exists(right):
            all_diffs = []
            for f in os.listdir(left):
                all_diffs += "Found %s but not %s.\n" % (os.path.join(
                    left_prefix, f), os.path.join(right_prefix, f))
                left_file = os.path.join(left, f)
                with open(left_file, 'r') as myfile:
                    data = myfile.readlines()
                    all_diffs += difflib.unified_diff(
                        data, [], '"' + os.path.join(left_prefix, f) + '"',
                        "/dev/null")
            return ''.join(all_diffs)

        # Left side might not exist.
        if not os.path.exists(left):
            all_diffs = []
            for f in os.listdir(right):
                all_diffs += "Found %s but not %s.\n" % (os.path.join(
                    right_prefix, f), os.path.join(left_prefix, f))
                right_file = os.path.join(right, f)
                with open(right_file, 'r') as myfile:
                    data = myfile.readlines()
                    all_diffs += difflib.unified_diff(
                        [], data, "/dev/null",
                        '"' + os.path.join(right_prefix, f) + '"')
            return ''.join(all_diffs)

        diff = filecmp.dircmp(left, right)
        # Now compare all the differing files.
        all_diffs = []
        for f in diff.left_only:
            all_diffs += "Found %s but not %s.\n" % (os.path.join(
                left_prefix, f), os.path.join(right_prefix, f))
            left_file = os.path.join(left, f)
            with open(left_file, 'r') as myfile:
                data = myfile.readlines()
                all_diffs += difflib.unified_diff(
                    data, [], '"' + os.path.join(left_prefix, f) + '"',
                    "/dev/null")
        for f in diff.right_only:
            all_diffs += "Found %s but not %s.\n" % (os.path.join(
                right_prefix, f), os.path.join(left_prefix, f))
            right_file = os.path.join(right, f)
            with open(right_file, 'r') as myfile:
                data = myfile.readlines()
                all_diffs += difflib.unified_diff(
                    [], data, "/dev/null",
                    '"' + os.path.join(right_prefix, f) + '"')
        for f in diff.diff_files:
            left_file = os.path.join(left, f)
            right_file = os.path.join(right, f)
            with open(left_file, 'r') as myfile0, open(right_file,
                                                       'r') as myfile1:
                data0 = myfile0.readlines()
                data1 = myfile1.readlines()
                all_diffs += difflib.unified_diff(
                    data0, data1, '"' + os.path.join(left_prefix, f) + '"',
                    '"' + os.path.join(right_prefix, f) + '"')
        return ''.join(all_diffs)
Example #53
0
import tarfile, os, filecmp

os.chdir('../data')
if not os.path.exists('new1'):
    os.mkdir('new1')
if not os.path.exists('new2/new3'):
    os.makedirs('new2/new3')

tf = tarfile.open('files.tar.bz2', 'r:bz2')
tf.extractall('new1')
tf.extractall('new2')
tf.extractall('new2/new3')
tf.close()

cmp = filecmp.dircmp('new1', 'new2')
cmp.report()

os.chdir('../src')
Example #54
0
    try:
        if (input_is_directory(arguments[1])):
            dir1 = arguments[1]
        else:
            raise ValueError("Input 1 is not a valid Directory")
        if (input_is_directory(arguments[2])):
            dir2 = arguments[2]
        else:
            raise ValueError("Input 2 is not a valid Directory")
    except IndexError:
        raise IndexError("Not enough Directorienames given")

    return dir1, dir2


if __name__ == "__main__":
    print("Start")
    start_ts = time.time()

    try:
        dir1, dir2 = get_directories_from_arguments(sys.argv)
        dcmp = filecmp.dircmp(dir1, dir2)
        depth = 1
        compare_directories(dcmp, depth)
    except Exception as e:
        print(e)

    end_ts = time.time()
    duration = round(end_ts - start_ts)

    print("End after %s seconds" % (duration))
Example #55
0
 def assert_dirs_equal(self, dir1, dir2):
     diff = filecmp.dircmp(dir1, dir2)
     self._assert_dirs_equal_cmp(diff)
Example #56
0
#! /usr/bin/env/python
# -*- coding:utf-8 -*-

import filecmp

filecmp.dircmp('example/dir1','example/dir2').report_full_closure()
Example #57
0
import filecmp

a = "./dir1"
b = "./dir2"

dirobj = filecmp.dircmp(a, b, ['test.py'])

print "-------------------report---------------------"
dirobj.report()
print "-------------report_partial_closure-----------"
dirobj.report_partial_closure()
print "-------------report_full_closure--------------"
dirobj.report_full_closure()

print "left_list:" + str(dirobj.left_list)
print "right_list:" + str(dirobj.right_list)
print "common:" + str(dirobj.common)
print "left_only:" + str(dirobj.left_only)
print "right_only:" + str(dirobj.right_only)
print "common_dirs:" + str(dirobj.common_dirs)
print "common_files:" + str(dirobj.common_files)
print "common_funny:" + str(dirobj.common_funny)
print "same_file:" + str(dirobj.same_files)
print "diff_files:" + str(dirobj.diff_files)
print "funny_files:" + str(dirobj.funny_files)
Example #58
0
 def dag_is_valid(self, dag_zip_path, workflow_package_path):
     with ZipFile(dag_zip_path, 'r') as zip_ref:
         zip_ref.extractall(self.comparision_tmp_dir)
         self.assertTrue(filecmp.dircmp(workflow_package_path, self.comparision_tmp_dir))
Example #59
0
import filecmp
compRand = filecmp.dircmp(
    '../margrave/examples/tutorial/xacml-code/xacmlA/0/',
    '../margrave/examples/tutorial/xacml-code/xacmlA/orig/')
compRand.report_full_closure()