If you make a mess (like I did) and you removed all the executable permissions of a directory (or you set executable permissions to everything) this can help.
It will walk through a tree of files setting or unsetting the executable mode of files or directories.
NOTE: Will autodetect executables if they contain the word ELF at the beggining, so it won't work in platforms that does not use ELF (windows, AIX, etc).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | #!/bin/env python
import os
import sys
import stat
def set_executable(top):
error_count=0
def set_exec(name):
mode = os.stat(name).st_mode
new_mode = mode
if new_mode & stat.S_IRUSR:
new_mode = new_mode | stat.S_IXUSR
if new_mode & stat.S_IRGRP:
new_mode = new_mode | stat.S_IXGRP
if new_mode & stat.S_IROTH:
new_mode = new_mode | stat.S_IXOTH
if (mode != new_mode):
print "Setting exec for '%s' (mode %o => %o)" % (name, mode, new_mode)
os.chmod(name, new_mode)
def unset_exec(name):
mode = os.stat(name).st_mode
new_mode = mode & ~(stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH)
if (mode != new_mode):
print "Unsetting exec for '%s' (mode %o => %o)" % (name, mode, new_mode)
os.chmod(name, new_mode)
for root, dirs, files in os.walk(top):
for name in files:
complete_name = os.path.join(root, name)
if os.path.islink(complete_name): continue
try:
f = open(complete_name, 'r')
header = f.read(4)
f.close()
if header[0:2] == '#!' or header[1:4] == 'ELF':
set_exec(complete_name)
else:
unset_exec(complete_name)
except Exception as e:
print "%s: %s" % (complete_name, e.__str__())
error_count += 1
for name in dirs:
complete_name = os.path.join(root, name)
set_exec(complete_name)
return error_count
if len(sys.argv) >= 2:
error_count = 0
for dir in sys.argv[1:]:
error_count += set_executable(sys.argv[1])
if error_count > 0:
print "There were errors"
sys.exit(1)
else:
print "Usage\n\t%s <dir1> [dir2|...]\n" % sys.argv[0]
|