gitinspector是一个python语言开发的开源的命令行下的代码统计工具,一般用于开发人员对自己的代码做统计。
命令行的一般用法:
# gitinspector -wTHL /path/code/repository // 自行执行看下效果,这里不再缀述
# gitinspector --format=html --timeline --localize-output -w /path/code/repository > /save/path/result.html // 生成html文件(可生成多种类型的文件)自行执行看下效果,这里不再缀述
也可去http://www.jiechic.com/archives/using-gitinspector-git-commits-statistics看效果。
今天主要是与大家交流一下gitinspector与jenkins如何结合:
环境为Centos 7.1。
如果是Centos 6.X请自行安装python2.7.5以上版本;下面提供安装命令:
# wget https://www.python.org/ftp/python/2.7.9/Python-2.7.9.tar.xz
# mv /usr/bin/python /usr/bin/python26
# tar xf Python-2.7.9.tgz
# cd Python-2.7.9
# ./configure
# make && make install
# ln -s /usr/local/bin/python2.7 /usr/bin/python
# sed -i "s#\#\!/usr/bin/python#\#\!/usr/bin/python26#g" /usr/bin/yum
一、部署jenkins CI环境
网上到处都是,过程不做缀述。
二、安装gitinspcetor
# wget https://raw.github.com/pypa/pip/master/contrib/get-pip.py --no-check-certificate
# python get-pip.py
# pip install gitinspector
我的jenkins与gitinspector在同一台服务器上
三、我用的gitlab,把代码clone到本地一份
# cd /local/code/repo/path
# git clone git@address:user/coderepo.git
四、编写python或其它语言的脚本,jenkins去执行的;不建议用shell。
我的脚本:
#!/usr/bin/python # -*- encoding: utf-8 -*- import sys import os import re import time import commands # 不要用os.system,用了的话你会很意外的*_* # 我们有三个版本库,所以下面是三个repo。你用一个就可以了 credit = "/path/code/repo" # 本地clone代码库路径的上层目录 datahub = "/path/code/repo" scoreweb = "/path/code/repo" HtmlTarget = "/jenkins/url" # 我是借助jenkins新建一个目录保存html文件,访问方法:http://ip:port/html/save/path/result.html CreateDate = time.strftime("%Y%m%d%H%M%S") ViewURL = "http://ip:port/jenkins/" # 我保存html文件的目录为jenkins ProjectList = [] ProjectList.append(credit) ProjectList.append(datahub) ProjectList.append(scoreweb) # tmpfile = "/tmp/temp.txt" # 如果用os.system执行shell命令最好是把结果放到一个临时文件中 count = 0 def delFile(PATH): os.chdir(PATH) S, R = commands.getstatusoutput("find . -type f -ctime +1 | xargs rm -f") if S == 0: print "Delete html file 1 days ago." else: print "Delete file failed" def historyHtml(PATH, project): print "\033[34m#################\033[0m" print "\033[34m# History #\033[0m" print "\033[34m#################\033[0m" for file in os.listdir(PATH): print os.path.join(ViewURL, file) print "\033[31m============%s END============\033[0m" % project print "" for projectPATH in ProjectList: os.chdir(r"%s" % projectPATH) item = projectPATH.split('/')[4] gitStatus, gitResult = commands.getstatusoutput("git pull") if gitStatus == 0: print "\033[31m============Update Project %s Code============\033[0m" % item print "Code update complete. %s" % gitResult print "" try: simpleStatus, simpleResult = commands.getstatusoutput("gitinspector -wTHL %s" % projectPATH) #os.system("gitinspector -wTHL %s > %s" % (projectPATH, tmpfile)) except Exception as e: count += 1 print e else: if simpleStatus == 0: print "\033[34m#################\033[0m" print "\033[34m# Simple INFO #\033[0m" print "\033[34m#################\033[0m" print "" print simpleResult else: count += 1 print "\033[34m#################\033[0m" print "\033[34m# Simple INFO #\033[0m" print "\033[34m#################\033[0m" print "" print simpleResult try: viewStatus, viewResult = commands.getstatusoutput("gitinspector --format=html --timeline --localize-output -w %s" % projectPATH) except Exception as e: count += 1 print e else: print viewStatus if viewStatus == 0: try: htmlFile = open("%s/%s/%s_%s.html" % (HtmlTarget, item, item, CreateDate), 'w+') # gitinspector生成的html文件中有一个google的jquery文件,我把它替换为本地 htmlFile.writelines(viewResult.replace("https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js", "../../gitinspector/jquery.js")) except Exception as e: count += 1 htmlFile.close() print "\033[34m#################\033[0m" print "\033[34m# View INFO #\033[0m" print "\033[34m#################\033[0m" print e print "" else: htmlFile.close() print "\033[34m#################\033[0m" print "\033[34m# View INFO #\033[0m" print "\033[34m#################\033[0m" print "Access the following URL to view details" print "%s%s/%s_%s.html" % (ViewURL, item, item, CreateDate) print "" else: count += 1 print "Exception:" print viewStatus print viewResult print "" delFile("%s/%s" % (HtmlTarget, item)) historyHtml("%s/%s" % (HtmlTarget, item), item) else: print "\033[31m============Update Project %s Code============\033[0m" % item time.sleep(2) print "Code update failed." print "" sys.exit(5) if count == 0: sys.exit() else: sys.exit(126) |
五、最重要的一步,修改gitinspector的源代码
# vim /usr/lib/python2.7/site-packages/gitinspector/terminal.py +122
#注释的这一行是源代码,注释的上一行是修改后的;因为gitrepector在命令行执行命令会获取你的终端输出,会先检验你的终端字符集,而用jenkins去执行脚本时,sys.stdin.encoding这一名获取不到终端字符集,所以直接写死为'utf-8'就行了。
117 def convert_command_line_to_utf8():
118 try:
119 argv = []
120
121 for arg in sys.argv:
122 argv.append(arg.decode("utf-8", "replace"))
123 # argv.append(arg.decode(sys.stdin.encoding, "replace"))
124
125 return argv
126 except AttributeError:
127 return sys.argv
六、添加jenkins自由风格的工程
勾选Build periodically定时执行
在构建标签中先Execute shell 这个应该都会吧,因为我是jenkins本地所以我的是
/usr/bin/python /root/code/statistical_code.py
保存
搞定
jenkins效果:
点击链接后: