python调用git生成log文件_python解析git log后生成页面显示git更新日志信息
使用git log可以查到git上項目的更新日志。
如下兩個git項目,我想把git的日志信息解析成一個便于在瀏覽器上查看的頁面。
https://github.com/gityf/lua
https://github.com/gityf/db
使用git log命令獲取git更新日志信息:
D:\git\github\lua>git log > ..\lua.gitlog
D:\git\github\db>git log > ..\dbutils.gitlog
生成兩個git更新日志文件lua.gitlog和dbutils.gitlog。
文件內容格式是這樣的:
commit a5ff94375f48c7de067a1c191cb60d720bf4f726
Author: Mr.YF
Date: Tue Jan 12 19:43:33 2016 +0800
add thrift binary analysis code and doc.
commit 9dc9437070867e8d8ba7c9d4365efe3f2c317caf
Author: Mr.YF
Date: Mon Jan 11 19:51:48 2016 +0800
add comments.
git log命令會輸出關鍵字"commit","Merge","Author","Date"
使用下面腳本分析git日志,生成html的頁面,便于查看。
'''
Created on 2016-1-25
@author: Mr.YF
'''
import os
import sys
def header():
headerHtml = '''//W3C//DTD HTML 4.01 Transitional//EN "http://www.w3.org/TR/html4/loose.dtd">
Statistics Report for gitcheck.body {
font-family: arial, helvetica, sans-serif;
font-size: 12px;
font-weight: normal;
color: black;
background: white;
}
th,td {
font-size: 10px;
}
h1 {
font-size: x-large;
margin-bottom: 0.5em;
}
h2 {
font-family: helvetica, arial;
font-size: x-large;
font-weight: bold;
font-style: italic;
color: #6020a0;
margin-top: 0em;
margin-bottom: 0em;
}
h3 {
font-family: helvetica, arial;
font-size: 16px;
font-weight: bold;
color: #b00040;
background: #e8e8d0;
margin-top: 0em;
margin-bottom: 0em;
}
li {
margin-top: 0.25em;
margin-right: 2em;
}
.hr {margin-top: 0.25em;
border-color: black;
border-bottom-style: solid;
}
.in {color: #6020a0; font-weight: bold; text-align: left;}
.frontend {background: #e8e8d0;}
.s {background: #e0e0e0;}
.a0 {background: #FF99CC; font-weight: bold;}
.a1 {background: #CCFF99;}
.a2 {background: #CCFFFF;}
.a3 {background: #CCCCFF;}
.a4 {background: #66CCCC;}
.a5 {background: #CCFF66;}
.a6 {background: #FFCC99;}
.maintain {background: #c07820;}
.rls {letter-spacing: 0.2em; margin-right: 1px;}
a.px:link {color: #ffff40; text-decoration: none;}
a.px:visited {color: #ffff40; text-decoration: none;}
a.px:hover {color: #ffffff; text-decoration: none;}
a.lfsb:link {color: #000000; text-decoration: none;}
a.lfsb:visited {color: #000000; text-decoration: none;}
a.lfsb:hover {color: #505050; text-decoration: none;}
table.tbl { border-collapse: collapse; border-style: none;}
table.tbl td { text-align: right; border-width: 1px 1px 1px 1px; border-style: solid solid solid solid; padding: 2px 3px; border-color: gray; white-space: nowrap;}
table.tbl td.ac { text-align: center;}
table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}
table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}
table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}
table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}
table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}
table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}
table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}
u {text-decoration:none; border-bottom: 1px dotted black;}
-->
GitCheck Center
> General git information
return headerHtml
def footer():
footHtml = '''
'''return footHtml
def row():
rowHtml = '''
%s%s%s%s%s'''return rowHtml
def indexMainRow():
rowHtml = '''
IDbranch git log info.'''return rowHtml
def indexRow():
rowHtml = '''
%d%s'''return rowHtml
def tdClassId(pageId):
pageId += 1
if pageId >= 7:
pageId = 1
return pageId
def gitcheck(inFileName="gitcheck.in", outFileName="gitcheck.out.html"):
fn = open(inFileName, "r")
fnOut = open(outFileName, "w")
fnOut.write(header())
fnOut.write(row() % (0, "commit","Merge","Author","Date", "Comment"))
lines = fn.readlines()
beginTag = False
pageId = 1
goodLine = {0:"",1:"",2:"",3:"",4:""}
for line in lines:
if len(line) <= 2:
continue
if line.startswith("commit"):
beginTag = True
goodLine[0] = line[6:]
pageId = tdClassId(pageId)
continue
if beginTag:
if line.startswith("Merge: "):
goodLine[1] = line[7:]
elif line.startswith("Author: "):
goodLine[2] = line[8:]
elif line.startswith("Date: "):
goodLine[3] = line[6:]
else :
goodLine[4] = line
beginTag = False
fnOut.write(row() % (pageId,goodLine[0],goodLine[1],goodLine[2],goodLine[3],goodLine[4]))
goodLine = {0:"",1:"",2:"",3:"",4:""}
fn.close()
fnOut.write(footer())
fnOut.flush()
fnOut.close()
def lsHtmls(dirName="gitinfo"):
htmlFiles = []
allFile = os.listdir(dirName)
for f in allFile:
if f.endswith(".html"):
htmlFiles.append(f)
print f
return htmlFiles
def createIndex(dirName):
files = lsHtmls(dirName)
if len(files) > 0:
fnOut = open("index.html", "w")
fnOut.write(header())
fnOut.write(indexMainRow())
pageId = 1
ii = 1
for f in files:
fnOut.write(indexRow() % (ii, pageId, f, f))
ii = tdClassId(ii)
pageId += 1
fnOut.write(footer());
fnOut.close()
if __name__ == '__main__':
if len(sys.argv) >= 3:
gitcheck(sys.argv[1], sys.argv[2])
elif len(sys.argv) >= 2:
createIndex(sys.argv[1])
把python代碼保存為gitcheck.py
使用如下命令解析日志文件lua.gitlog和dbutils.gitlog。
F:\pyworkspace\gitcheck>python gitcheck.py dbutils.gitlog gitinfo\dbutils.html
F:\pyworkspace\gitcheck>python gitcheck.py lua.gitlog gitinfo\lua_thrift_demo.html
F:\pyworkspace\gitcheck>python gitcheck.py gitinfo
dbutils.html
lua_thrift_demo.html
python gitcheck.py gitinfo會把目錄gitinfo下所有的html文件匯總生成一個index.html主頁。
效果如下:
主頁
跳轉到某個項目查看git日志信息
Done.
總結
以上是生活随笔為你收集整理的python调用git生成log文件_python解析git log后生成页面显示git更新日志信息的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: svnadmin: e000002: 无
- 下一篇: python 递归 分叉_浅谈Pytho