OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 """Takes the output of the build step and turns it into a compressed | 6 """Takes the output of the build step and turns it into a compressed |
7 archive ready for distribution. | 7 archive ready for distribution. |
8 | 8 |
9 This script assumes the build script has been run to compile the add-in. | 9 This script assumes the build script has been run to compile the add-in. |
10 It zips up all files required for the add-in installation and places the | 10 It zips up all files required for the add-in installation and places the |
11 result in out/vs_addin/vs_addin.tgz. | 11 result in out/vs_addin/vs_addin.tgz. |
12 """ | 12 """ |
13 | 13 |
14 import os | 14 import os |
15 import re | 15 import re |
16 import fileinput | 16 import fileinput |
17 import win32api | 17 import win32api |
18 import shutil | 18 import shutil |
19 import tarfile | 19 import tarfile |
20 import zipfile | 20 import zipfile |
| 21 import string |
21 import sys | 22 import sys |
22 from os.path import join | 23 from os.path import join |
23 | 24 |
24 SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__)) | 25 SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__)) |
25 | 26 |
26 # Checkout root | 27 # Checkout root |
27 ROOT = os.path.dirname(os.path.dirname(SCRIPT_DIR)) | 28 ROOT = os.path.dirname(os.path.dirname(SCRIPT_DIR)) |
28 | 29 |
29 # Root output directory. | 30 # Root output directory. |
30 BUILD_DIR = join(ROOT, 'out', 'vs_addin') | 31 BUILD_DIR = join(ROOT, 'out', 'vs_addin') |
31 | 32 |
32 # Directory that contains the build assemblies. | 33 # Directory that contains the build assemblies. |
33 ASSEMBLY_DIRECTORY = join(BUILD_DIR, 'Debug') | 34 ASSEMBLY_DIRECTORY_2010 = join(BUILD_DIR, '2010', 'Debug') |
| 35 ASSEMBLY_DIRECTORY_2012 = join(BUILD_DIR, '2012', 'Debug') |
34 | 36 |
35 # Directory containing static installer resources. | 37 # Directory containing static installer resources. |
36 RESOURCE_DIRECTORY = join(SCRIPT_DIR, 'InstallerResources') | 38 RESOURCE_DIRECTORY = join(SCRIPT_DIR, 'InstallerResources') |
37 | 39 |
38 # Base name of the final zip file. | 40 # Base name of the final zip file. |
39 OUTPUT_NAME = join(BUILD_DIR, 'vs_addin.tgz') | 41 OUTPUT_NAME = join(BUILD_DIR, 'vs_addin.tgz') |
40 | 42 |
41 # AddIn metadata file path. We will modify this with the version #. | 43 # AddIn metadata file path. We will modify this with the version #. |
42 ADDIN_METADATA = join(RESOURCE_DIRECTORY, 'NativeClientVSAddIn.AddIn') | 44 ADDIN_METADATA = join(RESOURCE_DIRECTORY, 'NativeClientVSAddIn.AddIn') |
43 | 45 |
44 # AddIn dll file path. We will obtain our add-in version from this. | 46 # AddIn dll file path. We will obtain our add-in version from this. |
45 ADDIN_ASSEMBLY = join(ASSEMBLY_DIRECTORY, 'NativeClientVSAddIn.dll') | 47 ADDIN_ASSEMBLY_2010 = join(ASSEMBLY_DIRECTORY_2010, 'NativeClientVSAddIn.dll') |
| 48 ADDIN_ASSEMBLY_2012 = join(ASSEMBLY_DIRECTORY_2012, 'NativeClientVSAddIn.dll') |
46 | 49 |
47 # Regex list to exclude from the archive. If a file path matches any of the | 50 # Regex list to exclude from the archive. If a file path matches any of the |
48 # expressions during a call to AddFolderToArchive it is excluded from the | 51 # expressions during a call to AddFolderToArchive it is excluded from the |
49 # archive file. | 52 # archive file. |
50 EXCLUDES = [ | 53 EXCLUDES = [ |
51 r'\.svn', # Exclude .svn directories. | 54 r'\.svn', # Exclude .svn directories. |
52 r'\.swp', # Exclude .swp files. | 55 r'\.swp$', # Exclude .swp files. |
| 56 r'\.pyc$', # Exclude .pyc files. |
53 r'examples\\.*\\chrome_data', | 57 r'examples\\.*\\chrome_data', |
54 r'examples\\.*\\Debug', | 58 r'examples\\.*\\Debug', |
55 r'examples\\.*\\newlib', | 59 r'examples\\.*\\newlib', |
56 r'examples\\.*\\glibc', | 60 r'examples\\.*\\glibc', |
57 r'examples\\.*\\PNaCl', | 61 r'examples\\.*\\PNaCl', |
58 r'examples\\.*\\win', | 62 r'examples\\.*\\win', |
59 r'examples\\.*\\ipch', | 63 r'examples\\.*\\ipch', |
60 r'examples\\.*\\*.sdf', | 64 r'examples\\.*\\*.sdf', |
61 r'examples\\.*\\*.suo', | 65 r'examples\\.*\\*.suo', |
62 # Exclude .AddIn file for now since we need to modify it with version info. | 66 # Exclude .AddIn file for now since we need to modify it with version info. |
63 re.escape(ADDIN_METADATA)] | 67 re.escape(ADDIN_METADATA)] |
64 | 68 |
65 # List of source/destination pairs to include in archive file. | 69 # List of source/destination pairs to include in archive file. |
66 FILE_LIST = [ | 70 FILE_LIST = [ |
67 (ADDIN_ASSEMBLY, ''), | 71 (ADDIN_ASSEMBLY_2010, '2010'), |
68 (join(ASSEMBLY_DIRECTORY, 'NaCl.Build.CPPTasks.dll'), 'NaCl')] | 72 (ADDIN_ASSEMBLY_2012, '2012'), |
| 73 (join(ASSEMBLY_DIRECTORY_2010, 'NativeClientVSAddIn.AddIn'), '2010'), |
| 74 (join(ASSEMBLY_DIRECTORY_2012, 'NativeClientVSAddIn.AddIn'), '2012'), |
| 75 (join(ASSEMBLY_DIRECTORY_2010, 'NaCl.Build.CPPTasks.dll'), 'NaCl')] |
69 | 76 |
70 | 77 |
71 def AddFolderToArchive(path, archive, root=""): | 78 def AddFolderToArchive(path, archive, root=""): |
72 """Adds an entire folder and sub folders to an open archive object. | 79 """Adds an entire folder and sub folders to an open archive object. |
73 | 80 |
74 The archive must already be open and it is not closed by this function. | 81 The archive must already be open and it is not closed by this function. |
75 | 82 |
76 Args: | 83 Args: |
77 path: Folder to add. | 84 path: Folder to add. |
78 archive: Already open archive file. | 85 archive: Already open archive file. |
79 | 86 |
80 Returns: | 87 Returns: |
81 Nothing. | 88 Nothing. |
82 """ | 89 """ |
83 # Ensure the path ends in trailing slash. | 90 # Ensure the path ends in trailing slash. |
84 path = path.rstrip("/\\") + "\\" | 91 path = path.rstrip("/\\") + "\\" |
85 for dir_path, dir_names, files in os.walk(path): | 92 for dir_path, dir_names, files in os.walk(path): |
86 for filename in files: | 93 for filename in files: |
87 read_path = join(dir_path, filename) | 94 read_path = join(dir_path, filename) |
88 | 95 |
89 # If the file path matches an exclude, don't include it. | 96 # If the file path matches an exclude, don't include it. |
90 if any(re.search(expr, read_path) for expr in EXCLUDES): | 97 if any(re.search(expr, read_path) for expr in EXCLUDES): |
91 continue | 98 continue |
92 | 99 |
93 zip_based_dir = dir_path[len(path):] | 100 zip_based_dir = dir_path[len(path):] |
94 write_path = join(root, zip_based_dir, filename) | 101 write_path = join(root, zip_based_dir, filename) |
95 WriteFileToArchive(archive, read_path, write_path) | 102 WriteFileToArchive(archive, read_path, write_path) |
96 | 103 |
97 | 104 |
98 def AddVersionModifiedAddinFile(archive): | 105 def CopyAddinFile(assembly, path, vs_version): |
99 """Modifies the .AddIn file with the build version and adds to the zip. | 106 """Copy the .AddIn file to the given path while making the necessary |
| 107 replacements. |
100 | 108 |
101 The version number is obtained from the NativeClientAddIn.dll assembly which | 109 The version number is obtained from the NativeClientAddIn.dll assembly which |
102 is built during the build process. | 110 is built during the build process. |
103 | |
104 Args: | |
105 archive: Already open archive file. | |
106 """ | 111 """ |
107 path = '\\VarFileInfo\\Translation' | 112 infopath = '\\VarFileInfo\\Translation' |
108 pairs = win32api.GetFileVersionInfo(ADDIN_ASSEMBLY, path) | 113 pairs = win32api.GetFileVersionInfo(assembly, infopath) |
109 lang, codepage = pairs[0] | 114 lang, codepage = pairs[0] |
110 path = u'\\StringFileInfo\\%04X%04X\\ProductVersion' % (lang, codepage) | 115 infopath = u'\\StringFileInfo\\%04X%04X\\ProductVersion' % (lang, codepage) |
111 prodVersion = win32api.GetFileVersionInfo(ADDIN_ASSEMBLY, path) | 116 prodVersion = win32api.GetFileVersionInfo(assembly, infopath) |
112 version = "[%s]" % prodVersion | 117 version = "[%s]" % prodVersion |
113 print "\nNaCl VS Add-in Build version: %s\n" % (version) | 118 print "\nNaCl VS Add-in Build version: %s\n" % (version) |
114 | 119 |
115 metadata_filename = os.path.basename(ADDIN_METADATA) | 120 metadata_filename = os.path.basename(ADDIN_METADATA) |
116 modified_file = join(ASSEMBLY_DIRECTORY, metadata_filename) | 121 modified_file = join(path, metadata_filename) |
117 | 122 |
118 # Copy the metadata file to new location and modify the version info. | 123 # Copy the metadata file to new location and modify the version info. |
119 with open(ADDIN_METADATA, 'r') as source_file: | 124 with open(ADDIN_METADATA, 'r') as source_file: |
120 with open(modified_file, 'w') as dest_file: | 125 with open(modified_file, 'w') as dest_file: |
121 for line in source_file: | 126 data = source_file.read() |
122 dest_file.write(line.replace("[REPLACE_ADDIN_VERSION]", version)) | 127 replacements = {'VS_VERSION': vs_version, 'ADDIN_VERSION': version} |
123 | 128 data = string.Template(data).substitute(replacements) |
124 WriteFileToArchive(archive, modified_file, metadata_filename) | 129 dest_file.write(data) |
125 | 130 |
126 | 131 |
127 def Error(msg): | 132 def Error(msg): |
128 sys.stderr.write(msg + '\n') | 133 sys.stderr.write(msg + '\n') |
129 sys.exit(1) | 134 sys.exit(1) |
130 | 135 |
131 | 136 |
132 def WriteFileToArchive(archive, filename, archive_name): | 137 def WriteFileToArchive(archive, filename, archive_name): |
133 archive_name = join('vs_addin', archive_name) | 138 archive_name = join('vs_addin', archive_name) |
134 if archive_name.replace('\\', '/') in archive.getnames(): | 139 if archive_name.replace('\\', '/') in archive.getnames(): |
135 print 'Skipping: %s' % archive_name | 140 print 'Skipping: %s' % archive_name |
136 return | 141 return |
137 print 'Adding: %s' % archive_name | 142 print 'Adding: %s' % archive_name |
138 archive.add(filename, archive_name) | 143 archive.add(filename, archive_name) |
139 | 144 |
140 | 145 |
141 def CopyWithReplacement(src, dest, replacements): | 146 def CopyWithReplacement(src, dest, replacements): |
142 if os.path.exists(dest): | 147 if os.path.exists(dest): |
143 shutil.rmtree(dest) | 148 shutil.rmtree(dest) |
144 os.makedirs(dest) | 149 os.makedirs(dest) |
145 src_basename = os.path.basename(src) | 150 src_basename = os.path.basename(src) |
146 dest_basename = os.path.basename(dest) | 151 dest_basename = os.path.basename(dest) |
147 for filename in os.listdir(src): | 152 |
148 srcfile = join(src, filename) | 153 olddir = os.getcwd() |
149 # skip non-files, in particular .svn folders. | 154 try: |
150 if not os.path.isfile(srcfile): | 155 os.chdir(src) |
151 continue | 156 for root, dirs, filenames in os.walk('.'): |
152 destfile = join(dest, filename.replace(src_basename, dest_basename)) | 157 for filename in filenames: |
153 with open(srcfile, "rb") as f: | 158 srcfile = join(root, filename) |
154 data = f.read() | 159 # skip non-files, in particular .svn folders. |
155 for pat, subst in replacements.iteritems(): | 160 if not os.path.isfile(srcfile): |
156 data = data.replace(pat, subst) | 161 continue |
157 with open(destfile, "wb") as f: | 162 |
158 f.write(data) | 163 destdir = join(dest, root.replace(src_basename, dest_basename)) |
| 164 if not os.path.exists(destdir): |
| 165 os.makedirs(destdir) |
| 166 |
| 167 destfile = join(destdir, filename.replace(src_basename, dest_basename)) |
| 168 with open(srcfile, "rb") as f: |
| 169 data = f.read() |
| 170 for pat, subst in replacements.iteritems(): |
| 171 data = data.replace(pat, subst) |
| 172 with open(destfile, "wb") as f: |
| 173 f.write(data) |
| 174 finally: |
| 175 os.chdir(olddir) |
159 | 176 |
160 | 177 |
161 def main(): | 178 def main(): |
162 if not os.path.exists(BUILD_DIR): | 179 if not os.path.exists(BUILD_DIR): |
163 Error("build dir not found: %s" % BUILD_DIR) | 180 Error("build dir not found: %s" % BUILD_DIR) |
164 | 181 |
| 182 CopyAddinFile(ADDIN_ASSEMBLY_2010, ASSEMBLY_DIRECTORY_2010, '10.0') |
| 183 CopyAddinFile(ADDIN_ASSEMBLY_2012, ASSEMBLY_DIRECTORY_2012, '11.0') |
| 184 |
165 archive = tarfile.open(OUTPUT_NAME, 'w:gz') | 185 archive = tarfile.open(OUTPUT_NAME, 'w:gz') |
| 186 |
166 for source_dest in FILE_LIST: | 187 for source_dest in FILE_LIST: |
167 file_name = os.path.basename(source_dest[0]) | 188 file_name = os.path.basename(source_dest[0]) |
168 dest = join(source_dest[1], file_name) | 189 dest = join(source_dest[1], file_name) |
169 WriteFileToArchive(archive, source_dest[0], dest) | 190 WriteFileToArchive(archive, source_dest[0], dest) |
170 | 191 |
171 AddFolderToArchive(RESOURCE_DIRECTORY, archive) | 192 AddFolderToArchive(RESOURCE_DIRECTORY, archive) |
172 | 193 |
173 # Duplicate the NaCl64 platform but rename it to NaCl32 | 194 # Duplicate the NaCl64 platform but rename it to NaCl32 |
174 src = join(RESOURCE_DIRECTORY, 'NaCl64') | 195 src = join(RESOURCE_DIRECTORY, 'NaCl64') |
175 | 196 |
(...skipping 20 matching lines...) Expand all Loading... |
196 '64': '32', | 217 '64': '32', |
197 '.nexe': '.pexe', | 218 '.nexe': '.pexe', |
198 'nacl_link.xml': 'pnacl_link.xml', | 219 'nacl_link.xml': 'pnacl_link.xml', |
199 '$(ProjectName)_$(PlatformArchitecture)': '$(ProjectName)', | 220 '$(ProjectName)_$(PlatformArchitecture)': '$(ProjectName)', |
200 } | 221 } |
201 | 222 |
202 dest = join(BUILD_DIR, 'PNaCl') | 223 dest = join(BUILD_DIR, 'PNaCl') |
203 CopyWithReplacement(src, dest, pnacl_replacements) | 224 CopyWithReplacement(src, dest, pnacl_replacements) |
204 AddFolderToArchive(dest, archive, "PNaCl") | 225 AddFolderToArchive(dest, archive, "PNaCl") |
205 | 226 |
206 AddVersionModifiedAddinFile(archive) | |
207 archive.close() | 227 archive.close() |
208 | 228 |
209 | 229 |
210 if __name__ == '__main__': | 230 if __name__ == '__main__': |
211 main() | 231 main() |
OLD | NEW |