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