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