prebuilts_fetcher.py revision 5d1f7b1de12d16ceb2c938c56701a3e8bfa558f7
1# Copyright 2014 The Chromium Authors. All rights reserved. 2# Use of this source code is governed by a BSD-style license that can be 3# found in the LICENSE file. 4 5"""This module fetches and syncs prebuilts from Google Cloud Storage (GCS). 6 7See prebuilts/README for a description of the respository <> GCS sync mechanism. 8""" 9 10import hashlib 11import logging 12import os 13import urllib 14 15 16_PREBUILTS_BUCKET = 'chromium-telemetry' 17 18# Bypass the GCS download logic in unittests and use the *_ForTests mock. 19in_test_harness = False 20 21 22def GetIfChanged(local_file_path): 23 """Downloads the file from GCS, only if the local one is outdated.""" 24 is_changed = _IsChanged(local_file_path) 25 26 # In test harness mode we are only interested in checking that the proper 27 # .sha1 files have been checked in (hence the call to _IsChanged() above). 28 if in_test_harness: 29 return _GetIfChanged_ForTests(local_file_path) 30 31 if not is_changed: 32 return 33 obj_name = _GetRemoteFileID(local_file_path) 34 url = 'https://storage.googleapis.com/%s/%s' % (_PREBUILTS_BUCKET, obj_name) 35 logging.info('Downloading %s prebuilt from %s.' % (local_file_path, url)) 36 urllib.urlretrieve(url, local_file_path) 37 assert(not _IsChanged(local_file_path)), 'GCS download failed.' 38 39 40def _IsChanged(local_file_path): 41 """Checks whether the local_file_path exists and matches the expected hash.""" 42 is_changed = True 43 expected_hash = _GetRemoteFileID(local_file_path) 44 if os.path.exists(local_file_path): 45 with open(local_file_path, 'rb') as f: 46 local_hash = hashlib.sha1(f.read()).hexdigest() 47 is_changed = (local_hash != expected_hash) 48 return is_changed 49 50 51def _GetRemoteFileID(local_file_path): 52 """Returns the checked-in hash which identifies the name of file in GCS.""" 53 hash_path = local_file_path + '.sha1' 54 with open(hash_path, 'rb') as f: 55 return f.read(1024).rstrip() 56 57 58def _GetIfChanged_ForTests(local_file_path): 59 """This is the mock version of |GetIfChanged| used only in unittests.""" 60 # Just truncate / create an empty file. 61 open(local_file_path, 'wb').close() 62