Utilities - chmodr.py

From PeformIQ Upgrade
Jump to navigation Jump to search

Change the mode of files and directories recursively from here to a useful value. Used to fix my cygwin file permissions which keep getting reset to 0000


Script

$ cat ~/bin/chmodr.py
#!/usr/bin/env python

import os
import glob

#--------------------------------------------------------------------------

executables = ('exe', 'py', 'rb', 'sh')

#--------------------------------------------------------------------------

def chmod(dir):

    os.chdir(dir)

    print os.getcwd()

    files = glob.glob('*')

    ordinary    = []
    directories = []

    for file in files:

        if os.path.isdir(file):
           directories.append(file)
        else:
           ordinary.append(file)

    for file in ordinary:
        extension = os.path.splitext(file)[1][1:].lower()
        if (extension in executables):
            print "  Chmod executable file 0775 %s [%s]" % (file, extension)
            os.chmod(file, 0775)
        else:
            print "  Chmod file 0664 %s [%s]" % (file, extension)
            os.chmod(file, 0664)



    for directory in directories:
        os.chmod(directory, 0775)
        print "  Chmod dir 0775 %s" % directory


    for directory in directories:
        chmod(directory)

    os.chdir('..')

#--------------------------------------------------------------------------

chmod('.')

#--------------------------------------------------------------------------