Exemplo n.º 1
0
    def run(self):

        super(DocCLI, self).run()

        if self.options.module_path is not None:
            for i in self.options.module_path.split(os.pathsep):
                module_loader.add_directory(i)

        # list modules
        if self.options.list_dir:
            paths = module_loader._get_paths()
            for path in paths:
                self.find_modules(path)

            CLI.pager(self.get_module_list_text())
            return 0

        if len(self.args) == 0:
            raise AnsibleOptionsError("Incorrect options passed")

        # process command line module list
        text = ''
        for module in self.args:

            try:
                filename = module_loader.find_plugin(module)
                if filename is None:
                    self.display.warning(
                        "module %s not found in %s\n" %
                        (module, DocCLI.print_paths(module_loader)))
                    continue

                if any(filename.endswith(x) for x in self.BLACKLIST_EXTS):
                    continue

                try:
                    doc, plainexamples, returndocs = module_docs.get_docstring(
                        filename)
                except:
                    self.display.vvv(traceback.print_exc())
                    self.display.error(
                        "module %s has a documentation error formatting or is missing documentation\nTo see exact traceback use -vvv"
                        % module)
                    continue

                if doc is not None:

                    all_keys = []
                    for (k, v) in doc['options'].iteritems():
                        all_keys.append(k)
                    all_keys = sorted(all_keys)
                    doc['option_keys'] = all_keys

                    doc['filename'] = filename
                    doc['docuri'] = doc['module'].replace('_', '-')
                    doc['now_date'] = datetime.date.today().strftime(
                        '%Y-%m-%d')
                    doc['plainexamples'] = plainexamples
                    doc['returndocs'] = returndocs

                    if self.options.show_snippet:
                        text += DocCLI.get_snippet_text(doc)
                    else:
                        text += DocCLI.get_man_text(doc)
                else:
                    # this typically means we couldn't even parse the docstring, not just that the YAML is busted,
                    # probably a quoting issue.
                    raise AnsibleError("Parsing produced an empty object.")
            except Exception, e:
                self.display.vvv(traceback.print_exc())
                raise AnsibleError(
                    "module %s missing documentation (or could not parse documentation): %s\n"
                    % (module, str(e)))
Exemplo n.º 2
0
    def run(self):

        if self.options.module_path is not None:
            for i in self.options.module_path.split(os.pathsep):
                module_loader.add_directory(i)

        # list modules
        if self.options.list_dir:
            paths = module_loader._get_paths()
            for path in paths:
                self.find_modules(path)

            CLI.pager(self.get_module_list_text())
            return 0

        if len(self.args) == 0:
            raise AnsibleOptionsError("Incorrect options passed")

        # process command line module list
        text = ''
        for module in self.args:

            filename = module_loader.find_plugin(module)
            if filename is None:
                self.display.warning("module %s not found in %s\n" % (module, DocCLI.print_paths(module_loader)))
                continue

            if any(filename.endswith(x) for x in self.BLACKLIST_EXTS):
                continue

            try:
                doc, plainexamples, returndocs = module_docs.get_docstring(filename)
            except:
                self.display.vvv(traceback.print_exc())
                self.display.error("module %s has a documentation error formatting or is missing documentation\nTo see exact traceback use -vvv" % module)
                continue

            if doc is not None:

                all_keys = []
                for (k,v) in doc['options'].iteritems():
                    all_keys.append(k)
                all_keys = sorted(all_keys)
                doc['option_keys'] = all_keys

                doc['filename']         = filename
                doc['docuri']           = doc['module'].replace('_', '-')
                doc['now_date']         = datetime.date.today().strftime('%Y-%m-%d')
                doc['plainexamples']    = plainexamples
                doc['returndocs']       = returndocs

                if self.options.show_snippet:
                    text += DocCLI.get_snippet_text(doc)
                else:
                    text += DocCLI.get_man_text(doc)
            else:
                # this typically means we couldn't even parse the docstring, not just that the YAML is busted,
                # probably a quoting issue.
                self.display.warning("module %s missing documentation (or could not parse documentation)\n" % module)

        CLI.pager(text)
        return 0
Exemplo n.º 3
0
class DocCLI(CLI):
    """ Vault command line class """

    BLACKLIST_EXTS = ('.pyc', '.swp', '.bak', '~', '.rpm')
    IGNORE_FILES = ["COPYING", "CONTRIBUTING", "LICENSE", "README", "VERSION"]

    def __init__(self, args, display=None):

        super(DocCLI, self).__init__(args, display)
        self.module_list = []

    def parse(self):

        self.parser = CLI.base_parser(
            usage='usage: %prog [options] [module...]',
            epilog='Show Ansible module documentation',
        )

        self.parser.add_option("-M",
                               "--module-path",
                               action="store",
                               dest="module_path",
                               default=C.DEFAULT_MODULE_PATH,
                               help="Ansible modules/ directory")
        self.parser.add_option("-l",
                               "--list",
                               action="store_true",
                               default=False,
                               dest='list_dir',
                               help='List available modules')
        self.parser.add_option(
            "-s",
            "--snippet",
            action="store_true",
            default=False,
            dest='show_snippet',
            help='Show playbook snippet for specified module(s)')

        self.options, self.args = self.parser.parse_args()
        self.display.verbosity = self.options.verbosity

    def run(self):

        super(DocCLI, self).run()

        if self.options.module_path is not None:
            for i in self.options.module_path.split(os.pathsep):
                module_loader.add_directory(i)

        # list modules
        if self.options.list_dir:
            paths = module_loader._get_paths()
            for path in paths:
                self.find_modules(path)

            CLI.pager(self.get_module_list_text())
            return 0

        if len(self.args) == 0:
            raise AnsibleOptionsError("Incorrect options passed")

        # process command line module list
        text = ''
        for module in self.args:

            try:
                filename = module_loader.find_plugin(module)
                if filename is None:
                    self.display.warning(
                        "module %s not found in %s\n" %
                        (module, DocCLI.print_paths(module_loader)))
                    continue

                if any(filename.endswith(x) for x in self.BLACKLIST_EXTS):
                    continue

                try:
                    doc, plainexamples, returndocs = module_docs.get_docstring(
                        filename)
                except:
                    self.display.vvv(traceback.print_exc())
                    self.display.error(
                        "module %s has a documentation error formatting or is missing documentation\nTo see exact traceback use -vvv"
                        % module)
                    continue

                if doc is not None:

                    all_keys = []
                    for (k, v) in doc['options'].iteritems():
                        all_keys.append(k)
                    all_keys = sorted(all_keys)
                    doc['option_keys'] = all_keys

                    doc['filename'] = filename
                    doc['docuri'] = doc['module'].replace('_', '-')
                    doc['now_date'] = datetime.date.today().strftime(
                        '%Y-%m-%d')
                    doc['plainexamples'] = plainexamples
                    doc['returndocs'] = returndocs

                    if self.options.show_snippet:
                        text += DocCLI.get_snippet_text(doc)
                    else:
                        text += DocCLI.get_man_text(doc)
                else:
                    # this typically means we couldn't even parse the docstring, not just that the YAML is busted,
                    # probably a quoting issue.
                    raise AnsibleError("Parsing produced an empty object.")
            except Exception, e:
                self.display.vvv(traceback.print_exc())
                raise AnsibleError(
                    "module %s missing documentation (or could not parse documentation): %s\n"
                    % (module, str(e)))

        CLI.pager(text)
        return 0