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/.
5
6from __future__ import absolute_import, print_function
7
8import os
9import signal
10import six
11import subprocess
12import sys
13from distutils.spawn import find_executable
14
15here = os.path.dirname(os.path.realpath(__file__))
16topsrcdir = os.path.join(here, os.pardir, os.pardir)
17
18
19def run_process(cmd):
20    proc = subprocess.Popen(cmd)
21
22    # ignore SIGINT, the mozlint subprocess should exit quickly and gracefully
23    orig_handler = signal.signal(signal.SIGINT, signal.SIG_IGN)
24    proc.wait()
25    signal.signal(signal.SIGINT, orig_handler)
26    return proc.returncode
27
28
29def run_mozlint(hooktype, args):
30    if isinstance(hooktype, six.binary_type):
31        hooktype = hooktype.decode("UTF-8", "replace")
32
33    python = find_executable("python3")
34    if not python:
35        print("error: Python 3 not detected on your system! Please install it.")
36        sys.exit(1)
37
38    cmd = [python, os.path.join(topsrcdir, "mach"), "lint"]
39
40    if "commit" in hooktype:
41        # don't prevent commits, just display the lint results
42        run_process(cmd + ["--workdir=staged"])
43        return False
44    elif "push" in hooktype:
45        return run_process(cmd + ["--outgoing"] + args)
46
47    print("warning: '{}' is not a valid mozlint hooktype".format(hooktype))
48    return False
49
50
51def hg(ui, repo, **kwargs):
52    hooktype = kwargs["hooktype"]
53    return run_mozlint(hooktype, kwargs.get("pats", []))
54
55
56def git():
57    hooktype = os.path.basename(__file__)
58    if hooktype == "hooks.py":
59        hooktype = "pre-push"
60    return run_mozlint(hooktype, [])
61
62
63if __name__ == "__main__":
64    sys.exit(git())
65