blob: 730416056ed459ec1dc19fb39663d17b6d19f26a [file] [log] [blame]
Tang Cheng7ea3d852012-07-06 08:54:51 +08001# -*- coding: utf-8
2'''
3Created on 2012-2-25
4更新 VC2008 工程编译输出的临时文件路径
5@author: cheng.tang
6'''
7
8
9import os,sys,re
10from optparse import OptionParser
11import xml.dom.minidom
12
13class vcprojred:
14 XML_HEADER_RE = re.compile(r'<\?\s*xml\s*version=\"([0-9.]*)\"\s*encoding=\"(\w*)\"\s*\?>')
15 def __init__(self):
16 self.root_dir_ = None
17 self.proj_count_ = 0
18
19 def run(self,argv):
20 self._parse_opt(argv)
21 self._run_dir(self.options.solutiondir)
22
23 def error(self,msg,exitcode = 1):
24 print "Error: %s" % msg
25 if exitcode > 0:
26 sys.exit(exitcode)
27
28 def _run_dir(self,soludir):
Tang Chengd4431b22012-07-06 09:07:25 +080029 soludir = os.path.realpath(soludir)
Tang Cheng7ea3d852012-07-06 08:54:51 +080030 if not os.path.isdir(soludir):
31 self.error(u"解决方案路径不存在")
32 self._check_dir(soludir)
33 print u"处理工程[%d]个" % self.proj_count_
34
35 def _check_dir(self,projdir):
36 if os.path.basename(projdir) in ["..","."]:
37 return
38 files = os.listdir(projdir)
39 for item in files:
40 full_name = os.path.join(projdir,item)
41 if os.path.isdir(full_name):
42 self._check_dir(full_name)
43 elif os.path.isfile(full_name):
44 self._deal_proj(full_name)
45 else:
46 self.error(u"文件[%s]异常")
47
48 def _load_xml_file(self,filename):
49 xml = open(filename,'r')
50 lines = xml.readlines()
51 xml.close()
52 if len(lines) == 0:
53 return None
54 m = vcprojred.XML_HEADER_RE.search(lines[0])
55 encoding = 'gb2312'
56 if m:
57 encoding = m.group(2)
58 try:
59 l = re.compile(encoding).sub('utf-8',lines[0])
60 lines[0] = l
61 content = ''.join(lines).decode(encoding).encode('utf-8')
62 except Exception,ex:
63 self.error(u"格式错误,%s" % ex, 0)
64 content = None
65
66 return (content,encoding)
67
68 def _save_xml_file(self,filename,content,encoding):
69 new_file = filename + '.bak'
70 if os.path.exists(new_file):
71 os.unlink(new_file)
72 os.rename(filename, new_file)
73 new_content = content
74 with open(filename,'ab') as f:
75 f.write(new_content)
76 f.close()
77
78 def _deal_proj(self,projfile):
79 file_name = os.path.basename(projfile)
80 exts = os.path.splitext(file_name)
81 if exts[-1] not in ['.vcproj']:
82 return None
83 # parse project
84 print u"处理工程 [%s]" % projfile
85
86 prjdom = xml_encoding = None
87 try:
88 content,xml_encoding = self._load_xml_file(projfile)
89 prjdom = xml.dom.minidom.parseString(content)
90 except Exception,ex:
91 self.error(u'分析错误,[%s]%s' % (projfile,ex))
92
93 if not prjdom:
94 self.error(u'文件[%s]解析错误' % projfile,0)
95 return False
96
97 prj_nodes = prjdom.getElementsByTagName("VisualStudioProject")
98 if not prj_nodes:
99 return
100 configurations_nodes = prj_nodes[0].getElementsByTagName("Configurations")
101 if not configurations_nodes:
102 return
103 configuration_nodes = configurations_nodes[0].getElementsByTagName("Configuration")
104
105 if not configuration_nodes:
106 return
107
108 for config in configuration_nodes:
109 #print "Config[%s]" % config.getAttribute("Name")
110 config_name = config.getAttribute("Name")
111 intermediate_dir = config.getAttribute("IntermediateDirectory")
112 #print "Config[%s]dir[%s]" % (config_name,intermediate_dir)
113 new_path = "$(SolutionDir)%s\\$(ProjectName)\\$(ConfigurationName)" % self.options.outputdir
114 config.setAttribute("IntermediateDirectory",new_path)
115
116 self._save_xml_file(projfile, prjdom.toxml(xml_encoding), xml_encoding)
117 self.proj_count_ += 1
118
119
120 def _parse_opt(self,argv):
121 parser = OptionParser()
122 parser.add_option("-d","--destination",dest="solutiondir",type="string")
123 parser.add_option("-o","--output_name",dest="outputdir",type="string",default="build")
124 (options,args) = parser.parse_args()
125 self.options = options
126 self.args = args
127 if not self.options.solutiondir:
128 parser.error(u"未指定工程路径")
129
130
131if __name__ == "__main__":
132 red = vcprojred()
133 red.run(sys.argv)