没有惊喜的正常处理
源代码
1 # -*- coding: UTF-8 -*-
2 """weeklystat auto WeekLy StatCVS HTML analyze
3 - 每周StatCVS 分析结果汇总!
4 - 通过 cvs log -d "yyyy-mm-dd"\< > cvs.log
5 - 限制Log 分析的配合得到本周的变更信息!
6 - 汇出为XML
7 @version: $Id: weeklystat.py,v 1.5 2005/01/04 10:16:09 zhouqi Exp $
8 @author: U{Zoom.Quiet<mailto:[email protected]>}
9
10 """
11 import sys,os,string
12 import time
13 from elementtree import ElementTree
14 from elementtree.ElementTree import Element, SubElement, tostring
15 class WeeklyStat:
16 """主类,集成所有处理
17 """
18 def __init__(self):
19 """初始化参数
20 - "scm"
21 ,"MTA"
22 ,"sinaInterface"
23 ,"sina668"
24 ,"sinasys"
25 ,"webmail"
26 ,"runmanage"
27 ,"sinaIndex"
28 """
29 self.xml = "/usr/home/statcvs/snapcvs/pub/weeklystat.xml"
30 self.log = ""
31 self.xmlhead = """<?xml version="1.0" encoding="UTF-8"?>
32 <!-- edited with elementtree (http://effbot.org/zone/element-index.htm) by Zoom.Quiet -->
33 <?xml-stylesheet type="text/xsl" href="weeklystat.xsl"?>
34 """
35 self.cvsroot = "/usr/home/statcvs/snapcvs/"
36 self.pubroot = "/usr/home/statcvs/snapcvs/pub"
37 self.zday = time.strftime("%Y-%m-%d",time.localtime(time.time()))
38 self.mFiles="cat %s/index.html | grep \"Files: \" | cut -d \" \" -f 2|cut -d \"<\" -f 1"
39 self.mRevisions="cat %s/index.html | grep \"Revisions: \" | cut -d \" \" -f 2|cut -d \"<\" -f 1"
40 self.mLines="cat %s/index.html | grep \"Code: \" | cut -d \":\" -f 2 | cut -d \"<\" -f 1 | cut -d \" \" -f 2"
41
42 self.report = {}
43 self.cvs = ("scm"
44 ,"MTA"
45 ,"sinaInterface"
46 ,"sinasys"
47 ,"webmail"
48 ,"runmanage"
49 ,"sinaIndex")
50 def wlkCvsCo(self):
51 """逐一搜索所有模块综合汇报信息
52 """
53 dirs = {}
54 os.chdir(self.cvsroot)
55 for key in self.cvs:
56 #os.chdir(self.cvsroot+key)
57 dirs[key]={}
58 subdir = os.listdir(self.cvsroot+key)
59 for node in subdir:
60 if("CVS"==node or "CVSROOT"==node or os.path.isfile(self.cvsroot+key+"/"+node)):
61 pass
62 else:
63 dirs[key][node]={}
64 return dirs
65 def ckAll(self):
66 """逐一搜索所有模块综合汇报信息
67 """
68 dirs = self.wlkCvsCo()
69 print dirs
70 for cvs in dirs:
71 os.chdir(self.cvsroot+cvs)
72 #print self.cvsroot+cvs
73 for module in dirs[cvs]:
74 page = self.pubroot+"/"+cvs+"/"+module
75 # 修改文件分析
76 findout = os.popen(self.mFiles%page)
77 r = findout.read()
78 if(0!= len(r)):
79 dirs[cvs][module]["files"]=r[:-1]
80 # 修改次数分析
81 findout = os.popen(self.mRevisions%page)
82 r = findout.read()
83 if(0!= len(r)):
84 dirs[cvs][module]["commits"]=r[:-1]
85 # 修改行数分析
86 findout = os.popen(self.mLines%page)
87 r = findout.read()
88 if(0!= len(r)):
89 dirs[cvs][module]["lines"]=r[:-1]
90 self.report = dirs
91 self.put2xml()
92 def put2xml(self):
93 """汇出数据为XML文件!
94 - 使用轻型XML操作库 Elements and Element Trees
95 - @see: U{Elements<http://effbot.org/zone/element.htm>}
96 """
97 stat = self.report
98 print stat
99 tree = ElementTree.parse(self.xml).getroot()
100 week = tree.findall("week")
101 # 先判定是否要记录
102 done = 0
103 for node in week:
104 if(self.zday==node.attrib["date"]):
105 done = 1
106 break
107 cvs = node.findall("cvs")
108 if(0==done):
109 print "need recording...will exp. as xml file"
110 neweek = Element("week")
111 neweek.set("date", self.zday)
112 statcount = {'files':0
113 ,'commits':0
114 ,'lines':0
115 }
116 for proj in stat:
117 newcvs = SubElement(neweek, "cvs")
118 newcvs.set("name", proj)
119 modcount = {'files':0
120 ,'commits':0
121 ,'lines':0
122 }
123 for moudle in stat[proj]:
124 submod = SubElement(newcvs, "module")
125 submod.set("name", moudle)
126 if(0==len(stat[proj][moudle])):
127 submod.set("files", "0")
128 submod.set("commits", "0")
129 submod.set("lines", "0")
130 else:
131 for item in stat[proj][moudle]:
132 #print stat[proj][moudle][item]
133 submod.set(item, str(stat[proj][moudle][item]))
134 modcount[item]+=int(stat[proj][moudle][item])
135 statcount[item]+=int(stat[proj][moudle][item])
136 for totl in modcount:
137 newcvs.set(totl, str(modcount[totl]))
138 for totl in statcount:
139 neweek.set(totl, str(statcount[totl]))
140 tree.append(neweek)
141 else:
142 print "recorded ...xml file not edit!"
143 return
144 code = tostring(tree)
145 if __name__ == '__main__':
146 """基本运行模式
147 """
148 week = WeeklyStat()
149 week.ckAll()
解说
没有什么新的代码,全部是原来 CvsWeeklyStat/WeeklycvsHow/modifiles.py 中提到的东西
系统命令
- {{{...
self.mFiles="cat %s/index.html | grep \"Files: \" | cut -d \" \" -f 2|cut -d \"<\" -f 1" self.mRevisions="cat %s/index.html | grep \"Revisions: \" | cut -d \" \" -f 2|cut -d \"<\" -f 1" self.mLines="cat %s/index.html | grep \"Code: \" | cut -d \":\" -f 2 | cut -d \"<\" -f 1 | cut -d \" \" -f 2"
- 组合一些 Unix 的系统工具来代替 Python 的正则表达式的字串处理
- 意思就是:
#cat /path/to/statcvs分析结果的首页/index.html | grep "Files: " | cut -d " " -f 2|cut -d "<\" -f 1 #找到对应 index.html 中含 "Files: "的这行内容: <div>Files: 8</div> #然后从空格处断开取后一半 8</div> #再从"</"断开取前一半 8 # 就是 statcvs 帮忙分析出的当前CVS的当前模块在约定时限内的统计信息
--- 简直就是无赖的匹配处理!
可以想到Unix 多年来的高强度使用,不论多么变态的处理要求,都有对应的专门小工具来处理的!哈哈哈!!
XML 生成
- 有些花头的就是 XML 的生成时,复杂了一些,
- 因为想体现各个CVS中各个模块的信息,所以就加了一层数据
也就是self.report = {}这个数据记录对象的结构加深为:
self.report = {#整体统计 ,'files':"" ,'commits':"" ,'lines':"" ,{'name':"" #cvs ,'files':"" ,'commits':"" ,'lines':"" ,{'name':"" #moudle ,'files':"" ,'commits':"" ,'lines':"" } } , ... }
- 实际上,与输出的XML数据层次是一致的!
- 当然,纯使用 for in 循环来处理是够傻的!但是没有难度!
所以有 将 XML 文档作为对象处理的“Python 化” 等等
-- ZoomQuiet [2005-01-05 11:20:51]