Skip to content

Instantly share code, notes, and snippets.

@informationsea
Created October 27, 2012 17:35
Show Gist options
  • Save informationsea/3965455 to your computer and use it in GitHub Desktop.
Save informationsea/3965455 to your computer and use it in GitHub Desktop.
Fix permission by suffix of filename and prefix of file
#!/usr/bin/env python
import argparse
import os
import os.path
import stat
def _main():
parser = argparse.ArgumentParser(description='fix permission by suffix of filename and prefix of file')
parser.add_argument('dir', default='.', nargs='?', help='fix directory (default:%(default)s)')
parser.add_argument('--executable-suffix', default=['.sh'], nargs='+')
parser.add_argument('-o','--permit-access-to-other', action='store_true')
parser.add_argument('-g','--permit-access-to-group', action='store_true')
options = parser.parse_args()
executable = stat.S_IXUSR | stat.S_IWUSR | stat.S_IRUSR
normal = stat.S_IWUSR | stat.S_IRUSR
if options.permit_access_to_group:
executable |= stat.S_IXGRP | stat.S_IRGRP
normal |= stat.S_IRGRP
if options.permit_access_to_other:
executable |= stat.S_IXOTH | stat.S_IROTH
normal |= stat.S_IROTH
for root, dirs, files in os.walk(options.dir):
for onedir in dirs:
os.chmod(os.path.join(root, onedir), executable)
for onefile in files:
os.chmod(os.path.join(root, onefile), normal)
with file(os.path.join(root, onefile)) as f:
if f.read(2) == '#!':
os.chmod(os.path.join(root, onefile), executable)
if __name__ == '__main__':
_main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment