Chromium Code Reviews| Index: tools/memory_inspector/memory_inspector/backends/prebuilts_fetcher.py |
| diff --git a/tools/memory_inspector/memory_inspector/backends/prebuilts_fetcher.py b/tools/memory_inspector/memory_inspector/backends/prebuilts_fetcher.py |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..fd3fe97abab70f684212a67fb21329e026aecaef |
| --- /dev/null |
| +++ b/tools/memory_inspector/memory_inspector/backends/prebuilts_fetcher.py |
| @@ -0,0 +1,55 @@ |
| +# Copyright 2014 The Chromium Authors. All rights reserved. |
| +# Use of this source code is governed by a BSD-style license that can be |
| +# found in the LICENSE file. |
| + |
| +"""This module fetches and syncs prebuilts from Google Cloud Storage (GCS). |
| + |
| +See prebuilts/README for a description of the respository <> GCS sync mechanism. |
| +""" |
| + |
| +import hashlib |
| +import logging |
| +import os |
| +import urllib |
| + |
| + |
| +_PREBUILTS_BUCKET = 'chromium-telemetry' |
| + |
| +# Bypass the GCS download logic in unittests and use the *_ForTests mock. |
| +in_test_harness = False |
| + |
| + |
| +def GetIfChanged(local_file_path): |
| + """Downloads the file from GCS, only if the local one is outdated.""" |
| + (is_changed, obj_name) = _IsChanged(local_file_path) |
| + |
| + # In test harness mode we are only interested in checking that the proper |
| + # .sha1 files have been checked in (hence the call to _IsChanged() above). |
| + if in_test_harness: |
| + return _GetIfChanged_ForTests(local_file_path) |
| + |
| + if not is_changed: |
| + return |
| + url = 'https://storage.googleapis.com/%s/%s' % (_PREBUILTS_BUCKET, obj_name) |
| + logging.info('Downloading %s prebuilt from %s.' % (local_file_path, url)) |
| + urllib.urlretrieve(url, local_file_path) |
| + assert(not _IsChanged(local_file_path)[0]), 'GCS download failed.' |
| + |
| + |
| +def _IsChanged(local_file_path): |
|
Philippe
2014/02/10 16:41:03
Nit: it might be slightly clearer to split this in
Primiano Tucci (use gerrit)
2014/02/10 19:45:10
Yeah, makes sense.
|
| + """Checks whether the local_file_path exists and matches the expected hash.""" |
| + is_changed = True |
| + hash_path = local_file_path + '.sha1' |
| + with open(hash_path, 'rb') as f: |
| + expected_hash = f.read(1024).rstrip() |
| + if os.path.exists(local_file_path): |
| + with open(local_file_path, 'rb') as f: |
| + local_hash = hashlib.sha1(f.read()).hexdigest() |
| + is_changed = (local_hash != expected_hash) |
| + return (is_changed, expected_hash) |
| + |
| + |
| +def _GetIfChanged_ForTests(local_file_path): |
| + """This is the mock version of |GetIfChanged| used only in unittests.""" |
| + with open(local_file_path, 'wb') as f: |
| + f.write("mock_prebuilt") |
|
Philippe
2014/02/10 16:41:03
It's not immediately clear to me why you are writi
Primiano Tucci (use gerrit)
2014/02/10 19:45:10
Yup, actually this should be just a truncate :)
|