def dircmp(a, b): # Compare whether two directories are the same # To make this as fast as possible, it uses the statcache print ' dircmp', a, b a_list = dircache.listdir(a) b_list = dircache.listdir(b) for x in a_list: if x in skiplist: pass elif x not in b_list: return 0 else: ax = path.cat(a, x) bx = path.cat(b, x) if statcache.isdir(ax) and statcache.isdir(bx): if not dircmp(ax, bx): return 0 else: try: if not cmpcache.cmp(ax, bx): return 0 except posix.error: return 0 for x in b_list: if x in skiplist: pass elif x not in a_list: return 0 return 1
def gdiff_ab(w, m, item): # Call SGI's gdiff utility x = getselection(w) if x: a, b = path.cat(w.a, x), path.cat(w.b, x) if path.isdir(a) or path.isdir(b): stdwin.fleep() # This is for files only else: diffcmd = 'gdiff' diffcmd = diffcmd + mkarg(a) + mkarg(b) + ' &' print diffcmd sts = posix.system(diffcmd) if sts: print 'Exit status', sts
def phase4(dd): # Find out differences between common subdirectories # A new dircmp object is created for each common subdirectory, # these are stored in a dictionary indexed by filename. # The hide and ignore properties are inherited from the parent dd.subdirs = {} for x in dd.common_dirs: a_x = path.cat(dd.a, x) b_x = path.cat(dd.b, x) dd.subdirs[x] = newdd = dircmp().new(a_x, b_x) newdd.hide = dd.hide newdd.ignore = dd.ignore newdd.run()
def cp_ba(w, m, item): x = getselection(w) if x: if x[-1:] = '/': x = x[:-1] ax = path.cat(w.a, x) bx = path.cat(w.b, x) if path.isdir(bx): if path.exists(ax): m = 'Can\'t copy directory to existing target' stdwin.message(m) return runcmd('cp -r' + mkarg(bx) + mkarg(w.a)) else: runcmd('cp' + mk2arg(w.b, x) + mkarg(ax)) update(w)
def action(w, string, i, detail): (h, v), clicks, button, mask = detail if clicks = 2: name = path.cat(w.name, string) try: w2 = anywin.open(name) w2.parent = w except posix.error, why: stdwin.message('Can\'t open ' + name + ': ' + why[1])
def open(name): name = path.cat(name, '') list = dircache.opendir(name)[:] list.sort() dircache.annotate(name, list) w = listwin.open(name, list) w.name = name w.action = action return w
def phase2(dd): # Distinguish files, directories, funnies dd.common_dirs = [] dd.common_files = [] dd.common_funny = [] # for x in dd.common: a_path = path.cat(dd.a, x) b_path = path.cat(dd.b, x) # ok = 1 try: a_stat = statcache.stat(a_path) except posix.error, why: # print 'Can\'t stat', a_path, ':', why[1] ok = 0 try: b_stat = statcache.stat(b_path) except posix.error, why: # print 'Can\'t stat', b_path, ':', why[1] ok = 0
def rm_b(w, m, item): x = getselection(w) if x: if x[-1:] = '/': x = x[:-1] x = path.cat(w.b, x) if path.isdir(x): if askyesno('Recursively remove B directory ' + x, 1): runcmd('rm -rf' + mkarg(x)) else: runcmd('rm -f' + mkarg(x)) update(w)
def copytree(src, dst): names = posix.listdir(src) posix.mkdir(dst, 0777) dot_dotdot = '.', '..' for name in names: if name not in dot_dotdot: srcname = path.cat(src, name) dstname = path.cat(dst, name) #print 'Copying', srcname, 'to', dstname try: #if path.islink(srcname): # linkto = posix.readlink(srcname) # posix.symlink(linkto, dstname) #elif path.isdir(srcname): if path.isdir(srcname): copytree(srcname, dstname) else: copy2(srcname, dstname) # XXX What about devices, sockets etc.? except posix.error, why: print 'Could not copy', srcname, 'to', dstname, print '(', why[1], ')'
def diffdata(a, b, flags): # Compute directory differences. # a_only = [('A only:', header_action), ('', header_action)] b_only = [('B only:', header_action), ('', header_action)] ab_diff = [('A <> B:', header_action), ('', header_action)] ab_same = [('A == B:', header_action), ('', header_action)] data = [a_only, b_only, ab_diff, ab_same] # a_list = dircache.listdir(a)[:] b_list = dircache.listdir(b)[:] dircache.annotate(a, a_list) dircache.annotate(b, b_list) a_list.sort() b_list.sort() # for x in a_list: if x in ['./', '../']: pass elif x not in b_list: a_only.append(x, a_only_action) else: ax = path.cat(a, x) bx = path.cat(b, x) if path.isdir(ax) and path.isdir(bx): if flags = '-r': same = dircmp(ax, bx) else: same = 0 else: try: same = cmp.cmp(ax, bx) except posix.error: same = 0 if same: ab_same.append(x, ab_same_action) else: ab_diff.append(x, ab_diff_action)
def visit(pattern, dirname, names): if path.islink(dirname): names[:] = [] return if path.ismount(dirname): print 'descend into', dirname n = len(pattern) for name in names: name = path.cat(dirname, name) try: linkto = posix.readlink(name) if linkto[:n] = pattern: print name, '->', linkto except posix.error: pass
def mouselistwindow(w, type, detail): (h, v), clicks, button = detail[:3] d = w.begindrawing() lh = d.lineheight() if 0 <= v < lh*len(w.list): i = v / lh else: i = -1 if w.selected <> i: hideselection(w, d) w.selected = i showselection(w, d) if type = WE_MOUSE_DOWN and clicks >= 2 and i >= 0: name = path.cat(w.dirname, w.list[i]) if name[-1:] = '/': if clicks = 2: G.windows.append(openlistwindow(name[:-1]))
def cmpfiles(a, b, common): res = ([], [], []) for x in common: res[cmp(path.cat(a, x), path.cat(b, x))].append(x) return res
# List windows -- to display list of files and subdirectories def openlistwindow(dirname): list = posix.listdir(dirname) list.sort() i = 0 while i < len(list): if list[i] = '.' or list[i] = '..': del list[i] else: i = i+1 for i in range(len(list)): name = list[i] if path.isdir(path.cat(dirname, name)): list[i] = list[i] + '/' width = maxwidth(list) width = width + stdwin.textwidth(' ') # XXX X11 stdwin bug workaround height = len(list) * stdwin.lineheight() stdwin.setdefwinsize(width, min(height, 500)) w = stdwin.open(dirname) stdwin.setdefwinsize(0, 0) w.setdocsize(width, height) w.drawproc = drawlistwindow w.mouse = mouselistwindow w.close = closelistwindow w.dirname = dirname w.list = list w.selected = -1 return w
import fnmatch def glob(pathname): if not has_magic(pathname): return [pathname] dirname, basename = path.split(pathname) if dirname[-1:] = '/' and dirname <> '/': dirname = dirname[:-1] if has_magic(dirname): list = glob(dirname) else: list = [dirname] if not has_magic(basename): result = [] for dirname in list: if basename or path.isdir(dirname): name = path.cat(dirname, basename) if path.exists(name): result.append(name) else: result = [] for dirname in list: sublist = glob1(dirname, basename) for name in sublist: result.append(path.cat(dirname, name)) return result def glob1(dirname, pattern): if not dirname: dirname = '.' try: names = posix.listdir(dirname) except posix.error:
if selname: for icol in range(len(w.data)): for irow in range(2, len(w.data[icol])): if w.data[icol][irow][0] = selname: tablewin.select(w, (icol, irow)) break # Action functions for table items in directory diff windows def header_action(w, string, (icol, irow), (pos, clicks, button, mask)): tablewin.select(w, (-1, -1)) def a_only_action(w, string, (icol, irow), (pos, clicks, button, mask)): tablewin.select(w, (icol, irow)) if clicks = 2: w2 = anyopen(path.cat(w.a, string)) if w2: w2.parent = w def b_only_action(w, string, (icol, irow), (pos, clicks, button, mask)): tablewin.select(w, (icol, irow)) if clicks = 2: w2 = anyopen(path.cat(w.b, string)) if w2: w2.parent = w def ab_diff_action(w, string, (icol, irow), (pos, clicks, button, mask)): tablewin.select(w, (icol, irow)) if clicks = 2: w2 = anydiff(path.cat(w.a, string), path.cat(w.b, string), '') w2.parent = w
def ab_same_action(w, string, sel, detail): ax = path.cat(w.a, string) if path.isdir(ax): ab_diff_action(w, string, sel, detail) else: a_only_action(w, string, sel, detail)
def dodiff(w, flags): x = getselection(w) if x: w2 = anydiff(path.cat(w.a, x), path.cat(w.b, x), flags) w2.parent = w
def mk2arg(head, x): return mkarg(path.cat(head, x))
def annotate(head, list): # Add '/' suffixes to directories for i in range(len(list)): if path.isdir(path.cat(head, list[i])): list[i] = list[i] + "/"
def open_b(w, m, item): x = getselection(w) if x: w2 = anyopen(path.cat(w.b, x)) if w2: w2.parent = w
def annotate(head, list): # Add '/' suffixes to directories for i in range(len(list)): if path.isdir(path.cat(head, list[i])): list[i] = list[i] + '/'