def dofind(substr): """Python way to run recursive find looking for filenames matching a given string""" r1 = re.compile('.*%s.*' %header.to_lower(substr)) cwd = os.getcwd() # starting directory n = len(cwd) filelist = [] for (path, dirs, files) in os.walk(cwd): for f in files: if r1.match(header.to_lower(f)): filelist.append(os.path.join(path[n+1:],f)) for d in dirs: if r1.match(header.to_lower(d)): filelist.append(os.path.join(path[n+1:],d)) filelist.sort(header.smart_sort) return filelist
def list_syncs(data): """Prints the stored sync locations and lets the user choose one or more to synchronize. It returns a list of the full file path names""" header.print_database(data,shownum=False) choice=raw_input("Choose: ") if choice != "": choicelist = [header.to_lower(f) for f in choice.split()] docs = grep_data(choicelist,data) return docs
def expand_glob(files,data): """Expand shell globs using the file names in data""" outlist = [] trashnames = [s['name'] for s in data] # get list of names for f in files: junk = fnmatch.filter(trashnames,header.to_lower(f.strip('/'))) if len(junk) == 0: print "### Warning! File '%s' not in trash. Skipping." %f else: outlist = outlist + junk return set(outlist) # convert to set to make unique list of names
header.make_trash_backup() if len(data) == 0: print "The trash is empty." header.remove_trash_backup() sys.exit() restore = [] if len(sys.argv) == 1: # undelete last file restore.append(data[-1]) header.write_trash(data[:-1],mode='w') else: files = expand_glob(sys.argv[1:],data) junk = [] for s in data: # loop over deleted file names if header.to_lower(s['name']) in files: restore.append(s) else: junk.append(s) header.write_trash(junk,mode='w') for s in restore: if os.path.exists(s['org_path']): # original folder still exists if header.file_exists(s['org_path'],s['org_name']): junk = os.path.join(s['org_path'],s['org_name']) choice = raw_input('Warning! "%s" exists. Overwrite (y/N)?' %junk) if choice[0].lower=='y': temp = header.do_match(junk) os.system('rm "%s"' %temp) restorefile(s) else:
header.write_trash([blah]) os.system('mv "%s" %s/"%s"' %(oldpath,trashdir,newname)) if __name__ == "__main__": if len(sys.argv) == 1: print __doc__ sys.exit() if '-h' in sys.argv or '-help' in sys.argv or '--help' in sys.argv: print __doc__ sys.exit() namelist = [] trashdir = header.nearest_trashdir(os.getcwd()) data = header.read_trash() namelist = [header.to_lower(a['name']) for a in data] docs = header.read_cmdline(sys.argv[1:]) for s in docs: path = os.path.split(os.path.abspath(s)) filename = path[1] lowerfname = header.to_lower(filename) trashdir = header.nearest_trashdir(path[0]) if lowerfname in namelist: i = 2 while '%s_#%d' %(filename,i) in namelist: i = i + 1 deletefile(s,'%s_#%d' %(filename,i),filename,trashdir) else: deletefile(s,filename,filename,trashdir)
asource = {'name' : '', 'path' : ''} for f in docs: junk,name = os.path.split(f) if name in namelist: print '"%s" already defined. Skipping' %name else: asource['name'] = name asource['path'] = location newlist.append(asource.copy()) header.write_database(newlist,syncFile) elif flag in ('d','u','x'): if len(sys.argv) < 3: docs = list_syncs(data) else: tmp = [header.to_lower(f) for f in sys.argv[2:]] for f in tmp: if tmp[-1] == '/': tmp = tmp[:-1] # strip off any trailing forward slash docs = grep_data(tmp,data) if flag == 'x': # delete deletelist = [s['name'] for s in docs] # names of documents to delete namelist = [s['name'] for s in data] # names of all documents idxlist = [namelist.index(a) for a in deletelist] # indexes to delete if len(idxlist) == 0: print "No locations deleted" else: header.remove_database(idxlist,syncFile) else: for s in docs: if flag == 'u': # upload