|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf8 -*- |
| 3 | + |
| 4 | +import pysvn |
| 5 | +import time |
| 6 | +import locale |
| 7 | +import os |
| 8 | +import shutil |
| 9 | +import json |
| 10 | +from env import * |
| 11 | + |
| 12 | + |
| 13 | +def get_login(realm, username, may_save): |
| 14 | + return True, Config.username, Config.password, True |
| 15 | + |
| 16 | +def get_local_version(project): |
| 17 | + entry = client.info(project.get_local_code_path()); |
| 18 | + return entry.commit_revision.number; |
| 19 | + |
| 20 | +def get_remote_need_build_version(project,current_version): |
| 21 | + #只获取提交日志中包含 “#build#” 关键字的版本号,该关键字表示该版本需要进行build |
| 22 | + keyword = '#build#'; |
| 23 | + revision = pysvn.Revision(pysvn.opt_revision_kind.number,current_version); |
| 24 | + log_list = client.log(project.get_svn_path(),revision_end = revision); |
| 25 | + validRevision = current_version; |
| 26 | + |
| 27 | + for log in log_list: |
| 28 | + log_revision = log.revision.number; |
| 29 | + if keyword in log.message and log_revision > validRevision: |
| 30 | + print_log('check buildable version:' + str(log_revision) + ',log:' + log.message); |
| 31 | + validRevision = log_revision; |
| 32 | + return validRevision; |
| 33 | + |
| 34 | +def svn_update(project,target_version): |
| 35 | + try: |
| 36 | + code_path = project.get_local_code_path(); |
| 37 | + client.cleanup(code_path); |
| 38 | + client.revert(code_path,True); |
| 39 | + revision = pysvn.Revision(pysvn.opt_revision_kind.number,target_version); |
| 40 | + client.update(code_path,revision = revision); |
| 41 | + print_log('update ' + project.get_name() + ' successfully'); |
| 42 | + except Exception,err: |
| 43 | + print_log('update ' + project.get_name() + ' error:' + str(err)); |
| 44 | + |
| 45 | +def print_log(msg): |
| 46 | + now = time.strftime("%Y-%m-%d %H:%M:%S"); |
| 47 | + print now,msg; |
| 48 | + |
| 49 | +def check_need_update(project): |
| 50 | + current_version = get_local_version(project); |
| 51 | + latest_version = get_remote_need_build_version(project,current_version);#仅查询svn上需要build的最新版本号,防止无效获取并build |
| 52 | + return latest_version > current_version,latest_version; |
| 53 | + |
| 54 | +def check_out(project): |
| 55 | + ret = client.checkout(project.get_svn_path(), project.get_local_code_path()); |
| 56 | + |
| 57 | +def ensure_checkout(): |
| 58 | + for project in Config.projects: |
| 59 | + project_name = project.get_name(); |
| 60 | + local_code_dir = project.get_local_code_path(); |
| 61 | + exist = os.path.exists(local_code_dir);#判断是否存在该目录,因为如果存在目录则说明已经成功检出过 |
| 62 | + if not exist: |
| 63 | + try: |
| 64 | + print_log('check out ' + project_name + ' start'); |
| 65 | + check_out(project); |
| 66 | + print_log('check out ' + project_name + ' successfully'); |
| 67 | + except Exception,err: |
| 68 | + print_log('check out ' + project_name + ' failed:' + str(err)); |
| 69 | + shutil.rmtree(local_code_dir);#检出失败时,递归删除该目录,方便下次再次检出 |
| 70 | + raise Exception,err;#重新抛出该异常,中断后续操作,保证只有在检出成功的情况下才继续进行 |
| 71 | + |
| 72 | +def auto_update(): |
| 73 | + while True: |
| 74 | + for project in Config.projects: |
| 75 | + project_name = project.get_name(); |
| 76 | + try: |
| 77 | + result = check_need_update(project); |
| 78 | + need_update = result[0]; |
| 79 | + version = result[1]; |
| 80 | + if(need_update): |
| 81 | + print_log(project_name +' new version detected:' + str(version)); |
| 82 | + svn_update(project,version); |
| 83 | + build(project); |
| 84 | + else: |
| 85 | + print_log(project_name + ' no avaliable version!'); |
| 86 | + except Exception,err: |
| 87 | + print_log(project_name + ' auto update error:' + str(err)); |
| 88 | + time.sleep(Config.check_interval_seconds); |
| 89 | + |
| 90 | +def build(project): |
| 91 | + project_name = project.get_name(); |
| 92 | + try: |
| 93 | + print_log('build project ' + project.get_name() + ' start'); |
| 94 | + print_log(os.popen('npm run build --prefix ' + project.get_local_code_path()).readlines()); |
| 95 | + print_log('build project ' + project.get_name() + ' end'); |
| 96 | + except Exception,err: |
| 97 | + print_log('build project ' + project_name + ' error: ' + str(err)); |
| 98 | + |
| 99 | + |
| 100 | +def setlocale(): |
| 101 | + language_code, encoding = locale.getdefaultlocale() |
| 102 | + if language_code is None: |
| 103 | + language_code = 'en_US' |
| 104 | + if encoding is None: |
| 105 | + encoding = 'UTF-8' |
| 106 | + if encoding.lower() == 'utf': |
| 107 | + encoding = 'UTF-8' |
| 108 | + |
| 109 | + locale.setlocale( locale.LC_ALL, '%s.%s' % (language_code, encoding)) |
| 110 | + |
| 111 | +if "__main__" == __name__: |
| 112 | + setlocale(); |
| 113 | + client = pysvn.Client(); |
| 114 | + client.callback_get_login = get_login; |
| 115 | + ensure_checkout(); |
| 116 | + # check_out(); |
| 117 | + auto_update(); |
| 118 | + # print_log(os.popen('ls').readlines()); |
| 119 | + |
0 commit comments