Bug 1843532 - Refactor Statistics::computeMMU to make it easier to understand r=sfink
[gecko.git] / tools / lint / hooks.py
blobfead62ec7e09128e5be3e9f4e8eaed1441198a36
1 #!/usr/bin/env python
2 # This Source Code Form is subject to the terms of the Mozilla Public
3 # License, v. 2.0. If a copy of the MPL was not distributed with this
4 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 import os
7 import signal
8 import subprocess
9 import sys
10 from distutils.spawn import find_executable
12 import six
14 here = os.path.dirname(os.path.realpath(__file__))
15 topsrcdir = os.path.join(here, os.pardir, os.pardir)
18 def run_process(cmd):
19 proc = subprocess.Popen(cmd)
21 # ignore SIGINT, the mozlint subprocess should exit quickly and gracefully
22 orig_handler = signal.signal(signal.SIGINT, signal.SIG_IGN)
23 proc.wait()
24 signal.signal(signal.SIGINT, orig_handler)
25 return proc.returncode
28 def run_mozlint(hooktype, args):
29 if isinstance(hooktype, six.binary_type):
30 hooktype = hooktype.decode("UTF-8", "replace")
32 python = find_executable("python3")
33 if not python:
34 print("error: Python 3 not detected on your system! Please install it.")
35 sys.exit(1)
37 cmd = [python, os.path.join(topsrcdir, "mach"), "lint"]
39 if "commit" in hooktype:
40 # don't prevent commits, just display the lint results
41 run_process(cmd + ["--workdir=staged"])
42 return False
43 elif "push" in hooktype:
44 return run_process(cmd + ["--outgoing"] + args)
46 print("warning: '{}' is not a valid mozlint hooktype".format(hooktype))
47 return False
50 def hg(ui, repo, **kwargs):
51 hooktype = kwargs["hooktype"]
52 return run_mozlint(hooktype, kwargs.get("pats", []))
55 def git():
56 hooktype = os.path.basename(__file__)
57 if hooktype == "hooks.py":
58 hooktype = "pre-push"
59 return run_mozlint(hooktype, [])
62 if __name__ == "__main__":
63 sys.exit(git())