之前是用ruby折腾的,现在学习python,然后原来为一个项目做的,现在为多项目,
现在记录一下。
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
|
#!/usr/bin/python
# -*- coding:utf-8 -*-
import
re
import
sys
import
os
import
filecmp
import
smtplib
import
tempfile
import
linecache
import
datetime
from
email.mime.text
import
MIMEText
svn_ser
=
{
"AAA"
:
"http://bbb.163.net/svn/AAA"
,
"BBB"
:
"http://aaa.163.net/svn/BBB"
}
svn_rel_ser
=
''
svn_rel
=
''
svn_rel_dir
=
"/branches"
svn_tmp_dir
=
tempfile.mkdtemp()
tar_list_name
=
svn_tmp_dir
+
"/tar.list"
maillist
=
"/home/"
+
os.getlogin().replace(
'.'
,'')
+
"/.maillist.txt"
svn_name, svn_comment
=
'
', '
'
old_dir
=
os.getcwd()
run_user
=
[
"li.li"
,
"se.ni"
,
"163"
]
def
check_user():
if
os.getlogin()
in
run_user:
if
not
os.path.exists(maillist):
print
"Mail List File Not Exist!"
print
"Please VIM /home/%s/.maillist.txt"
%
os.getlogin().replace(
'.'
,'')
sys.exit(
1
)
else
:
print
"Permission Denied!"
sys.exit(
1
)
def
send_mail(tar_name, diff):
ci_time
=
datetime.datetime.now().strftime(
'%Y-%m-%d %H:%M:%S'
)
mail_host
=
"smtp.163.com"
mail_user
=
"test@163.com"
mail_s
=
"abcabc"
sub
=
"SVN_Release"
content
=
'''
Module: RTL
Comment: %s
Version: %s
TarName: %s
TarSize: %s
CiTime: %s
============= DiffSVN: ======================
%s
==========================================
'''
%
(svn_comment, re.findall('[
0
-
9
]{
3
}', tar_name)[
0
], tar_name, os.path.getsize(tar_name), ci_time, diff )
f
=
open
(maillist,
'r'
)
for
i
in
f:
if
not
re.match(
'^#'
,i):
me
=
"SVN"
+
"<"
+
mail_user
+
">"
msg
=
MIMEText(content,_subtype
=
'plain'
,_charset
=
'gb2312'
)
msg[
'Subject'
]
=
sub
msg[
'From'
]
=
me
msg[
'To'
]
=
i
try
:
server
=
smtplib.SMTP()
server.connect(mail_host)
server.login(mail_user,mail_s)
server.sendmail(me, i , msg.as_string())
server.close()
except
Exception, e:
print
"Send to %s ERROR"
%
i
f.close()
def
usage():
tmp
=
'''
usage
script project_name [comment]
e.g. script AAA "Test by me"
script aaa
'''
print
tmp
def
co_check(co_ser):
os.chdir(svn_tmp_dir)
os.system(
"svn co %s/Code/RTL 1>/dev/null 2>&1"
%
co_ser)
def
create_tar_list():
file_path
=
[]
tar_list
=
[]
for
filepath,pathname,filename
in
os.walk(svn_tmp_dir):
#filepath fullpath
#pathname fullpath child dir
#filename child dir file
if
os.path.exists(filepath
+
"/"
+
filepath.split(
'/'
)[
-
1
]
+
".f"
):
file_path.append(filepath
+
"/"
+
filepath.split(
'/'
)[
-
1
]
+
'.f'
)
tmp_line
=
[]
true_path
=
''
for
i
in
file_path:
f
=
open
(i,
'r'
)
for
line
in
f:
if
re.search(r
'^\/\/'
,line.strip()):
next
elif
re.search(r
'^\.|^\w+'
,line.strip()):
if
re.search(r
'\/\/'
,line):
tmp_line
=
line.strip().split(
'//'
)[
0
].rstrip().split(
'/'
)
else
:
tmp_line
=
line.strip().split(
'/'
)
if
'.'
in
tmp_line:
del
tmp_line[tmp_line.index(
'.'
)]
if
'..'
in
tmp_line:
del
tmp_line[tmp_line.index(
'..'
)]
ture_path
=
os.path.dirname(i)
+
"/"
+
'/'
.join(tmp_line)
if
os.path.isfile(ture_path):
tar_list.append(
"."
+
ture_path.split(svn_tmp_dir)[
-
1
])
f.close()
open
( tar_list_name,
'w'
).write(
'\n'
.join(tar_list))
def
tar_file_name():
os.system(
'cd %s && mkdir rel tmp && cd rel && svn co %s 1>/dev/null 2>&1'
%
(svn_tmp_dir, svn_rel_ser))
#can not find tar?
tmp_name
=
os.popen(
"cd %s && file $(svn log -v | grep 'tar.bz2' | awk '{print \".\"$NF}') | grep -v ERROR"
%
(svn_tmp_dir
+
"/rel/"
+
svn_rel )).read()
if
tmp_name.strip()
=
=
'':
if
svn_name
=
=
"AAA"
:
return
[svn_name
+
"_Release.001.tar.bz2"
]
else
:
return
[svn_name
+
"_001.tar.bz2"
]
else
:
tmp_name
=
tmp_name.split()[
0
].split(
'/'
)[
-
1
].strip()[
0
:
-
1
]
if
svn_name
=
=
'AAA'
:
tmp_no
=
int
(tmp_name.split(
'.'
)[
-
3
])
+
1
new_name
=
svn_name
+
"_Release.%03d.tar.bz2"
%
tmp_no
return
[new_name,tmp_name.split(
'/'
)[
-
1
]]
else
:
tmp_no
=
int
(tmp_name.split(
'_'
)[
-
1
][
0
:
3
])
+
1
new_name
=
svn_name
+
"_%03d.tar.bz2"
%
tmp_no
return
[new_name,tmp_name.split(
'/'
)[
-
1
]]
def
diff_svn(tar_name):
diff_tmp
=
[]
tar_path
=
"./rel"
+
os.popen(
"cd %s/rel && find . -name %s"
%
(svn_tmp_dir, tar_name)).read().strip()[
1
:]
os.system(
"cd %s && tar xf %s -C %s"
%
(svn_tmp_dir, tar_path, svn_tmp_dir
+
"/tmp"
))
f
=
open
(tar_list_name,
'r'
)
for
i
in
f:
i
=
i.strip()
filename
=
svn_tmp_dir
+
"/tmp"
+
i[
1
:]
if
os.path.isfile(svn_tmp_dir
+
"/tmp"
+
i[
1
:]):
#exist file ---> cmp
if
not
filecmp.
cmp
(svn_tmp_dir
+
i[
1
:],svn_tmp_dir
+
"/tmp"
+
i[
1
:]):
diff_tmp.append(os.path.dirname(i.strip()))
else
:
diff_tmp.append(os.path.dirname(i.strip()))
f.close()
return
list
(
set
(diff_tmp))
#--------------------------
if
len
(sys.argv) <
=
1
:
usage()
sys.exit(
1
)
else
:
try
:
svn_name, svn_comment
=
str
.upper(sys.argv[
1
]), sys.argv[
2
]
except
:
svn_name, svn_comment
=
str
.upper(sys.argv[
1
]),
"Auto Release"
if
not
svn_ser.has_key(svn_name):
usage()
sys.exit(
1
)
if
svn_name
=
=
'AAA'
:
svn_rel_ser, svn_rel
=
svn_ser[svn_name]
+
"-REL"
, svn_name
+
"-REL"
else
:
svn_rel_ser, svn_rel
=
svn_ser[svn_name]
+
"_REL"
, svn_name
+
"_REL"
svn_rel_dir
=
svn_tmp_dir
+
"/rel/"
+
svn_rel
+
"/branches"
check_user()
co_check( svn_ser[svn_name])
create_tar_list()
tar_name
=
tar_file_name()
if
"001"
in
tar_name[
0
]:
diff
=
''
else
:
diff
=
diff_svn(tar_name[
1
])
if
len
(diff)
=
=
0
:
diff
=
''
else
:
diff
=
"\n"
.join(diff)
# svn ci
if
raw_input
(
"Are You Sure Release? [y|n] \n "
).lower().strip()
=
=
"y"
:
os.system(
"cd %s && tar -jc -T tar.list -f %s"
%
(svn_tmp_dir, tar_name[
0
]))
os.chdir(svn_rel_dir)
os.system(
"mv ../../../%s ."
%
tar_name[
0
])
os.system(
"svn add %s"
%
tar_name[
0
])
os.system(
"svn ci %s -m '%s'"
%
(tar_name[
0
], svn_comment))
# send mail
send_mail(tar_name[
0
],diff)
print
"SVN Release OK"
else
:
pass
# delete temp file
os.chdir(
'/tmp'
)
os.system(
'rm -rf $(ls -1 /tmp | grep -E "tmp[a-zA-Z0-9]+")'
)
sys.exit(
0
)
|
本文转自 nonono11 51CTO博客,原文链接:http://blog.51cto.com/abian/1587570,如需转载请自行联系原作者