#!/usr/local/bin/python ## ## Replaces buggy perl script (ewww!) with a (hopefully) less buggy ## python script. ## import os, sys, string, getopt, errno sys.path = sys.path + ['/fs/tcc/bin'] import make_reg CMDOPTS = ['-r', '-pcp', '--help'] HELPDESC = """Wholewheat (codename: Oatbran) -- Red light, green light! wholewheat [-r ()] [-pcp ()] [--help]""" def mkReg(): """Makes a registry file from RegView's diff format. Jon Hill was wrong, and SpudBoy's script is broken.""" ret = 0 dfile = 'No File Given' if len(sys.argv) > 1 and sys.argv[1] not in CMDOPTS: dfile = sys.argv[1] del sys.argv[1] if len(sys.argv) > 1 and sys.argv[1] not in CMDOPTS: rfile = sys.argv[1] del sys.argv[1] else: rfile = os.path.splitext(dfile)[0] + '.reg' try: make_reg.difftoreg( dfile, rfile ) except: ret = -1 return ret def pcDir( path, olddir ): """Directories need to be the format of ([A-Z,0-9][a-z,0-9]*), separated by single spaces.""" newdir = string.join( string.split( string.capwords(olddir) ), ' ' ) if newdir == olddir: return print 'Directory "%s" -> "%s"' % (olddir, newdir) if os.path.exists( os.path.join(path, newdir) ): print " Warning: %s can't be renamed (%s already exists)" % \ (olddir, newdir) return try: os.rename( os.path.join(path, olddir), os.path.join(path, newdir) ) except os.error, errval: print " Error #%i: %s" % errval.args return def pcFile( path, fname ): "Files need to be all lower case -- except for .lnk files (?)" newfile = string.lower( fname ) if newfile == fname or os.path.splitext(newfile)[1] == '.lnk': return print 'File "%s" -> "%s"' % (fname, newfile) if os.path.exists(newfile): print " Warning: %s can't be renamed (%s already exists)" % \ (fname, newfile) return try: os.rename( os.path.join(path, fname), os.path.join(path, newfile) ) except os.error, errval: print " Error #%i: %s" % errval.args return def doPCase( blah, path, files ): print 'Entering %s' % path for each in files: if os.path.islink(each): print 'Warning: %s is a link. Skipping' % each files.remove( each ) elif os.path.isdir( os.path.join(path, each) ): pcDir( path, each ) elif os.path.isfile( os.path.join(path, each) ): pcFile( path, each ) else: print "Warning: Don't know what %s is! Skipping" % each files.remove( each ) return def mkPretty(): """Walks through a directory, pretty casing all the way.""" startdir = '.' if len(sys.argv) > 1 and sys.argv[1] not in CMDOPTS: if os.path.isdir(sys.argv[1]): startdir = sys.argv[1] del sys.argv[1] else: print 'Not a directory: %s' % sys.argv[1] return -1 os.path.walk( startdir, doPCase, 0 ) return 0 def cmdLine(): "Partially parses the command line." invalid_arguments = 1 if '--help' not in sys.argv: while len(sys.argv) > 1: if sys.argv[1] == '-r': del sys.argv[1] invalid_arguments = mkReg() if invalid_arguments: break elif sys.argv[1] == '-pcp': del sys.argv[1] invalid_arguments = mkPretty() if invalid_arguments: break else: invalid_arguments = 1 print "Unknown argument: %s" % sys.argv[1] break if invalid_arguments: print HELPDESC return cmdLine()