没有惊喜的正常处理

源代码

   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:Zoom.Quiet@gmail.com>}
   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()

解说

系统命令

XML 生成


-- ZoomQuiet [2005-01-05 11:20:51]

CvsWeeklyStat/WeeklycvsHow/weeklystat.py (last edited 2009-12-25 07:12:42 by localhost)