OLD | NEW |
1 # Copyright 2013 The Chromium Authors. All rights reserved. | 1 # Copyright 2013 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 import ast | 5 import ast |
6 import contextlib | 6 import contextlib |
7 import fnmatch | 7 import fnmatch |
8 import json | 8 import json |
9 import os | 9 import os |
10 import pipes | 10 import pipes |
11 import re | 11 import re |
12 import shlex | 12 import shlex |
13 import shutil | 13 import shutil |
| 14 import stat |
14 import subprocess | 15 import subprocess |
15 import sys | 16 import sys |
16 import tempfile | 17 import tempfile |
17 import zipfile | 18 import zipfile |
18 | 19 |
19 # Some clients do not add //build/android/gyp to PYTHONPATH. | 20 # Some clients do not add //build/android/gyp to PYTHONPATH. |
20 import md5_check # pylint: disable=relative-import | 21 import md5_check # pylint: disable=relative-import |
21 | 22 |
22 sys.path.append(os.path.join(os.path.dirname(__file__), os.pardir, os.pardir)) | 23 sys.path.append(os.path.join(os.path.dirname(__file__), os.pardir, os.pardir)) |
23 from pylib.constants import host_paths | 24 from pylib.constants import host_paths |
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
191 return device_state.strip() == 'device' | 192 return device_state.strip() == 'device' |
192 | 193 |
193 | 194 |
194 def CheckZipPath(name): | 195 def CheckZipPath(name): |
195 if os.path.normpath(name) != name: | 196 if os.path.normpath(name) != name: |
196 raise Exception('Non-canonical zip path: %s' % name) | 197 raise Exception('Non-canonical zip path: %s' % name) |
197 if os.path.isabs(name): | 198 if os.path.isabs(name): |
198 raise Exception('Absolute zip path: %s' % name) | 199 raise Exception('Absolute zip path: %s' % name) |
199 | 200 |
200 | 201 |
| 202 def IsSymlink(zip_file, name): |
| 203 zi = zip_file.getinfo(name) |
| 204 |
| 205 # The two high-order bytes of ZipInfo.external_attr represent |
| 206 # UNIX permissions and file type bits. |
| 207 return stat.S_ISLNK(zi.external_attr >> 16L) |
| 208 |
| 209 |
201 def ExtractAll(zip_path, path=None, no_clobber=True, pattern=None, | 210 def ExtractAll(zip_path, path=None, no_clobber=True, pattern=None, |
202 predicate=None): | 211 predicate=None): |
203 if path is None: | 212 if path is None: |
204 path = os.getcwd() | 213 path = os.getcwd() |
205 elif not os.path.exists(path): | 214 elif not os.path.exists(path): |
206 MakeDirectory(path) | 215 MakeDirectory(path) |
207 | 216 |
208 with zipfile.ZipFile(zip_path) as z: | 217 with zipfile.ZipFile(zip_path) as z: |
209 for name in z.namelist(): | 218 for name in z.namelist(): |
210 if name.endswith('/'): | 219 if name.endswith('/'): |
211 continue | 220 continue |
212 if pattern is not None: | 221 if pattern is not None: |
213 if not fnmatch.fnmatch(name, pattern): | 222 if not fnmatch.fnmatch(name, pattern): |
214 continue | 223 continue |
215 if predicate and not predicate(name): | 224 if predicate and not predicate(name): |
216 continue | 225 continue |
217 CheckZipPath(name) | 226 CheckZipPath(name) |
218 if no_clobber: | 227 if no_clobber: |
219 output_path = os.path.join(path, name) | 228 output_path = os.path.join(path, name) |
220 if os.path.exists(output_path): | 229 if os.path.exists(output_path): |
221 raise Exception( | 230 raise Exception( |
222 'Path already exists from zip: %s %s %s' | 231 'Path already exists from zip: %s %s %s' |
223 % (zip_path, name, output_path)) | 232 % (zip_path, name, output_path)) |
224 z.extract(name, path) | 233 if IsSymlink(z, name): |
| 234 dest = os.path.join(path, name) |
| 235 MakeDirectory(os.path.dirname(dest)) |
| 236 os.symlink(z.read(name), dest) |
| 237 else: |
| 238 z.extract(name, path) |
225 | 239 |
226 | 240 |
227 def AddToZipHermetic(zip_file, zip_path, src_path=None, data=None, | 241 def AddToZipHermetic(zip_file, zip_path, src_path=None, data=None, |
228 compress=None): | 242 compress=None): |
229 """Adds a file to the given ZipFile with a hard-coded modified time. | 243 """Adds a file to the given ZipFile with a hard-coded modified time. |
230 | 244 |
231 Args: | 245 Args: |
232 zip_file: ZipFile instance to add the file to. | 246 zip_file: ZipFile instance to add the file to. |
233 zip_path: Destination path within the zip file. | 247 zip_path: Destination path within the zip file. |
234 src_path: Path of the source file. Mutually exclusive with |data|. | 248 src_path: Path of the source file. Mutually exclusive with |data|. |
(...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
476 | 490 |
477 md5_check.CallAndRecordIfStale( | 491 md5_check.CallAndRecordIfStale( |
478 on_stale_md5, | 492 on_stale_md5, |
479 record_path=record_path, | 493 record_path=record_path, |
480 input_paths=input_paths, | 494 input_paths=input_paths, |
481 input_strings=input_strings, | 495 input_strings=input_strings, |
482 output_paths=output_paths, | 496 output_paths=output_paths, |
483 force=force, | 497 force=force, |
484 pass_changes=True) | 498 pass_changes=True) |
485 | 499 |
OLD | NEW |