利用Python统计源代码行数以及对源代码排版
来源:互联网 发布:仅展示三天朋友圈知乎 编辑:程序博客网 时间:2024/06/08 19:33
近段时间写的一个软件需要做软件著作权登记,需要统计源代码行数以及提交部分源代码,刚好利用Python写了这么一个小工具。
用法如下:
在windows命令行窗口,使用python python_count_line.py source_code_root_directory即可。
其中,source_code_root_directory为需要统计的源代码的根目录,源代码目录中如果有其它子目录,也可以递归遍历。
源代码如下:
import os,sysdef count_line(filename):""" count the line number of file"""with open(filename) as file:lines = file.readlines()file.close()return len(lines)def read_lines_and_write_to_target(source_filename,target_file,begin_linenum):linenum = begin_linenumwith open(source_filename) as source_file:lines = source_file.readlines()for i in range(len(lines)):target_file.write("%4d"%linenum+" ")target_file.write(lines[i])linenum = linenum+1source_file.close()return linenumdef flatten(lst):for x in lst:if isinstance(x,list):for x in flatten(x):yield xelse:yield xdef count_files(root_dir,file_filter):process_list = get_process_files(root_dir,file_filter)process_list = list(flatten(process_list))print "Flatten Process List", print process_listline_count = 0for file in process_list:line_count = line_count + count_line(file)print file,"line_number",line_count#Generate Source Files in Oneline_count = 1target_filename=root_dir+"\\"+"result.txt"with open(target_filename,"w") as target_file:for file in process_list:target_file.write("///////"+file+"///////\n")line_count = read_lines_and_write_to_target(file,target_file,line_count)target_file.close()print "Result is generated in target file:",target_filenamedef get_process_files(root_dir,file_filter):""" process all files in directory that matches file_filter """cur_dir = os.path.abspath(root_dir)file_list = os.listdir(cur_dir)print file_listprocess_list=[]#processing the file_list, retrieve all target files to process_listfor file in file_list:fullfile = cur_dir+"\\"+fileprint fullfileif os.path.isfile(fullfile):print "is file"found_flag = Falsefor filter in file_filter:if file.rfind(filter) != -1:print "match!",fileprocess_list.append(fullfile)found_flag = Trueif found_flag==False:print "pass this file",fullfileelif os.path.isdir(fullfile):print "is dir,recursive"dir_extra_list = get_process_files(fullfile,file_filter)print dir_extra_listif len(dir_extra_list)!=0:process_list.append(dir_extra_list)else:print "not defined"return process_listif __name__ == '__main__':print sys.argvroot_dir = sys.argv[1]print "INFO: Root_Dir:",root_dirfile_filter = [".cpp",".h"]count_files(root_dir,file_filter)
也许以后还用得着。
REF:
http://rosettacode.org/wiki/Flatten_a_list
- 利用Python统计源代码行数以及对源代码排版
- 统计源代码行数
- xcode统计源代码行数
- 统计源代码行数
- xcode统计源代码行数
- xcode统计源代码行数
- xcode统计源代码行数
- 统计源代码行数Shell脚本
- 统计Eclipse工程源代码行数
- IOS项目统计源代码行数
- 统计某文件夹下源代码行数 Python脚本
- 统计源代码行数的shell脚本
- 统计源代码行数Shell脚本[00原创]
- 统计项目源代码文件的行数
- 统计一个目录下源代码行数
- 源代码行数统计工具 Any Code Counter
- Python小工具——统计文件夹下源代码的行数
- 统计项目源代码文件的行数,基于c#(原创)
- 在家也能健身(03):肩三角肌
- 编程打断拆分构件
- kernel: cciss: fifo full
- Linux中TASK_RUNNING状态的进程链表
- Xen 虚拟化背景知识
- 利用Python统计源代码行数以及对源代码排版
- 简明 Vim 练级攻略
- Should I use android: process =“:remote” in my reciver?
- 使用EGL Rich UI访问数据库(5-8课)
- Xen 工作原理与体系架构
- 浅谈移动互联网广告设计
- 解决Beyond Compare中文乱码的问题
- Google Protobuf的使用
- 对象