def counting(self): # 每个COUNT_STATICS输出一个统计文件 for i in COUNT_STATICS: ci = CountingItem(i) fs = ci.get_counting_files() fbasename = ci.get_counting_file_name() cr_one = CountingResult() if COUNTING_DETAILS: report_dirname = fbasename + time.strftime(FILE_TIME_FORMAT) + REPORT_SUFFIX report_file = codecs.open(report_dirname, mode="w", encoding="utf-8") assert report_file count_sum = len(fs) ncounter = 1 usable.log("%s的文件总数: %d" % (fbasename, count_sum)) stime = time.time() for j in fs: if j.endswith("rc"): c = Counting_RC(j) else: c = Couting_CPP(j) cr = c.counting() if not cr._success: usable.error("统计文件%s 失败。" % (j)) else: cr_one += cr if COUNTING_DETAILS: report_file.write(cr.get_details()) if SHOW_PROCESSES: usable.log("[%s:进度 %d/%d] :%s" % (fbasename, ncounter, count_sum, j)) ncounter += 1 if COUNTING_DETAILS: report_file.close() fname = ci.get_counting_file_name() + COUNTING_RESULT_SUFFIX cr_one._filecount = len(fs) cr_one._filelist = fs cr_one.save_to(fname) usable.log("%s 统计耗时:%f" % (fbasename, time.time() - stime))
def save_to(self, filename): try: with codecs.open(filename, "w+", encoding="utf-8") as f: lns = [] lns.append("代码行数: %d" % (self._codelines,)) lns.append("注释行数: %d" % (self._commentlines,)) lns.append("空白行数: %d" % (self._spacelines,)) lns.append("文件个数: %d" % (self._filecount,)) if COUNT_FILE_LIST: lns.append("文件列表:") for n in self._filelist: lns.append(n) f.write(TEXT_LINE_SEPRATOR.join(lns)) except Exception, e: usable.error("保存统计信息到%s 失败, 错误信息:%s" % (filename, e.message)) f.close()
def get_counting_files(self): def get_items(): items = [] if isinstance(self._item, str): items.append(self._item) elif isinstance(self._item, tuple): items = list(self._item) return items items = get_items() fs = [] for i in items: if i not in COUNT_ITEMS: usable.error("配置文件COUNT_ITEMS中不包含:%s" % (i,)) for j in COUNT_ITEMS[i]: if os.path.isfile(j): fs.append(j) elif os.path.isdir(j): fs += find_files(j, FILE_FILTER, True) else: usable.error("无效文件,非文件也非文件夹!") return fs
def error(self, msg): usable.error(msg)