예제 #1
0
def Reprefix(patt, repl, files, switch=cmdline.FlagClass()):
    crcf = crcfile.crc_file('.', transparent=True)
    files = GlobList(files)
    if patt == '.':
	patt = ''
    if repl == '.':
	repl = ''
    repl = repl.replace('~', patt)
    if switch.get('r'):
	patt_re = re.compile('^' + patt)

    for fn in files:
	dir, ofn = os.path.split(fn)
	if switch.get('r'):
	    if patt_re.search(fn):
		newname = patt_re.sub(ofn, repl, 1)
	elif ofn[:len(patt)] == patt:
	    newname = repl + ofn[len(patt):]
	else:
	    continue
	if dir:
	    newname = os.path.join(dir, newname)
	if switch.get('C'):
	    SafeCopyFile(fn, newname, switch.get('c'), switch.get('f'))
	    crcf.Copy(fn, newname)
	else:
	    SafeRename(fn, newname, switch.get('c'), switch.get('f'))
	    crcf.RenameFile(fn, newname)
예제 #2
0
def Resuffix(patt, repl, files, switch=cmdline.FlagClass()):
    crcf = crcfile.crc_file('.', transparent=True)
    files = GlobList(files)
    if patt == '.':
	patt = ''
    if repl == '.':
	repl = ''
    if switch.get('r'):
	patt_re = re.compile(patt + '$')

    for fn in files:
	if switch.get('r'):
	    if patt_re.search(fn):
		newname = patt_re.sub(fn, repl, 1)
	elif patt == '':
	    newname = fn + repl
	elif fn[-len(patt):] == patt:
	    newname = fn[:-len(patt)] + repl
	else:
	    continue
	if switch.get('C'):
	    SafeCopy(fn, newname, switch.get('c'), switch.get('f'))
	    crcf.Copy(fn, newname)
	else:
	    SafeRename(fn, newname, switch.get('c'), switch.get('f'))
	    crcf.RenameFile(fn, newname)
예제 #3
0
def DoDir(d, sizedict, flags, ignorelist):
    #print "  ",d
    if os.path.exists(d):
	collision = False
	lowercase_list = []
#	if not flags.quiet:
#	    print "+",d
#	saved = crcfile.ReadCRCFile(d)
#	if flags.force or not saved:
	#saved = crcfile.CRCDir(d, flags)
	crcf = crcfile.crc_file(d, flags=flags)
	saved = crcf.Create(force=flags.get('force'))
	if flags.recurse:
	    for f in deanlib.SubDirs(d):
		if not f in ignorelist:
		    DoDir(os.path.join(d, f), sizedict, flags, ignorelist)

	#print "saved", saved
	for f in saved.keys():
	    if f.lower() in lowercase_list:
		collision = True
	    lowercase_list.append(f.lower())
	    if not f in ignorelist:
		full = os.path.join(d, f)
		sz = saved[f][1:]
		sizedict[sz] = sizedict.get(sz, []) + [full]
	#print "sizedict", sizedict

	if flags.prune or flags.empty:
	    if not os.listdir(d):
		if flags.prune:
		    os.rmdir(d)
		else:
		    sizedict['empties'].append(d)
	if collision and not flags.quiet:
	    print "! case collision detected!"
예제 #4
0
def DispatchDir(srcdir, dstdir, dstCRC, filespec, switch, sz, sort_by, reverse, limit):
    # switch a c d e f g k l n p q r v x
    if not os.path.isdir(srcdir):
	return 0
    if switch.get('v'):
	print "DispatchDir", srcdir, dstdir, filespec, switch, sz
    if not os.path.exists(srcdir):
	if not switch.get('q'):
	    print "Source directory doesn't seem to exist:", srcdir
	sys.exit(1)
    try:
	if not dstdir.startswith(':') and os.path.samefile(srcdir, dstdir):
	    if not switch.get('q'):
		print "Directories appear to be the same:", srcdir, dstdir
	    return 0
    except:
	pass

    files = GetFiles(srcdir, filespec, switch.get('g'))
    files.sort(key=sort_by)
    if reverse:
	files.reverse()

    if switch.get('v'):
	print files
    srcCRC = crcfile.crc_file(srcdir)
    count = 0
    thisdstdir = dstdir
    for f in files:
	fsz = cmdline.FlagClass(sz)
	if os.path.isdir(os.path.join(srcdir, f)):
	    continue
	if switch.get('v'):
	    print f
	d = f
	if switch.get('r'):
	    d = deanlib.CleanName(d, rename=rename_types.get(switch['r'][0].lower()))
	if fsz:
	    if switch.get('v'):
		print 'fsz =', fsz
	    import imaglib
	    if not imaglib.ImgSizeCheck(os.path.join(srcdir, f), fsz):
		if switch.get('v'):
		    print "! failed filter"
		continue
	found = False
	root, ext = os.path.splitext(d)
	if switch.get('x') and ext and ext[1:].isdigit():
	    d = root
	    root, ext = os.path.splitext(d)
	if dstdir == '/dev/null':
	    if not filelib.SafeRemove(os.path.join(srcdir, f), sw=switch):
		srcCRC.RemoveFile(f)
		continue
	elif dstdir.startswith(':'):
	    if not dstdir[-1] in f:
		if switch.get('v'):
		    print "! no prefix directory found"
		continue
	    thisdstdir = f[:f.find(dstdir[-1])].lower()
	    if os.path.exists(thisdstdir):
	        pass
	    elif switch.get('m'):
		os.mkdir(thisdstdir)
	    else:
		if not switch.get('q'):
		    print "Destination directory doesn't seem to exist:", thisdstdir
		continue

	if switch.get('a'):  # Go through append-style rename process
	    if not RenameAppend(f, d, srcdir, thisdstdir, switch):
		if switch.get('v'):
		    print "! failed rename"
		continue
	elif not RenameOrCopy(os.path.join(srcdir, f), os.path.join(thisdstdir, d), switch):
	    if switch.get('v'):
		print "! failed destination"
	    continue
	count += 1

	if dstdir.startswith(':'):
	    pass
	else:
	    crc = srcCRC.GetFile(f)
	    if crc and dstCRC:
		dstCRC.AddFile(d, crc)
	    if not switch.get('k'):
		srcCRC.RemoveFile(f)

	if limit and count >= limit:
	    break

    srcCRC.CleanFileList()
    del srcCRC
    if switch.get('d'):
	try:
	    os.rmdir(srcdir)
	except:
	    pass
    return count
예제 #5
0
def Dispatch(srcdirspec, dstdir, filespec, switch=None, sz=None):
    if srcdirspec is None or dstdir is None:
	if not switch.get('q'):
	    print "Source or destination wasn't specified."
	sys.exit(1)
    if not switch:
	switch = cmdline.FlagClass()
    if not sz:
	sz = cmdline.FlagClass()
    if switch['v']:
	sz['v'] = switch['v']
    dstCRC = None
    if dstdir.startswith(':'):
	pass
    elif not os.path.exists(dstdir):
	if switch.get('m'):
	    os.mkdir(dstdir)
	else:
	    if not switch.get('q'):
		print "Destination directory doesn't seem to exist:", dstdir
	    sys.exit(1)
    elif dstdir != '/dev/null':
	dstCRC = crcfile.crc_file(dstdir)
    count = 0
    sort_by = sort_bys['n']
    reverse = False
    if switch.get('s'):
	sw_s = switch['s'][-1]
	if sw_s.startswith('-'):
	    reverse = not reverse
	    sw_s = sw_s[1:]
	sort_by = sort_bys.get(sw_s, sort_by)
    limit = sz.getnum('ct', 0)
    if limit < 0:
	limit = -limit
	reverse = not reverse
    if switch.get('w'):  # wecursion (experimental)
	# cannot glob
	global dirlist
	dirlist = []
	deanlib.WalkDirs(srcdirspec, DirAccum, topdown=True, quiet=switch.get('q'))
	for dirspec in dirlist:
	    dirspec = dirspec[len(srcdirspec) + 1:]
	    rdstdir = os.path.join(dstdir, dirspec)
	    if not os.path.exists(rdstdir):
		os.mkdir(rdstdir)
	    dstCRC = crcfile.crc_file(rdstdir)
	    count += DispatchDir(os.path.join(srcdirspec, dirspec), rdstdir, dstCRC, filespec, switch, sz, sort_by, reverse, limit)
#	    crcfile.WriteCRCFile(rdstdir, dstCRC)
    else:
	for srcdir in glob.glob(srcdirspec):
	    count += DispatchDir(srcdir, dstdir, dstCRC, filespec, switch, sz, sort_by, reverse, limit)
    del dstCRC
#    if dstdir != '/dev/null':
#	crcfile.WriteCRCFile(dstdir, dstCRC)
    if switch.get('k'):
	print count, "files copied"
    else:
	print count, "files moved"

    return count