import filecmp
import os, sys
# bash : find . -type f // to recursively file all of file only in directory
def diffExtract(basedir, diffdir):
# Determine the items that exist in both directories
d1_contents=set([])
d2_contents=set([])
for root, subFolders, files in os.walk(basedir):
for file in files:
subdir=root.replace(basedir, '') # just keep the common subdir path
d1_contents.add(os.path.join(subdir,file))
# print 'Dir1Files:', d1_contents
for root2, subFolders2, files2 in os.walk(diffdir):
for file in files2:
subdir2=root2.replace(diffdir, '')
d2_contents.add(os.path.join(subdir2,file))
# print 'Dir2files:', d2_contents
common = list(d1_contents & d2_contents) # get the common file by join the Set
'''
common_files = [ f
for f in common
if os.path.isfile(os.path.join("~/build/QCT2050", f))
]
for f in common :
ff=os.path.join("~/build/QCT2050", f)
print "JoinPath:", ff
if os.path.isfile(ff) :
common_files.append(ff)
print 'Common files:', common_files
'''
print "removing the common file from ", diffdir
delcount=0
for f in common :
ff1=basedir + f
ff2=diffdir + f
if (os.path.isfile(ff1) and os.path.isfile(ff2) and filecmp.cmp(ff1, ff2, shallow=False)) or os.path.islink(ff2) : #(os.path.isfile(ff)) :
#common_files.append(ff)
sys.stdout.write(".")
sys.stdout.flush()
os.remove(ff2)
delcount += 1
print '\n%d common files are deleted from directory, %s.' % (delcount, diffdir)
return
# Compare the directories
#match, mismatch, errors = filecmp.cmpfiles(basedir,
# diffdir,
# common)
#print 'Match:', match
#print 'Mismatch:', mismatch
#print 'Errors:', errors
# ===========================================
def listFileRecursive(dir):
# Determine the items that exist in both directories
for root, subFolders, files in os.walk(basedir):
for file in files:
print (os.path.join(root,file))
def mkfile(filename, body=None):
with open(filename, 'w') as f:
f.write(body or filename)
return
def make_example_dir(top):
if not os.path.exists(top):
os.mkdir(top)
curdir = os.getcwd()
os.chdir(top)
os.mkdir('dir1')
os.mkdir('dir2')
mkfile('dir1/file_only_in_dir1.txt')
mkfile('dir2/file_only_in_dir2.txt')
os.mkdir('dir1/dir_only_in_dir1')
os.mkdir('dir2/dir_only_in_dir2')
os.mkdir('dir1/common_dir')
os.mkdir('dir2/common_dir')
mkfile('dir1/common_file', 'this file is the same.txt')
mkfile('dir2/common_file', 'this file is the same.txt')
mkfile('dir1/not_the_same.txt')
mkfile('dir2/not_the_same.txt')
mkfile('dir1/file_in_dir1', 'This is a file in dir1.txt')
os.mkdir('dir2/file_in_dir1')
os.chdir(curdir)
return
if __name__ == '__main__':
#os.chdir(os.path.dirname(__file__) or os.getcwd())
#make_example_dir('example')
#make_example_dir('example/dir1/common_dir')
#make_example_dir('example/dir2/common_dir')
print ''
print 'Usage : %s basedir diffdir' % (sys.argv[0])
basedir=sys.argv[1] # The common part directory
diffdir=sys.argv[2] # The different part directory
print 'This program is to compare the content of base directory, and variant directory, then delete all of common and same content file from variant directory.'
print 'Warning : This program will really make change on the variant directory, %s.' % (diffdir)
print 'Please make sure that is what you want.'
raw_input("Press Enter to continue ...");
diffExtract(basedir, diffdir)
沒有留言:
張貼留言