def load(request): """ 版本列表 :param request: :return: """ client = pysvn.Client() logger.info("load svn") for program in ALL_SVN_LIST: dir = getLocalDir(program) url = client.info(dir).data['url'] logger.info("program:[%s],url:[%s]" % (program, url)) # trunk info = None try: info = BranchInfo.objects().get(programName=program, branchTag="trunk") except: pass if info is None: info = BranchInfo() info.programName = program info.branchTag = "trunk" info.createDate = "999999" info.save() branches = [] branches.extend(getBranches(client, url, "branches")) try: branches.extend(getBranches(client, url, "tags")) except Exception as e: pass for x in branches: info = None try: info = BranchInfo.objects().get(programName=program, branchTag=x) except: pass if info is None: info = BranchInfo() info.programName = program info.branchTag = x customerTag = getCustomerTag(x) customer = Customer.objects(tag=customerTag) if len(customer) > 0: info.customerTag = customerTag info.createDate = getCreateDate(x) info.save() return redirect("/programBranch/list/", locals())
def get_branch_from_url(svn_url): """ 根据SVN链接获取BranchInfo对象 :param svn_url: :return: """ try: arr = None if svn_url.find('/server5/') > 0: arr = svn_url.split(SVN_ROOT + 'server5/')[1].split('/', 1) else: arr = svn_url.split(SVN_ROOT)[1].split('/', 1) program_name = arr[0] branch_tag = arr[1] branch_info = BranchInfo.objects(programName=program_name, branchTag=branch_tag) if len(branch_info) == 0: logger.error( '根据SVN:[url=%s][program_name=%s][branch_tag=%s]获取BranchInfo对象为空!' % (svn_url, program_name, branch_tag)) return None return branch_info[0] except Exception as e: logger.error('根据SVN:[url=%s]获取BranchInfo对象异常![%s]' % (svn_url, getTraceBack())) return None
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 svn_list(request): """ 版本列表 :param request: :return: """ branches = BranchInfo.objects().order_by('-programName') return render_to_response("programBranch/programBranch_list.html", locals(), context_instance=RequestContext(request))
def get_branch_from_program_tag(program_name, branch_tag): """ 根据程序名称和分支获取BranchInfo对象 :param program_name: :param branch_tag: """ branch_info = BranchInfo.objects(programName=program_name, branchTag=branch_tag) if len(branch_info) == 0: return None return branch_info[0]
def get_branch_info(self): try: program_name, branch_tag = self._get_program_name_and_branch_tag() branch_info = BranchInfo.objects(programName=program_name, branchTag=branch_tag) if len(branch_info) == 0: logger.error('根据SVN:[url=%s][program_name=%s][branch_tag=%s]获取BranchInfo对象为空!' % (self.svn_url, program_name, branch_tag)) return None return branch_info[0] except Exception as e: logger.error('根据SVN:[url=%s]获取BranchInfo对象异常![%s][%s]' % (self.svn_url, e.message, getTraceBack())) return None
def get_branch_info(self): try: program_name, branch_tag = self._get_program_name_and_branch_tag() branch_info = BranchInfo.objects(programName=program_name, branchTag=branch_tag) if len(branch_info) == 0: logger.error( '根据SVN:[url=%s][program_name=%s][branch_tag=%s]获取BranchInfo对象为空!' % (self.svn_url, program_name, branch_tag)) return None return branch_info[0] except Exception as e: logger.error('根据SVN:[url=%s]获取BranchInfo对象异常![%s][%s]' % (self.svn_url, e.message, getTraceBack())) return None
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_branch_from_url(svn_url): """ 根据SVN链接获取BranchInfo对象 :param svn_url: :return: """ try: arr = None if svn_url.find('/server5/') > 0: arr = svn_url.split(SVN_ROOT + 'server5/')[1].split('/', 1) else: arr = svn_url.split(SVN_ROOT)[1].split('/', 1) program_name = arr[0] branch_tag = arr[1] branch_info = BranchInfo.objects(programName=program_name, branchTag=branch_tag) if len(branch_info) == 0: logger.error('根据SVN:[url=%s][program_name=%s][branch_tag=%s]获取BranchInfo对象为空!' % (svn_url, program_name, branch_tag)) return None return branch_info[0] except Exception as e: logger.error('根据SVN:[url=%s]获取BranchInfo对象异常![%s]' % (svn_url, getTraceBack())) return None
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 key_create(request): if request.method == "GET": branchs = BranchInfo.objects().order_by('-programName', 'branchTag') program_list = [] tag_dict = {} for branch in branchs: program_name = branch.programName if program_name in tag_dict: tag_dict[program_name].append({ 'id': str(branch.id), 'tag': branch.branchTag }) else: program_list.append(program_name) tag_dict[program_name] = [{'id': str(branch.id), 'tag': branch.branchTag}] program_list_json = json.dumps(program_list) tag_dict_json = json.dumps(tag_dict) return render_to_response("programBranch/key_submit_create.html", locals(), context_instance=RequestContext(request)) elif request.method == 'POST': response = {"success": False, "error": ""} try: # 获取参数 json_str = request.POST.get('json', None) if not json_str: response['error'] = '必要参数为空!' return HttpResponse(json.dumps(response), mimetype="application/json") json_obj = json.loads(json_str) remark = json_obj['remark'] id = json_obj['id'] revision_1 = int(json_obj['revision_1']) revision_2 = json_obj['revision_2'] revision_2 = revision_1 if revision_2 is None else int(revision_2) branch = BranchInfo.objects(pk=id) if not branch: response['error'] = '未找到分支对象!' return HttpResponse(json.dumps(response), mimetype="application/json") branch = branch[0] # 验证是否重叠 exists = KeySubmit.objects(branch=branch, revision_start__lte=revision_1, revision_end__gte=revision_2) if exists: response['error'] = '关键提交重叠!' return HttpResponse(json.dumps(response), mimetype="application/json") key_submit = KeySubmit() key_submit.branch = branch key_submit.revision_start = revision_1 key_submit.revision_end = revision_2 key_submit.remark = remark key_submit.create_time = datetime.datetime.now() key_submit.create_user = User.objects.get(pk=request.user.id) key_submit.save() 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 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 key_create(request): if request.method == "GET": branchs = BranchInfo.objects().order_by('-programName', 'branchTag') program_list = [] tag_dict = {} for branch in branchs: program_name = branch.programName if program_name in tag_dict: tag_dict[program_name].append({ 'id': str(branch.id), 'tag': branch.branchTag }) else: program_list.append(program_name) tag_dict[program_name] = [{ 'id': str(branch.id), 'tag': branch.branchTag }] program_list_json = json.dumps(program_list) tag_dict_json = json.dumps(tag_dict) return render_to_response("programBranch/key_submit_create.html", locals(), context_instance=RequestContext(request)) elif request.method == 'POST': response = {"success": False, "error": ""} try: # 获取参数 json_str = request.POST.get('json', None) if not json_str: response['error'] = '必要参数为空!' return HttpResponse(json.dumps(response), mimetype="application/json") json_obj = json.loads(json_str) remark = json_obj['remark'] id = json_obj['id'] revision_1 = int(json_obj['revision_1']) revision_2 = json_obj['revision_2'] revision_2 = revision_1 if revision_2 is None else int(revision_2) branch = BranchInfo.objects(pk=id) if not branch: response['error'] = '未找到分支对象!' return HttpResponse(json.dumps(response), mimetype="application/json") branch = branch[0] # 验证是否重叠 exists = KeySubmit.objects(branch=branch, revision_start__lte=revision_1, revision_end__gte=revision_2) if exists: response['error'] = '关键提交重叠!' return HttpResponse(json.dumps(response), mimetype="application/json") key_submit = KeySubmit() key_submit.branch = branch key_submit.revision_start = revision_1 key_submit.revision_end = revision_2 key_submit.remark = remark key_submit.create_time = datetime.datetime.now() key_submit.create_user = User.objects.get(pk=request.user.id) key_submit.save() 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 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")