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 subprocess 11import sys 12from distutils.spawn import find_executable 13 14here = os.path.dirname(os.path.realpath(__file__)) 15topsrcdir = os.path.join(here, os.pardir, os.pardir) 16 17 18def 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 28def 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 48def hg(ui, repo, **kwargs): 49 hooktype = kwargs['hooktype'] 50 return run_mozlint(hooktype, kwargs.get('pats', [])) 51 52 53def git(): 54 hooktype = os.path.basename(__file__) 55 if hooktype == 'hooks.py': 56 hooktype = 'pre-push' 57 return run_mozlint(hooktype, []) 58 59 60if __name__ == '__main__': 61 sys.exit(git()) 62