|
|
@ -3,6 +3,7 @@ import argparse
|
|
|
|
import fnmatch
|
|
|
|
import fnmatch
|
|
|
|
import re
|
|
|
|
import re
|
|
|
|
import os
|
|
|
|
import os
|
|
|
|
|
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
import sys
|
|
|
|
|
|
|
|
|
|
|
|
DFHACK_ROOT = os.path.normpath(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|
|
|
DFHACK_ROOT = os.path.normpath(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|
|
@ -109,6 +110,21 @@ class TabLinter(Linter):
|
|
|
|
|
|
|
|
|
|
|
|
linters = [cls() for cls in Linter.__subclasses__() if not cls.ignore]
|
|
|
|
linters = [cls() for cls in Linter.__subclasses__() if not cls.ignore]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def walk_all(root_path):
|
|
|
|
|
|
|
|
for cur, dirnames, filenames in os.walk(root_path):
|
|
|
|
|
|
|
|
for filename in filenames:
|
|
|
|
|
|
|
|
full_path = os.path.join(cur, filename)
|
|
|
|
|
|
|
|
yield full_path
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def walk_git_files(root_path):
|
|
|
|
|
|
|
|
p = subprocess.Popen(['git', '-C', root_path, 'ls-files', root_path], stdout=subprocess.PIPE)
|
|
|
|
|
|
|
|
for line in p.stdout.readlines():
|
|
|
|
|
|
|
|
path = line.decode('utf-8').strip()
|
|
|
|
|
|
|
|
full_path = os.path.join(root_path, path)
|
|
|
|
|
|
|
|
yield full_path
|
|
|
|
|
|
|
|
if p.wait() != 0:
|
|
|
|
|
|
|
|
raise RuntimeError('git exited with %r' % p.returncode)
|
|
|
|
|
|
|
|
|
|
|
|
def main(args):
|
|
|
|
def main(args):
|
|
|
|
root_path = os.path.abspath(args.path)
|
|
|
|
root_path = os.path.abspath(args.path)
|
|
|
|
if not os.path.exists(args.path):
|
|
|
|
if not os.path.exists(args.path):
|
|
|
@ -118,38 +134,40 @@ def main(args):
|
|
|
|
check_patterns = load_pattern_files(args.check_patterns)
|
|
|
|
check_patterns = load_pattern_files(args.check_patterns)
|
|
|
|
ignore_patterns = load_pattern_files(args.ignore_patterns)
|
|
|
|
ignore_patterns = load_pattern_files(args.ignore_patterns)
|
|
|
|
|
|
|
|
|
|
|
|
for cur, dirnames, filenames in os.walk(root_path):
|
|
|
|
walk_iter = walk_all
|
|
|
|
for filename in filenames:
|
|
|
|
if args.git_only:
|
|
|
|
full_path = os.path.join(cur, filename)
|
|
|
|
walk_iter = walk_git_files
|
|
|
|
rel_path = full_path.replace(root_path, '').replace('\\', '/').lstrip('/')
|
|
|
|
|
|
|
|
if not valid_file(rel_path, check_patterns, ignore_patterns):
|
|
|
|
for full_path in walk_iter(root_path):
|
|
|
|
continue
|
|
|
|
rel_path = full_path.replace(root_path, '').replace('\\', '/').lstrip('/')
|
|
|
|
if args.verbose:
|
|
|
|
if not valid_file(rel_path, check_patterns, ignore_patterns):
|
|
|
|
print('Checking:', rel_path)
|
|
|
|
continue
|
|
|
|
lines = []
|
|
|
|
if args.verbose:
|
|
|
|
with open(full_path, 'rb') as f:
|
|
|
|
print('Checking:', rel_path)
|
|
|
|
lines = f.read().split(b'\n')
|
|
|
|
lines = []
|
|
|
|
for i, line in enumerate(lines):
|
|
|
|
with open(full_path, 'rb') as f:
|
|
|
|
try:
|
|
|
|
lines = f.read().split(b'\n')
|
|
|
|
lines[i] = line.decode('utf-8')
|
|
|
|
for i, line in enumerate(lines):
|
|
|
|
except UnicodeDecodeError:
|
|
|
|
|
|
|
|
msg_params = (rel_path, i + 1, 'Invalid UTF-8 (other errors will be ignored)')
|
|
|
|
|
|
|
|
error('%s:%i: %s' % msg_params)
|
|
|
|
|
|
|
|
if args.github_actions:
|
|
|
|
|
|
|
|
print('::error file=%s,line=%i::%s' % msg_params)
|
|
|
|
|
|
|
|
lines[i] = ''
|
|
|
|
|
|
|
|
for linter in linters:
|
|
|
|
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
linter.check(lines)
|
|
|
|
lines[i] = line.decode('utf-8')
|
|
|
|
except LinterError as e:
|
|
|
|
except UnicodeDecodeError:
|
|
|
|
error('%s: %s' % (rel_path, e))
|
|
|
|
msg_params = (rel_path, i + 1, 'Invalid UTF-8 (other errors will be ignored)')
|
|
|
|
|
|
|
|
error('%s:%i: %s' % msg_params)
|
|
|
|
if args.github_actions:
|
|
|
|
if args.github_actions:
|
|
|
|
print(e.github_actions_workflow_command(rel_path))
|
|
|
|
print('::error file=%s,line=%i::%s' % msg_params)
|
|
|
|
if args.fix:
|
|
|
|
lines[i] = ''
|
|
|
|
linter.fix(lines)
|
|
|
|
for linter in linters:
|
|
|
|
contents = '\n'.join(lines)
|
|
|
|
try:
|
|
|
|
with open(full_path, 'wb') as f:
|
|
|
|
linter.check(lines)
|
|
|
|
f.write(contents.encode('utf-8'))
|
|
|
|
except LinterError as e:
|
|
|
|
|
|
|
|
error('%s: %s' % (rel_path, e))
|
|
|
|
|
|
|
|
if args.github_actions:
|
|
|
|
|
|
|
|
print(e.github_actions_workflow_command(rel_path))
|
|
|
|
|
|
|
|
if args.fix:
|
|
|
|
|
|
|
|
linter.fix(lines)
|
|
|
|
|
|
|
|
contents = '\n'.join(lines)
|
|
|
|
|
|
|
|
with open(full_path, 'wb') as f:
|
|
|
|
|
|
|
|
f.write(contents.encode('utf-8'))
|
|
|
|
|
|
|
|
|
|
|
|
if success:
|
|
|
|
if success:
|
|
|
|
print('All linters completed successfully')
|
|
|
|
print('All linters completed successfully')
|
|
|
@ -163,6 +181,8 @@ if __name__ == '__main__':
|
|
|
|
help='Path to scan (default: current directory)')
|
|
|
|
help='Path to scan (default: current directory)')
|
|
|
|
parser.add_argument('--fix', action='store_true',
|
|
|
|
parser.add_argument('--fix', action='store_true',
|
|
|
|
help='Attempt to modify files in-place to fix identified issues')
|
|
|
|
help='Attempt to modify files in-place to fix identified issues')
|
|
|
|
|
|
|
|
parser.add_argument('--git-only', action='store_true',
|
|
|
|
|
|
|
|
help='Only check files tracked by git')
|
|
|
|
parser.add_argument('--github-actions', action='store_true',
|
|
|
|
parser.add_argument('--github-actions', action='store_true',
|
|
|
|
help='Enable GitHub Actions workflow command output')
|
|
|
|
help='Enable GitHub Actions workflow command output')
|
|
|
|
parser.add_argument('-v', '--verbose', action='store_true',
|
|
|
|
parser.add_argument('-v', '--verbose', action='store_true',
|
|
|
|