Source code

Revision control

Other Tools

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
6
from __future__ import absolute_import, print_function
7
8
import os
9
import signal
10
import subprocess
11
import sys
12
from distutils.spawn import find_executable
13
14
here = os.path.dirname(os.path.realpath(__file__))
15
topsrcdir = os.path.join(here, os.pardir, os.pardir)
16
17
18
def run_process(cmd):
19
proc = subprocess.Popen(cmd)
20
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
26
27
28
def run_mozlint(hooktype, args):
29
# --quiet prevents warnings on eslint, it will be ignored by other linters
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)
34
35
cmd = [python, os.path.join(topsrcdir, 'mach'), 'lint', '--quiet']
36
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)
43
44
print("warning: '{}' is not a valid mozlint hooktype".format(hooktype))
45
return False
46
47
48
def hg(ui, repo, **kwargs):
49
hooktype = kwargs['hooktype']
50
return run_mozlint(hooktype, kwargs.get('pats', []))
51
52
53
def git():
54
hooktype = os.path.basename(__file__)
55
if hooktype == 'hooks.py':
56
hooktype = 'pre-push'
57
return run_mozlint(hooktype, [])
58
59
60
if __name__ == '__main__':
61
sys.exit(git())