使用python分析git log日志示例_python

用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)

时间: 2024-09-30 23:30:09

使用python分析git log日志示例_python的相关文章

Python 分析Nginx访问日志并保存到MySQL数据库实例_python

使用Python 分析Nginx access 日志,根据Nginx日志格式进行分割并存入MySQL数据库.一.Nginx access日志格式如下: 复制代码 代码如下: $remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" "$http_x_forwarded_f

python分析nignx访问日志脚本分享_python

#!/usr/bin/env python # coding=utf-8 #------------------------------------------------------ # Name: nginx 日志分析脚本 # Purpose: 此脚本只用来分析nginx的访问日志 # Version: 1.0 # Author: LEO # Created: 2013-05-07 # Modified: 2013-05-07 # Copyright: (c) LEO 2013 #-----

快速排序的算法思想及Python版快速排序的实现示例_python

快速排序是C.R.A.Hoare于1962年提出的一种划分交换排序.它采用了一种分治的策略,通常称其为分治法(Divide-and-ConquerMethod). 1.分治法的基本思想 分治法的基本思想是:将原问题分解为若干个规模更小但结构与原问题相似的子问题.递归地解这些子问题,然后将这些子问题的解组合为原问题的解. 2.快速排序的基本思想 设当前待排序的无序区为R[low..high],利用分治法可将快速排序的基本思想描述为: (1)分解: 在R[low..high]中任选一个记录作为基准(

python分析apache访问日志脚本分享_python

#!/usr/bin/env python # coding=utf-8 #------------------------------------------------------ # Name: Apache 日志分析脚本 # Purpose: 此脚本只用来分析Apache的访问日志 # Version: 2.0 # Author: LEO # Created: 2013-4-26 # Modified: 2013-5-4 # Copyright: (c) LEO 2013 #------

Python分析NGINX LOG版本二

不好意思,上一版逻辑有错误,(只分析了一次就没了) 此版改正. 按同事要改,作成传参数形式,搞定. #!/usr/bin/env python # coding: utf-8 ################################### # User:chengang # # Email:aguncn@163.com # # Date:2016-02-25 # ################################### import time import datetime

Python分析MySQL慢日志并定期发送邮件

因为开发针对某系统做稳定性建设,需要对数据库系统的慢日志进行审计,检查优化.和开发沟通选择定期发送慢查询到开发的邮箱的方式,每日汇总,然后一起评估 优化slow query . 工具实现 mail.py  脚本  代码如下 复制代码 #!/usr/bin/env python   # -*- coding: utf-8 -*- # mail.py import os.path import time import sys import os import json import string i

python代码制作configure文件示例_python

在lua中,一直用lua作为config文件,或承载数据的文件 - 好处是lua本身就很好阅读,然后无需额外写解析的代码,还支持在configure文件中读环境变量,条件判断等. 在lua中通过loadfile, setfenv实现) python: cat config.py bar = 10 foo=100 cat python_as_config.py: ns = {} execfile('config.py', ns) print "\n".join(sorted(dir(ns

python实现保存网页到本地示例_python

学习python示例:实现保存网页到本地 复制代码 代码如下: #coding=utf-8__auther__ = 'xianbao'import urllibimport osdef reporthook(blocks_read, block_size, total_size): if not blocks_read:  print '打开连接'  return if total_size < 0:  print "%d正在读取(%dbytes完成)"%(blocks_read

python实现绘制树枝简单示例_python

python是解释型语言,本文介绍了Python下利用turtle实现绘图功能的示例,本例所示为Python绘制一个树枝,具体实现代码如下:      python是解释型语言,本文介绍了Python下利用turtle实现绘图功能的示例,本例所示为Python绘制一个树枝,具体实现代码如下: import turtle def branch(length,level): if level<=0: return turtle.forward(length) turtle.left(45) bran