Eh, I forgot the attachment.
#!/usr/bin/env python import md5 from glob import glob from optparse import OptionParser from os import chdir, path, rename, remove from sys import argv
def check(checksums): checksums = open(checksums, 'r') chgfiles = {} msngfiles = [] for fline in checksums.readlines(): line = fline.split(' *') orig = line[0].upper() try: file_ = open(line[1].strip(),'rb') chunk = 8196 sum_ = md5.new() while(1): chunkdata = file_.read(chunk) if not chunkdata: break sum_.update(chunkdata) new = sum_.hexdigest().upper() file_.close() if new == orig: print '.', else: chgfiles[line[1]] = (orig, new) except IOError: msngfiles.append(line[1]) checksums.close() chgfileskeys = chgfiles.keys() chgfileskeys.sort() msngfiles.sort() print '\n' if len(chgfiles) != 0: print 'Files changed:' for key in chgfileskeys: print key.strip('\n'), 'changed from:\n\t', chgfiles[key][0], \ 'to\n\t', chgfiles[key][1] print '\n\t', len(chgfiles), 'file(s) changed.\n' if len(msngfiles) != 0: print 'Files not found:' for x in range(len(msngfiles)): print '\t', msngfiles[x] print '\n\t', len(msngfiles), 'file(s) not found.\n' print "\n\tChecksums Verified\n" def rename_md5(newdirname, checksums): dict_ = md5format(checksums) # David <[EMAIL PROTECTED]>: # What's going on here? Looks like you assume there are always # 4 files (001string 1, 0001string 2, etc), which you rename to # string1, string2, etc. This logic is unsafe because (1) those files # might not exist, and (2) if some files exist but not others then you # rename them from eg: 0001string 2 (if 0001string 1 does not exist) to # sting1. I don't know if this is ententional. dirlist = glob('./00[1-4]string [1-4]') dirlist.sort() for x in range(4): rename(dirlist[x], newdirname[x]) print '\t', dirlist[x], 'renamed to:', newdirname[x] chdir('./%s' % newdirname[x]) for oldfilename in glob('*.mp3'): newfilename = oldfilename[3:] rename(oldfilename, newfilename) print '\t\t', oldfilename, 'renamed to:', newfilename dict_ = md5fname_edit(dict_, dirlist[x], newdirname[x]) chdir('..') md5write(dict_, checksums) replace('Webpage.htm', dirlist, newdirname) print '\n\tDirectories and Files renamed.' def md5format(checksums): dict_ = {} checksums = open(checksums, 'r') for line in checksums.readlines(): splitline = line.split(' *') dict_[splitline[1]] = (splitline[0], splitline[1].split('\\')) checksums.close() return dict_ def md5fname_edit(dict_, olddir, newdir): for x in dict_.keys(): if dict_[x][1][0] == olddir[2:]: dict_[x] = (dict_[x][0], [newdir, dict_[x][1][1]]) if dict_[x][1][1][0] == '0': dict_[x] = (dict_[x][0], [dict_[x][1][0], dict_[x][1][1][3:]]) return dict_ def md5write(dict_, checksums): keys = dict_.keys() keys.sort() checksums = open(checksums, 'w') for x in keys: try: checksums.writelines('%s *%s/%s' % (dict_[x][0], dict_[x][1][0], dict_[x][1][1])) except IndexError: checksums.writelines('%s *%s' % (dict_[x][0], dict_[x][1][0])) def replace(file_, oldlist, newlist): new = open(file_, 'r').read() for x in range(4): new = new.replace(oldlist[x][2:], newlist[x], 1) remove(file_) file_ = open(file_, 'w', len(new)) file_.write(new) def main(): fullpath = path.abspath(path.dirname(argv[0])) # David <[EMAIL PROTECTED]>: # This is broken: what path are you trying to change to here? # - Assuming for the moment that you want to change to the same dir as # the script. # TODO: Fix this. #chdir(fullpath[:-10]) chdir(fullpath) checksums = path.join(fullpath, 'checksums.md5') newdirname = ('string1', 'string2', 'string3', 'string4') usage = "%prog [options]" parser = OptionParser(usage) parser.add_option('-c', '--check', action='store_true', dest='check', help='verify checksums') parser.add_option('-r', '--rename', action='store_true', dest='rename', help='rename files to a more usable form (write rights ' 'needed)') (options, args) = parser.parse_args() # Generate an empty checksums file if one doesn't already exist. Otherwise # the logic has problems later: if not path.isfile(checksums): f = open(checksums, 'w') f.close() did_something = False # Set to true if some logic ran if options.check: check(checksums) chdir(fullpath) did_something = True if options.rename: chdir('..') rename_md5(newdirname, checksums) did_something = True if not did_something: parser.print_help() if __name__ == '__main__': main()
-- http://mail.python.org/mailman/listinfo/python-list