Created
October 2, 2012 09:00
-
-
Save maduck/3817562 to your computer and use it in GitHub Desktop.
Directory Listing with permission bits
This file contains 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/python | |
# -*- coding: utf-8 -*- | |
import sys | |
import os | |
import stat | |
import argparse | |
import hashlib | |
parser = argparse.ArgumentParser(description='list files with file bits') | |
parser.add_argument('--format', '-f', default='%(bitmask)o %(filename)s', help='Output format in sprintf syntax, default: "%(default)s", usable: bitmask, filename, md5sum, mode') | |
parser.add_argument('--recursive', '-r', default=False, action="store_true", help='List sub-contents of given directories') | |
parser.add_argument('--exclude', '-e', default=[], action='append', help='exclude every file that contains the given word. can be given multiple times') | |
parser.add_argument('filename', nargs='+', help='List of files to display') | |
args = parser.parse_args() | |
def get_md5_hash(filename): | |
md5 = hashlib.md5() | |
f = open(filename, 'r') | |
with open(filename, 'rb') as f: | |
for chunk in iter(lambda: f.read(8192), b''): | |
md5.update(chunk) | |
return md5.hexdigest() | |
def render_mode(st_mode): | |
return ''.join(st_mode & 0400 >> i and state or '-' for i, state in enumerate('rwxrwxrwx')) | |
def print_rights(filename): | |
exclude = False | |
for pattern in args.exclude: | |
if pattern in filename: | |
exclude = True | |
if not exclude: | |
try: | |
md5sum = '' | |
mode = os.stat(filename).st_mode | |
if stat.S_ISDIR(mode): | |
if filename[-1] != '/': | |
filename = filename + '/' | |
elif 'md5sum' in args.format: | |
md5sum = get_md5_hash(filename) | |
print args.format % { | |
'bitmask': mode & 0777, | |
'filename': filename, | |
'md5sum': md5sum, | |
'mode': render_mode(mode), | |
} | |
except Exception, e: | |
sys.stderr.write(str(e) + '\n') | |
for filename in args.filename: | |
if args.recursive and os.path.isdir(filename): | |
for root, dirs, files in os.walk(filename): | |
print_rights(root) | |
for name in files: | |
full_filename = os.path.join(root, name) | |
print_rights(full_filename) | |
else: | |
print_rights(filename) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment