def testListPackageResources(self):
        with files.TemporaryDirectory() as t:
            self.assertEqual([], sorted(pkg_resources.ListPackageResources(t)))
            Touch(os.path.join(t, 'foo.py'), '"""Foo module."""')
            self.assertEqual(['foo.py'],
                             sorted(pkg_resources.ListPackageResources(t)))
            Touch(os.path.join(t, '__init__.py'), '"""Package marker."""')
            self.assertEqual(['__init__.py', 'foo.py'],
                             sorted(pkg_resources.ListPackageResources(t)))
            os.makedirs(os.path.join(t, 'pkg'))
            self.assertEqual(['__init__.py', 'foo.py', 'pkg' + os.sep],
                             sorted(pkg_resources.ListPackageResources(t)))
            Touch(os.path.join(t, 'pkg', '__init__.py'),
                  '"""Package marker."""')
            self.assertEqual(['__init__.py', 'foo.py', 'pkg' + os.sep],
                             sorted(pkg_resources.ListPackageResources(t)))
            Touch(os.path.join(t, 'bar'), 'BAR')
            self.assertEqual(['__init__.py', 'bar', 'foo.py', 'pkg' + os.sep],
                             sorted(pkg_resources.ListPackageResources(t)))
            self.assertEqual(
                b'BAR',
                pkg_resources.GetResourceFromFile(os.path.join(t, 'bar')))

            with self.assertRaises(IOError):
                pkg_resources.GetResourceFromFile(
                    os.path.join(t, 'non_existant_file'))
Beispiel #2
0
def _ListSupportedRuntimes(dockerfiles_dir):
    """Lists supported runtimes based on the content of a given directory.

  Args:
    dockerfiles_dir: str, The directory with default Dockerfiles for apps. The
        Dockerfiles are under format is {runtime}_app/Dockerfile.

  Returns:
    [str], list of runtimes found.
  """
    resources = pkg_resources.ListPackageResources(dockerfiles_dir)
    # Strips trailing '_app/'.
    return [x[:-5] for x in resources if not x.startswith('.')]
Beispiel #3
0
def GetAllManagedVMsRuntimes():
    """Returns the list of runtimes supported by Managed VMs.

  The list of supported runtimes is built based on the default Dockerfiles
  provided with the SDK.

  Raises:
    InternalError: if there is no directory with default Dockerfiles.

  Returns:
    [str], List of runtimes supported for Managed VMs.
  """
    dockerfiles_dir = GetGCloudDockerfilesDir()
    resources = pkg_resources.ListPackageResources(dockerfiles_dir)
    # Strips trailing '_app/'.
    dockerfile_runtimes = [x[:-5] for x in resources if not x.startswith('.')]

    # We also support mappings from other strings to these runtime IDs.
    # java7->java, for example.
    return dockerfile_runtimes + config.CANONICAL_RUNTIMES.keys()
Beispiel #4
0
def _GetValidator(schema, schema_dir):
    """"Construct a validator that uses the given schema.

  The validator is able to resolve references to all other schemas in the same
  directory.

  Args:
    schema: The schema to validate against.
    schema_dir: The full path to the directory containing the schema.

  Returns:
    A validator.
  """
    validator = validators.validator_for(schema)(schema)
    validator_store = validator.resolver.store
    for resource in pkg_resources.ListPackageResources(schema_dir):
        schema = yaml.load(
            pkg_resources.GetResourceFromFile(
                os.path.join(schema_dir, resource)))
        validator_store[resource] = schema
    return validator