Python實(shí)現(xiàn)一個(gè)Git日志統(tǒng)計(jì)分析的小工具
前言
本文介紹的是利用Python實(shí)現(xiàn)的一個(gè)小工具,用于分析Git commit log,獲得Git Project每個(gè)成員的簡(jiǎn)單行為數(shù)據(jù)。
Warning:代碼量不能代表程序員能力水平!
啟動(dòng)參數(shù)
共5個(gè)。
- Repo地址
- Commit 起始日期
- Commit 結(jié)束日期
- Git倉(cāng)庫(kù)子目錄
- 統(tǒng)計(jì)分析結(jié)果CSV文件目標(biāo)路徑
exec_git
Git Log命令:
git -C {} log --since={} --until={} --pretty=tformat:%ae --shortstat --no-merges -- {} > {}
填入?yún)?shù),調(diào)用系統(tǒng)命令'os.system()',輸出結(jié)果至本地臨時(shí)文件。讀取至內(nèi)存,簡(jiǎn)單的String Array。
parse
Git Log輸出有3種格式,對(duì)應(yīng)3種正則表達(dá)式。
REPATTERN_FULL = r"\s(\d+)\D+(\d+)\D+(\d+)\D+\n" REPATTERN_INSERT_ONLY = r"\s(\d+)\D+(\d+)\sinsertion\D+\n" REPATTERN_DELETE_ONLY = r"\s(\d+)\D+(\d+)\sdeletion\D+\n"
遍歷得到的數(shù)據(jù),首先構(gòu)造一個(gè)以Author為Key,分析結(jié)果為Value的字典。
分析結(jié)果構(gòu)造一個(gè)元祖,包括:
- Commit 次數(shù)
- 增加代碼行數(shù)
- 刪除代碼行數(shù)
- 變更代碼行數(shù)
save_csv
簡(jiǎn)單省略。
示例代碼:
#!/usr/local/bin/python3
# -*- coding: utf-8 -*-
'''Analyse git branch commit log, for every version, every person.'''
import os
import sys
import re
import csv
GIT_LOG = r'git -C {} log --since={} --until={} --pretty=tformat:%ae --shortstat --no-merges -- {} > {}'
REPATTERN_FULL = r"\s(\d+)\D+(\d+)\D+(\d+)\D+\n"
REPATTERN_INSERT_ONLY = r"\s(\d+)\D+(\d+)\sinsertion\D+\n"
REPATTERN_DELETE_ONLY = r"\s(\d+)\D+(\d+)\sdeletion\D+\n"
CSV_FILE_HEADER = ["Author", "Commit", "Insert", "Delete", "Loc"]
def exec_git(repo, since, until, subdir):
'''Execute git log commant, return string array.'''
logfile = os.path.join(os.getcwd(), 'gitstats.txt')
git_log_command = GIT_LOG.format(repo, since, until, subdir, logfile)
os.system(git_log_command)
lines = None
with open(logfile, 'r', encoding='utf-8') as logfilehandler:
lines = logfilehandler.readlines()
return lines
def save_csv(stats, csvfile):
'''save stats data to csv file.'''
with open(csvfile, 'w', encoding='utf-8') as csvfilehandler:
writer = csv.writer(csvfilehandler)
writer.writerow(CSV_FILE_HEADER)
for author, stat in stats.items():
writer.writerow([author, stat[0], stat[1], stat[2], stat[3]])
def parse(lines):
'''Analyse git log and sort to csv file.'''
prog_full = re.compile(REPATTERN_FULL)
prog_insert_only = re.compile(REPATTERN_INSERT_ONLY)
prog_delete_only = re.compile(REPATTERN_DELETE_ONLY)
stats = {}
for i in range(0, len(lines), 3):
author = lines[i]
#empty = lines[i+1]
info = lines[i+2]
#change = 0
insert, delete = int(0), int(0)
result = prog_full.search(info)
if result:
#change = result[0]
insert = int(result.group(2))
delete = int(result.group(3))
else:
result = prog_insert_only.search(info)
if result:
#change = result[0]
insert = int(result.group(2))
delete = int(0)
else:
result = prog_delete_only.search(info)
if result:
#change = result[0]
insert = int(0)
delete = int(result.group(2))
else:
print('Regular expression fail!')
return
loc = insert - delete
stat = stats.get(author)
if stat is None:
stats[author] = [1, insert, delete, loc]
else:
stat[0] += 1
stat[1] += insert
stat[2] += delete
stat[3] += loc
return stats
if __name__ == "__main__":
print('gitstats begin')
if len(sys.argv) != 6:
print('Invalid argv parameters.')
exit(0)
REPO = os.path.join(os.getcwd(), sys.argv[1])
SINCE = sys.argv[2]
UNTIL = sys.argv[3]
SUB_DIR = sys.argv[4]
CSV_FILE = os.path.join(os.getcwd(), sys.argv[5])
LINES = exec_git(REPO, SINCE, UNTIL, SUB_DIR)
assert LINES is not None
STATS = parse(LINES)
save_csv(STATS, CSV_FILE)
print('gitstats done')
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
將TensorFlow的模型網(wǎng)絡(luò)導(dǎo)出為單個(gè)文件的方法
本篇文章主要介紹了將TensorFlow的網(wǎng)絡(luò)導(dǎo)出為單個(gè)文件的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-04-04
解決Python報(bào)錯(cuò):ValueError:operands?could?not?be?broadcast?t
這篇文章主要給大家介紹了關(guān)于解決Python報(bào)錯(cuò):ValueError:operands?could?not?be?broadcast?together?with?shapes的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-02-02
利用 Python ElementTree 生成 xml的實(shí)例
這篇文章主要介紹了利用 Python ElementTree 生成 xml的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03
將Python中的數(shù)據(jù)存儲(chǔ)到系統(tǒng)本地的簡(jiǎn)單方法
這篇文章主要介紹了將Python中的數(shù)據(jù)存儲(chǔ)到系統(tǒng)本地的簡(jiǎn)單方法,主要使用了pickle模塊,需要的朋友可以參考下2015-04-04
python HTTPX庫(kù)實(shí)現(xiàn)同步異步請(qǐng)求用法示例
這篇文章主要為大家介紹了python HTTPX庫(kù)實(shí)現(xiàn)同步異步請(qǐng)求用法示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01
Python讀取xlsx數(shù)據(jù)生成圖標(biāo)代碼實(shí)例
這篇文章主要介紹了Python讀取xlsx數(shù)據(jù)生成圖標(biāo)代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08
numpy 對(duì)矩陣中Nan的處理:采用平均值的方法
今天小編就為大家分享一篇numpy 對(duì)矩陣中Nan的處理:采用平均值的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-10-10
Python機(jī)器學(xué)習(xí)應(yīng)用之基于線性判別模型的分類篇詳解
線性判別分析(Linear?Discriminant?Analysis,?LDA)是一種監(jiān)督學(xué)習(xí)的降維方法,也就是說(shuō)數(shù)據(jù)集的每個(gè)樣本是有類別輸出。和之前介紹的機(jī)器學(xué)習(xí)降維之主成分分析(PCA)方法不同,PCA是不考慮樣本類別輸出的無(wú)監(jiān)督學(xué)習(xí)方法2022-01-01

