Пример #1
0
def LoadProvider(provider_name, ignore_package_requirements=True):
    """Loads the all modules in the 'provider_name' package.

  This function first checks the specified provider's Python package
  requirements file, if one exists, and verifies that all requirements are met.
  Next, it loads all modules in the specified provider's package. By loading
  these modules, relevant classes (e.g. VMs) will register themselves.

  Args:
    provider_name: The name of the package whose modules should be loaded.
        Usually the name of the provider in lower case (e.g. the package name
        for the 'GCP' provider is 'gcp'.
    ignore_package_requirements: boolean. If True, the provider's Python package
        requirements file is ignored.
  """
    if not ignore_package_requirements:
        requirements.CheckProviderRequirements(provider_name)
    provider_package_path = os.path.join(__path__[0], provider_name)
    try:
        # Iterating through this generator will load all modules in the provider
        # directory. Simply loading those modules will cause relevant classes
        # to register themselves so that we can run with that provider.
        modules = [
            module for module in import_util.LoadModulesForPath(
                [provider_package_path], __name__ + '.' + provider_name)
        ]
        if not modules:
            raise ImportError('No modules found for provider.')
    except:
        logging.error('Unable to load provider %s.', provider_name)
        raise
Пример #2
0
def LoadProvider(provider_name, ignore_package_requirements=True):
    """Loads the all modules in the 'provider_name' package.

  This function first checks the specified provider's Python package
  requirements file, if one exists, and verifies that all requirements are met.
  Next, it loads all modules in the specified provider's package. By loading
  these modules, relevant classes (e.g. VMs) will register themselves.

  Args:
    provider_name: string chosen from VALID_CLOUDS. The name of the provider
        whose modules should be loaded.
    ignore_package_requirements: boolean. If True, the provider's Python package
        requirements file is ignored.
  """
    if provider_name in _imported_providers:
        return

    # Check package requirements from the provider's pip requirements file.
    normalized_name = provider_name.lower()
    if not ignore_package_requirements:
        requirements.CheckProviderRequirements(normalized_name)

    # Load all modules in the provider's directory. Simply loading those modules
    # will cause relevant classes (e.g. VM and disk classes) to register
    # themselves so that they can be instantiated during resource provisioning.
    provider_package_path = os.path.join(__path__[0], normalized_name)
    try:
        modules = tuple(
            import_util.LoadModulesForPath([provider_package_path],
                                           __name__ + '.' + normalized_name))
        if not modules:
            raise ImportError('No modules found for provider %s.' %
                              provider_name)
    except Exception:
        logging.error('Unable to load provider %s.', provider_name)
        raise

    # Signal that the provider's modules have been imported.
    _imported_providers.add(provider_name)
    events.provider_imported.send(provider_name)
 def testUnfulfilledRequirements(self):
   # AWS does have a requirements file, but it contains packages that are not
   # installed as part of the test environment.
   with self.assertRaises(errors.Setup.PythonPackageRequirementUnfulfilled):
     requirements.CheckProviderRequirements('aws')
 def testNoRequirementsFile(self):
   # If a provider does not have a requirements file, then there can be no
   # unfulfilled requirement.
   requirements.CheckProviderRequirements('fakeprovider')