forked from xuezhulian/Coverage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenPrePushFile.py
66 lines (56 loc) · 1.74 KB
/
genPrePushFile.py
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
#!/usr/bin/env python
#coding=utf-8
import os
import stat
import sys
import string
defaultencoding = 'utf-8'
if sys.getdefaultencoding() != defaultencoding:
reload(sys)
sys.setdefaultencoding(defaultencoding)
def gen_by_componet_name(name):
content = """
cd ../RCodeCoverage
echo '----------------'
rate=$(python coverage.py %s | grep "RCoverageRate:" | sed 's/RCoverageRate:\([0-9-]*\).*/\\1/g')
if [ $rate -eq -1 ]; then
echo '没有覆盖率信息,跳过...'
exit 0
fi
if [ $(echo "$rate < 80.0" | bc) = 1 ];then
echo '代码覆盖率为'$rate',不满足需求'
echo '----------------'
exit 1
else
echo '代码覆盖率为'$rate',即将上传代码'
fi
echo '----------------'
exit 0
""" % (name,)
return content
if __name__ == '__main__':
dirpath = sys.path[0]
dirpath = '/'.join(dirpath.split('/')[0:-1])
if not os.path.exists(dirpath):
print 'pre push hook failed with invalid dirpath'
exit()
dirlists = set()
for file in os.listdir(dirpath):
if file == 'RCodeCoverage':
continue
if os.path.isdir(dirpath + '/' + file):
dirlists.add(file)
for name in dirlists:
dirpath = sys.path[0]
dirpath = dirpath.replace(dirpath.split('/')[-1],name)
filepath = os.path.join(dirpath, '.git', 'hooks', 'pre-push.sample')
if os.path.exists(filepath):
os.rename(filepath,filepath[0:-7])
filepath = filepath[0:-7]
if not os.path.exists(filepath):
continue
with open(filepath, 'wb') as f:
if name == 'RBigApp':
name = ''
f.write(gen_by_componet_name(name).encode(encoding='UTF-8'))
os.chmod(filepath, stat.S_IRWXU|stat.S_IRGRP|stat.S_IROTH)