8
8
import shutil
9
9
import json
10
10
from config import *
11
-
11
+ import subprocess
12
12
13
13
def get_login (realm , username , may_save ):
14
14
return True , Config .username , Config .password , True
@@ -44,6 +44,16 @@ def svn_update(project,target_version):
44
44
45
45
def print_log (msg ):
46
46
now = time .strftime ("%Y-%m-%d %H:%M:%S" );
47
+ date = time .strftime ('%Y-%m-%d' );
48
+ try :
49
+ if not os .path .exists (Config .log_dir ):
50
+ os .makedirs (Config .log_dir );
51
+ fo = open (Config .log_dir + 'log.txt' , 'a' );
52
+ fo .write (now + ' ' + str (msg ) + '\n ' );
53
+ fo .close ();
54
+ except Exception ,err :
55
+ print now ,'write log file error:' + str (err );
56
+
47
57
print now ,msg ;
48
58
49
59
def check_need_update (project ):
@@ -79,8 +89,9 @@ def auto_update():
79
89
version = result [1 ];
80
90
if (need_update ):
81
91
print_log (project_name + ' new version detected:' + str (version ));
82
- svn_update (project ,version );
83
- build (project );
92
+ svn_update (project ,version );#更新该目录至指定svn版本
93
+ build (project );#build 该项目
94
+ copy_to_dist (project );#复制生成后的文件到发布目录,主要防止生成时间过长造成发布目录访问受影响
84
95
else :
85
96
print_log (project_name + ' no avaliable version!' );
86
97
except Exception ,err :
@@ -91,23 +102,38 @@ def build(project):
91
102
project_name = project .get_name ();
92
103
try :
93
104
print_log ('build project ' + project .get_name () + ' start' );
94
- print_log (os .popen ('npm run build --prefix ' + project .get_local_code_path ()).readlines ());
105
+ scripts = 'npm run build --prefix ' + project .get_local_code_path ();
106
+ run_shell_scripts (scripts );
95
107
print_log ('build project ' + project .get_name () + ' finished' );
96
108
except Exception ,err :
97
109
print_log ('build project ' + project_name + ' error: ' + str (err ));
110
+ raise Exception ,err ;
98
111
99
112
def copy_to_dist (project ):
100
113
project_name = project .get_name ();
101
- dist_dir = project .get_dist_dir ();
114
+ dist_dir = project .get_dist_path ();
102
115
src_dist_dir = project .get_local_code_path () + '/dist' ;#表示源码build之后的生成目录
103
116
try :
117
+ scripts = '' ;
118
+ if os .path .exists (dist_dir ):
119
+ print_log ('delete directory ' + dist_dir );
120
+ scripts = 'rm -r ' + dist_dir ;
121
+ run_shell_scripts (scripts ); #删除发布目录
122
+
104
123
print_log ('copy project ' + project_name + ' distribution to ' + dist_dir + ' start' );
105
- print_log ( os . popen ( 'rm -r ' + dist_dir ). readlines ()); #删除发布目录
106
- print_log ( os . popen ( 'cp -r ' + src_dist_dir + ' ' + dist_dir ). readlines () ); #拷贝源码目录中的dist至发布目录
124
+ scripts = 'cp -r ' + src_dist_dir + ' ' + dist_dir ;
125
+ run_shell_scripts ( scripts ); #拷贝源码目录中的dist至发布目录
107
126
print_log ('copy project ' + project_name + ' distribution to ' + dist_dir + ' finished' );
108
127
except Exception , err :
109
128
print_log ('copy project ' + project_name + ' distribution to ' + dist_dir + ' error: ' + str (err ));
110
-
129
+
130
+ def run_shell_scripts (scripts ):
131
+ p = subprocess .Popen (scripts ,shell = True ,stdout = subprocess .PIPE );
132
+ out = p .communicate ();
133
+ print_log (out );
134
+ if (p .returncode != 0 ):
135
+ raise Exception ('run "' + scripts + '" error! Exit code is ' + str (p .returncode ));
136
+ print_log ('run "' + scripts + '" successfully!' );
111
137
112
138
def setlocale ():
113
139
language_code , encoding = locale .getdefaultlocale ()
@@ -117,15 +143,14 @@ def setlocale():
117
143
encoding = 'UTF-8'
118
144
if encoding .lower () == 'utf' :
119
145
encoding = 'UTF-8'
146
+ locale .setlocale ( locale .LC_ALL , '%s.%s' % (language_code , encoding ));
147
+
120
148
121
- locale .setlocale ( locale .LC_ALL , '%s.%s' % (language_code , encoding ))
122
149
123
150
if "__main__" == __name__ :
124
151
setlocale ();
125
152
client = pysvn .Client ();
126
153
client .callback_get_login = get_login ;
127
154
ensure_checkout ();
128
- # check_out();
129
155
auto_update ();
130
- # print_log(os.popen('ls').readlines());
131
-
156
+
0 commit comments