/
check_update.py
101 lines (83 loc) · 2.75 KB
/
check_update.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
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
#!coding: utf-8
import logging
import subprocess
import os
from sendmail import send_mail
#
# Setup logging format
#
FORMAT = "%(levelname)7s %(asctime)s [%(filename)13s:%(lineno)4d] %(message)s"
DATEFMT = "%H:%M:%S"
logging.basicConfig(level=logging.DEBUG, format=FORMAT, datefmt=DATEFMT)
_logger = logging.getLogger()
logger =_logger
def shellCmd(cmd):
_logger.debug('[running] %s' %
' '.join(["" if x == None else x for x in cmd]))
try:
out = subprocess.check_output(cmd, stderr=subprocess.STDOUT)
except subprocess.CalledProcessError, e:
_logger.debug('CalledProcessError: %s' % str(e))
return (e.returncode, e.output)
except Exception, e:
_logger.error('exception: %s' % str(e))
return (1, str(e))
for m in out.split('\n'):
_logger.debug(m)
return (0, out)
def gitfetch_cmd(config):
os.chdir(config['git_path'])
_logger.debug(os.getcwd())
fetch_cmd = ['git', 'fetch', '-v', config['remote_respo']]
result = 1
(result, out) = shellCmd(fetch_cmd)
if result == 1:
return 1
return (result, out)
def git_log_cmd(config, diff_id):
os.chdir(config['git_path'])
fetch_cmd = ['git', 'log', diff_id]
result = 1
(result, out) = shellCmd(fetch_cmd)
if result == 1:
return 1
return (result, out)
def check_update(config):
update = False
result, out = gitfetch_cmd(config)
if result != 0:
update = False
else:
msg = ''
for line in out.split('\n'):
if line.find(config['remote_respo']) > 0:
branch = line.split('/')[1]
if line.find('up to date') == -1:
update = True
msg = '\n[' + branch + '] ' + \
'分支有更新, commit信息如下\n'
diff_id = line.split()[0]
msg += logby_diffid(config, diff_id)
out = msg
_logger.info(out)
return (result, update, out)
def logby_diffid(config, diff_id):
os.chdir(config['git_path'])
msg = ''
msg += 15 * '=' + 'git log ' + diff_id + 15 * '=' + '\n'
msg += git_log_cmd(config, diff_id)[1]
msg += 50 * '='
return msg
def Notice(config, msg):
content = ''
content += '[Project]: ' + config['Project_name'] + '\n'
content += '[server address]:%s\n' % config['server_address']
content += msg + '\n'
content += '''
=================================================
这是一封自动转发的邮件,用来通知大家服务器是否有更新,
如果有任何问题,请联系智刚。
=================================================
'''
send_mail(config['mailto_list'], config[
'Project_name'] + u"代码有更新", content)