龙空技术网

Python统计gitlab上的代码量

强势围观 133

前言:

眼前看官们对“python代码统计”大致比较着重,兄弟们都需要学习一些“python代码统计”的相关知识。那么小编在网摘上汇集了一些关于“python代码统计””的相关文章,希望兄弟们能喜欢,大家一起来了解一下吧!

年底了,各大公司都要求每个人统计自己今年的代码量。

使用python写了一个统计gitlab上代码量。

首先安装python-gitlab插件。具体代码如下:

#!/usr/bin/pythonimport gitlabimport xlwt#用户git账户的tokenprivate_token = ''#git地址private_host = ''#提交代码的域账户名称1author_email1 = ''#提交代码的域账户名称2author_email2 = ''def getAllProjects(): client = gitlab.Gitlab(private_host,private_token=private_token) projects = client.projects.list(membership=True,all=True) return projectsdef getAllBranchByProject(project): branches = project.branches.list() return branchesdef getCommitByBranch(project,branch): author_commits=[] commits = project.commits.list(all=True,ref_name = branch.name) for commit in commits: committer_email = commit.committer_email title = commit.title message = commit.message if ('Merge' in message) or ('Merge' in title): print('Merge跳过') continue else: if (str(author_email1) and committer_email.find(author_email1) >= 0) or (str(author_email2) and committer_email.find(author_email2) >= 0): author_commits.append(commit) return author_commitsdef getCodeByCommit(commit,project): commit_info = project.commits.get(commit.id) code =commit_info.stats return codedef getAuthorCode(): data=[] projects = getAllProjects() for project in projects: branches = getAllBranchByProject(project) for branch in branches: print('获取工程',project.name,'分支',branch.name,"的提交记录") branchdata = {} branchdata['projectname'] = project.name branchdata['branchename'] = branch.name author_commits = getCommitByBranch(project,branch) codes = [] for commit in author_commits: print('获取提交',commit.id,"的代码量") code = getCodeByCommit(commit,project) codes.append(code) record=calculate(codes) branchdata['commitcount'] = len(author_commits) branchdata['codecount'] = record data.append(branchdata) return datadef writeExcel(excelPath,data): workbook = xlwt.Workbook() #获取第一个sheet页 sheet = workbook.add_sheet('git') row0=['工程名称','分支名称','提交次数','新增代码','删除代码','总计代码'] for i in range(0,len(row0)): sheet.write(0,i,row0[i]) addcount = 0 delcount =0 totalcount = 0 commitcount = 0 for i in range(0,len(data)): recode = data[i] j=0 sheet.write(i+1,j,recode['projectname']) sheet.write(i+1,j+1,recode['branchename']) commitcount +=(int)(recode['commitcount']) sheet.write(i+1,j+2,recode['commitcount']) addcount += (int)(recode['codecount']['additions']) sheet.write(i+1,j+3,recode['codecount']['additions']) delcount +=(int)(recode['codecount']['deletions']) sheet.write(i+1,j+4,recode['codecount']['deletions']) totalcount +=(int)(recode['codecount']['total']) sheet.write(i+1,j+5,recode['codecount']['total']) sheet.write(len(data)+1,2,commitcount) sheet.write(len(data)+1,3,addcount) sheet.write(len(data)+1,4,delcount) sheet.write(len(data)+1,5,totalcount) workbook.save(excelPath)def calculate(data): record ={} addacount =0 deletecount =0 totaolcount =0 for i in data: addacount+= int(i['additions']) deletecount+= int(i['deletions']) totaolcount+= int(i['total']) record['additions'] = addacount record['deletions'] = deletecount record['total'] = totaolcount return recordif __name__ == '__main__': data = getAuthorCode() writeExcel('d:/11.xls',data)


标签: #python代码统计