/usr/lib/python3/dist-packages/pylama/hook.py is in python3-pylama 7.3.3-1.1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 | """SCM hooks. Integration with git and mercurial."""
from __future__ import absolute_import
import sys
from os import path as op, chmod
from subprocess import Popen, PIPE
from .main import LOGGER, process_paths
from .config import parse_options, setup_logger
try:
from configparser import ConfigParser # noqa
except ImportError: # Python 2
from ConfigParser import ConfigParser
def run(command):
"""Run a shell command.
:return str: Stdout
"""
p = Popen(command.split(), stdout=PIPE, stderr=PIPE)
(stdout, stderr) = p.communicate()
return (p.returncode, [line.strip() for line in stdout.splitlines()],
[line.strip() for line in stderr.splitlines()])
def git_hook(error=True):
"""Run pylama after git commit."""
_, files_modified, _ = run("git diff-index --cached --name-only HEAD")
options = parse_options()
setup_logger(options)
candidates = list(map(str, files_modified))
if candidates:
process_paths(options, candidates=candidates, error=error)
def hg_hook(ui, repo, node=None, **kwargs):
"""Run pylama after mercurial commit."""
seen = set()
paths = []
if len(repo):
for rev in range(repo[node], len(repo)):
for file_ in repo[rev].files():
file_ = op.join(repo.root, file_)
if file_ in seen or not op.exists(file_):
continue
seen.add(file_)
paths.append(file_)
options = parse_options()
setup_logger(options)
if paths:
process_paths(options, candidates=paths)
def install_git(path):
"""Install hook in Git repository."""
hook = op.join(path, 'pre-commit')
with open(hook, 'w') as fd:
fd.write("""#!/usr/bin/env python
import sys
from pylama.hook import git_hook
if __name__ == '__main__':
sys.exit(git_hook())
""")
chmod(hook, 484)
def install_hg(path):
""" Install hook in Mercurial repository. """
hook = op.join(path, 'hgrc')
if not op.isfile(hook):
open(hook, 'w+').close()
c = ConfigParser()
c.readfp(open(hook, 'r'))
if not c.has_section('hooks'):
c.add_section('hooks')
if not c.has_option('hooks', 'commit'):
c.set('hooks', 'commit', 'python:pylama.hooks.hg_hook')
if not c.has_option('hooks', 'qrefresh'):
c.set('hooks', 'qrefresh', 'python:pylama.hooks.hg_hook')
c.write(open(hook, 'w+'))
def install_hook(path):
""" Auto definition of SCM and hook installation. """
git = op.join(path, '.git', 'hooks')
hg = op.join(path, '.hg')
if op.exists(git):
install_git(git)
LOGGER.warn('Git hook has been installed.')
elif op.exists(hg):
install_hg(hg)
LOGGER.warn('Mercurial hook has been installed.')
else:
LOGGER.error('VCS has not found. Check your path.')
sys.exit(1)
# pylama:ignore=F0401,E1103,D210,F0001
|