Exemple #1
0
def Load(path=None, cli=None, one_time_use_ok=False, verbose=False):
    """Loads the defdault CLI tree from the Python file path.

  Args:
    path: The path name of the Python file the CLI tree was dumped to. None
      for the default CLI tree path.
    cli: The CLI. If not None and path fails to import, a new CLI tree is
      generated, written to path, and returned.
    one_time_use_ok: If True and the load fails then the CLI tree is generated
      on the fly for one time use.
    verbose: Display a status line for up to date CLI trees if True.

  Raises:
    CliTreeVersionError: loaded tree version mismatch
    ImportModuleError: import errors

  Returns:
    The CLI tree.
  """
    if path is None:
        try:
            path = CliTreePath()
        except SdkRootNotFoundError:
            if cli and one_time_use_ok:
                tree = _GenerateRoot(cli)
                return resource_projector.MakeSerializable(tree)
            raise
    while True:
        # This loop executes 1 or 2 times, the second time either succeeds in
        # loading the newly created CLI tree or fails with an import exception.
        try:
            tree = module_util.ImportPath(path).TREE
            if _IsValidCliTreeVersion(tree, path, VERSION,
                                      _GetDefaultCliCommandVersion(),
                                      bool(cli), verbose):
                return tree
            del tree
            # The CLI tree exists but doesn't match VERSION. Clobber path to make
            # sure it's regenerated.
            try:
                os.remove(path)
            except OSError:
                pass
        except module_util.ImportModuleError:
            if not cli:
                raise
        Dump(cli=cli, path=path)
        cli = None
def _Load(path, cli=None, force=False, verbose=False):
    """Load() helper. Returns a tree or None if the tree failed to load."""
    try:
        if not force:
            tree = module_util.ImportPath(path).TREE
            if _IsUpToDate(tree, path, bool(cli), verbose):
                return tree
            del tree
        # Clobber path to make sure it's regenerated.
        try:
            os.remove(path)
        except OSError:
            pass
    except module_util.ImportModuleError:
        if not cli:
            raise
    return None
Exemple #3
0
def Load(path=None, cli=None, one_time_use_ok=False):
    """Loads a CLI tree from the Python file path.

  Args:
    path: The path name of the Python file the CLI tree was dumped to. None
      for the default CLI tree path.
    cli: The CLI. If not None and path fails to import, a new CLI tree is
      generated, written to path, and returned.
    one_time_use_ok: If True and the load fails then the CLI tree is generated
      on the fly for one time use.

  Returns:
    The CLI tree.
  """
    if path is None:
        try:
            path = CliTreePath()
        except SdkRootNotFoundException:
            if cli and one_time_use_ok:
                tree = _GenerateRoot(cli)
                setattr(tree, _LOOKUP_VERSION, VERSION)
                return resource_projector.MakeSerializable(tree)
            raise
    while True:
        # This loop executes 1 or 2 times, the second time either succeeds in
        # loading the newly created CLI tree or fails with an import exception.
        try:
            tree = module_util.ImportPath(path).TREE
            if tree.get(_LOOKUP_VERSION) == VERSION:
                return tree
            del tree
            # The CLI tree exists but doesn't match VERSION. Clobber path to make
            # sure it's regenerated.
            try:
                os.remove(path)
            except OSError:
                pass
        except module_util.ImportModuleError:
            if not cli:
                raise
        Dump(cli=cli, path=path)
        cli = None
def ListAll(directory=None):
    """Returns the CliTreeInfo list of all available CLI trees.

  Args:
    directory: The config directory containing the CLI tree modules.

  Raises:
    CliTreeVersionError: loaded tree version mismatch
    ImportModuleError: import errors

  Returns:
    The CLI tree.
  """
    # List all CLIs by searching directories in order. .py, .pyc, and .json
    # files are treated as CLI modules/data, where the file base name is the name
    # of the CLI root command.
    directories = [
        directory,  # Explicit caller override dir
        cli_tree.CliTreeConfigDir(),  # Config dir shared across installations
        cli_tree.CliTreeDir(),  # Installation dir controlled by the updater
    ]

    trees = []
    for directory in directories:
        if not directory or not os.path.exists(directory):
            continue
        for (dirpath, _, filenames) in os.walk(directory):
            for filename in sorted(filenames):  # For stability across runs.
                base, extension = os.path.splitext(filename)
                if base == '__init__' or '.' in base:
                    # Ignore Python droppings and names containing more than one dot.
                    continue
                path = os.path.join(dirpath, filename)
                error = ''
                tree = None
                if extension in ('.py', '.pyc'):
                    try:
                        module = module_util.ImportPath(path)
                    except module_util.ImportModuleError as e:
                        error = six.text_type(e)
                    try:
                        tree = module.TREE
                    except AttributeError:
                        tree = None
                elif extension == '.json':
                    try:
                        tree = json.loads(files.ReadFileContents(path))
                    except Exception as e:  # pylint: disable=broad-except, record all errors
                        error = six.text_type(e)
                if tree:
                    version = tree.get(cli_tree.LOOKUP_VERSION, 'UNKNOWN')
                    cli_version = tree.get(cli_tree.LOOKUP_CLI_VERSION,
                                           'UNKNOWN')
                    del tree
                else:
                    version = 'UNKNOWN'
                    cli_version = 'UNKNOWN'
                trees.append(
                    CliTreeInfo(command=base,
                                path=_ParameterizePath(path),
                                version=version,
                                cli_version=cli_version,
                                command_installed=bool(
                                    files.FindExecutableOnPath(base)),
                                error=error))
            # Don't search subdirectories.
            break
    return trees