2017-03-08 05:27:50 +01:00
|
|
|
#!/usr/bin/env python
|
|
|
|
"""Python syntax checker with lint friendly output."""
|
2019-07-12 08:46:20 +02:00
|
|
|
from __future__ import (absolute_import, division, print_function)
|
|
|
|
__metaclass__ = type
|
2017-03-08 05:27:50 +01:00
|
|
|
|
|
|
|
import sys
|
|
|
|
|
2021-05-06 22:24:09 +02:00
|
|
|
ENCODING = 'utf-8'
|
|
|
|
ERRORS = 'replace'
|
|
|
|
Text = type(u'')
|
2017-03-08 05:27:50 +01:00
|
|
|
|
|
|
|
|
2021-05-06 22:24:09 +02:00
|
|
|
def main():
|
2018-02-28 00:05:39 +01:00
|
|
|
for path in sys.argv[1:] or sys.stdin.read().splitlines():
|
2019-07-02 17:56:12 +02:00
|
|
|
with open(path, 'rb') as source_fd:
|
2021-05-06 22:24:09 +02:00
|
|
|
source = source_fd.read()
|
2017-03-08 05:27:50 +01:00
|
|
|
|
|
|
|
try:
|
2021-05-06 22:24:09 +02:00
|
|
|
compile(source, path, 'exec', dont_inherit=True)
|
|
|
|
except SyntaxError as ex:
|
|
|
|
extype, message, lineno, offset = type(ex), ex.text, ex.lineno, ex.offset
|
|
|
|
except BaseException as ex: # pylint: disable=broad-except
|
|
|
|
extype, message, lineno, offset = type(ex), str(ex), 0, 0
|
|
|
|
else:
|
|
|
|
continue
|
|
|
|
|
|
|
|
result = "%s:%d:%d: %s: %s" % (path, lineno, offset, extype.__name__, safe_message(message))
|
|
|
|
|
|
|
|
if sys.version_info <= (3,):
|
|
|
|
result = result.encode(ENCODING, ERRORS)
|
|
|
|
|
|
|
|
print(result)
|
|
|
|
|
|
|
|
|
|
|
|
def safe_message(value):
|
|
|
|
"""Given an input value as text or bytes, return the first non-empty line as text, ensuring it can be round-tripped as UTF-8."""
|
|
|
|
if isinstance(value, Text):
|
|
|
|
value = value.encode(ENCODING, ERRORS)
|
|
|
|
|
|
|
|
value = value.decode(ENCODING, ERRORS)
|
|
|
|
value = value.strip().splitlines()[0].strip()
|
|
|
|
|
|
|
|
return value
|
2017-03-08 05:27:50 +01:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|