使用python分析git log日志示例


Posted in Python onFebruary 27, 2014

用git来管理工程的开发,git log是非常有用的‘历史'资料,需求就是来自这里,我们希望能对git log有一个定制性强的过滤。此段脚本就是在完成这种类型的任务。对于一个repo所有branch中的commit,脚本将会把message中存在BUG ID的一类commits给提取整理出来,并提供了额外的search_key, 用于定制过滤。

# -*- coding: utf-8 -*-
# created by vince67 Feb.2014
# nuovince@gmail.com
import re
import os
import subprocess

def run(project_dir, date_from, date_to, search_key, filename):
    bug_dic = {}
    bug_branch_dic = {}
    try:
        os.chdir(project_dir)
    except Exception, e:
        raise e
    branches_list = []
    branches_list = get_branches()
    for branch in branches_list:
        bug_branch_dic = deal_branch(date_from,
                                     date_to,
                                     branch,
                                     search_key)
        for item in bug_branch_dic:
            if item not in bug_dic:
                bug_dic[item] = bug_branch_dic[item]
            else:
                bug_dic[item] += bug_branch_dic[item]
    log_output(filename, bug_dic)

# abstract log of one branch
def deal_branch(date_from, date_to, branch, search_key):
    try:
        os.system('git checkout ' + branch)
        os.system('git pull ')
    except Exception, error:
        print error
    cmd_git_log = ["git",
                   "log",
                   "--stat",
                   "--no-merges", 
                   "-m",
                   "--after="+date_from,
                   "--before="+date_to]
    proc = subprocess.Popen(cmd_git_log,
                            stdout=subprocess.PIPE,
                            stderr=subprocess.PIPE)
    stdout, stderr = proc.communicate()
    bug_branch_dic = deal_lines(date_from,
                                date_to,
                                search_key,
                                stdout)
    return bug_branch_dic
# write commits log to file
def log_output(filename, bug_dic):
    fi = open(filename, 'w')
    for item in bug_dic:
        m1 = '--'*5 + 'BUG:' + item + '--'*20 + '\n'
        fi.write(m1)
        for commit in bug_dic[item]:
            fi.write(commit)
    fi.close()

# analyze log 
def deal_lines(date_from, date_to, search_key, stdout):
    bug_dic = {}
    for line in stdout.split('commit '):
        if re.search('Bug: \d+', line) is not None and re.search(search_key, line) is not None:
            bug_id = line.split('Bug: ')[1].split('\n')[0]
            if bug_id not in bug_dic:
                bug_dic[bug_id] = [line]
            else:
                bug_dic[bug_id] += [line]
    return bug_dic

# get all branches of a project
def get_branches():
    branch_list = []
    branches = []
    tmp_str = ''
    try:
        cmd_git_remote = 'git remote show origin'
        proc = subprocess.Popen(cmd_git_remote.split(),
                                stdout=subprocess.PIPE,
                                stderr=subprocess.PIPE)
        stdout, stderr = proc.communicate()
        tmp_str = stdout.split('Local branches configured')[0]
        try:
            tmp_str = tmp_str.split('Remote branches:\n')[1]
        except:
            tmp_str = tmp_str.split('Remote branch:\n')[1]
        branches = tmp_str.split('\n')
        for branch in branches[0:-1]:
            if re.search(' tracked', branch) is not None:
                branch = branch.replace('tracked', '').strip(' ')
                branch_list.append(branch)
    except Exception, error:
        if branch_list == []:
            print "Can not get any branch!"
    return branch_list

if __name__ == '__main__':
    # path of the .git project. example: "/home/username/projects/jekyll_vincent"
    project_dir = ""
    date_from = "2014-01-25"
    date_to = "2014-02-26"
    # only search 'Bug: \d+' for default
    search_key = ""
    # name of output file. example:"/home/username/jekyll_0125_0226.log"
    filename = ""
    run(project_dir, date_from, date_to, search_key, filename)
Python 相关文章推荐
python操作MongoDB基础知识
Nov 01 Python
python读取csv文件示例(python操作csv)
Mar 11 Python
Python解析nginx日志文件
May 11 Python
Python实现拷贝/删除文件夹的方法详解
Aug 29 Python
python异步存储数据详解
Mar 19 Python
python远程邮件控制电脑升级版
May 23 Python
numpy.where() 用法详解
May 27 Python
selenium 安装与chromedriver安装的方法步骤
Jun 12 Python
深入了解Django View(视图系统)
Jul 23 Python
python利用7z批量解压rar的实现
Aug 07 Python
python实现串口通信的示例代码
Feb 10 Python
解决redis与Python交互取出来的是bytes类型的问题
Jul 16 Python
python去掉字符串中重复字符的方法
Feb 27 #Python
tornado捕获和处理404错误的方法
Feb 26 #Python
python为tornado添加recaptcha验证码功能
Feb 26 #Python
python实现博客文章爬虫示例
Feb 26 #Python
python处理中文编码和判断编码示例
Feb 26 #Python
python实现网页链接提取的方法分享
Feb 25 #Python
python3模拟百度登录并实现百度贴吧签到示例分享(百度贴吧自动签到)
Feb 24 #Python
You might like
PHP的serialize序列化数据以及JSON格式化数据分析
2015/10/10 PHP
PHP检测用户是否关闭浏览器的方法
2016/02/14 PHP
基于php双引号中访问数组元素报错的解决方法
2018/02/01 PHP
PHP使用PDO实现mysql防注入功能详解
2019/12/20 PHP
再谈Yii Framework框架中的事件event原理与应用
2020/04/07 PHP
学习ExtJS border布局
2009/10/08 Javascript
ExtJs设置GridPanel表格文本垂直居中示例
2013/07/15 Javascript
onmouseover和onmouseout的一些问题思考
2013/08/14 Javascript
jQuery中extend函数的实现原理详解
2015/02/03 Javascript
最精简的JavaScript实现鼠标拖动效果的方法
2015/05/11 Javascript
jQuery点击其他地方时菜单消失的实现方法
2016/04/22 Javascript
JS简单随机数生成方法
2016/09/05 Javascript
jQuery实现拖动剪裁图片作为头像
2016/12/28 Javascript
AngularJS双向绑定和依赖反转实例详解
2017/04/15 Javascript
简单谈谈原生js的math对象
2017/06/27 Javascript
Angular4的输入属性与输出属性实例详解
2017/11/29 Javascript
axios全局请求参数设置,请求及返回拦截器的方法
2018/03/05 Javascript
echarts同一页面中四个图表切换的js数据交互方法示例
2018/07/03 Javascript
JS闭包经典实例详解
2018/12/20 Javascript
小程序scroll-view安卓机隐藏横向滚动条的实现详解
2019/05/16 Javascript
node.js命令行教程图文详解
2019/05/27 Javascript
vue2和vue3的v-if与v-for优先级对比学习
2020/10/10 Javascript
vue打开其他项目页面并传入数据详解
2020/11/25 Vue.js
python中readline判断文件读取结束的方法
2014/11/08 Python
Python字典,函数,全局变量代码解析
2017/12/18 Python
Python函数any()和all()的用法及区别介绍
2018/09/14 Python
修改默认的pip版本为对应python2.7的方法
2018/11/06 Python
python简单实现矩阵的乘,加,转置和逆运算示例
2019/07/10 Python
python 检测nginx服务邮件报警的脚本
2020/12/31 Python
几个Linux面试题笔试题
2012/12/01 面试题
什么时候用assert
2015/05/08 面试题
真诚的求职信
2014/07/04 职场文书
拾金不昧表扬稿
2015/01/16 职场文书
2015年林业工作总结
2015/05/14 职场文书
python中print格式化输出的问题
2021/04/16 Python
Python使用华为API为图像设置多个锚点标签
2022/04/12 Python