📄 dirdiff.py
字号:
break else: raw_input('<RET> to continue ..?') #printlines(diffs) else: print ' - same' errs = [] return ##############################################################################def walk_check(tree_root, excluded): # shorten absolute paths by root dir element def rcp(fnm): return fnm.replace(tree_root, '$ROOT$') # field os errors during walk, but collect notification in errlist def ignore_OSError(s): pass exlist = [] d1 = {} d2 = {} dlinks = [] flinks = [] #print 'tree_root', tree_root, rcp(tree_root)## firstel = tree_root.split('/')[0]## if firstel == '.' or firstel == '..' or tree_root[0] != '/':## tree_root = normpath(join(os.getcwd(), tree_root)) for root, dirs, files in walk(tree_root, onerror=ignore_OSError, exlist=exlist, excluded=excluded): #print 'root', root, rcp(root) for d in dirs: dir = join(root, d) if islink(dir): #print 'Linked dir', rcp(dir), '->', rcp(realpath(dir)) dlinks.append((rcp(dir), rcp(realpath(dir)))) for f in files: #print f if unwanted_suff(f): continue file = join(root, f) if islink(file): #print 'Linked file', file, '->', realpath(file) flinks.append((rcp(file), rcp(realpath(file)))) continue d = d1.setdefault(f, {}) d[root] = 1 ft = d1.items() ft.sort() dups = [] for f in ft: if len(f[1]) > 1: dirs = f[1].keys() dirs.sort() #print '\'%s\' exists in \'%s\' and' % (f[0], rcp(dirs[0])) ## dups.append('\'%s\' in \'%s\' and' % (f[0], rcp(dirs[0])))## for d in dirs[1:]:## #print '\t\'%s\'' % (rcp(d))## dups.append('\t\'%s\'' % (rcp(d))) dups.append((f[0], dirs)) if dups: print print 'Following possibly duplicated files found:' for d in dups: #print d print '\'%s\' in:' % (d[0]) for dr in d[1]: print '\t%s' % (rcp(dr)) else: print 'No duplicated files found' if dlinks: print print 'Following soft linked directories found:' for d in dlinks: print d[0], '->', d[1] if flinks: print print 'Following soft linked files found:' for d in flinks: print d[0], '->', d[1] if exlist: if exlist[0]: print '\nXXX The following errors occurred:' for err in exlist[0]: print err if exlist[1]: print '\nXXX The following directories were not visited multiple times:' for d in exlist[1]: print '\'%s\' -> \'%s\'' % (d[0], d[1]) if exlist[2]: print '\nXXX The following dirs were excluded:' for d in exlist[2]: print d ##############################################################################def eprint(s): sys.stderr.write(s + '\n')############################################################################### def get_excluded(el): elist = el.split(',') rel = [] for e in elist: rel.append(e) return rel ############################################################################## ##############################################################################def usage(nm, msg=None): if msg: eprint('%s: %s\n' % (nm, msg)) eprint('%s: Compare files in two given directories' % (nm)) eprint('Usage:') eprint('\t%s [flags] dir1 dir2' % (nm)) eprint('Flags:') eprint('\t-f<file> Check only <file>') eprint('\t-c Provide context info for Python files') eprint('\t-h This help') exit(1)##############################################################################def help_exclude(): print 'Excluded directories are given as a comma-separated (no spaces) list of one or more names' print 'The names may be ' ###############################################################################def main(): debug = 0 context = 0 dowalk = 0 helpme = 0 dir1 = DEFAULT_FIRSTDIR dir2 = DEFAULT_SECDIR file = None ferr = [] diffiles = [] notinfirst = [] notinsecond = [] excluded = [] scriptname = os.path.basename(argv[0]) try: optlist, args = getopt.getopt(argv[1:], 'dhcf:we:') except getopt.error, s: print 'foo', str(s) #raise usage(scriptname, msg=str(s)) for opt in optlist: if opt[0] == '-h': #usage(scriptname) helpme += 1 if opt[0] == '-d': debug += 1 if opt[0] == '-c': context = 1 if opt[0] == '-w': dowalk = 1 if opt[0] == '-f': file = basename(opt[1]) if opt[0] == '-e': excluded = get_excluded(opt[1]) #print args #raw_input('...') if helpme: usage(scriptname) if helpme > 1: help_exclude() if len(args) == 2 and not dowalk: dir1 = resolve_path(args[0]) dir2 = resolve_path(args[1]) elif len(args) == 1 and dowalk: dir1 = resolve_path(args[0]) else: usage(scriptname, msg='wrong number of arguments') #print 'dirs', dir1, dir2 #raw_input('...') while 1: print '%s: CAUTION - WILL ONLY CHECK VISIBLE FILES - ENSURE ALL ARE CHECKED OUT' % (scriptname) print '<RET> to continue, <SPC><RET> to abort' ans = raw_input('?') if ans == '': break elif ans == ' ': return if dowalk: walk_check(dir1, excluded) return firstdir = lsdir(dir1, ferr) secdir = lsdir(dir2, ferr) infirst = 0 for f in firstdir: #print f if file: if basename(f) == file: infirst = 1 break else: if secdir.has_key(f): continue else: notinsecond.append(f) insecond = 0 for f in secdir: #print f if file: if basename(f) == file: insecond = 1 break else: if firstdir.has_key(f): #do_diff(f) diffiles.append(f) else: notinfirst.append(f) if len(notinsecond): print 'Files in first dir but not in second:' for f in notinsecond: print f print if len(notinfirst): print 'Files in second dir but not in first:' for f in notinfirst: print f print if file: if infirst and insecond: diffiles.append(file) else: if not infirst: print 'File %s not found' % (join(dir1, file)) if not insecond: print 'File %s not found' % (join(dir2, file)) if len(ferr): print 'Errors encountered:' for f in ferr: print f[-1] diffiles.sort() for f in diffiles: do_diff(f, dir1, dir2, debug, context) ############################################################################### Call main when run as scriptif __name__ == '__main__': main()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -