def CanAccessToolchainBucket(): """Checks whether the user has access to gs://chrome-wintoolchain/.""" gsutil = download_from_google_storage.Gsutil( download_from_google_storage.GSUTIL_DEFAULT_PATH, boto_path=None) code, _, _ = gsutil.check_call('ls', 'gs://chrome-wintoolchain/') return code == 0
def _DownloadAshChromeIfNecessary(version, is_download_for_bots=False): """Download a given version of ash-chrome if not already exists. Currently, a special constant version value is support: "for_bots", the reason is that version number is still not pinned to chromium/src, so a constant value is needed to make sure that after the builder who downloads and isolates ash-chrome, the tester knows where to look for the binary to use. Additionally, a is_download_for_bots boolean argument is introduced to indicate whether this function is downloading for bots specifically or downloading while running tests, and it is needed because when version is "for_bots", the expected behavior is different in the 2 scenarios: when downloading for bots to isolate, we always want to update for_bots/ to reflect the latest version; however, when downloading while running tests, we want to skip updating to latest because swarming testers aren't supposed to have external network access. TODO(crbug.com/1107010): remove the support once ash-chrome version is pinned to chromium/src. Args: version: A string representing the version, such as "793554". Raises: RuntimeError: If failed to download the specified version, for example, if the version is not present on gcs. """ def IsAshChromeDirValid(ash_chrome_dir): # This function assumes that once 'chrome' is present, other dependencies # will be present as well, it's not always true, for example, if the test # runner process gets killed in the middle of unzipping (~2 seconds), but # it's unlikely for the assumption to break in practice. return os.path.isdir(ash_chrome_dir) and os.path.isfile( os.path.join(ash_chrome_dir, 'chrome')) ash_chrome_dir = _GetAshChromeDirPath(version) if not is_download_for_bots and IsAshChromeDirValid(ash_chrome_dir): return shutil.rmtree(ash_chrome_dir, ignore_errors=True) os.makedirs(ash_chrome_dir) with tempfile.NamedTemporaryFile() as tmp: import download_from_google_storage gsutil = download_from_google_storage.Gsutil( download_from_google_storage.GSUTIL_DEFAULT_PATH) gs_version = (_GetLatestVersionOfAshChrome() if is_download_for_bots else version) logging.info('Ash-chrome version: %s', gs_version) gs_path = _GS_URL_BASE + '/' + gs_version + '/' + _GS_ASH_CHROME_PATH exit_code = gsutil.call('cp', gs_path, tmp.name) if exit_code: raise RuntimeError('Failed to download: "%s"' % gs_path) # https://bugs.python.org/issue15795. ZipFile doesn't preserve permissions. # And in order to workaround the issue, this function is created and used # instead of ZipFile.extractall(). # The solution is copied from: # https://stackoverflow.com/questions/42326428/zipfile-in-python-file-permission def ExtractFile(zf, info, extract_dir): zf.extract(info.filename, path=extract_dir) perm = info.external_attr >> 16 os.chmod(os.path.join(extract_dir, info.filename), perm) with zipfile.ZipFile(tmp.name, 'r') as zf: # Extra all files instead of just 'chrome' binary because 'chrome' needs # other resources and libraries to run. for info in zf.infolist(): ExtractFile(zf, info, ash_chrome_dir) _remove_unused_ash_chrome_versions(version)