svartalf is sharing code with you
Bitbucket is a code hosting site. Unlimited public and private repositories. Free for small teams.
Don't show this againmisc / mercurial_hooks.py
- commit
- a6358968a918
- parent
- e35beb1727f9
- branch
- default
Граббер новостей с dsi.ru
1 |
b46b660a4042
|
# -*- coding: utf-8 -*- |
2 |
b46b660a4042
|
|
3 |
e35beb1727f9
|
""" |
4 |
b46b660a4042
|
Примеры хуков для mercurial |
5 |
e35beb1727f9
|
""" |
6 |
b46b660a4042
|
|
7 |
b46b660a4042
|
import os |
8 |
b46b660a4042
|
import re |
9 |
b46b660a4042
|
|
10 |
b46b660a4042
|
from mercurial import patch |
11 |
b46b660a4042
|
|
12 |
b46b660a4042
|
def print_check(ui, repo, **kwargs): |
13 |
e35beb1727f9
|
"""Отмена коммита, если в коде есть print |
14 |
b46b660a4042
|
|
15 |
e35beb1727f9
|
Подключение в .hgrc: |
16 |
e35beb1727f9
|
precommit = python:.hg/hooks.py:print_check""" |
17 |
e35beb1727f9
|
|
18 |
b46b660a4042
|
linenum, header = 0, False |
19 |
b46b660a4042
|
|
20 |
b46b660a4042
|
files = {} |
21 |
b46b660a4042
|
|
22 |
b46b660a4042
|
diff = patch.diff(repo) |
23 |
b46b660a4042
|
|
24 |
b46b660a4042
|
for num, filediff in enumerate(diff): |
25 |
b46b660a4042
|
for line in filediff.split('\n'): |
26 |
b46b660a4042
|
if header: |
27 |
b46b660a4042
|
m = re.match(r'(?:---|\+\+\+) ([^\t]+)', line) |
28 |
b46b660a4042
|
if m and m.group(1) != '/dev/null': |
29 |
b46b660a4042
|
filename = m.group(1).split('/', 1)[-1] |
30 |
b46b660a4042
|
if line.startswith('+++ '): |
31 |
b46b660a4042
|
header = False |
32 |
b46b660a4042
|
continue |
33 |
b46b660a4042
|
if line.startswith('diff '): |
34 |
b46b660a4042
|
header = True |
35 |
b46b660a4042
|
continue |
36 |
b46b660a4042
|
m = re.match(r'@@ -\d+,\d+ \+(\d+),', line) |
37 |
b46b660a4042
|
if m: |
38 |
b46b660a4042
|
linenum = int(m.group(1)) |
39 |
b46b660a4042
|
continue |
40 |
b46b660a4042
|
m = re.match(r'^(?:-|\+)\s*print', line) |
41 |
b46b660a4042
|
if m: |
42 |
b46b660a4042
|
if not files.has_key(filename): |
43 |
b46b660a4042
|
files[filename] = [] |
44 |
b46b660a4042
|
files[filename].append((linenum, line)) |
45 |
b46b660a4042
|
if line and line[0] in ' +': |
46 |
b46b660a4042
|
linenum += 1 |
47 |
b46b660a4042
|
|
48 |
b46b660a4042
|
if files: |
49 |
b46b660a4042
|
ui.warn('Error! Print statements founded\n') |
50 |
b46b660a4042
|
for file in files: |
51 |
b46b660a4042
|
ui.warn('\nFile: %s\n' % file) |
52 |
b46b660a4042
|
for error in files[file]: |
53 |
b46b660a4042
|
ui.warn('\tLine %s: %s\n' % (error[0], error[1].strip(' -+')[:70])) |
54 |
b46b660a4042
|
|
55 |
b46b660a4042
|
return bool(files) |
56 |
b46b660a4042
|
|
57 |
e35beb1727f9
|
def console_check(ui, repo, **kwargs): |
58 |
e35beb1727f9
|
"""Отмена коммита, если в .js файлах есть вызов console.* |
59 |
e35beb1727f9
|
|
60 |
e35beb1727f9
|
Подключение в .hgrc: |
61 |
e35beb1727f9
|
precommit = python:.hg/hooks.py:console_check""" |
62 |
e35beb1727f9
|
|
63 |
e35beb1727f9
|
linenum, header = 0, False |
64 |
e35beb1727f9
|
|
65 |
e35beb1727f9
|
files = {} |
66 |
e35beb1727f9
|
|
67 |
e35beb1727f9
|
diff = patch.diff(repo) |
68 |
e35beb1727f9
|
|
69 |
e35beb1727f9
|
for num, filediff in enumerate(diff): |
70 |
e35beb1727f9
|
for line in filediff.split('\n'): |
71 |
e35beb1727f9
|
if header: |
72 |
e35beb1727f9
|
m = re.match(r'(?:---|\+\+\+) ([^\t]+)', line) |
73 |
e35beb1727f9
|
if m and m.group(1) != '/dev/null': |
74 |
e35beb1727f9
|
filename = m.group(1).split('/', 1)[-1] |
75 |
e35beb1727f9
|
if line.startswith('+++ '): |
76 |
e35beb1727f9
|
header = False |
77 |
e35beb1727f9
|
continue |
78 |
e35beb1727f9
|
if line.startswith('diff '): |
79 |
e35beb1727f9
|
header = True |
80 |
e35beb1727f9
|
continue |
81 |
e35beb1727f9
|
if not os.path.splitext(filename)[1] in ('.js', '.html'): |
82 |
e35beb1727f9
|
continue |
83 |
e35beb1727f9
|
m = re.match(r'@@ -\d+,\d+ \+(\d+),', line) |
84 |
e35beb1727f9
|
if m: |
85 |
e35beb1727f9
|
linenum = int(m.group(1)) |
86 |
e35beb1727f9
|
continue |
87 |
e35beb1727f9
|
m = re.match(r'^(?:-|\+)\s*console.', line) |
88 |
e35beb1727f9
|
if m: |
89 |
e35beb1727f9
|
if not files.has_key(filename): |
90 |
e35beb1727f9
|
files[filename] = [] |
91 |
e35beb1727f9
|
files[filename].append((linenum, line)) |
92 |
e35beb1727f9
|
if line and line[0] in ' +': |
93 |
e35beb1727f9
|
linenum += 1 |
94 |
e35beb1727f9
|
|
95 |
e35beb1727f9
|
if files: |
96 |
e35beb1727f9
|
ui.warn('Error! JS console statements founded\n') |
97 |
e35beb1727f9
|
for file in files: |
98 |
e35beb1727f9
|
ui.warn('\nFile: %s\n' % file) |
99 |
e35beb1727f9
|
for error in files[file]: |
100 |
e35beb1727f9
|
ui.warn('\tLine %s: %s\n' % (error[0], error[1].strip(' -+')[:70])) |
101 |
e35beb1727f9
|
|
102 |
b46b660a4042
|
def clean_cache(ui, repo, **kwargs): |
103 |
e35beb1727f9
|
"""После обновления репозитория удаляем .pyc файлы |
104 |
b46b660a4042
|
|
105 |
e35beb1727f9
|
Подключение в .hgrc: |
106 |
e35beb1727f9
|
preupdate = python:.hg/hooks.py:clean_cache""" |
107 |
b46b660a4042
|
|
108 |
b46b660a4042
|
for root, dirs, files in os.walk(repo.root): |
109 |
b46b660a4042
|
for file in files: |
110 |
b46b660a4042
|
if os.path.splitext(file)[1] == '.pyc': |
111 |
e35beb1727f9
|
os.remove(os.path.join(root, file) |