Skip to content

Commit e1e4029

Browse files
author
Cloud-init-user
committed
update
1 parent 945743a commit e1e4029

File tree

1 file changed

+37
-12
lines changed

1 file changed

+37
-12
lines changed

autoBuilder.py

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import shutil
99
import json
1010
from config import *
11-
11+
import subprocess
1212

1313
def get_login(realm, username, may_save):
1414
return True, Config.username, Config.password, True
@@ -44,6 +44,16 @@ def svn_update(project,target_version):
4444

4545
def print_log(msg):
4646
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+
4757
print now,msg;
4858

4959
def check_need_update(project):
@@ -79,8 +89,9 @@ def auto_update():
7989
version = result[1];
8090
if(need_update):
8191
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);#复制生成后的文件到发布目录,主要防止生成时间过长造成发布目录访问受影响
8495
else:
8596
print_log(project_name + ' no avaliable version!');
8697
except Exception,err:
@@ -91,23 +102,38 @@ def build(project):
91102
project_name = project.get_name();
92103
try:
93104
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);
95107
print_log('build project ' + project.get_name() + ' finished');
96108
except Exception,err:
97109
print_log('build project ' + project_name + ' error: ' + str(err));
110+
raise Exception,err;
98111

99112
def copy_to_dist(project):
100113
project_name = project.get_name();
101-
dist_dir = project.get_dist_dir();
114+
dist_dir = project.get_dist_path();
102115
src_dist_dir = project.get_local_code_path() + '/dist';#表示源码build之后的生成目录
103116
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+
104123
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至发布目录
107126
print_log('copy project ' + project_name + ' distribution to ' + dist_dir + ' finished');
108127
except Exception, err:
109128
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!');
111137

112138
def setlocale():
113139
language_code, encoding = locale.getdefaultlocale()
@@ -117,15 +143,14 @@ def setlocale():
117143
encoding = 'UTF-8'
118144
if encoding.lower() == 'utf':
119145
encoding = 'UTF-8'
146+
locale.setlocale( locale.LC_ALL, '%s.%s' % (language_code, encoding));
147+
120148

121-
locale.setlocale( locale.LC_ALL, '%s.%s' % (language_code, encoding))
122149

123150
if "__main__" == __name__:
124151
setlocale();
125152
client = pysvn.Client();
126153
client.callback_get_login = get_login;
127154
ensure_checkout();
128-
# check_out();
129155
auto_update();
130-
# print_log(os.popen('ls').readlines());
131-
156+

0 commit comments

Comments
 (0)