This repository was archived by the owner on Sep 1, 2023. It is now read-only.
forked from python-mode/python-mode
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhook.py
More file actions
113 lines (81 loc) · 2.81 KB
/
Copy pathhook.py
File metadata and controls
113 lines (81 loc) · 2.81 KB
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
111
112
113
""" 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
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():
""" Run pylama after git commit. """
from .main import check_files
_, files_modified, _ = run("git diff-index --cached --name-only HEAD")
options = parse_options()
setup_logger(options)
check_files([f for f in map(str, files_modified)], options)
def hg_hook(ui, repo, node=None, **kwargs):
""" Run pylama after mercurial commit. """
from .main import check_files
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)
check_files(paths, options)
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(path, '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(path, '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(git)
LOGGER.warn('Mercurial hook has been installed.')
else:
LOGGER.error('VCS has not found. Check your path.')
sys.exit(1)
# lint_ignore=F0401,E1103