def compsum(fname, hash, mode): newhash = getsum(fname,mode) if hash != newhash: LogUtil.printlog("Hash mismatch: Expecting: %s got %s for %s" % (hash, newhash, fname)) Globals.failed += 1 else: if Globals.verbose: LogUtil.printlog("Hash match: Expecting: %s got %s for %s" % (hash, newhash, fname)) Globals.passed += 1
def checkfile(path, method): checksum = getsum(path, method) if checksum in basename(path) or checksum in basename(path).upper(): Globals.passed += 1 if Globals.verbose: LogUtil.printlog("Found: %s in: %s" % (checksum, path)) else: Globals.failed += 1 LogUtil.printlog("Not found: %s in: %s" % (checksum,path))
def printsum(path,method,cont=False): fname = None if cont: fname = _getnextfname(method) if isdir(path): #walk here for root, dirs, files in walk(path): for file in files: if cont: if fname == relpath(join(root,file)): log.debug("FOUND: %s" % fname) fname = _getnextfname(method) continue else: log.warn("NOT FOUND: %s is not %s" % (fname,relpath(join(root,file)))) log.info("Continue from this point? y/(n):") yesno = raw_input() if yesno != "y": "Bailing..." exit() cont=False if method == "crc": LogUtil.printlog("%s %s" % (relpath(join(root,file)), getsum(join(root,file), method))) else: LogUtil.printlog("%s %s" % (getsum(join(root,file), method), relpath(join(root,file)))) else: if method == "crc": LogUtil.printlog("%s %s" % (relpath(path), getsum(path,method))) else: LogUtil.printlog("%s %s" % (getsum(path,method), relpath(path)))
def checkpath(path, method): if not exists(path): log.warn("Not found: %s" % path) return if isdir(path): for fname in listdir(path): if isdir(join(path,fname)): LogUtil.printlog("--- DIR: %s ---" % fname) checkpath(join(path,fname), method) LogUtil.printlog("--- END ---") else: checkfile(join(path,fname), method) else: checkfile(path, method)
def getsum(path, method): f = open(path,"rb") progress = None size = stat(path).st_size if Globals.progress and size != 0: widgets = ["%s" % Console.trimstring(basename(path).encode("utf-8")), Percentage(), ' ', Bar(left="[", right="]"), ' ', ETA()] progress = ProgressBar(term_width=Globals.consolesize, widgets=widgets, maxval=size).start() #lol arbitrary 2MB try: h = Hash(f.read(2097152), method) except Exception as e: LogUtil.printlog("Error: %s in %s" % (e, path)) return if Globals.progress and progress: progress.update(f.tell()) try: chunk = f.read(2097152) except Exception as e: LogUtil.printlog("Error: %s in %s" % (e, path)) return if Globals.progress and progress: progress.update(f.tell()) while chunk: h.update(chunk) try: chunk = f.read(2097152) except Exception as e: LogUtil.printlog("Error: %s in %s" % (e, path)) return if Globals.progress and progress: progress.update(f.tell()) f.close() #if Globals.progress: progress.finish() if Globals.progress: Console.clearline() return h.gibehex()
def checksums(fname, method): f = codopen(fname,"rb","utf-8") mode = None if splitext(path)[1] == ".crc" or splitext(path)[1] == ".sfv": mode = "crc" else: mode = "md5" lineno = 0 for line in f: if line.startswith("#") or line.startswith(";"): lineno += 1 continue line = line.strip() hash = None if mode == "crc": try: fname, hash = line.rsplit(None, 1) except: log.warn("Invalid line (%s): %s" % (lineno, line)) Globals.invalid += 1 else: try: hash, fname = line.split(None, 1) except: log.warn("Invalid line (%s): %s" % (lineno, line)) Globals.invalid += 1 lineno += 1 if isabs(fname): if exists(fname): compsum(fname,hash,mode) else: LogUtil.printlog("File not found: %s" % fname) Globals.notfound += 1 else: if Globals.options.aster: if fname[0] == "*": fname = fname[1:] if exists(join(Globals.cwd, fname)): compsum(join(Globals.cwd, fname),hash,mode) else: LogUtil.printlog("File not found: %s" % fname)
def printtree(node, options, level): #if not dirfirst, combine lists #sort lists if options.depth >= 0 and level > options.depth: return combined = None if options.sortfirst: node.dirs.sort() node.files.sort() if options.dirfirst: combined = node.dirs+node.files else: combined = node.files+node.dirs else: if options.dirfirst: combined = node.dirs+node.files else: combined = node.files+node.dirs combined.sort() prefix = options.indent*level #{name} #{size} #{modtime} #{createtime} for item in combined: if options.minsize and (item.size < options.minsize): continue if options.maxsize and (item.size > options.maxsize): continue size = item.size if not options.human else size_to_human(item.size, round=options.rounding) if isinstance(item, Dir): #recurse if not options.nodirs: if options.dirnamebefore: LogUtil.printlog(options.dir.format(name=item.name, path=item.path, size=size, modtime=item.modtime, createtime=item.createtime, prefix=prefix)) printtree(item, options, level+1) if not options.nodirs: if not options.dirnamebefore: LogUtil.printlog(options.dir.format(name=item.name, path=item.path, size=size, modtime=item.modtime, createtime=item.createtime, prefix=prefix)) else: if not options.nofiles: #print stats LogUtil.printlog(options.file.format(name=item.name, path=item.parent.path, size=size, modtime=item.modtime, createtime=item.createtime, prefix=prefix))
for path in paths: drive, rest = splitdrive(path) drive = drives[drive.upper()] drive.stats = True progress = None if options.progress: #allow progress if not rest.lstrip("/\\"): widgets = ["Reading %s" % drive.letter, Percentage(), ' ', Bar(left="[", right="]"), ' ', ETA()] progress = ProgressWrapper(ProgressBar(term_width=consolesize, widgets=widgets, maxval=drive.totalsize-drive.free).start()) else: widgets = ["Reading %s " % drive.letter, AnimatedMarker(), ' ', Timer()] progress = ProgressWrapper(ProgressBar(term_width=consolesize, widgets=widgets, maxval=drive.totalsize-drive.free).start()) start(path, drive, progress) if progress: progress.finish() #do final printing stuff here I guess for drive in sorted(drives.keys()): if drives[drive].stats: d = drives[drive] #{name}{letter}{fs}{free}{totalsize}{serial}{type}{stats}{size} size = d.size if not options.human else size_to_human(d.size) if options.drivehead: LogUtil.printlog(options.drivehead.format(name=d.name, letter=d.letter, fs=d.fs, free=d.free, totalsize=d.totalsize, serial=d.serial, type=d.type, stats=d.stats, size=size, totaldirs=d.totaldirs, totalfiles=d.totalfiles)) printtree(d, options, 0) if options.drivefoot: LogUtil.printlog(options.drivefoot.format(name=d.name, letter=d.letter, fs=d.fs, free=d.free, totalsize=d.totalsize, serial=d.serial, type=d.type, stats=d.stats, size=size, totaldirs=d.totaldirs, totalfiles=d.totalfiles))