源代码

   1 # -*- coding: UTF-8 -*-
   2 """modifiles auto check
   3     - 每周CVS 更新文件分析脚本!
   4     - 自动查找本周更新过的文件,计数
   5     - 汇出为XML
   6 @version: $Id: modifiles.py,v 1.3 2004/12/26 14:10:04 zhouqi Exp $
   7 @author: U{Zoom.Quiet<mailto:Zoom.Quiet@gmail.com>}
   8 """
   9 import sys,os,string
  10 import time
  11 from elementtree import ElementTree
  12 from elementtree.ElementTree import Element, SubElement, tostring
  13 class modifiles:
  14     """主类,集成所有处理
  15     """
  16     def __init__(self):
  17         """初始化参数
  18         """
  19         self.xml = "/home/zoomq/share/statcvs/weeklystat.xml"
  20         self.xmlhead = """<?xml version="1.0" encoding="UTF-8"?>
  21 <!-- edited with elementtree (http://effbot.org/zone/element-index.htm) by Zoom.Quiet  -->
  22 <?xml-stylesheet type="text/xsl" href="statcvs.xsl"?>
  23         """
  24         self.cvsroot = "/cvs/root/"
  25         self.finder = "find %s -mtime -7 -type f | wc -l | awk '{print $1}'"
  26         self.report = {}
  27         self.cvs = ("scm"
  28             ,"MTA"
  29             ,"sinaInterface"
  30             ,"sina668"
  31             ,"sinasys"
  32             ,"webmail"
  33             ,"runmanage"
  34             ,"sinaIndex")
  35 
  36     def checking(self):
  37         """逐一处理所有模块
  38         """
  39         os.chdir(self.cvsroot)
  40         for key in self.cvs:
  41             os.chdir(self.cvsroot+key)
  42             re = os.popen(self.finder%(self.cvsroot+key))
  43             sre = re.read()
  44             self.report[key] = sre[:-1]
  45         self.put2xml()
  46     def put2xml(self):
  47         """汇出数据为XML文件!
  48             - 使用轻型XML操作库 Elements and Element Trees
  49             - @see: U{Elements<http://effbot.org/zone/element.htm>}
  50         """
  51         zday = time.strftime("%Y-%m-%d",time.localtime(time.time()))
  52         tree = ElementTree.parse(self.xml).getroot()
  53         week = tree.findall("week")
  54         # 先判定是否要记录
  55         done = 0
  56         for node in week:
  57             if(zday==node.attrib["date"]):
  58                   done = 1
  59                   break
  60             cvs = node.findall("cvs")
  61         if(0==done):
  62             print "need recording...will exp. as xml file"           
  63             neweek = Element("week")
  64             neweek.set("date", zday)            
  65             statcount = 0
  66             for proj in self.report:
  67                 newcvs = SubElement(neweek, "cvs")
  68                 newcvs.set("name", proj)
  69                 statcount +=int(self.report[proj])                
  70                 newcvs.set("modifiles", self.report[proj]) 
  71                 newcvs.set("commit", "null")
  72             neweek.set("modifiles", str(statcount))
  73             tree.append(neweek)
  74         else:
  75             print "recorded ...xml file not edit!"
  76             return
  77         #ElementTree.ElementTree(tree).write("out.xml")
  78         code = tostring(tree)
  79         open(self.xml,"w").write(self.xmlhead+code)
  80 if __name__ == '__main__':
  81     """基本运行模式
  82     """
  83     week = modifiles()
  84     week.checking()

编写过程

八股文般的开始

收集必要参数

XML汇出

ElementTree

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