Bug 1853320 [wpt PR 41940] - [FLEDGE] Split up runs of /trusted-scoring-signals.https...
[gecko.git] / tools / lint / hooks.py
blob3d2161005e2ef46da727107bd7ffd845affe8567
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 here = os.path.dirname(os.path.realpath(__file__))
13 topsrcdir = os.path.join(here, os.pardir, os.pardir)
16 def run_process(cmd):
17 proc = subprocess.Popen(cmd)
19 # ignore SIGINT, the mozlint subprocess should exit quickly and gracefully
20 orig_handler = signal.signal(signal.SIGINT, signal.SIG_IGN)
21 proc.wait()
22 signal.signal(signal.SIGINT, orig_handler)
23 return proc.returncode
26 def run_mozlint(hooktype, args):
27 if isinstance(hooktype, bytes):
28 hooktype = hooktype.decode("UTF-8", "replace")
30 python = find_executable("python3")
31 if not python:
32 print("error: Python 3 not detected on your system! Please install it.")
33 sys.exit(1)
35 cmd = [python, os.path.join(topsrcdir, "mach"), "lint"]
37 if "commit" in hooktype:
38 # don't prevent commits, just display the lint results
39 run_process(cmd + ["--workdir=staged"])
40 return False
41 elif "push" in hooktype:
42 return run_process(cmd + ["--outgoing"] + args)
44 print("warning: '{}' is not a valid mozlint hooktype".format(hooktype))
45 return False
48 def hg(ui, repo, **kwargs):
49 hooktype = kwargs["hooktype"]
50 return run_mozlint(hooktype, kwargs.get("pats", []))
53 def git():
54 hooktype = os.path.basename(__file__)
55 if hooktype == "hooks.py":
56 hooktype = "pre-push"
57 return run_mozlint(hooktype, [])
60 if __name__ == "__main__":
61 sys.exit(git())