def get_portal_status(portal_pack): svn_utils = SvnUtils() svn_url = portal_pack.svn_url portal_is_old = False current_revision = svn_utils.get_current_svn_revision(svn_url) previous_revision = int(portal_pack.svn_version) if current_revision > previous_revision: portal_is_old = True machine_json = {'portal_svn_info': []} portal_json = { 'svn_url': svn_url, 'previous_revision': previous_revision, 'current_revision': current_revision, 'is_old': portal_is_old, 'key_submits': [] } portal_key_submits = svn_utils.get_key_submit_by_revision( svn_url, previous_revision, revision_start=current_revision) if portal_key_submits: for key_submit in portal_key_submits: d = { 'start': key_submit.revision_start, 'end': key_submit.revision_end, 'remark': key_submit.remark } portal_json['key_submits'].append(d) machine_json['portal_svn_info'].append(portal_json) return machine_json['portal_svn_info'], portal_is_old, portal_key_submits
def check_brach_info(self): # 获取portal所有分支对象 branchs = BranchInfo.objects() svn_utils = SvnUtils() exist = 0 unknown = 0 not_exist = 0 for item in branchs: svn_url = SvnUtils.get_svn_url(item.programName, item.branchTag) flag = None try: flag = svn_utils.svn_url_exists(svn_url) except Exception as e: logger.error(getTraceBack()) logger.error('校验BranchInfo[id=%s][url=%s]异常![%s]' % (str(item.id), svn_url, e.message)) item.is_exist = flag item.save() if flag is True: exist += 1 elif flag is False: not_exist += 1 else: unknown += 1 logger.info('校验BranchInfo对象成功!共计[%s]条!有效路径[%s]条!无效路径[%s]条!未知路径[%s]条!' % (len(branchs), exist, not_exist, unknown))
def sync_svn_info(self): svn_dict = {} # 获取portal所有分支对象 branchs = BranchInfo.objects(programName__in=[TT_SERVICE, QUOTER, XT_TRADE_CLIENT, TT_MANAGEMENT_PORTAL, DEAMON, UPDATER]) client = pysvn.Client() revision_start = pysvn.Revision(pysvn.opt_revision_kind.head) revision_end = pysvn.Revision(pysvn.opt_revision_kind.number, 0) limit = 1 for item in branchs: svn_url = SvnUtils.get_svn_url(item.programName, item.branchTag) try: # get last svn log data = client.log(svn_url, revision_start=revision_start, revision_end=revision_end, limit=limit)[0].data svn_revision = data['revision'].number svn_author = data['author'] svn_message = data.get('message', '') svn_commit_time = time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime(data['date'])) svn_dict[svn_url] = [str(svn_revision), '%s %s %s -> %s' % (svn_revision, svn_commit_time, svn_author, svn_message)] except Exception as e: logger.error(getTraceBack()) logger.error('同步SVN信息[url:%s]异常![%s]' % (svn_url, e.message)) global _svn_dict _svn_dict = svn_dict logger.info('同步SVN信息成功!共计[%s]条!' % len(_svn_dict))
def get_serve_status(svn_info): server_is_old = False if svn_info: svn_info = json.loads(svn_info) svn_utils = SvnUtils() machine_json = {'server_svn_info': []} for server_svn_url in svn_info: value = svn_info[server_svn_url] server_previous_revision = value[1] server_current_revision = svn_utils.get_current_svn_revision( server_svn_url) branch_is_old = False if server_current_revision > server_previous_revision: server_is_old = True branch_is_old = True server_json = { 'server_svn_url': server_svn_url, 'server_previous_revision': server_previous_revision, 'server_current_revision': server_current_revision, 'is_old': branch_is_old, 'key_submits': [] } server_key_submits = svn_utils.get_key_submit_by_revision( server_svn_url, server_previous_revision, revision_start=server_current_revision) for server_key_submit in server_key_submits: server_json['key_submits'].append({ 'start': server_key_submit.revision_start, 'end': server_key_submit.revision_end, 'remark': server_key_submit.remark }) machine_json['server_svn_info'].append(server_json) return machine_json['server_svn_info'], server_is_old, server_key_submits
def sync_svn_info(self): svn_dict = {} # 获取portal所有分支对象 branchs = BranchInfo.objects(programName__in=[ TT_SERVICE, QUOTER, XT_TRADE_CLIENT, TT_MANAGEMENT_PORTAL, DEAMON, UPDATER ]) client = pysvn.Client() revision_start = pysvn.Revision(pysvn.opt_revision_kind.head) revision_end = pysvn.Revision(pysvn.opt_revision_kind.number, 0) limit = 1 for item in branchs: svn_url = SvnUtils.get_svn_url(item.programName, item.branchTag) try: # get last svn log data = client.log(svn_url, revision_start=revision_start, revision_end=revision_end, limit=limit)[0].data svn_revision = data['revision'].number svn_author = data['author'] svn_message = data.get('message', '') svn_commit_time = time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime(data['date'])) svn_dict[svn_url] = [ str(svn_revision), '%s %s %s -> %s' % (svn_revision, svn_commit_time, svn_author, svn_message) ] except Exception as e: logger.error(getTraceBack()) logger.error('同步SVN信息[url:%s]异常![%s]' % (svn_url, e.message)) global _svn_dict _svn_dict = svn_dict logger.info('同步SVN信息成功!共计[%s]条!' % len(_svn_dict))
def svn_list_read(request): response = {"success": False, "error": ""} if request.method == "GET": try: id = request.GET.get('id', None) revision_start_number = request.GET.get('revision_start', None) revision_end_number = request.GET.get('revision_end', None) limit = int(request.GET.get('limit', 30)) if not id: response['error'] = '你要参数为空!' return HttpResponse(json.dumps(response), mimetype="application/json") branch = BranchInfo.objects(pk=id) if not branch: response['error'] = '未找到分支对象!' return HttpResponse(json.dumps(response), mimetype="application/json") branch = branch[0] # 读取最近50条更新 svn_url = SvnUtils.get_svn_url(branch.programName, branch.branchTag) client = pysvn.Client() revision_start = pysvn.Revision(pysvn.opt_revision_kind.head) if revision_start_number: revision_start = pysvn.Revision(pysvn.opt_revision_kind.number, int(revision_start_number)) limit += 1 revision_end = pysvn.Revision(pysvn.opt_revision_kind.number, 0 if not revision_end_number else revision_end_number) svn_logs = client.log(svn_url, revision_start=revision_start, revision_end=revision_end, limit=limit) if revision_start_number: # 过滤掉页面已有数据 svn_logs = svn_logs[1:] svn_log_json = convert_svn_log_json(svn_logs) key_submit_dict = {} if svn_logs: svn_revision_start = svn_logs[0].data['revision'].number svn_revision_end = svn_logs[len(svn_logs) - 1].data['revision'].number # 查询已有关键提交,标记之 key_submits = KeySubmit.objects(Q(branch=branch) & (Q(revision_end__lte=svn_revision_start) & Q(revision_end__gte=svn_revision_end)) | (Q(revision_start__lte=svn_revision_start) & Q(revision_start__gte=svn_revision_end))).order_by('-revision_start') for item in key_submits: revision_start_item = item.revision_start revision_end_item = item.revision_end # 版本号开始在区间内 if svn_revision_start >= revision_start_item >= svn_revision_end: result = {'type': ['start'], 'remark': item.remark, 'create_user': item.create_user.username, 'id': str(item.id), 'time': item.create_time.strftime('%Y-%m-%d %H:%M:%S')} key_submit_dict[revision_start_item] = result # 版本号结束在区间内 if svn_revision_start >= revision_end_item >= svn_revision_end: result = {'type': ['end'], 'remark': item.remark, 'create_user': item.create_user.username, 'id': str(item.id), 'time': item.create_time.strftime('%Y-%m-%d %H:%M:%S')} if revision_end_item in key_submit_dict: key_submit_dict[revision_end_item]['type'].append('end') else: key_submit_dict[revision_end_item] = result response['data'] = svn_log_json response['key_submit'] = key_submit_dict response['success'] = True return HttpResponse(json.dumps(response), mimetype="application/json") except Exception as e: response["error"] = "程序异常![%s]" % str(e) logger.error(response["error"] + getTraceBack()) return HttpResponse(json.dumps(response), mimetype="application/json") else: response['error'] = '请使用Http-GET方式请求!' return HttpResponse(json.dumps(response), mimetype="application/json")
def operate(request): """ SVN相关操作 :param request: :return: """ response = {"success": False, "error": ""} if request.method == "POST": try: # 获取参数 cmd = request.POST.get('cmd', None) id = request.POST.get('id', None) if not cmd or not id: response["error"] = "必要参数为空!" return HttpResponse(json.dumps(response), mimetype="application/json") branch = BranchInfo.objects(pk=id) if len(branch) == 0: response["error"] = "未找到对象!" return HttpResponse(json.dumps(response), mimetype="application/json") branch = branch[0] path = getLocalDir(branch.programName) + "/" + branch.branchTag # 直接删除URL,无须变更操作目录 if cmd != 'delete': os.chdir(path) if cmd == 'update': p = Popen(["svn", "up"], stdin=PIPE, stdout=PIPE, stderr=PIPE) outStr, errorStr = p.communicate() if len(errorStr) > 0: response["error"] = '更新失败![%s]' % errorStr return HttpResponse(json.dumps(response), mimetype="application/json") elif cmd == 'clean': p = Popen(["make", "clean"], stdin=PIPE, stdout=PIPE, stderr=PIPE) p.wait() elif cmd == 'compile': p = Popen(["make", "-j8", "all"], stdin=PIPE, stdout=PIPE, stderr=PIPE) outStr, errorStr = p.communicate() if len(errorStr) > 0: response["error"] = '编译失败![%s]' % errorStr return HttpResponse(json.dumps(response), mimetype="application/json") elif cmd == 'delete': svn_url = SvnUtils.get_svn_url(branch.programName, branch.branchTag) logger.info('删除目录 : [%s]!' % svn_url) if SvnUtils().svn_url_exists(svn_url): p = Popen(["svn", "delete", "-m", "%s delete %s" % (User.objects.get(pk=request.user.id).username, svn_url), svn_url], stdin=PIPE, stdout=PIPE, stderr=PIPE) outStr, errorStr = p.communicate() if len(errorStr.strip()) > 0 and not str(errorStr).startswith('svn: E160013:'): response["error"] = '删除失败![%s]' % errorStr return HttpResponse(json.dumps(response, ensure_ascii=False), mimetype="application/json") branch.delete() response["id"] = id else: response["error"] = "限制操作!" return HttpResponse(json.dumps(response), mimetype="application/json") response["success"] = True response["error"] = "执行成功!" return HttpResponse(json.dumps(response), mimetype="application/json") except Exception, e: response["error"] = "系统异常![%s]" % str(e) logger.error(response["error"] + getTraceBack()) return HttpResponse(json.dumps(response), mimetype="application/json")
def svn_list_read(request): response = {"success": False, "error": ""} if request.method == "GET": try: id = request.GET.get('id', None) revision_start_number = request.GET.get('revision_start', None) revision_end_number = request.GET.get('revision_end', None) limit = int(request.GET.get('limit', 30)) if not id: response['error'] = '你要参数为空!' return HttpResponse(json.dumps(response), mimetype="application/json") branch = BranchInfo.objects(pk=id) if not branch: response['error'] = '未找到分支对象!' return HttpResponse(json.dumps(response), mimetype="application/json") branch = branch[0] # 读取最近50条更新 svn_url = SvnUtils.get_svn_url(branch.programName, branch.branchTag) client = pysvn.Client() revision_start = pysvn.Revision(pysvn.opt_revision_kind.head) if revision_start_number: revision_start = pysvn.Revision(pysvn.opt_revision_kind.number, int(revision_start_number)) limit += 1 revision_end = pysvn.Revision( pysvn.opt_revision_kind.number, 0 if not revision_end_number else revision_end_number) svn_logs = client.log(svn_url, revision_start=revision_start, revision_end=revision_end, limit=limit) if revision_start_number: # 过滤掉页面已有数据 svn_logs = svn_logs[1:] svn_log_json = convert_svn_log_json(svn_logs) key_submit_dict = {} if svn_logs: svn_revision_start = svn_logs[0].data['revision'].number svn_revision_end = svn_logs[len(svn_logs) - 1].data['revision'].number # 查询已有关键提交,标记之 key_submits = KeySubmit.objects( Q(branch=branch) & (Q(revision_end__lte=svn_revision_start) & Q(revision_end__gte=svn_revision_end)) | (Q(revision_start__lte=svn_revision_start) & Q(revision_start__gte=svn_revision_end))).order_by( '-revision_start') for item in key_submits: revision_start_item = item.revision_start revision_end_item = item.revision_end # 版本号开始在区间内 if svn_revision_start >= revision_start_item >= svn_revision_end: result = { 'type': ['start'], 'remark': item.remark, 'create_user': item.create_user.username, 'id': str(item.id), 'time': item.create_time.strftime('%Y-%m-%d %H:%M:%S') } key_submit_dict[revision_start_item] = result # 版本号结束在区间内 if svn_revision_start >= revision_end_item >= svn_revision_end: result = { 'type': ['end'], 'remark': item.remark, 'create_user': item.create_user.username, 'id': str(item.id), 'time': item.create_time.strftime('%Y-%m-%d %H:%M:%S') } if revision_end_item in key_submit_dict: key_submit_dict[revision_end_item]['type'].append( 'end') else: key_submit_dict[revision_end_item] = result response['data'] = svn_log_json response['key_submit'] = key_submit_dict response['success'] = True return HttpResponse(json.dumps(response), mimetype="application/json") except Exception as e: response["error"] = "程序异常![%s]" % str(e) logger.error(response["error"] + getTraceBack()) return HttpResponse(json.dumps(response), mimetype="application/json") else: response['error'] = '请使用Http-GET方式请求!' return HttpResponse(json.dumps(response), mimetype="application/json")
def operate(request): """ SVN相关操作 :param request: :return: """ response = {"success": False, "error": ""} if request.method == "POST": try: # 获取参数 cmd = request.POST.get('cmd', None) id = request.POST.get('id', None) if not cmd or not id: response["error"] = "必要参数为空!" return HttpResponse(json.dumps(response), mimetype="application/json") branch = BranchInfo.objects(pk=id) if len(branch) == 0: response["error"] = "未找到对象!" return HttpResponse(json.dumps(response), mimetype="application/json") branch = branch[0] path = getLocalDir(branch.programName) + "/" + branch.branchTag # 直接删除URL,无须变更操作目录 if cmd != 'delete': os.chdir(path) if cmd == 'update': p = Popen(["svn", "up"], stdin=PIPE, stdout=PIPE, stderr=PIPE) outStr, errorStr = p.communicate() if len(errorStr) > 0: response["error"] = '更新失败![%s]' % errorStr return HttpResponse(json.dumps(response), mimetype="application/json") elif cmd == 'clean': p = Popen(["make", "clean"], stdin=PIPE, stdout=PIPE, stderr=PIPE) p.wait() elif cmd == 'compile': p = Popen(["make", "-j8", "all"], stdin=PIPE, stdout=PIPE, stderr=PIPE) outStr, errorStr = p.communicate() if len(errorStr) > 0: response["error"] = '编译失败![%s]' % errorStr return HttpResponse(json.dumps(response), mimetype="application/json") elif cmd == 'delete': svn_url = SvnUtils.get_svn_url(branch.programName, branch.branchTag) logger.info('删除目录 : [%s]!' % svn_url) if SvnUtils().svn_url_exists(svn_url): p = Popen([ "svn", "delete", "-m", "%s delete %s" % (User.objects.get(pk=request.user.id).username, svn_url), svn_url ], stdin=PIPE, stdout=PIPE, stderr=PIPE) outStr, errorStr = p.communicate() if len(errorStr.strip()) > 0 and not str( errorStr).startswith('svn: E160013:'): response["error"] = '删除失败![%s]' % errorStr return HttpResponse(json.dumps(response, ensure_ascii=False), mimetype="application/json") branch.delete() response["id"] = id else: response["error"] = "限制操作!" return HttpResponse(json.dumps(response), mimetype="application/json") response["success"] = True response["error"] = "执行成功!" return HttpResponse(json.dumps(response), mimetype="application/json") except Exception, e: response["error"] = "系统异常![%s]" % str(e) logger.error(response["error"] + getTraceBack()) return HttpResponse(json.dumps(response), mimetype="application/json")