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, config_files):
16    if not config_files or type(config_files) != list:
17      raise ValueError(
18          'Must supply a list of config files to the BinaryManager')
19    configs = [dependency_manager.BaseConfig(config) for config in config_files]
20    self._dependency_manager = dependency_manager.DependencyManager(configs)
21
22  def FetchPathWithVersion(self, binary_name, os_name, arch, os_version=None):
23    """ Return a path to the executable for <binary_name>, or None if not found.
24
25    Will attempt to download from cloud storage if needed.
26    """
27    return self._WrapDependencyManagerFunction(
28        self._dependency_manager.FetchPathWithVersion, binary_name, os_name,
29        arch, os_version)
30
31  def FetchPath(self, binary_name, os_name, arch, os_version=None):
32    """ Return a path to the executable for <binary_name>, or None if not found.
33
34    Will attempt to download from cloud storage if needed.
35    """
36    return self._WrapDependencyManagerFunction(
37        self._dependency_manager.FetchPath, binary_name, os_name, arch,
38        os_version)
39
40  def LocalPath(self, binary_name, os_name, arch, os_version=None):
41    """ Return a local path to the given binary name, or None if not found.
42
43    Will not download from cloud_storage.
44    """
45    return self._WrapDependencyManagerFunction(
46        self._dependency_manager.LocalPath, binary_name, os_name, arch,
47        os_version)
48
49  def _WrapDependencyManagerFunction(
50      self, function, binary_name, os_name, arch, os_version):
51    platform = '%s_%s' % (os_name, arch)
52    if os_version:
53      try:
54        versioned_platform = '%s_%s_%s' % (os_name, os_version, arch)
55        return function(binary_name, versioned_platform)
56      except dependency_manager.NoPathFoundError:
57        logging.warning(
58            'Cannot find path for %s on platform %s. Falling back to %s.',
59            binary_name, versioned_platform, platform)
60    return function(binary_name, platform)
61
62