# -*- coding: utf-8 | |
''' | |
Created on 2012-2-25 | |
更新 VC2008 工程编译输出的临时文件路径 | |
@author: cheng.tang | |
''' | |
import os,sys,re | |
from optparse import OptionParser | |
import xml.dom.minidom | |
class vcprojred: | |
XML_HEADER_RE = re.compile(r'<\?\s*xml\s*version=\"([0-9.]*)\"\s*encoding=\"(\w*)\"\s*\?>') | |
def __init__(self): | |
self.root_dir_ = None | |
self.proj_count_ = 0 | |
def run(self,argv): | |
self._parse_opt(argv) | |
self._run_dir(self.options.solutiondir) | |
def error(self,msg,exitcode = 1): | |
print "Error: %s" % msg | |
if exitcode > 0: | |
sys.exit(exitcode) | |
def _run_dir(self,soludir): | |
if not os.path.isdir(soludir): | |
self.error(u"解决方案路径不存在") | |
self._check_dir(soludir) | |
print u"处理工程[%d]个" % self.proj_count_ | |
def _check_dir(self,projdir): | |
if os.path.basename(projdir) in ["..","."]: | |
return | |
files = os.listdir(projdir) | |
for item in files: | |
full_name = os.path.join(projdir,item) | |
if os.path.isdir(full_name): | |
self._check_dir(full_name) | |
elif os.path.isfile(full_name): | |
self._deal_proj(full_name) | |
else: | |
self.error(u"文件[%s]异常") | |
def _load_xml_file(self,filename): | |
xml = open(filename,'r') | |
lines = xml.readlines() | |
xml.close() | |
if len(lines) == 0: | |
return None | |
m = vcprojred.XML_HEADER_RE.search(lines[0]) | |
encoding = 'gb2312' | |
if m: | |
encoding = m.group(2) | |
try: | |
l = re.compile(encoding).sub('utf-8',lines[0]) | |
lines[0] = l | |
content = ''.join(lines).decode(encoding).encode('utf-8') | |
except Exception,ex: | |
self.error(u"格式错误,%s" % ex, 0) | |
content = None | |
return (content,encoding) | |
def _save_xml_file(self,filename,content,encoding): | |
new_file = filename + '.bak' | |
if os.path.exists(new_file): | |
os.unlink(new_file) | |
os.rename(filename, new_file) | |
new_content = content | |
with open(filename,'ab') as f: | |
f.write(new_content) | |
f.close() | |
def _deal_proj(self,projfile): | |
file_name = os.path.basename(projfile) | |
exts = os.path.splitext(file_name) | |
if exts[-1] not in ['.vcproj']: | |
return None | |
# parse project | |
print u"处理工程 [%s]" % projfile | |
prjdom = xml_encoding = None | |
try: | |
content,xml_encoding = self._load_xml_file(projfile) | |
prjdom = xml.dom.minidom.parseString(content) | |
except Exception,ex: | |
self.error(u'分析错误,[%s]%s' % (projfile,ex)) | |
if not prjdom: | |
self.error(u'文件[%s]解析错误' % projfile,0) | |
return False | |
prj_nodes = prjdom.getElementsByTagName("VisualStudioProject") | |
if not prj_nodes: | |
return | |
configurations_nodes = prj_nodes[0].getElementsByTagName("Configurations") | |
if not configurations_nodes: | |
return | |
configuration_nodes = configurations_nodes[0].getElementsByTagName("Configuration") | |
if not configuration_nodes: | |
return | |
for config in configuration_nodes: | |
#print "Config[%s]" % config.getAttribute("Name") | |
config_name = config.getAttribute("Name") | |
intermediate_dir = config.getAttribute("IntermediateDirectory") | |
#print "Config[%s]dir[%s]" % (config_name,intermediate_dir) | |
new_path = "$(SolutionDir)%s\\$(ProjectName)\\$(ConfigurationName)" % self.options.outputdir | |
config.setAttribute("IntermediateDirectory",new_path) | |
self._save_xml_file(projfile, prjdom.toxml(xml_encoding), xml_encoding) | |
self.proj_count_ += 1 | |
def _parse_opt(self,argv): | |
parser = OptionParser() | |
parser.add_option("-d","--destination",dest="solutiondir",type="string") | |
parser.add_option("-o","--output_name",dest="outputdir",type="string",default="build") | |
(options,args) = parser.parse_args() | |
self.options = options | |
self.args = args | |
if not self.options.solutiondir: | |
parser.error(u"未指定工程路径") | |
if __name__ == "__main__": | |
red = vcprojred() | |
red.run(sys.argv) |