def SetUp(self): class _FakeStream(object): @staticmethod def close(): self.completions_closed = True @staticmethod def write(s): self.completions_value = s cli_dir = os.path.join(self.temp_path, 'data', 'cli') files.MakeDir(cli_dir) self.WalkTestCli('sdk4') with files.FileWriter(os.path.join(cli_dir, 'gcloud_completions.py')) as f: self.root = generate.ListCompletionTree(cli=self.test_cli, out=f) self.completions_closed = False self.completions_value = None self.StartObjectPatch(lookup, '_OpenCompletionsOutputStream', return_value=_FakeStream()) if 'gcloud_completions' in sys.modules: # At least one test exercises the real import in the lookup module. That # one skips this branch, but it poisons sys.modules and hangs around for # the remaining tests. This mocks the subsequent tests to return the test # CLI tree generated above. self.StartObjectPatch(lookup, 'LoadCompletionCliTree', return_value=self.root) self.StartObjectPatch(lookup, '_GetInstallationRootDir', return_value=self.temp_path) self.env = {lookup.IFS_ENV_VAR: ' '}
def Run(self, args): branch = args.branch.split('.') if args.branch else None if args.completions: generate.ListCompletionTree(cli=self._cli_power_users_only, branch=branch) else: cli_tree.Dump(cli=self._cli_power_users_only, path='-', branch=branch)
def UpdateCliTrees(cli=None, commands=None, directory=None, tarball=None, force=False, verbose=False, warn_on_exceptions=False): """(re)generates the CLI trees in directory if non-existent or out of date. This function uses the progress tracker because some of the updates can take ~minutes. Args: cli: The default CLI. If not None then the default CLI is also updated. commands: Update only the commands in this list. directory: The directory containing the CLI tree JSON files. If None then the default installation directories are used. tarball: For packaging CLI trees. --commands specifies one command that is a relative path in this tarball. The tarball is extracted to a temporary directory and the command path is adjusted to point to the temporary directory. force: Update all exitsing trees by forcing them to be out of date if True. verbose: Display a status line for up to date CLI trees if True. warn_on_exceptions: Emits warning messages in lieu of exceptions. Used during installation. Raises: NoCliTreeGeneratorForCommand: A command in commands is not supported (doesn't have a generator). """ directories = _GetDirectories( directory=directory, warn_on_exceptions=warn_on_exceptions) if not commands: commands = set([cli_tree.DEFAULT_CLI_NAME] + list(GENERATORS.keys())) failed = [] for command in sorted(commands): if command != cli_tree.DEFAULT_CLI_NAME: tree = LoadOrGenerate(command, directories=directories, tarball=tarball, force=force, verbose=verbose, warn_on_exceptions=warn_on_exceptions) if not tree: failed.append(command) elif cli: def _Mtime(path): try: return os.path.getmtime(path) except OSError: return 0 # Update the CLI tree. cli_tree_path = cli_tree.CliTreePath(directory=directories[0]) cli_tree.Load(cli=cli, path=cli_tree_path, force=force, verbose=verbose) # Update the static completion CLI tree if older than the CLI tree. To # keep static completion startup lightweight we don't track the release # in the tree data. Using the modify time is a minimal sanity check. completion_tree_path = lookup.CompletionCliTreePath( directory=directories[0]) cli_tree_mtime = _Mtime(cli_tree_path) completion_tree_mtime = _Mtime(completion_tree_path) if (force or not completion_tree_mtime or completion_tree_mtime < cli_tree_mtime): files.MakeDir(os.path.dirname(completion_tree_path)) with files.FileWriter(completion_tree_path) as f: generate_static.ListCompletionTree(cli, out=f) elif verbose: log.status.Print( '[{}] static completion CLI tree is up to date.'.format(command)) if failed: message = 'No CLI tree {} for [{}].'.format( text_utils.Pluralize(len(failed), 'generator'), ', '.join(sorted(failed))) if not warn_on_exceptions: raise NoCliTreeGeneratorForCommand(message) log.warning(message)