Created
April 18, 2014 04:57
-
-
Save introom/11025461 to your computer and use it in GitHub Desktop.
Python change all files mode to read only. (excluding hidden files)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
""" | |
This script sets all files (excluding hidden ones) | |
to read-only mode. | |
""" | |
from __future__ import print_function | |
import sys | |
import os | |
import glob | |
from subprocess import Popen | |
__author__ = "introom" | |
__email__ = "[email protected]" | |
def iter_files(path): | |
""" | |
by default, this emits all files under `path` that are not hidden. | |
aka., not starting with a . | |
""" | |
# assert os.path.isdir(path) | |
for fn in glob.glob(os.path.join(path, '*')): | |
if os.path.isdir(fn): | |
# oops, we are in py2 realm, no yield from (>= py3.3) | |
for fn_ in iter_files(fn): | |
yield fn_ | |
else: | |
yield fn | |
def chmod(files_): | |
for fn in files_: | |
# XXX | |
# It's bad, too much overheads for creating/destructing proc. | |
# But who the heck cares? | |
cmd = 'chmod -w {}'.format(fn) | |
Popen(cmd, shell=True) | |
def main(): | |
argv = sys.argv | |
if len(argv) != 2 or not os.path.isdir(argv[1]): | |
print("please specify a valid directory. e.g., python chmod.py $MYDIR", file=sys.stderr) | |
sys.exit(1) | |
chmod(iter_files(argv[1])) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment