2019-11-05 15:45:45 +00:00
|
|
|
import argparse
|
|
|
|
import sys
|
|
|
|
import tempfile
|
|
|
|
from pathlib import Path
|
|
|
|
|
2019-11-16 10:58:04 +00:00
|
|
|
import requests
|
2019-11-05 15:45:45 +00:00
|
|
|
import simplelogging
|
|
|
|
|
2019-11-22 11:50:24 +00:00
|
|
|
from padpo.pofile import PoFile
|
|
|
|
from padpo.checkers.doublespace import DoubleSpaceChecker
|
|
|
|
from padpo.checkers.empty import EmptyChecker
|
|
|
|
from padpo.checkers.fuzzy import FuzzyChecker
|
|
|
|
from padpo.checkers.grammalecte import GrammalecteChecker
|
|
|
|
from padpo.checkers.linelength import LineLengthChecker
|
|
|
|
from padpo.checkers.nbsp import NonBreakableSpaceChecker
|
2019-11-05 15:45:45 +00:00
|
|
|
|
2019-11-22 11:50:24 +00:00
|
|
|
log = None
|
2019-11-18 16:35:37 +00:00
|
|
|
|
|
|
|
|
2019-11-05 15:45:45 +00:00
|
|
|
checkers = [
|
|
|
|
DoubleSpaceChecker(),
|
|
|
|
EmptyChecker(),
|
2019-11-22 11:50:24 +00:00
|
|
|
FuzzyChecker(),
|
2019-11-05 15:45:45 +00:00
|
|
|
GrammalecteChecker(),
|
2019-11-22 11:50:24 +00:00
|
|
|
LineLengthChecker(),
|
|
|
|
NonBreakableSpaceChecker(),
|
2019-11-05 15:45:45 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
2019-11-18 10:50:14 +00:00
|
|
|
def check_file(path, pull_request_info=None):
|
2019-11-05 15:45:45 +00:00
|
|
|
file = PoFile(path)
|
|
|
|
|
|
|
|
for checker in checkers:
|
|
|
|
checker.check_file(file)
|
|
|
|
|
2019-11-18 10:50:14 +00:00
|
|
|
return file.display_warnings(pull_request_info)
|
2019-11-05 15:45:45 +00:00
|
|
|
|
|
|
|
|
2019-11-18 10:50:14 +00:00
|
|
|
def check_directory(path, pull_request_info=None):
|
2019-11-05 15:45:45 +00:00
|
|
|
path = Path(path)
|
|
|
|
any_error = False
|
|
|
|
for file in path.rglob("*.po"):
|
2019-11-18 10:50:14 +00:00
|
|
|
any_error = check_file(file, pull_request_info) or any_error
|
2019-11-05 15:45:45 +00:00
|
|
|
return any_error
|
|
|
|
|
|
|
|
|
2019-11-18 10:50:14 +00:00
|
|
|
def check_path(path, pull_request_info=None):
|
2019-11-05 15:45:45 +00:00
|
|
|
path = Path(path)
|
|
|
|
if path.is_dir():
|
2019-11-18 10:50:14 +00:00
|
|
|
return check_directory(path, pull_request_info)
|
2019-11-05 15:45:45 +00:00
|
|
|
else:
|
2019-11-18 10:50:14 +00:00
|
|
|
return check_file(path, pull_request_info)
|
|
|
|
|
|
|
|
|
|
|
|
class PullRequestInfo:
|
|
|
|
def __init__(self):
|
|
|
|
self._data = {}
|
|
|
|
|
|
|
|
def add_file(self, filename, temp_path, diff):
|
|
|
|
self._data[str(temp_path)] = (temp_path, diff, filename)
|
|
|
|
|
|
|
|
def diff(self, path):
|
|
|
|
if str(path) in self._data:
|
|
|
|
return self._data[str(path)][1]
|
|
|
|
return ""
|
|
|
|
|
|
|
|
def temp_path(self, path):
|
|
|
|
if str(path) in self._data:
|
|
|
|
return self._data[str(path)][0]
|
|
|
|
return ""
|
|
|
|
|
|
|
|
def filename(self, path):
|
|
|
|
if str(path) in self._data:
|
|
|
|
return self._data[str(path)][2]
|
|
|
|
return ""
|
2019-11-05 15:45:45 +00:00
|
|
|
|
|
|
|
|
2019-11-16 10:25:22 +00:00
|
|
|
def pull_request_files(pull_request):
|
2019-11-16 10:58:04 +00:00
|
|
|
pull_request = pull_request.replace("/pull/", "/pulls/")
|
|
|
|
request = requests.get(
|
|
|
|
f"https://api.github.com/repos/{pull_request}/files"
|
|
|
|
)
|
|
|
|
request.raise_for_status()
|
|
|
|
# TODO remove directory at end of execution
|
|
|
|
temp_dir = tempfile.mkdtemp(prefix="padpo_")
|
2019-11-18 10:50:14 +00:00
|
|
|
pr = PullRequestInfo()
|
2019-11-16 10:58:04 +00:00
|
|
|
for file in request.json():
|
|
|
|
filename = file["filename"]
|
|
|
|
temp_file = Path(temp_dir) / filename
|
|
|
|
content_request = requests.get(file["raw_url"])
|
|
|
|
content_request.raise_for_status()
|
|
|
|
temp_file_dir = temp_file.parent
|
|
|
|
temp_file_dir.mkdir(parents=True, exist_ok=True)
|
|
|
|
temp_file.write_bytes(content_request.content)
|
2019-11-18 10:50:14 +00:00
|
|
|
pr.add_file(filename, temp_file, file["patch"])
|
|
|
|
return temp_dir, pr
|
2019-11-16 10:25:22 +00:00
|
|
|
|
|
|
|
|
2019-11-18 13:59:05 +00:00
|
|
|
def main():
|
2019-11-18 17:16:51 +00:00
|
|
|
global log
|
|
|
|
log = simplelogging.get_logger("__main__")
|
|
|
|
|
2019-11-05 15:45:45 +00:00
|
|
|
parser = argparse.ArgumentParser(description="Linter for *.po files.")
|
|
|
|
parser.add_argument("-v", "--verbose", action="count", default=0)
|
2019-11-19 09:37:08 +00:00
|
|
|
files = parser.add_mutually_exclusive_group(required=True)
|
2019-11-16 10:25:22 +00:00
|
|
|
files.add_argument(
|
|
|
|
"-i",
|
|
|
|
"--input-path",
|
2019-11-05 15:45:45 +00:00
|
|
|
metavar="PATH",
|
|
|
|
type=str,
|
|
|
|
help="path of the file or directory to check",
|
2019-11-16 10:25:22 +00:00
|
|
|
default="",
|
|
|
|
)
|
|
|
|
files.add_argument(
|
|
|
|
"-g",
|
|
|
|
"--github",
|
|
|
|
metavar="python/python-docs-fr/pull/978",
|
|
|
|
type=str,
|
|
|
|
help="path of pull request in GitHub to check",
|
|
|
|
default="",
|
|
|
|
)
|
|
|
|
files.add_argument(
|
|
|
|
"-p",
|
|
|
|
"--python-docs-fr",
|
|
|
|
metavar="978",
|
|
|
|
type=int,
|
|
|
|
help="ID of pull request in python-docs-fr repository",
|
|
|
|
default=0,
|
2019-11-05 15:45:45 +00:00
|
|
|
)
|
|
|
|
args = parser.parse_args()
|
|
|
|
if args.verbose < 1:
|
|
|
|
log.reduced_logging()
|
|
|
|
elif args.verbose < 2:
|
|
|
|
log.normal_logging()
|
|
|
|
else:
|
|
|
|
log.full_logging()
|
|
|
|
|
2019-11-16 10:25:22 +00:00
|
|
|
if args.input_path:
|
|
|
|
path = args.input_path
|
2019-11-18 10:50:14 +00:00
|
|
|
pull_request_info = None
|
2019-11-16 10:25:22 +00:00
|
|
|
else:
|
|
|
|
pull_request = ""
|
|
|
|
if args.github:
|
|
|
|
pull_request = args.github
|
|
|
|
if args.python_docs_fr:
|
|
|
|
pull_request = f"python/python-docs-fr/pull/{args.python_docs_fr}"
|
2019-11-18 10:50:14 +00:00
|
|
|
path, pull_request_info = pull_request_files(pull_request)
|
2019-11-16 10:25:22 +00:00
|
|
|
|
2019-11-18 10:50:14 +00:00
|
|
|
any_error = check_path(path, pull_request_info=pull_request_info)
|
2019-11-05 15:45:45 +00:00
|
|
|
if any_error:
|
|
|
|
sys.exit(1)
|