def test_sanitize_default_fqdn(self):
     fake_fqdn = 'fakehost.fakedomain'
     self.useFixture(fixtures.MonkeyPatch('socket.getfqdn',
                                          lambda: fake_fqdn))
     result = generator._sanitize_default('host', fake_fqdn)
     self.assertEqual('oslo', result)
     result = generator._sanitize_default('neutron_id',
                                          'id-%s' % fake_fqdn)
     self.assertEqual('id-oslo', result)
 def test_sanitize_value_matches_hostname(self):
     fake_hostname = 'foo'
     self.useFixture(fixtures.MonkeyPatch('socket.gethostname',
                                          lambda: fake_hostname))
     result = generator._sanitize_default('something', fake_hostname)
     self.assertEqual(fake_hostname, result)
     result = generator._sanitize_default('neutron_id',
                                          'id-%s' % fake_hostname)
     self.assertEqual('id-oslo', result)
Beispiel #3
0
def write_docbook_rootwrap(package_name, repo, target, verbose=0):
    """Write a DocBook table for rootwrap options.

    Prints a docbook-formatted table for options in a project's
    rootwrap.conf configuration file.
    """
    target = target or '../../doc/common/tables/'

    # The sample rootwrap.conf path is not the same in all projects. It is
    # either in etc/ or in etc/<project>/, so we check both locations.
    conffile = os.path.join(repo, 'etc', package_name, 'rootwrap.conf')
    if not os.path.exists(conffile):
        conffile = os.path.join(repo, 'etc', 'rootwrap.conf')
        if not os.path.exists(conffile):
            return

    # Python's configparser doesn't pass comments through. We need those
    # to have some sort of description for the options. This simple parser
    # doesn't handle everything configparser does, but it handles everything
    # in the currentr rootwrap example conf files.
    curcomment = ''
    curgroup = 'DEFAULT'
    options = []
    for line in open(conffile):
        line = line.strip()
        if line.startswith('#'):
            if curcomment != '':
                curcomment += ' '
            curcomment += line[1:].strip()
        elif line.startswith('['):
            if line.endswith(']'):
                curgroup = line[1:-1]
                curcomment = ''
        elif '=' in line:
            key, val = line.split('=')
            options.append((curgroup, key.strip(), val.strip(), curcomment))
            curcomment = ''

    if len(options) == 0:
        return

    if not os.path.isdir(target):
        os.makedirs(target)

    file_path = ("%(target)s/%(package_name)s-rootwrap.xml" %
                 {'target': target, 'package_name': package_name})
    groups_file = open(file_path, 'w')
    groups_file.write(TABLE_HEADER % {'pkg': package_name, 'cat': 'rootwrap'})
    curgroup = None
    for group, optname, default, desc in options:
        if group != curgroup:
            curgroup = group
            groups_file.write('''      <tr>
        <th colspan="2">[%s]</th>
      </tr>\n''' % group)
        if desc == '':
            desc = "No help text available for this option."
        groups_file.write('      <tr>\n')
        default = generator._sanitize_default(optname, str(default))
        groups_file.write('        <td>%s = %s</td>\n' %
                          (optname, xml.sax.saxutils.escape(default)))
        groups_file.write('        <td>%s</td>\n' %
                          xml.sax.saxutils.escape(desc))
        groups_file.write('      </tr>\n')
    groups_file.write(TABLE_FOOTER)
    groups_file.close()
Beispiel #4
0
def write_docbook(package_name, options, target, verbose=0):
    """Write DocBook tables.

    Prints a docbook-formatted table for every group of options.
    """
    target = target or '../../doc/common/tables/'
    options_by_cat = {}

    # Compute the absolute path of the git repository (the relative path is
    # prepended to sys.path in autohelp.py)
    target_abspath = os.path.abspath(sys.path[0])

    # This regex will be used to sanitize file paths and uris
    uri_re = re.compile(r'(^[^:]+://)?%s' % target_abspath)

    with open(package_name + '.flagmappings') as f:
        for line in f:
            opt, categories = line.split(' ', 1)
            for category in categories.split():
                options_by_cat.setdefault(category, []).append(opt)

    if not os.path.isdir(target):
        os.makedirs(target)

    for cat in options_by_cat.keys():
        file_path = ("%(target)s/%(package_name)s-%(cat)s.xml" %
                     {'target': target, 'package_name': package_name,
                      'cat': cat})
        groups_file = open(file_path, 'w')
        groups_file.write(TABLE_HEADER % {'pkg': package_name,
                                          'cat': category})
        curgroup = None
        for optname in options_by_cat[cat]:
            group, option = options.get_option(optname)
            if group != curgroup:
                curgroup = group
                groups_file.write('''      <tr>
        <th colspan="2">[%s]</th>
      </tr>\n''' % group)
            if not option.help:
                option.help = "No help text available for this option."
            if ((type(option).__name__ == "ListOpt") and (
                    type(option.default) == list)):
                option.default = ", ".join(option.default)
            groups_file.write('      <tr>\n')
            default = generator._sanitize_default(option.name,
                                                  str(option.default))
            # This should be moved to generator._sanitize_default
            # NOTE(gpocentek): The first element in the path is the current
            # project git repository path. It is not useful to test values
            # against it, and it causes trouble if it is the same as the python
            # module name. So we just drop it.
            for pathelm in sys.path[1:]:
                if pathelm == '':
                    continue
                if pathelm.endswith('/'):
                    pathelm = pathelm[:-1]
                if default.startswith(pathelm):
                    default = default.replace(pathelm,
                                              '/usr/lib/python/site-packages')
                    break
            if uri_re.search(default):
                default = default.replace(target_abspath,
                                          '/usr/lib/python/site-packages')
            groups_file.write('        <td>%s = %s</td>\n' %
                              (option.dest, default))
            groups_file.write('        <td>(%s) %s</td>\n' %
                              (type(option).__name__,
                               xml.sax.saxutils.escape(option.help)))
            groups_file.write('      </tr>\n')
        groups_file.write(TABLE_FOOTER)
        groups_file.close()
def write_docbook_rootwrap(package_name, repo, target, verbose=0):
    """Write a DocBook table for rootwrap options.

    Prints a docbook-formatted table for options in a project's
    rootwrap.conf configuration file.
    """
    target = target or '../../doc/common/tables/'

    # The sample rootwrap.conf path is not the same in all projects. It is
    # either in etc/ or in etc/<project>/, so we check both locations.
    conffile = os.path.join(repo, 'etc', package_name, 'rootwrap.conf')
    if not os.path.exists(conffile):
        conffile = os.path.join(repo, 'etc', 'rootwrap.conf')
        if not os.path.exists(conffile):
            return

    # Python's configparser doesn't pass comments through. We need those
    # to have some sort of description for the options. This simple parser
    # doesn't handle everything configparser does, but it handles everything
    # in the currentr rootwrap example conf files.
    curcomment = ''
    curgroup = 'DEFAULT'
    options = []
    for line in open(conffile):
        line = line.strip()
        if line.startswith('#'):
            if curcomment != '':
                curcomment += ' '
            curcomment += line[1:].strip()
        elif line.startswith('['):
            if line.endswith(']'):
                curgroup = line[1:-1]
                curcomment = ''
        elif '=' in line:
            key, val = line.split('=')
            options.append((curgroup, key.strip(), val.strip(), curcomment))
            curcomment = ''

    if len(options) == 0:
        return

    if not os.path.isdir(target):
        os.makedirs(target)

    parser = etree.XMLParser(remove_blank_text=True)
    xml = etree.XML(BASE_XML % {'pkg': package_name, 'cat': 'rootwrap'},
                    parser)
    tbody = xml.find(".//{http://docbook.org/ns/docbook}tbody")

    curgroup = None
    for group, optname, default, desc in options:
        if group != curgroup:
            curgroup = group
            tr = etree.Element('tr')
            th = etree.Element('th', colspan="2")
            th.text = "[%s]" % group
            tr.append(th)
            tbody.append(tr)
        if desc == '':
            desc = "No help text available for this option."
        default = generator._sanitize_default(optname, str(default))

        tr = etree.Element('tr')
        tbody.append(tr)

        td = etree.Element('td')
        td.text = "%s = %s" % (optname, default)
        tr.append(td)

        td = etree.Element('td')
        td.text = desc
        tr.append(td)

    file_path = ("%(target)s/%(package_name)s-rootwrap.xml" %
                 {'target': target, 'package_name': package_name})
    with open(file_path, 'w') as fd:
        fd.write(etree.tostring(xml, pretty_print=True,
                                xml_declaration=True,
                                encoding="UTF-8"))
def write_docbook(package_name, options, target, verbose=0):
    """Write DocBook tables.

    Prints a docbook-formatted table for every group of options.
    """
    target = target or '../../doc/common/tables/'
    options_by_cat = {}

    # Compute the absolute path of the git repository (the relative path is
    # prepended to sys.path in autohelp.py)
    target_abspath = os.path.abspath(sys.path[0])

    # This regex will be used to sanitize file paths and uris
    uri_re = re.compile(r'(^[^:]+://)?%s' % target_abspath)

    with open(package_name + '.flagmappings') as f:
        for line in f:
            opt, categories = line.split(' ', 1)
            for category in categories.split():
                options_by_cat.setdefault(category, []).append(opt)

    if not os.path.isdir(target):
        os.makedirs(target)

    for cat in options_by_cat.keys():
        parser = etree.XMLParser(remove_blank_text=True)
        xml = etree.XML(BASE_XML % {'pkg': package_name, 'cat': cat}, parser)
        tbody = xml.find(".//{http://docbook.org/ns/docbook}tbody")
        curgroup = None
        for optname in options_by_cat[cat]:
            group, option = options.get_option(optname)
            if group != curgroup:
                curgroup = group
                tr = etree.Element('tr')
                th = etree.Element('th', colspan="2")
                th.text = "[%s]" % group
                tr.append(th)
                tbody.append(tr)
            if not option.help:
                option.help = "No help text available for this option."
            if ((type(option).__name__ == "ListOpt") and (
                    type(option.default) == list)):
                option.default = ", ".join(option.default)
            default = generator._sanitize_default(option.name,
                                                  str(option.default))
            # This should be moved to generator._sanitize_default
            # NOTE(gpocentek): The first element in the path is the current
            # project git repository path. It is not useful to test values
            # against it, and it causes trouble if it is the same as the python
            # module name. So we just drop it.
            for pathelm in sys.path[1:]:
                if pathelm == '':
                    continue
                if pathelm.endswith('/'):
                    pathelm = pathelm[:-1]
                if default.startswith(pathelm):
                    default = default.replace(pathelm,
                                              '/usr/lib/python/site-packages')
                    break
            if uri_re.search(default):
                default = default.replace(target_abspath,
                                          '/usr/lib/python/site-packages')

            tr = etree.Element('tr')
            tbody.append(tr)

            td = etree.Element('td')
            td.text = "%s = %s" % (option.dest, default)
            tr.append(td)

            td = etree.Element('td')
            td.text = "(%s) %s" % (type(option).__name__, option.help)
            tr.append(td)

        file_path = ("%(target)s/%(package_name)s-%(cat)s.xml" %
                     {'target': target, 'package_name': package_name,
                      'cat': cat})
        with open(file_path, 'w') as fd:
            fd.write(etree.tostring(xml, pretty_print=True,
                                    xml_declaration=True,
                                    encoding="UTF-8"))
def write_docbook_rootwrap(package_name, repo, verbose=0, target='./'):
    """Write a DocBook table for rootwrap options.

    Prints a docbook-formatted table for options in a project's
    rootwrap.conf configuration file.
    """

    # The sample rootwrap.conf path is not the same in all projects. It is
    # either in etc/ or in etc/<project>/, so we check both locations.
    conffile = os.path.join(repo, 'etc', package_name, 'rootwrap.conf')
    if not os.path.exists(conffile):
        conffile = os.path.join(repo, 'etc', 'rootwrap.conf')
        if not os.path.exists(conffile):
            return

    # Python's configparser doesn't pass comments through. We need those
    # to have some sort of description for the options. This simple parser
    # doesn't handle everything configparser does, but it handles everything
    # in the currentr rootwrap example conf files.
    curcomment = ''
    curgroup = 'DEFAULT'
    options = []
    for line in open(conffile):
        line = line.strip()
        if line.startswith('#'):
            if curcomment != '':
                curcomment += ' '
            curcomment += line[1:].strip()
        elif line.startswith('['):
            if line.endswith(']'):
                curgroup = line[1:-1]
                curcomment = ''
        elif '=' in line:
            key, val = line.split('=')
            options.append((curgroup, key.strip(), val.strip(), curcomment))
            curcomment = ''

    if len(options) == 0:
        return

    if not os.path.isdir(target):
        os.makedirs(target)

    file_path = ("%(target)s/%(package_name)s-rootwrap.xml" %
                 {'target': target, 'package_name': package_name})
    groups_file = open(file_path, 'w')
    groups_file.write(TABLE_HEADER % {'pkg': package_name, 'cat': 'rootwrap'})
    curgroup = None
    for group, optname, default, desc in options:
        if group != curgroup:
            curgroup = group
            groups_file.write('''      <tr>
        <th colspan="2">[%s]</th>
      </tr>\n''' % group)
        if desc == '':
            desc = "No help text available for this option."
        groups_file.write('      <tr>\n')
        default = generator._sanitize_default(optname, str(default))
        groups_file.write('        <td>%s = %s</td>\n' %
                          (optname, xml.sax.saxutils.escape(default)))
        groups_file.write('        <td>%s</td>\n' %
                          xml.sax.saxutils.escape(desc))
        groups_file.write('      </tr>\n')
    groups_file.write(TABLE_FOOTER)
    groups_file.close()
def write_docbook(package_name, options, verbose=0, target='./'):
    """Write DocBook tables.

    Prints a docbook-formatted table for every group of options.
    """
    options_by_cat = {}

    # Compute the absolute path of the git repository (the relative path is
    # prepended to sys.path in autohelp.py)
    target_abspath = os.path.abspath(sys.path[0])

    # This regex will be used to sanitize file paths and uris
    uri_re = re.compile(r'(^[^:]+://)?%s' % target_abspath)

    with open(package_name + '.flagmappings') as f:
        for line in f:
            opt, categories = line.split(' ', 1)
            for category in categories.split():
                options_by_cat.setdefault(category, []).append(opt)

    if not os.path.isdir(target):
        os.makedirs(target)

    for cat in options_by_cat.keys():
        file_path = ("%(target)s/%(package_name)s-%(cat)s.xml" %
                     {'target': target, 'package_name': package_name,
                      'cat': cat})
        groups_file = open(file_path, 'w')
        groups_file.write(TABLE_HEADER % {'pkg': package_name,
                                          'cat': category})
        curgroup = None
        for optname in options_by_cat[cat]:
            group, option = options.get_option(optname)
            if group != curgroup:
                curgroup = group
                groups_file.write('''      <tr>
        <th colspan="2">[%s]</th>
      </tr>\n''' % group)
            if not option.help:
                option.help = "No help text available for this option."
            if ((type(option).__name__ == "ListOpt") and (
                    type(option.default) == list)):
                option.default = ", ".join(option.default)
            groups_file.write('      <tr>\n')
            default = generator._sanitize_default(option.name,
                                                  str(option.default))
            # This should be moved to generator._sanitize_default
            # NOTE(gpocentek): The first element in the path is the current
            # project git repository path. It is not useful to test values
            # against it, and it causes trouble if it is the same as the python
            # module name. So we just drop it.
            for pathelm in sys.path[1:]:
                if pathelm == '':
                    continue
                if pathelm.endswith('/'):
                    pathelm = pathelm[:-1]
                if default.startswith(pathelm):
                    default = default.replace(pathelm,
                                              '/usr/lib/python/site-packages')
                    break
            if uri_re.search(default):
                default = default.replace(target_abspath,
                                          '/usr/lib/python/site-packages')
            groups_file.write('        <td>%s = %s</td>\n' %
                              (option.dest, default))
            groups_file.write('        <td>(%s) %s</td>\n' %
                              (type(option).__name__,
                               xml.sax.saxutils.escape(option.help)))
            groups_file.write('      </tr>\n')
        groups_file.write(TABLE_FOOTER)
        groups_file.close()