Python实现一个Git日志统计分析的小工具

yipeiwu_com5年前Python基础

前言

本文介绍的是利用Python实现的一个小工具,用于分析Git commit log,获得Git Project每个成员的简单行为数据。

Warning:代码量不能代表程序员能力水平!

启动参数

共5个。

  • Repo地址
  • Commit 起始日期
  • Commit 结束日期
  • Git仓库子目录
  • 统计分析结果CSV文件目标路径

exec_git

Git Log命令:

git -C {} log --since={} --until={} --pretty=tformat:%ae --shortstat --no-merges -- {} > {}

填入参数,调用系统命令'os.system()',输出结果至本地临时文件。读取至内存,简单的String Array。

parse

Git Log输出有3种格式,对应3种正则表达式。

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"

遍历得到的数据,首先构造一个以Author为Key,分析结果为Value的字典。

分析结果构造一个元祖,包括:

  • Commit 次数
  • 增加代码行数
  • 删除代码行数
  • 变更代码行数

save_csv

简单省略。

示例代码:

#!/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')

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对【听图阁-专注于Python设计】的支持。

相关文章

Python实现的读取电脑硬件信息功能示例

本文实例讲述了Python实现的读取电脑硬件信息功能。分享给大家供大家参考,具体如下: 上学那会,老师让我用java获取电脑硬件信息,CPU, 硬盘,MAC等,那个时候感觉搞了好久。。。...

解析Python中的__getitem__专有方法

__getitem__ 来看个简单的例子就明白: def __getitem__(self, key): return self.data[key] >>> f...

python 求一个列表中所有元素的乘积实例

如下所示: # 求一个列表中所有元素的乘积 from functools import reduce lt = [1,2,3,4,5] ln = reduce(lambda x...

Python实现二维曲线拟合的方法

如下所示: from numpy import * import numpy as np import matplotlib.pyplot as plt plt.close() f...

Python科学计算环境推荐——Anaconda

Python科学计算环境推荐——Anaconda

Anaconda是一个和Canopy类似的科学计算环境,但用起来更加方便。自带的包管理器conda也很强大。 首先是下载安装。Anaconda提供了Python2.7和Python3.4...