mirror of
				https://github.com/zulip/zulip.git
				synced 2025-11-04 05:53:43 +00:00 
			
		
		
		
	Add option to determine file type in lister.py using shebang.
This commit is contained in:
		
				
					committed by
					
						
						Tim Abbott
					
				
			
			
				
	
			
			
			
						parent
						
							5063f16f82
						
					
				
				
					commit
					ec8ae1f4c5
				
			@@ -2,17 +2,35 @@
 | 
				
			|||||||
from __future__ import print_function
 | 
					from __future__ import print_function
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import os
 | 
					import os
 | 
				
			||||||
 | 
					import sys
 | 
				
			||||||
import subprocess
 | 
					import subprocess
 | 
				
			||||||
 | 
					import re
 | 
				
			||||||
from collections import defaultdict
 | 
					from collections import defaultdict
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def get_ftype(fpath):
 | 
					def get_ftype(fpath, use_shebang):
 | 
				
			||||||
    ext = os.path.splitext(fpath)[1]
 | 
					    ext = os.path.splitext(fpath)[1]
 | 
				
			||||||
    if ext:
 | 
					    if ext:
 | 
				
			||||||
        return ext[1:]
 | 
					        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:
 | 
					    else:
 | 
				
			||||||
        return ''
 | 
					        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.
 | 
					    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.
 | 
				
			||||||
@@ -21,6 +39,7 @@ def list_files(targets=[], ftypes=[], modified_only=False, exclude=[], group_by_
 | 
				
			|||||||
    Other arguments:
 | 
					    Other arguments:
 | 
				
			||||||
    ftypes - List of file types on which to filter the search.
 | 
					    ftypes - List of file types on which to filter the search.
 | 
				
			||||||
        If ftypes is [], all files are included.
 | 
					        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.
 | 
					    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.
 | 
					    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
 | 
					            continue
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if ftypes or group_by_ftype:
 | 
					        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:
 | 
					            if ftypes and filetype not in ftypes_set:
 | 
				
			||||||
                continue
 | 
					                continue
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user