Fw: Script Optimization

David wizzardx at gmail.com
Sun May 4 05:45:09 EDT 2008


Forwarding back to the list.

---------- Forwarded message ----------
From: lev <levlozhkin at gmail.com>
Date: Sun, May 4, 2008 at 10:21 AM
Subject: Re: Script Optimization
To: David <wizzardx at gmail.com>


On May 4, 12:32 am, David <wizza... at gmail.com> wrote:
 > >  It's too long to post here (160 lines) so here's the link:
 > >  http://uppit.com/d2/CKOYHE/af78a6beeeed3e21a19d5871abb9b879/utils.py
 > >  (if that doesn't work:http://uppit.com/CKOYHE)
 >
 > >  Thanks in advance,
 > >  lev
 >

> Neither link works for me. I get an error page "Error: invalid
 > download linnk". How about you send it to the list as an attachment?
 >
 > David.

 Sorry.. but I don't know how to send an attachment here... I am not
 using a dedicated email client, just google groups, and the option
 seems to elude me.
 I forgot to comment it when I was writing it.... I am working on
 commenting it and making it more readable, and I'll post that tomorrow
 (I'm going to sleep... it's 2 in the morning)
 This is a bit long, sorry:

 #!/usr/bin/env python

 def main():
        from sys import argv
        from optparse import OptionParser
        from os import chdir,path
        fullpath = path.abspath(path.dirname(argv[0]))
        chdir(fullpath[:-10])
        checksums = '%s//checksums.md5' % fullpath
        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()
        if options.check:
                check(checksums)
                chdir(fullpath)
                pass
        if options.rename:
                chdir('..')
                rename(newdirname, checksums)
                pass
        if not options.check and not options.rename:
                parser.print_help()
                pass
        return

 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')
                        import md5
                        chunk = 8196
                        sum = md5.new()
                        while(1):
                                chunkdata = file.read(chunk)
                                if not chunkdata:
                                        break
                                sum.update(chunkdata)
                                pass
                        new = sum.hexdigest().upper()
                        file.close()
                        if  new == orig:
                                print '.',
                                pass
                        else:
                                chgfiles[line[1]] = (orig,new)
                                pass
                except IOError:
                        msngfiles.append(line[1])
                        pass
                pass
        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]
                        pass
                print '\n\t',len(chgfiles),'file(s) changed.\n'
                pass
        if len(msngfiles) != 0:
                print 'Files not found:'
                for x in range(len(msngfiles)):
                        print '\t',msngfiles[x]
                        pass
                print '\n\t', len(msngfiles),'file(s) not found.\n'
                pass
        print "\n\tChecksums Verified\n"
        return

 def rename(newdirname, checksums):
        from os import chdir, rename
        from glob import glob
        dict = md5format(checksums)
        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
                        pass
                dict = md5fname_edit(dict,dirlist[x],newdirname[x])
                pass
                chdir('..')
        md5write(dict, checksums)
        replace('Webpage.htm', dirlist, newdirname)
        print '\n\tDirectories and Files renamed.'
        return

 def md5format(checksums):
        dict = {}
        checksums = open(checksums, 'r')
        for line in checksums.readlines():
                splitline = line.split(' *')
                dict[splitline[1]] = (splitline[0],splitline[1].split('\\'))
                pass
        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:]])
                                pass
                        pass
                pass
        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]))
                        pass
                except IndexError:
                        checksums.writelines('%s *%s' %
(dict[x][0],dict[x][1][0]))
                        pass
                pass
        return

 def replace(file, oldlist, newlist):
        from os import remove
        new = open(file,'r').read()
        for x in range(4):
                new = new.replace(oldlist[x][2:],newlist[x],1)
                pass
        remove(file)
        file = open(file,'w', len(new))
        file.write(new)
        return

 if __name__ == '__main__':
        main()

 Thanks in advance,
 lev



More information about the Python-list mailing list