blob: 1eba84d76bf13e72851cb836f54160fbe5b21ea4 [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):
29 if not os.path.isdir(soludir):
30 self.error(u"解决方案路径不存在")
31 self._check_dir(soludir)
32 print u"处理工程[%d]个" % self.proj_count_
33
34 def _check_dir(self,projdir):
35 if os.path.basename(projdir) in ["..","."]:
36 return
37 files = os.listdir(projdir)
38 for item in files:
39 full_name = os.path.join(projdir,item)
40 if os.path.isdir(full_name):
41 self._check_dir(full_name)
42 elif os.path.isfile(full_name):
43 self._deal_proj(full_name)
44 else:
45 self.error(u"文件[%s]异常")
46
47 def _load_xml_file(self,filename):
48 xml = open(filename,'r')
49 lines = xml.readlines()
50 xml.close()
51 if len(lines) == 0:
52 return None
53 m = vcprojred.XML_HEADER_RE.search(lines[0])
54 encoding = 'gb2312'
55 if m:
56 encoding = m.group(2)
57 try:
58 l = re.compile(encoding).sub('utf-8',lines[0])
59 lines[0] = l
60 content = ''.join(lines).decode(encoding).encode('utf-8')
61 except Exception,ex:
62 self.error(u"格式错误,%s" % ex, 0)
63 content = None
64
65 return (content,encoding)
66
67 def _save_xml_file(self,filename,content,encoding):
68 new_file = filename + '.bak'
69 if os.path.exists(new_file):
70 os.unlink(new_file)
71 os.rename(filename, new_file)
72 new_content = content
73 with open(filename,'ab') as f:
74 f.write(new_content)
75 f.close()
76
77 def _deal_proj(self,projfile):
78 file_name = os.path.basename(projfile)
79 exts = os.path.splitext(file_name)
80 if exts[-1] not in ['.vcproj']:
81 return None
82 # parse project
83 print u"处理工程 [%s]" % projfile
84
85 prjdom = xml_encoding = None
86 try:
87 content,xml_encoding = self._load_xml_file(projfile)
88 prjdom = xml.dom.minidom.parseString(content)
89 except Exception,ex:
90 self.error(u'分析错误,[%s]%s' % (projfile,ex))
91
92 if not prjdom:
93 self.error(u'文件[%s]解析错误' % projfile,0)
94 return False
95
96 prj_nodes = prjdom.getElementsByTagName("VisualStudioProject")
97 if not prj_nodes:
98 return
99 configurations_nodes = prj_nodes[0].getElementsByTagName("Configurations")
100 if not configurations_nodes:
101 return
102 configuration_nodes = configurations_nodes[0].getElementsByTagName("Configuration")
103
104 if not configuration_nodes:
105 return
106
107 for config in configuration_nodes:
108 #print "Config[%s]" % config.getAttribute("Name")
109 config_name = config.getAttribute("Name")
110 intermediate_dir = config.getAttribute("IntermediateDirectory")
111 #print "Config[%s]dir[%s]" % (config_name,intermediate_dir)
112 new_path = "$(SolutionDir)%s\\$(ProjectName)\\$(ConfigurationName)" % self.options.outputdir
113 config.setAttribute("IntermediateDirectory",new_path)
114
115 self._save_xml_file(projfile, prjdom.toxml(xml_encoding), xml_encoding)
116 self.proj_count_ += 1
117
118
119 def _parse_opt(self,argv):
120 parser = OptionParser()
121 parser.add_option("-d","--destination",dest="solutiondir",type="string")
122 parser.add_option("-o","--output_name",dest="outputdir",type="string",default="build")
123 (options,args) = parser.parse_args()
124 self.options = options
125 self.args = args
126 if not self.options.solutiondir:
127 parser.error(u"未指定工程路径")
128
129
130if __name__ == "__main__":
131 red = vcprojred()
132 red.run(sys.argv)