2017-07-01 04:29:42 +03:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
2019-07-19 18:56:29 +03:00
|
|
|
from __future__ import absolute_import, print_function
|
|
|
|
|
2017-07-01 04:29:42 +03:00
|
|
|
import os
|
2018-03-29 23:39:29 +03:00
|
|
|
import signal
|
2020-08-20 23:35:26 +03:00
|
|
|
import six
|
2017-07-01 04:29:42 +03:00
|
|
|
import subprocess
|
|
|
|
import sys
|
2017-07-24 19:25:53 +03:00
|
|
|
from distutils.spawn import find_executable
|
2017-07-01 04:29:42 +03:00
|
|
|
|
|
|
|
here = os.path.dirname(os.path.realpath(__file__))
|
|
|
|
topsrcdir = os.path.join(here, os.pardir, os.pardir)
|
|
|
|
|
|
|
|
|
2018-03-29 23:39:29 +03:00
|
|
|
def run_process(cmd):
|
|
|
|
proc = subprocess.Popen(cmd)
|
|
|
|
|
|
|
|
# ignore SIGINT, the mozlint subprocess should exit quickly and gracefully
|
|
|
|
orig_handler = signal.signal(signal.SIGINT, signal.SIG_IGN)
|
|
|
|
proc.wait()
|
|
|
|
signal.signal(signal.SIGINT, orig_handler)
|
|
|
|
return proc.returncode
|
|
|
|
|
|
|
|
|
2017-07-01 04:29:42 +03:00
|
|
|
def run_mozlint(hooktype, args):
|
2020-08-20 23:35:26 +03:00
|
|
|
if isinstance(hooktype, six.binary_type):
|
2020-08-20 23:55:09 +03:00
|
|
|
hooktype = hooktype.decode("UTF-8", "replace")
|
2017-07-12 00:02:46 +03:00
|
|
|
# --quiet prevents warnings on eslint, it will be ignored by other linters
|
2020-07-09 17:09:31 +03:00
|
|
|
python = find_executable("python3")
|
2019-10-02 19:23:59 +03:00
|
|
|
if not python:
|
|
|
|
print("error: Python 3 not detected on your system! Please install it.")
|
|
|
|
sys.exit(1)
|
|
|
|
|
2020-07-09 17:09:31 +03:00
|
|
|
cmd = [python, os.path.join(topsrcdir, "mach"), "lint", "--quiet"]
|
2017-07-01 04:29:42 +03:00
|
|
|
|
2020-07-09 17:09:31 +03:00
|
|
|
if "commit" in hooktype:
|
2017-07-01 04:29:42 +03:00
|
|
|
# don't prevent commits, just display the lint results
|
2020-07-09 17:09:31 +03:00
|
|
|
run_process(cmd + ["--workdir=staged"])
|
2017-07-01 04:29:42 +03:00
|
|
|
return False
|
2020-07-09 17:09:31 +03:00
|
|
|
elif "push" in hooktype:
|
|
|
|
return run_process(cmd + ["--outgoing"] + args)
|
2017-07-01 04:29:42 +03:00
|
|
|
|
|
|
|
print("warning: '{}' is not a valid mozlint hooktype".format(hooktype))
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
def hg(ui, repo, **kwargs):
|
2020-07-09 17:09:31 +03:00
|
|
|
hooktype = kwargs["hooktype"]
|
|
|
|
return run_mozlint(hooktype, kwargs.get("pats", []))
|
2017-07-01 04:29:42 +03:00
|
|
|
|
|
|
|
|
2017-11-02 00:07:18 +03:00
|
|
|
def git():
|
2017-07-01 04:29:42 +03:00
|
|
|
hooktype = os.path.basename(__file__)
|
2020-07-09 17:09:31 +03:00
|
|
|
if hooktype == "hooks.py":
|
|
|
|
hooktype = "pre-push"
|
2017-11-02 00:07:18 +03:00
|
|
|
return run_mozlint(hooktype, [])
|
2017-07-01 04:29:42 +03:00
|
|
|
|
|
|
|
|
2020-07-09 17:09:31 +03:00
|
|
|
if __name__ == "__main__":
|
2017-07-01 04:29:42 +03:00
|
|
|
sys.exit(git())
|