1# Copyright 2015 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
5import logging
6
7import dependency_manager
8
9
10class BinaryManager(object):
11  """ This class is effectively a subclass of dependency_manager, but uses a
12      different number of arguments for FetchPath and LocalPath.
13  """
14
15  def __init__(self, configs):
16    self._dependency_manager = dependency_manager.DependencyManager(configs)
17
18  def FetchPath(self, binary_name, arch, os_name, os_version=None):
19    """ Return a path to the executable for <binary_name>, or None if not found.
20
21    Will attempt to download from cloud storage if needed.
22    """
23    platform = '%s_%s' % (os_name, arch)
24    if os_version:
25      try:
26        versioned_platform = '%s_%s_%s' % (os_name, os_version, arch)
27        return self._dependency_manager.FetchPath(
28            binary_name, versioned_platform)
29      except dependency_manager.NoPathFoundError:
30        logging.warning(
31            'Cannot find path for %s on platform %s. Falling back to %s.',
32            binary_name, versioned_platform, platform)
33    return self._dependency_manager.FetchPath(binary_name, platform)
34
35
36  def LocalPath(self, binary_name, arch, os_name, os_version=None):
37    """ Return a local path to the given binary name, or None if not found.
38
39    Will not download from cloud_storage.
40    """
41    platform = '%s_%s' % (os_name, arch)
42    if os_version:
43      try:
44        versioned_platform = '%s_%s_%s' % (os_name, os_version, arch)
45        return self._dependency_manager.LocalPath(
46            binary_name, versioned_platform)
47      except dependency_manager.NoPathFoundError:
48        logging.warning(
49            'Cannot find local path for %s on platform %s. Falling back to %s.',
50            binary_name, versioned_platform, platform)
51    return self._dependency_manager.LocalPath(binary_name, platform)
52