Add option to determine file type in lister.py using shebang.

This commit is contained in:
Eklavya Sharma
2016-03-23 01:43:21 +05:30
committed by Tim Abbott
parent 5063f16f82
commit ec8ae1f4c5

View File

@@ -2,17 +2,35 @@
from __future__ import print_function
import os
import sys
import subprocess
import re
from collections import defaultdict
def get_ftype(fpath):
def get_ftype(fpath, use_shebang):
ext = os.path.splitext(fpath)[1]
if ext:
return ext[1:]
elif use_shebang:
# opening a file may throw an OSError
with open(fpath) as f:
first_line = f.readline()
if re.search(r'^#!.*\bpython', first_line):
return 'py'
elif re.search(r'^#!.*sh', first_line):
return 'sh'
elif re.search(r'^#!.*\bperl', first_line):
return 'pl'
elif re.search(r'^#!', first_line):
print('Error: Unknown shebang in file "%s":\n%s' % (fpath, first_line), file=sys.stderr)
return ''
else:
return ''
else:
return ''
def list_files(targets=[], ftypes=[], modified_only=False, exclude=[], group_by_ftype=False):
def list_files(targets=[], ftypes=[], use_shebang=True, modified_only=False,
exclude=[], group_by_ftype=False):
"""
List files tracked by git.
Returns a list of files which are either in targets or in directories in targets.
@@ -21,6 +39,7 @@ def list_files(targets=[], ftypes=[], modified_only=False, exclude=[], group_by_
Other arguments:
ftypes - List of file types on which to filter the search.
If ftypes is [], all files are included.
use_shebang - Determine file type of extensionless files from their shebang.
modified_only - Only include files which have been modified.
exclude - List of paths to be excluded.
group_by_ftype - If True, returns a dict of lists keyed by file type.
@@ -50,7 +69,13 @@ def list_files(targets=[], ftypes=[], modified_only=False, exclude=[], group_by_
continue
if ftypes or group_by_ftype:
filetype = get_ftype(fpath)
try:
filetype = get_ftype(fpath, use_shebang)
except (OSError, UnicodeDecodeError) as e:
etype = e.__class__.__name__
print('Error: %s while determining type of file "%s":' % (etype, fpath), file=sys.stderr)
print(e, file=sys.stderr)
filetype = ''
if ftypes and filetype not in ftypes_set:
continue