OLD | NEW |
---|---|
(Empty) | |
1 #!/usr/bin/env python | |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
3 # Use of this source code is governed by a BSD-style license that can be | |
4 # found in the LICENSE file. | |
5 | |
6 """ This script creates the PPAPI project settings template. | |
7 | |
8 For copyright reasons, we should not directly distribute the PPAPI template | |
9 because it is nearly a clone of the Win32 template which is Copyrighted. | |
10 Instead, this script copies the existing Win32 template from the user's system | |
11 and intelligently modifies the copy to be the PPAPI template. | |
12 """ | |
13 | |
14 import pdb | |
binji
2012/07/24 22:13:08
remove debugging code
tysand
2012/07/24 23:19:49
Done.
| |
15 import os | |
16 import shutil | |
17 import string | |
18 import xml_patch | |
19 import xml.etree.ElementTree | |
20 | |
21 SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__)) | |
22 | |
23 PLATFORM_FILES = [ | |
24 ('Microsoft.Cpp.Win32.default.props', | |
25 'Microsoft.Cpp.[platform].default.props.patch', | |
26 'Microsoft.Cpp.PPAPI.default.props'), | |
27 ('Microsoft.Cpp.Win32.props', | |
28 'Microsoft.Cpp.[platform].props.patch', | |
29 'Microsoft.Cpp.PPAPI.props'), | |
30 ('Microsoft.Cpp.Win32.targets', | |
31 'Microsoft.Cpp.[platform].targets.patch', | |
32 'Microsoft.Cpp.PPAPI.targets'), | |
33 ('PlatformToolsets\\v100\\Microsoft.Cpp.Win32.v100.props', | |
34 'PlatformToolsets\\v100\\Microsoft.Cpp.[platform].v100.props.patch', | |
35 'PlatformToolsets\\v100\\Microsoft.Cpp.PPAPI.v100.props'), | |
36 ('PlatformToolsets\\v100\\Microsoft.Cpp.Win32.v100.targets', | |
37 'PlatformToolsets\\v100\\Microsoft.Cpp.[platform].v100.targets.patch', | |
38 'PlatformToolsets\\v100\\Microsoft.Cpp.PPAPI.v100.targets')] | |
39 | |
40 UI_FILES = [ | |
41 ('general.xml', | |
42 'Props\\ppapi_general.xml.patch', | |
43 'Props\\ppapi_general.xml'), | |
44 ('general_ps.xml', | |
45 'Props\\ppapi_general_ps.xml.patch', | |
46 'Props\\ppapi_general_ps.xml')] | |
47 | |
48 COPY_FILES = [ | |
49 'ImportAfter\\PPAPI.override.props'] | |
50 | |
51 | |
52 def PrependCopyright(source_file_name, dest_file_name): | |
53 """Adds the copyright notice from source file to the dest file. | |
54 | |
55 Since the patch_xml function does not read comments, the copyright is skipped | |
56 during the initial copy. This function adds it back and also attaches a | |
57 notice that the file was based on source_file_name and slightly modified. | |
58 | |
59 Args: | |
60 source_file_name: The original Win32 file. | |
61 dest_file_name: The existing PPAPI file. | |
62 | |
63 Returns: | |
64 None. | |
65 """ | |
66 with open(source_file_name, 'r') as source_file: | |
67 in_copyright = False | |
68 copyright_notice = '' | |
69 for line in source_file: | |
70 if '<!--' in line: | |
71 in_copyright = True | |
72 if in_copyright: | |
73 copyright_notice += line | |
74 if '-->' in line: | |
75 in_copyright = False | |
76 break | |
77 | |
78 with open(dest_file_name, 'r') as original: | |
79 xml_data = original.read() | |
80 | |
81 chrome_notice = ('<!-- This file has been copied and modified from %s during ' | |
82 'the installation process. -->\n\n' % (source_file_name)) | |
83 | |
84 with open(dest_file_name, 'w') as changed: | |
85 changed.writelines(copyright_notice + chrome_notice + xml_data) | |
86 | |
87 | |
88 def CreateTemplateFile(source, patch, dest): | |
89 """Creates a single PPAPI template file. | |
90 | |
91 Args: | |
92 source: The path source file to create from. | |
93 patch: The path to the patch file to apply. | |
94 dest: The path to the file to create. | |
95 Returns: | |
96 None. | |
97 """ | |
98 source_xml = xml.etree.ElementTree.parse(source) | |
99 patch_xml = xml.etree.ElementTree.parse(patch) | |
100 modified_xml = xml_patch.PatchXML(source_xml, patch_xml) | |
101 | |
102 if not os.path.exists(os.path.dirname(dest)): | |
103 os.makedirs(os.path.dirname(dest)) | |
104 | |
105 FixAttributesNamespace(modified_xml) | |
106 default_namespace = GetDefaultNamespace(modified_xml) | |
107 modified_xml.write(dest, default_namespace=default_namespace) | |
108 PrependCopyright(source, dest) | |
109 | |
110 | |
111 def GetDefaultNamespace(tree): | |
112 # Returns the uri (namespace identifier) of the root element. | |
113 tag = tree.getroot().tag | |
114 if tag[:1] == "{": | |
binji
2012/07/24 22:13:08
nice trick, tag[:1] is the same as tag[0] but it w
tysand
2012/07/24 23:19:49
Changed this to tag.startswith("{") following your
| |
115 uri, rest = tag[1:].rsplit("}", 1) | |
116 return uri | |
117 else: | |
118 return None | |
119 | |
120 | |
121 def FixAttributesNamespace(tree): | |
122 # ElementTree's implementation seems to be broken in that attributes | |
123 # do not inherit the default namespace of their node or parent nodes. | |
124 # This causes issues with ElementTree.write() when using a default namespace. | |
125 # Since the attributes do not have a namespace, the code that collects a | |
126 # mapping between local names and qualified names (with a namespace) breaks. | |
127 # The work-around is to give all attributes the default namespace. | |
128 default_namespace = GetDefaultNamespace(tree) | |
129 for elem in tree.getroot().getiterator(): | |
130 new_attrib = dict() | |
131 for key, value in elem.attrib.items(): | |
132 # If the attribute does not have a namespace yet then give it one. | |
133 if key[:1] != "{": | |
134 new_key = "{%s}%s" % (default_namespace, key) | |
135 new_attrib[new_key] = value | |
136 else: | |
137 new_attrib[key] = value | |
138 elem.attrib = new_attrib | |
139 | |
140 | |
141 def main(): | |
142 install_dir = os.path.expandvars( | |
143 '%ProgramFiles(x86)%\\MSBuild\\Microsoft.Cpp\\v4.0\\Platforms') | |
144 | |
145 # Note 1033 is code for the english language. | |
146 ui_xml_dir = os.path.expandvars( | |
147 '%ProgramFiles(x86)%\\MSBuild\\Microsoft.Cpp\\v4.0\\1033') | |
148 | |
149 win32_dir = os.path.join(install_dir, 'Win32') | |
150 ppapi_dir = os.path.join(install_dir, 'PPAPI') | |
151 patch_dir = os.path.join(SCRIPT_DIR, 'PPAPI_Patch') | |
152 | |
153 if not os.path.exists(win32_dir): | |
154 raise Exception('Win32 platform is not installed on this machine!') | |
155 | |
156 for template_creation in PLATFORM_FILES: | |
157 CreateTemplateFile( | |
158 os.path.join(win32_dir, template_creation[0]), | |
159 os.path.join(patch_dir, template_creation[1]), | |
160 os.path.join(ppapi_dir, template_creation[2])) | |
161 | |
162 for template_creation in UI_FILES: | |
163 CreateTemplateFile( | |
164 os.path.join(ui_xml_dir, template_creation[0]), | |
165 os.path.join(patch_dir, template_creation[1]), | |
166 os.path.join(ppapi_dir, template_creation[2])) | |
167 | |
168 for file_name in COPY_FILES: | |
169 copy_from = os.path.join(patch_dir, file_name) | |
170 copy_to = os.path.join(ppapi_dir, file_name) | |
171 if not os.path.exists(os.path.dirname(copy_to)): | |
172 os.makedirs(os.path.dirname(copy_to)) | |
173 shutil.copyfile(copy_from, copy_to) | |
174 | |
175 shutil.copyfile( | |
176 os.path.join(win32_dir, 'Microsoft.Build.CPPTasks.Win32.dll'), | |
177 os.path.join(ppapi_dir, 'Microsoft.Build.CPPTasks.PPAPI.dll')) | |
178 | |
179 | |
180 if __name__ == '__main__': | |
181 main() | |
OLD | NEW |