# -*- coding: cp936 -*-
import re
from os.path import dirname
import shutil
import os
pattern = '^#\s*include\s+(<)(.+)(>)(.*)$'
prog = re.compile(pattern)
find_sets = []
def create_dir(file_dir):
#print dirname(_dir);
_dir = dirname(file_dir)
if not os.path.exists(_dir):
os.makedirs(_dir)
def copyHeadFile(root_dir, src_path, des_path, deep = 0):
if((root_dir + src_path) in find_sets):
return
if( not os.path.exists(root_dir + src_path) ):
return
create_dir(des_path + src_path)
shutil.copyfile(root_dir + src_path, des_path + src_path);
find_sets.append(root_dir + src_path)
src_file = open(root_dir + src_path, 'r')
print ''.rjust(deep) + des_path + src_path
for line in src_file:
if prog.search(line):
line = re.sub('(^#\s*include\s+(<))|(>)(.*)$', '', line)
line = line.replace('/', '\\')
line = line.rstrip('\n')
copyHeadFile( root_dir, line, des_path, (deep + 4))
copyHeadFile("D:\\Program Files\\Microsoft Visual Studio 8\\VC\\include\\", "stdafx.h", "d:\\need_hpp\\" )
Python偶而玩玩也很爽。 这个脚本的来例:我写的demo用到了不少boost的东西,代码交给领导检查的时候,又不能把整个boost 都拷贝给他,这个脚本能把引用到的头文件导出到指定的目录里面。