| OLD | NEW |
| 1 # Copyright 2016 The Chromium Authors. All rights reserved. | 1 # Copyright 2016 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 """Takes care of manipulating the chrome's HTTP cache. | 5 """Takes care of manipulating the chrome's HTTP cache. |
| 6 """ | 6 """ |
| 7 | 7 |
| 8 from datetime import datetime | 8 from datetime import datetime |
| 9 import json | 9 import json |
| 10 import os | 10 import os |
| 11 import shutil |
| 11 import subprocess | 12 import subprocess |
| 12 import sys | 13 import sys |
| 13 import tempfile | 14 import tempfile |
| 14 import zipfile | 15 import zipfile |
| 15 | 16 |
| 16 _SRC_DIR = os.path.abspath(os.path.join( | 17 _SRC_DIR = os.path.abspath(os.path.join( |
| 17 os.path.dirname(__file__), '..', '..', '..')) | 18 os.path.dirname(__file__), '..', '..', '..')) |
| 18 | 19 |
| 19 sys.path.append(os.path.join(_SRC_DIR, 'build', 'android')) | 20 sys.path.append(os.path.join(_SRC_DIR, 'build', 'android')) |
| 20 from pylib import constants | 21 from pylib import constants |
| (...skipping 250 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 271 self._cache_directory_path, | 272 self._cache_directory_path, |
| 272 self._cache_backend_type, | 273 self._cache_backend_type, |
| 273 operation] | 274 operation] |
| 274 editor_tool_cmd.extend(args) | 275 editor_tool_cmd.extend(args) |
| 275 process = subprocess.Popen(editor_tool_cmd, stdout=subprocess.PIPE) | 276 process = subprocess.Popen(editor_tool_cmd, stdout=subprocess.PIPE) |
| 276 stdout_data, _ = process.communicate() | 277 stdout_data, _ = process.communicate() |
| 277 assert process.returncode == 0 | 278 assert process.returncode == 0 |
| 278 return stdout_data | 279 return stdout_data |
| 279 | 280 |
| 280 | 281 |
| 282 def ApplyUrlWhitelistToCacheArchive(cache_archive_path, |
| 283 whitelisted_urls, |
| 284 output_cache_archive_path): |
| 285 """Generate a new cache archive containing only whitelisted urls. |
| 286 |
| 287 Args: |
| 288 cache_archive_path: Path of the cache archive to apply the white listing. |
| 289 whitelisted_urls: Set of url to keep in cache. |
| 290 output_cache_archive_path: Destination path of cache archive containing only |
| 291 white-listed urls. |
| 292 """ |
| 293 cache_temp_directory = tempfile.mkdtemp(suffix='.cache') |
| 294 try: |
| 295 UnzipDirectoryContent(cache_archive_path, cache_temp_directory) |
| 296 backend = CacheBackend(cache_temp_directory, 'simple') |
| 297 cached_urls = backend.ListKeys() |
| 298 for cached_url in cached_urls: |
| 299 if cached_url not in whitelisted_urls: |
| 300 backend.DeleteKey(cached_url) |
| 301 for cached_url in backend.ListKeys(): |
| 302 assert cached_url in whitelisted_urls |
| 303 ZipDirectoryContent(cache_temp_directory, output_cache_archive_path) |
| 304 finally: |
| 305 shutil.rmtree(cache_temp_directory) |
| 306 |
| 307 |
| 281 if __name__ == '__main__': | 308 if __name__ == '__main__': |
| 282 import argparse | 309 import argparse |
| 283 parser = argparse.ArgumentParser(description='Tests cache back-end.') | 310 parser = argparse.ArgumentParser(description='Tests cache back-end.') |
| 284 parser.add_argument('cache_path', type=str) | 311 parser.add_argument('cache_path', type=str) |
| 285 parser.add_argument('backend_type', type=str, choices=BACKEND_TYPES) | 312 parser.add_argument('backend_type', type=str, choices=BACKEND_TYPES) |
| 286 command_line_args = parser.parse_args() | 313 command_line_args = parser.parse_args() |
| 287 | 314 |
| 288 cache_backend = CacheBackend( | 315 cache_backend = CacheBackend( |
| 289 cache_directory_path=command_line_args.cache_path, | 316 cache_directory_path=command_line_args.cache_path, |
| 290 cache_backend_type=command_line_args.backend_type) | 317 cache_backend_type=command_line_args.backend_type) |
| 291 keys = cache_backend.ListKeys() | 318 keys = cache_backend.ListKeys() |
| 292 print '{}\'s HTTP response header:'.format(keys[0]) | 319 print '{}\'s HTTP response header:'.format(keys[0]) |
| 293 print cache_backend.GetStreamForKey(keys[0], 0) | 320 print cache_backend.GetStreamForKey(keys[0], 0) |
| 294 cache_backend.DeleteKey(keys[1]) | 321 cache_backend.DeleteKey(keys[1]) |
| 295 assert keys[1] not in cache_backend.ListKeys() | 322 assert keys[1] not in cache_backend.ListKeys() |
| OLD | NEW |