lint: Use zulint's methods for lister and printing errors.

Remove lister, printer and README files from server_lib.
This commit is contained in:
Aman 2019-08-02 19:14:11 +05:30 committed by Tim Abbott
parent 101049de27
commit 7588333079
6 changed files with 2 additions and 195 deletions

View file

@ -137,7 +137,6 @@ prose_style_rules = [
markdown_docs_length_exclude = { markdown_docs_length_exclude = {
"zulip_bots/zulip_bots/bots/converter/doc.md", "zulip_bots/zulip_bots/bots/converter/doc.md",
"tools/server_lib/README.md",
} }
markdown_rules = RuleList( markdown_rules = RuleList(

View file

@ -7,7 +7,7 @@ from __future__ import absolute_import
import subprocess import subprocess
import sys import sys
from server_lib.printer import print_err, colors from zulint.printer import print_err, colors
from typing import List from typing import List

View file

@ -10,7 +10,7 @@ import subprocess
from collections import OrderedDict from collections import OrderedDict
from pathlib import PurePath from pathlib import PurePath
from server_lib import lister from zulint import lister
from typing import cast, Dict, List from typing import cast, Dict, List
TOOLS_DIR = os.path.dirname(os.path.abspath(__file__)) TOOLS_DIR = os.path.dirname(os.path.abspath(__file__))

View file

@ -1,11 +0,0 @@
# Server Lib
This directory contains file copied as is from the [zulip server repository](https://github.com/zulip/zulip)
as they were on the commit `343cb20d570a8f5c1f5b6e6842058b294a376593`.
## List of copied files
Any changes made to these files have been marked using git's conflict markers in the files.
1. `lister.py` <- `zulip/tools/lister.py` - No changes
2. `printer.py` <- `zulip/tools/linter_lib/printer.py` - Minor changes

View file

@ -1,130 +0,0 @@
#!/usr/bin/env python
from __future__ import print_function
from __future__ import absolute_import
import os
import sys
import subprocess
import re
from collections import defaultdict
import argparse
from six.moves import filter
from typing import Union, List, Dict
def get_ftype(fpath, use_shebang):
# type: (str, bool) -> str
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'^#!.*\bnode', first_line):
return 'js'
elif re.search(r'^#!.*\bruby', first_line):
return 'rb'
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=[], use_shebang=True, modified_only=False,
exclude=[], group_by_ftype=False, extless_only=False):
# type: (List[str], List[str], bool, bool, List[str], bool, bool) -> Union[Dict[str, List[str]], List[str]]
"""
List files tracked by git.
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.
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 files or directories to be excluded, relative to repository root.
group_by_ftype - If True, returns a dict of lists keyed by file type.
If False, returns a flat list of files.
extless_only - Only include extensionless files in output.
"""
ftypes = [x.strip('.') for x in ftypes]
ftypes_set = set(ftypes)
# Really this is all bytes -- it's a file path -- but we get paths in
# sys.argv as str, so that battle is already lost. Settle for hoping
# everything is UTF-8.
repository_root = subprocess.check_output(['git', 'rev-parse', '--show-toplevel']).strip().decode('utf-8')
exclude_abspaths = [os.path.abspath(os.path.join(repository_root, fpath)) for fpath in exclude]
cmdline = ['git', 'ls-files'] + targets
if modified_only:
cmdline.append('-m')
files_gen = (x.strip() for x in subprocess.check_output(cmdline, universal_newlines=True).split('\n'))
# throw away empty lines and non-files (like symlinks)
files = list(filter(os.path.isfile, files_gen))
result_dict = defaultdict(list) # type: Dict[str, List[str]]
result_list = [] # type: List[str]
for fpath in files:
# this will take a long time if exclude is very large
ext = os.path.splitext(fpath)[1]
if extless_only and ext:
continue
absfpath = os.path.abspath(fpath)
if any(absfpath == expath or absfpath.startswith(os.path.abspath(expath) + os.sep)
for expath in exclude_abspaths):
continue
if ftypes or group_by_ftype:
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
if group_by_ftype:
result_dict[filetype].append(fpath)
else:
result_list.append(fpath)
if group_by_ftype:
return result_dict
else:
return result_list
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="List files tracked by git and optionally filter by type")
parser.add_argument('targets', nargs='*', default=[],
help='''files and directories to include in the result.
If this is not specified, the current directory is used''')
parser.add_argument('-m', '--modified', action='store_true', default=False, help='list only modified files')
parser.add_argument('-f', '--ftypes', nargs='+', default=[],
help="list of file types to filter on. All files are included if this option is absent")
parser.add_argument('--ext-only', dest='extonly', action='store_true', default=False,
help='only use extension to determine file type')
parser.add_argument('--exclude', nargs='+', default=[],
help='list of files and directories to exclude from results, relative to repo root')
parser.add_argument('--extless-only', dest='extless_only', action='store_true', default=False,
help='only include extensionless files in output')
args = parser.parse_args()
listing = list_files(targets=args.targets, ftypes=args.ftypes, use_shebang=not args.extonly,
modified_only=args.modified, exclude=args.exclude, extless_only=args.extless_only)
for l in listing:
print(l)

View file

@ -1,51 +0,0 @@
from __future__ import print_function, absolute_import
import sys
import os
from itertools import cycle
# <<<<<<< zulip/zulip
# sys.path.append(os.path.join(os.path.dirname(__file__), '../..'))
# from scripts.lib.zulip_tools import ENDC, BOLDRED, GREEN, YELLOW, BLUE, MAGENTA, CYAN
# =======
# Color codes
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BLACKONYELLOW = '\x1b[0;30;43m'
WHITEONRED = '\x1b[0;37;41m'
BOLDRED = '\x1B[1;31m'
GREEN = '\x1b[32m'
YELLOW = '\x1b[33m'
BLUE = '\x1b[34m'
MAGENTA = '\x1b[35m'
CYAN = '\x1b[36m'
# >>>>>>> zulip/python-zulip-api
from typing import Union, Text
colors = cycle([GREEN, YELLOW, BLUE, MAGENTA, CYAN])
def print_err(name, color, line):
# type: (str, str, Union[Text, bytes]) -> None
# Decode with UTF-8 if in Python 3 and `line` is of bytes type.
# (Python 2 does this automatically)
if sys.version_info[0] == 3 and isinstance(line, bytes):
line = line.decode('utf-8')
print('{}{}{}|{end} {}{}{end}'.format(
color,
name,
' ' * max(0, 10 - len(name)),
BOLDRED,
line.rstrip(),
end=ENDC)
)
# Python 2's print function does not have a `flush` option.
sys.stdout.flush()