##############################
python去除C注释rmanno-zz170719
zz @ 2017-7-19
REF:
rman.3walkrmanno.py
##############################
#!/usr/bin/env python
# -*- coding:gb2312 -*-
#zz# coding=gbk
# rman.2walkallfn
#
from __future__ import print_function
import os
import sys
import re
import platform
import shutil
## 1. python version
def pr_ver():
print("Platform python_version:",platform.python_version())
## 2. main
def main():
print("module name:",__name__)
pr_ver()
#zz# main test infos..
if(0 > 0):
print("argv number:",len(sys.argv))
for i in range(len(sys.argv)):
print(sys.argv[i])
if len(sys.argv)<3 :
print();
print("usage: %s srcdir dstdir" %(sys.argv[0]))
#zz# walk all file
if(0 > 0):
print("os.sep",os.sep)
# for list_all in os.walk(os.path.dirname("G:\\zzrd_dingwork\\zztool_bsp_cl\\python_funs\\rm_anno")):
# for list_all in os.walk("G:\\zzrd_dingwork\\zztool_bsp_cl\\python_funs\\rm_anno"):
for list_all in os.walk(os.path.abspath("dir_src")):
print(list_all)
print()
#zz# 0. help info
if len(sys.argv)<3:
print()
print("usage: %s srcdir dstdir" % (sys.argv[0]))
print()
# return
exit(1)
else:
print("src[%s] ==> dest[%s]\n" % ( os.path.abspath(sys.argv[1]),os.path.abspath(sys.argv[2]) ) )
#zz# 1.print absolute path from a relative dir name
# dir_dest = os.path.abspath("dir_dest")
# dir_src = os.path.abspath("dir_src")
# print("src[%s] ==> dest[%s] !\n" % (dir_src,dir_dest) )
dir_src = os.path.abspath(sys.argv[1])
dir_dest = os.path.abspath(sys.argv[2])
print("src[%s] ==> dest[%s] !\n" % (dir_src,dir_dest) )
#zz# 2.walk all the files in the relative dir
for parents,dirs,files in os.walk(dir_src):
for fn in files:
if fn.endswith(".c") or fn.endswith(".h") or fn.endswith(".cc") or fn.endswith(".cpp"):
#zz# print(os.path.join(parents,fn))
#zz# print("%s%s%s" % (parents,os.sep,fn))
abs_src_fn = os.path.join(parents,fn)
abs_dst_fn = os.path.join(dir_dest,fn)
#zz# os.makedirs("hello")
# shutil.copy(abs_src_fn,abs_dst_fn)
rm_anno_cfile(abs_src_fn,abs_dst_fn)
#zz# remove annotate example
# rm_anno_cfile()
## 3. deal one file
def rm_anno_cfile(fn_src="fun_do_rty.c",fn_dest="fun_do_rty.c.txt"):
if not os.path.isfile(fn_src):
print("!!! File: %s, Not Exist" % fn_src)
return
#zz# 文件全路径 => 文件名 => 文件名无后缀
fn_filename = os.path.splitext(fn_src)[0]
fn_filetitle = os.path.basename(fn_filename)
#zz# 打开 in out 文件逐行处理
#zz# fn_dest = "new" + fn_filetitle + ".orig"
ff_out = open(fn_dest,"wb+")
ff_in = open(fn_src,"r")
list_in = ff_in.readlines()
ff_in.close()
#zz# 创建搜索用的,正则表达式
# re_anno = re.compile("//zz")
re_anno = re.compile("\s*//zz")
for line in list_in:
# 找到含有规定注释的行
res_anno = re_anno.search(line)
if None<>res_anno:
continue
else:
ff_out.write(line)
ff_out.close()
## 0. run main as default
if __name__ == "__main__" :
print("__main__ call!")
main()
##############################
阅读(945) | 评论(0) | 转发(0) |