Chromium Code Reviews| 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 """Creates a zip archive for the Chrome Remote Desktop Host installer. | 6 """Creates a zip archive for the Chrome Remote Desktop Host installer. |
| 7 | 7 |
| 8 This script builds a zip file that contains all the files needed to build an | 8 This script builds a zip file that contains all the files needed to build an |
| 9 installer for Chrome Remote Desktop Host. | 9 installer for Chrome Remote Desktop Host. |
| 10 | 10 |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 64 zipfile_base = os.path.splitext(os.path.basename(zip_path))[0] | 64 zipfile_base = os.path.splitext(os.path.basename(zip_path))[0] |
| 65 zip = zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED) | 65 zip = zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED) |
| 66 for (root, dirs, files) in os.walk(directory): | 66 for (root, dirs, files) in os.walk(directory): |
| 67 for f in files: | 67 for f in files: |
| 68 full_path = os.path.join(root, f) | 68 full_path = os.path.join(root, f) |
| 69 rel_path = os.path.relpath(full_path, directory) | 69 rel_path = os.path.relpath(full_path, directory) |
| 70 zip.write(full_path, os.path.join(zipfile_base, rel_path)) | 70 zip.write(full_path, os.path.join(zipfile_base, rel_path)) |
| 71 zip.close() | 71 zip.close() |
| 72 | 72 |
| 73 | 73 |
| 74 def copyFileIntoArchive(src_file, out_dir, files_root, dst_file, defs): | 74 def calcDestFilePath(out_dir, files_root, dst_file): |
| 75 """Copies the src_file into the out_dir, preserving the directory structure. | 75 """Calculates destination file path and creates directory. |
|
alexeypa (please no reviews)
2012/04/30 16:33:48
nit: This sounds like a perforce mapping. Basicall
garykac
2012/04/30 21:24:39
Done.
| |
| 76 | |
| 77 The |files_root| prefix is stripped from |dst_file| before | |
|
alexeypa (please no reviews)
2012/04/30 16:33:48
What happens if the file name does not start from
garykac
2012/04/30 21:24:39
No. I have a pending cl where I'll be expanding th
| |
| 78 appending to |out_dir|. | |
| 79 | |
| 80 For example, given: | |
| 81 out_dir = /output | |
| 82 files_root = host/installer/mac | |
| 83 dst_file = host/installer/mac/Scripts/keystone_install.sh | |
| 84 The final calculated path is: | |
| 85 /output/Scripts/keystone_install.sh | |
| 76 | 86 |
| 77 Args: | 87 Args: |
| 78 src_file: Full or relative path to source file to copy. | |
| 79 out_dir: Target directory where files are copied. | 88 out_dir: Target directory where files are copied. |
| 80 files_root: Path prefix which is stripped of dst_file before appending | 89 files_root: Path prefix which is stripped of dst_file before appending |
| 81 it to the out_dir. | 90 it to the out_dir. |
| 82 dst_file: Relative path (and filename) where src_file should be copied. | 91 dst_file: Relative path (and filename) where src_file should be copied. |
| 83 defs: Dictionary of variable definitions. | 92 Returns: |
| 93 Full path to destination file in |out_dir|. | |
| 84 """ | 94 """ |
| 85 root_len = len(files_root) | 95 # Strip of directory prefix. |
| 86 local_file_path = dst_file[root_len:] | 96 if dst_file.startswith(files_root): |
| 87 full_dst_file = os.path.join(out_dir, local_file_path) | 97 dst_file = dst_file[len(files_root):] |
| 88 dst_dir = os.path.dirname(full_dst_file) | 98 dst_file = os.path.join(out_dir, dst_file) |
| 99 # Make sure target directory exists. | |
| 100 dst_dir = os.path.dirname(dst_file) | |
| 89 if not os.path.exists(dst_dir): | 101 if not os.path.exists(dst_dir): |
| 90 os.makedirs(dst_dir, 0775) | 102 os.makedirs(dst_dir, 0775) |
| 91 | 103 return dst_file |
| 92 (base, ext) = os.path.splitext(src_file) | |
| 93 if ext == '.app': | |
| 94 shutil.copytree(src_file, full_dst_file) | |
| 95 elif ext in ['.sh', '.packproj', '.plist']: | |
| 96 copyFileWithDefs(src_file, full_dst_file, defs) | |
| 97 else: | |
| 98 shutil.copy2(src_file, full_dst_file) | |
| 99 | 104 |
| 100 | 105 |
| 101 def copyFileWithDefs(src_file, dst_file, defs): | 106 def copyFileWithDefs(src_file, dst_file, defs): |
| 102 """Copies from src_file to dst_file, performing variable substitution. | 107 """Copies from src_file to dst_file, performing variable substitution. |
| 103 | 108 |
| 104 Any @@variables@@ in the source are replaced with | 109 Any @@VARIABLE@@ in the source is replaced with the value of VARIABLE |
| 105 file with the out_dir, preserving the directory structure. | 110 in the |defs| dictionary when written to the destination file. |
| 106 | 111 |
| 107 Args: | 112 Args: |
| 108 src_file: Full or relative path to source file to copy. | 113 src_file: Full or relative path to source file to copy. |
| 109 dst_file: Relative path (and filename) where src_file should be copied. | 114 dst_file: Relative path (and filename) where src_file should be copied. |
| 110 defs: Dictionary of variable definitions. | 115 defs: Dictionary of variable definitions. |
| 111 """ | 116 """ |
| 112 data = open(src_file, 'r').read() | 117 data = open(src_file, 'r').read() |
| 113 for key, val in defs.iteritems(): | 118 for key, val in defs.iteritems(): |
| 114 try: | 119 try: |
| 115 data = data.replace('@@' + key + '@@', val) | 120 data = data.replace('@@' + key + '@@', val) |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 131 base_zip_name = os.path.basename(zip_file) | 136 base_zip_name = os.path.basename(zip_file) |
| 132 | 137 |
| 133 # We don't use the 'zipfile' module here because it doesn't restore all the | 138 # We don't use the 'zipfile' module here because it doesn't restore all the |
| 134 # file permissions correctly. We use the 'unzip' command manually. | 139 # file permissions correctly. We use the 'unzip' command manually. |
| 135 old_dir = os.getcwd(); | 140 old_dir = os.getcwd(); |
| 136 os.chdir(os.path.dirname(zip_file)) | 141 os.chdir(os.path.dirname(zip_file)) |
| 137 subprocess.call(['unzip', '-qq', '-o', base_zip_name]) | 142 subprocess.call(['unzip', '-qq', '-o', base_zip_name]) |
| 138 os.chdir(old_dir) | 143 os.chdir(old_dir) |
| 139 | 144 |
| 140 # Unzip into correct dir in out_dir. | 145 # Unzip into correct dir in out_dir. |
| 141 root_len = len(files_root) | 146 out_zip_path = calcDestFilePath(out_dir, files_root, zip_file) |
| 142 relative_zip_path = zip_file[root_len:] | |
| 143 out_zip_path = os.path.join(out_dir, relative_zip_path) | |
| 144 out_zip_dir = os.path.dirname(out_zip_path) | 147 out_zip_dir = os.path.dirname(out_zip_path) |
| 145 | 148 |
| 146 (src_dir, ignore1) = os.path.splitext(zip_file) | 149 (src_dir, ignore1) = os.path.splitext(zip_file) |
| 147 (base_dir_name, ignore2) = os.path.splitext(base_zip_name) | 150 (base_dir_name, ignore2) = os.path.splitext(base_zip_name) |
| 148 shutil.copytree(src_dir, os.path.join(out_zip_dir, base_dir_name)) | 151 shutil.copytree(src_dir, os.path.join(out_zip_dir, base_dir_name)) |
| 149 | 152 |
| 150 | 153 |
| 151 def buildHostArchive(temp_dir, zip_path, source_files_root, source_files, | 154 def buildHostArchive(temp_dir, zip_path, source_files_root, source_files, |
| 152 gen_files, gen_files_dst, defs): | 155 gen_files, gen_files_dst, defs): |
| 153 """Builds a zip archive with the files needed to build the installer. | 156 """Builds a zip archive with the files needed to build the installer. |
| 154 | 157 |
| 155 Args: | 158 Args: |
| 156 temp_dir: Temporary dir used to build up the contents for the archive. | 159 temp_dir: Temporary dir used to build up the contents for the archive. |
| 157 zip_path: Full path to the zip file to create. | 160 zip_path: Full path to the zip file to create. |
| 158 source_files_root: Path prefix to strip off |files| when adding to archive. | 161 source_files_root: Path prefix to strip off |files| when adding to archive. |
| 159 source_files: The array of files to add to archive. The path structure is | 162 source_files: The array of files to add to archive. The path structure is |
| 160 preserved (except for the |files_root| prefix). | 163 preserved (except for the |files_root| prefix). |
| 161 gen_files: Full path to binaries to add to archive. | 164 gen_files: Full path to binaries to add to archive. |
| 162 gen_files_dst: Relative path of where to add binary files in archive. | 165 gen_files_dst: Relative path of where to add binary files in archive. |
| 163 This array needs to parallel |binaries_src|. | 166 This array needs to parallel |binaries_src|. |
| 164 defs: Dictionary of variable definitions. | 167 defs: Dictionary of variable definitions. |
| 165 """ | 168 """ |
| 166 cleanDir(temp_dir) | 169 cleanDir(temp_dir) |
| 167 | 170 |
| 168 for file in source_files: | 171 for f in source_files: |
| 169 base_file = os.path.basename(file) | 172 dst_file = calcDestFilePath(temp_dir, source_files_root, f) |
| 170 (base, ext) = os.path.splitext(file) | 173 base_file = os.path.basename(f) |
| 174 (base, ext) = os.path.splitext(f) | |
| 171 if ext == '.zip': | 175 if ext == '.zip': |
| 172 copyZipIntoArchive(temp_dir, source_files_root, file) | 176 copyZipIntoArchive(temp_dir, source_files_root, f) |
| 177 elif ext in ['.sh', '.packproj', '.plist']: | |
| 178 copyFileWithDefs(f, dst_file, defs) | |
| 173 else: | 179 else: |
| 174 copyFileIntoArchive(file, temp_dir, source_files_root, file, defs) | 180 shutil.copy2(src_file, dst_file) |
| 175 | 181 |
| 176 for bs, bd in zip(gen_files, gen_files_dst): | 182 for bs, bd in zip(gen_files, gen_files_dst): |
| 177 copyFileIntoArchive(bs, temp_dir, '', bd, {}) | 183 dst_file = calcDestFilePath(temp_dir, source_files_root, bd) |
| 184 (base, ext) = os.path.splitext(bd) | |
| 185 if ext in ['.app', '.prefPane']: | |
|
alexeypa (please no reviews)
2012/04/30 16:33:48
nit: Maybe a better test would be checking if the
garykac
2012/04/30 21:24:39
Done.
| |
| 186 shutil.copytree(bs, dst_file) | |
| 187 else: | |
| 188 shutil.copy2(bs, dst_file) | |
| 178 | 189 |
| 179 createZip(zip_path, temp_dir) | 190 createZip(zip_path, temp_dir) |
| 180 | 191 |
| 181 | 192 |
| 182 def error(msg): | 193 def error(msg): |
| 183 sys.stderr.write('ERROR: %s' % msg) | 194 sys.stderr.write('ERROR: %s' % msg) |
| 184 sys.exit(1) | 195 sys.exit(1) |
| 185 | 196 |
| 197 | |
| 186 def usage(): | 198 def usage(): |
| 187 """Display basic usage information.""" | 199 """Display basic usage information.""" |
| 188 print ('Usage: %s\n' | 200 print ('Usage: %s\n' |
| 189 ' <temp-dir> <zip-path> <files-root-dir>\n' | 201 ' <temp-dir> <zip-path> <files-root-dir>\n' |
| 190 ' --source-files <list of source files...>\n' | 202 ' --source-files <list of source files...>\n' |
| 191 ' --generated-files <list of generated target files...>\n' | 203 ' --generated-files <list of generated target files...>\n' |
| 192 ' --generated-files-dst <dst for each generated file...>\n' | 204 ' --generated-files-dst <dst for each generated file...>\n' |
| 193 ' --defs <list of VARIABLE=value definitions...>' | 205 ' --defs <list of VARIABLE=value definitions...>' |
| 194 ) % sys.argv[0] | 206 ) % sys.argv[0] |
| 195 | 207 |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 246 | 258 |
| 247 result = buildHostArchive(temp_dir, zip_path, source_files_root, | 259 result = buildHostArchive(temp_dir, zip_path, source_files_root, |
| 248 source_files, generated_files, generated_files_dst, | 260 source_files, generated_files, generated_files_dst, |
| 249 defs) | 261 defs) |
| 250 | 262 |
| 251 return 0 | 263 return 0 |
| 252 | 264 |
| 253 | 265 |
| 254 if __name__ == '__main__': | 266 if __name__ == '__main__': |
| 255 sys.exit(main()) | 267 sys.exit(main()) |
| OLD | NEW |