|
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 """Create a zip archive for the Chrome Remote Desktop Host installer. | |
alexeypa (please no reviews)
2012/04/13 17:27:13
nit: Create -> Creates?
alexeypa (please no reviews)
2012/04/13 17:27:13
Add TODO about merging this script with the one th
garykac
2012/04/13 18:06:03
Done.
garykac
2012/04/13 18:06:03
Done.
| |
7 | |
8 This script builds a zip file that contains all the files needed to build an | |
9 installer for Chrome Remote Desktop Host. | |
10 | |
11 This zip archive is then used by the signing bots to: | |
12 (1) Sign the binaries | |
13 (2) Build the final installer | |
14 """ | |
15 | |
16 import os | |
17 import shutil | |
18 import sys | |
19 import zipfile | |
20 | |
21 | |
22 def cleanDir(dir): | |
23 """Delete and recreate the dir to make sure it is clean. | |
alexeypa (please no reviews)
2012/04/13 17:27:13
nit: Deletes and recreates
garykac
2012/04/13 18:06:03
Done.
| |
24 | |
25 Args: | |
26 dir: The directory to clean. | |
27 """ | |
28 try: | |
29 shutil.rmtree(dir) | |
30 except OSError: | |
31 if os.path.exists(dir): | |
32 raise | |
33 else: | |
34 pass | |
35 os.makedirs(dir, 0775) | |
36 | |
37 | |
38 def createZip(zip_path, directory): | |
39 """Creates a zipfile at zip_path for the given directory. | |
40 | |
41 Args: | |
42 zip_path: Path to zip file to create. | |
43 directory: Directory with contents to archive. | |
44 """ | |
45 zipfile_base = os.path.splitext(os.path.basename(zip_path))[0] | |
46 zip = zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED) | |
47 for (root, dirs, files) in os.walk(directory): | |
48 for f in files: | |
49 full_path = os.path.join(root, f) | |
50 rel_path = os.path.relpath(full_path, directory) | |
51 zip.write(full_path, os.path.join(zipfile_base, rel_path)) | |
52 zip.close() | |
53 | |
54 | |
55 def copyFileIntoArchive(src_file, out_dir, files_root, dst_file): | |
56 """Copy the src_file into the out_dir, preserving the directory structure. | |
alexeypa (please no reviews)
2012/04/13 17:27:13
Copies :-)
garykac
2012/04/13 18:06:03
Done.
| |
57 | |
58 Args: | |
59 src_file: Full or relative path to source file to copy. | |
60 out_dir: Target directory where files are copied. | |
61 files_root: Path prefix which is stripped of dst_file before appending | |
62 it to the temp_dir. | |
63 dst_file: Relative path (and filename) where src_file should be copied. | |
64 """ | |
65 root_len = len(files_root) | |
66 local_path = dst_file[root_len:] | |
67 full_dst_file = os.path.join(out_dir, local_path) | |
68 dst_dir = os.path.dirname(full_dst_file) | |
69 if not os.path.exists(dst_dir): | |
70 os.makedirs(dst_dir, 0775) | |
71 shutil.copy2(src_file, full_dst_file) | |
72 | |
73 | |
74 def buildHostArchive(temp_dir, zip_path, files_root, files, binaries_src, | |
75 binaries_dst): | |
76 """Build a zip archive with the files needed to build the installer. | |
alexeypa (please no reviews)
2012/04/13 17:27:13
Builds
garykac
2012/04/13 18:06:03
Done.
| |
77 | |
78 Args: | |
79 temp_dir: Temporary dir used to build up the contents for the archive. | |
80 zip_path: Full path to the zip file to create. | |
81 files_root: Path prefix to strip off |files| when adding to archive. | |
82 files: The array of files to add to archive. The path structure is | |
83 preserved (except for the |files_root| prefix). | |
84 binaries_src: Full path to binaries to add to archive. | |
85 binaries_dst: Relative path of where to add binary files in archive. | |
86 This array needs to parallel |binaries_src|. | |
87 """ | |
88 cleanDir(temp_dir) | |
89 | |
90 for file in files: | |
91 base_file = os.path.basename(file) | |
92 if base_file == '*': | |
93 # Copy entire directory tree. | |
94 for (root, dirs, files) in os.walk(os.path.dirname(file)): | |
95 for f in files: | |
96 full_path = os.path.join(root, f) | |
97 copyFileIntoArchive(full_path, temp_dir, files_root, full_path) | |
98 else: | |
99 copyFileIntoArchive(file, temp_dir, files_root, file) | |
100 | |
101 for bs, bd in zip(binaries_src, binaries_dst): | |
102 copyFileIntoArchive(bs, temp_dir, '', bd) | |
103 | |
104 createZip(zip_path, temp_dir) | |
105 | |
106 | |
107 def usage(): | |
108 """Display basic usage information.""" | |
109 print ('Usage:', sys.argv[0], | |
110 '<temp-dir> <zip-path> <files-root-dir>' | |
111 '--files <files...> ' | |
112 '--binaries-src <src binary files...> ' | |
alexeypa (please no reviews)
2012/04/13 17:27:13
nit: The way we map source files to their location
garykac
2012/04/13 18:06:03
We need a flat array of filenames for gyp dependen
| |
113 '--binaries-dst <dst for binary files...>') | |
114 | |
115 | |
116 def main(): | |
117 if len(sys.argv) < 5: | |
118 usage() | |
119 return 1 | |
120 | |
121 temp_dir = sys.argv[1] | |
122 zip_path = sys.argv[2] | |
123 files_root = sys.argv[3] | |
alexeypa (please no reviews)
2012/04/13 17:27:13
What if there are less arguments than required?
garykac
2012/04/13 18:06:03
Changed above check to ensure 3 or more args.
| |
124 | |
125 arg_mode = '' | |
126 files = [] | |
127 binaries_src = [] | |
128 binaries_dst = [] | |
129 for arg in sys.argv[4:]: | |
130 if arg == '--files': | |
131 arg_mode = 'files' | |
132 elif arg == '--binaries-src': | |
133 arg_mode = 'bin-src' | |
134 elif arg == '--binaries-dst': | |
135 arg_mode = 'bin-dst' | |
136 | |
137 elif arg_mode == 'files': | |
138 files.append(arg) | |
139 elif arg_mode == 'bin-src': | |
140 binaries_src.append(arg) | |
141 elif arg_mode == 'bin-dst': | |
142 binaries_dst.append(arg) | |
143 else: | |
144 usage() | |
145 return 1 | |
146 | |
147 # Ensure that files_root ends with a directory separator. | |
148 if files_root[-1:] != os.sep: | |
149 files_root += os.sep | |
150 | |
151 # Verify that the 2 'binaries' arrays have the same number of elements. | |
152 if len(binaries_src) < len(binaries_dst): | |
153 print "ERROR: --binaries-src and --binaries-dst should be the same length" | |
154 return 1 | |
155 while len(binaries_src) > len(binaries_dst): | |
156 binaries_dst.append('') | |
157 | |
158 result = buildHostArchive(temp_dir, zip_path, files_root, files, | |
159 binaries_src, binaries_dst) | |
160 | |
161 return 0 | |
162 | |
163 if __name__ == '__main__': | |
164 sys.exit(main()) | |
OLD | NEW |