mirror of
https://github.com/zulip/zulip.git
synced 2025-11-13 02:17:19 +00:00
Add options for file type in lister.py
Add option to filter files by their extension. Add option to return a dict of list of files keyed by their file type.
This commit is contained in:
committed by
Tim Abbott
parent
81aabb5831
commit
5063f16f82
@@ -3,17 +3,32 @@ from __future__ import print_function
|
|||||||
|
|
||||||
import os
|
import os
|
||||||
import subprocess
|
import subprocess
|
||||||
|
from collections import defaultdict
|
||||||
|
|
||||||
def list_files(targets=[], modified_only=False, exclude=[]):
|
def get_ftype(fpath):
|
||||||
|
ext = os.path.splitext(fpath)[1]
|
||||||
|
if ext:
|
||||||
|
return ext[1:]
|
||||||
|
else:
|
||||||
|
return ''
|
||||||
|
|
||||||
|
def list_files(targets=[], ftypes=[], modified_only=False, exclude=[], group_by_ftype=False):
|
||||||
"""
|
"""
|
||||||
List files tracked by git.
|
List files tracked by git.
|
||||||
Returns a list of files which are either in targets or in directories in targets.
|
Returns a list of files which are either in targets or in directories in targets.
|
||||||
If targets is [], list of all tracked files in current directory is returned.
|
If targets is [], list of all tracked files in current directory is returned.
|
||||||
|
|
||||||
Other arguments:
|
Other arguments:
|
||||||
|
ftypes - List of file types on which to filter the search.
|
||||||
|
If ftypes is [], all files are included.
|
||||||
modified_only - Only include files which have been modified.
|
modified_only - Only include files which have been modified.
|
||||||
exclude - List of paths to be excluded.
|
exclude - List of paths to be excluded.
|
||||||
|
group_by_ftype - If True, returns a dict of lists keyed by file type.
|
||||||
|
If False, returns a flat list of files.
|
||||||
"""
|
"""
|
||||||
|
ftypes = [x.strip('.') for x in ftypes]
|
||||||
|
ftypes_set = set(ftypes)
|
||||||
|
|
||||||
cmdline = ['git', 'ls-files'] + targets
|
cmdline = ['git', 'ls-files'] + targets
|
||||||
if modified_only:
|
if modified_only:
|
||||||
cmdline.append('-m')
|
cmdline.append('-m')
|
||||||
@@ -22,7 +37,7 @@ def list_files(targets=[], modified_only=False, exclude=[]):
|
|||||||
# throw away empty lines and non-files (like symlinks)
|
# throw away empty lines and non-files (like symlinks)
|
||||||
files = list(filter(os.path.isfile, files_gen))
|
files = list(filter(os.path.isfile, files_gen))
|
||||||
|
|
||||||
result = []
|
result = defaultdict(list) if group_by_ftype else []
|
||||||
|
|
||||||
for fpath in files:
|
for fpath in files:
|
||||||
# this will take a long time if exclude is very large
|
# this will take a long time if exclude is very large
|
||||||
@@ -34,6 +49,14 @@ def list_files(targets=[], modified_only=False, exclude=[]):
|
|||||||
if in_exclude:
|
if in_exclude:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
if ftypes or group_by_ftype:
|
||||||
|
filetype = get_ftype(fpath)
|
||||||
|
if ftypes and filetype not in ftypes_set:
|
||||||
|
continue
|
||||||
|
|
||||||
|
if group_by_ftype:
|
||||||
|
result['.' + filetype].append(fpath)
|
||||||
|
else:
|
||||||
result.append(fpath)
|
result.append(fpath)
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|||||||
Reference in New Issue
Block a user