/usr/lib/python3/dist-packages/autogenerate_config_docs/autohelp.py is in python3-openstack-doc-tools 0.34.0-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 | #!/usr/bin/env python
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
# A collection of tools for working with flags from OpenStack
# packages and documentation.
#
# For an example of usage, run this program with the -h switch.
#
# Must import this before argparse
from oslo_config import cfg
import argparse
import importlib
import os
import pickle
import re
import sys
import jinja2
import stevedore
try:
from sqlalchemy import exc
except Exception:
pass
sys.path.insert(0, '.')
from hooks import HOOKS # noqa
EXTENSIONS = ['oslo.cache',
'oslo.concurrency',
'oslo.db',
'oslo.log',
'oslo.messaging',
'oslo.middleware',
'oslo.policy',
'oslo.service']
_TYPE_DESCRIPTIONS = {
cfg.StrOpt: 'String',
cfg.BoolOpt: 'Boolean',
cfg.IntOpt: 'Integer',
cfg.FloatOpt: 'Floating point',
cfg.ListOpt: 'List',
cfg.DictOpt: 'Dict',
cfg.MultiStrOpt: 'Multi-valued',
cfg._ConfigFileOpt: 'List of filenames',
cfg._ConfigDirOpt: 'List of directory names',
}
register_re = re.compile(r'''^ +.*\.register_opts\((?P<opts>[^,)]+)'''
r'''(, (group=)?["'](?P<group>.*)["'])?\)''')
def import_modules(repo_location, package_name, verbose=0):
"""Import modules.
Loops through the repository, importing module by module to
populate the configuration object (cfg.CONF) created from Oslo.
"""
with open('ignore.list') as fd:
ignore_list = [l for l in fd.read().split('\n')
if l and (l[0] != '#')]
pkg_location = os.path.join(repo_location, package_name)
for root, dirs, files in os.walk(pkg_location):
skipdir = False
for excludedir in ('tests', 'locale',
os.path.join('db', 'migration'), 'transfer'):
if ((os.path.sep + excludedir + os.path.sep) in root or (
root.endswith(os.path.sep + excludedir))):
skipdir = True
break
if skipdir:
continue
for pyfile in files:
if pyfile.endswith('.py'):
abs_path = os.path.join(root, pyfile)
modfile = abs_path.split(repo_location, 1)[1]
modname = os.path.splitext(modfile)[0].split(os.path.sep)
modname = [m for m in modname if m != '']
modname = '.'.join(modname)
if modname.endswith('.__init__'):
modname = modname[:modname.rfind(".")]
if modname in ignore_list:
continue
try:
module = importlib.import_module(modname)
if verbose >= 1:
print("imported %s" % modname)
except ImportError as e:
"""
work around modules that don't like being imported in
this way FIXME This could probably be better, but does
not affect the configuration options found at this stage
"""
if verbose >= 2:
print("Failed to import: %s (%s)" % (modname, e))
continue
except cfg.DuplicateOptError as e:
"""
oslo.cfg doesn't allow redefinition of a config option, but
we don't mind. Don't fail if this happens.
"""
if verbose >= 2:
print(e)
continue
except cfg.NoSuchGroupError as e:
"""
If a group doesn't exist, we ignore the import.
"""
if verbose >= 2:
print(e)
continue
except exc.InvalidRequestError as e:
if verbose >= 2:
print(e)
continue
except Exception as e:
print("Impossible to import module %s" % modname)
raise e
_register_runtime_opts(module, abs_path, verbose)
_run_hook(modname)
# All the components provide keystone token authentication, usually using a
# pipeline. Since the auth_token options can only be discovered at runtime
# in this configuration, we force their discovery by importing the module.
try:
import keystonemiddleware.auth_token # noqa
except cfg.DuplicateOptError:
pass
def _run_hook(modname):
try:
HOOKS[modname]()
except KeyError:
pass
def _register_runtime_opts(module, abs_path, verbose):
"""Handle options not registered on module import.
This function parses the .py files to discover calls to register_opts in
functions and methods. It then explicitly call cfg.register_opt on each
option to register (most of) them.
"""
with open(abs_path) as fd:
for line in fd:
m = register_re.search(line)
if not m:
continue
opts_var = m.group('opts')
opts_group = m.group('group')
# Get the object (an options list) from the opts_var string.
# This requires parsing the string which can be of the form
# 'foo.bar'. We treat each element as an attribute of the previous.
register = True
obj = module
for item in opts_var.split('.'):
try:
obj = getattr(obj, item)
except AttributeError:
# FIXME(gpocentek): AttributeError is raised when a part of
# the opts_var string is not an actual attribute. This will
# need more parsing tricks.
register = False
if verbose >= 2:
print("Ignoring %(obj)s in %(module)s" %
{'obj': opts_var, 'module': module})
break
if register:
for opt in obj:
if not isinstance(opt, cfg.Opt):
continue
try:
cfg.CONF.register_opt(opt, opts_group)
except cfg.DuplicateOptError:
# ignore options that have already been registered
pass
def _sanitize_default(opt):
"""Adapts unrealistic default values."""
# If the Oslo version is recent enough, we can use the 'sample_default'
# attribute
if (hasattr(opt, 'sample_default') and opt.sample_default is not None):
return str(opt.sample_default)
if ((type(opt).__name__ == "ListOpt") and (type(opt.default) == list)):
return ", ".join(opt.default)
default = str(opt.default)
if default == os.uname()[1]:
return 'localhost'
if opt.name == 'bindir':
return '/usr/local/bin'
if opt.name == 'my_ip':
return '10.0.0.1'
if isinstance(opt, cfg.StrOpt) and default.strip() != default:
return '"%s"' % default
for pathelm in sys.path:
if pathelm in ('.', ''):
continue
if pathelm.endswith('/'):
pathelm = pathelm[:-1]
if pathelm in default:
default = re.sub(r'%s(/sources)?' % pathelm,
'/usr/lib/python/site-packages', default)
return default
def _get_overrides(package_name):
overrides_file = '%s.overrides' % package_name
if not os.path.exists(overrides_file):
return {}
overrides = {}
with open(overrides_file) as fd:
for line in fd:
if line == '#':
continue
try:
opt, sections = line.strip().split(' ', 1)
sections = [x.strip() for x in sections.split(' ')]
except ValueError:
continue
overrides[opt] = sections
return overrides
class OptionsCache(object):
def __init__(self, overrides={}, verbose=0):
self._verbose = verbose
self._opts_by_name = {}
self._opt_names = []
self._overrides = overrides
for optname in cfg.CONF._opts:
opt = cfg.CONF._opts[optname]['opt']
# We ignore some CLI opts by excluding SubCommandOpt objects
if not isinstance(opt, cfg.SubCommandOpt):
self._add_opt(optname, 'DEFAULT', opt)
for group in cfg.CONF._groups:
for optname in cfg.CONF._groups[group]._opts:
self._add_opt(group + '/' + optname, group,
cfg.CONF._groups[group]._opts[optname]['opt'])
self._opt_names.sort(OptionsCache._cmpopts)
def _add_opt(self, optname, group, opt):
if optname in self._opts_by_name:
if self._verbose >= 2:
print ("Duplicate option name %s" % optname)
return
opt.default = _sanitize_default(opt)
def fill(optname, group, opt):
if optname in self._opts_by_name:
return
self._opts_by_name[optname] = (group, opt)
self._opt_names.append(optname)
if optname in self._overrides:
for new_group in self._overrides[optname]:
if new_group == 'DEFAULT':
new_optname = opt.name
else:
new_optname = new_group + '/' + opt.name
fill(new_optname, new_group, opt)
else:
fill(optname, group, opt)
def __len__(self):
return len(self._opt_names)
def load_extension_options(self, module):
# Note that options loaded this way aren't added to _opts_by_module
loader = stevedore.named.NamedExtensionManager(
'oslo.config.opts',
names=(module,),
invoke_on_load=False
)
for ext in loader:
for group, opts in ext.plugin():
for opt in opts:
if group is None:
self._add_opt(opt.dest, 'DEFAULT', opt)
else:
self._add_opt(group + '/' + opt.dest, group, opt)
self._opt_names.sort(OptionsCache._cmpopts)
def maybe_load_extensions(self, repositories):
# Use the requirements.txt of the project to guess if an oslo module
# needs to be imported
needed_exts = set()
for repo in repositories:
base_path = os.path.dirname(repo)
for ext in EXTENSIONS:
requirements = os.path.join(base_path, 'requirements.txt')
with open(requirements) as fd:
for line in fd:
if line.startswith(ext):
needed_exts.add(ext)
for ext in needed_exts:
self.load_extension_options(ext)
def get_option_names(self):
return self._opt_names
def get_option(self, name):
return self._opts_by_name[name]
def dump(self):
"""Dumps the list of options with their attributes.
This output is consumed by the diff_branches script.
"""
for name, (group, option) in self._opts_by_name.items():
deprecated_opts = [{'group': deprecated.group,
'name': deprecated.name}
for deprecated in option.deprecated_opts]
help_str = option.help.strip() if option.help else "None"
new_option = {
'default': option.default,
'help': help_str,
'deprecated_opts': deprecated_opts,
'type': option.__class__.__name__.split('.')[-1]
}
self._opts_by_name[name] = (group, new_option)
print(pickle.dumps(self._opts_by_name))
@staticmethod
def _cmpopts(x, y):
if '/' in x and '/' in y:
prex = x[:x.find('/')]
prey = y[:y.find('/')]
if prex != prey:
return cmp(prex, prey)
return cmp(x, y)
elif '/' in x:
return 1
elif '/' in y:
return -1
else:
return cmp(x, y)
def pass_through(line):
"""Whether to ignore the line."""
return (not line.strip() or
line.startswith('#'))
def _get_options_by_cat(package_name):
options_by_cat = {}
with open(package_name + '.flagmappings') as f:
for line in f:
if pass_through(line):
continue
opt, categories = line.split(' ', 1)
for category in categories.split():
options_by_cat.setdefault(category, []).append(opt)
return options_by_cat
def _get_category_names(package_name):
package_headers = package_name + '.headers'
category_names = {}
for headers_file in ('shared.headers', package_headers):
try:
with open(headers_file) as f:
for line in f:
if pass_through(line):
continue
cat, nice_name = line.split(' ', 1)
category_names[cat] = nice_name.strip()
except IOError:
print("Cannot open %s (ignored)" % headers_file)
return category_names
def write_files(package_name, options, target, output_format):
"""Write tables.
Prints a table for every group of options.
"""
if not target:
if output_format == 'rst':
target = '../../doc/config-reference/source/tables'
else:
target = '../../doc/common/tables/'
options_by_cat = _get_options_by_cat(package_name)
category_names = _get_category_names(package_name)
if not os.path.isdir(target):
os.makedirs(target)
for cat in options_by_cat.keys():
env = {
'pkg': package_name,
'cat': cat,
'groups': [],
'items': [],
}
if cat in category_names:
env['nice_cat'] = category_names[cat]
else:
env['nice_cat'] = cat
print("No nicename for %s" % cat)
curgroup = None
items = None
for optname in options_by_cat[cat]:
group, option = options.get_option(optname)
if group != curgroup:
if group is not None:
curgroup = group
env['groups'].append(group)
if items is not None:
env['items'].append(items)
items = []
if not option.help:
option.help = "No help text available for this option."
helptext = option.help.strip().replace('\n', ' ')
helptext = ' '.join(helptext.split())
if option.deprecated_for_removal:
if not option.help.strip().startswith('DEPRECATED'):
helptext = 'DEPRECATED: ' + helptext
if option.deprecated_reason:
helptext += ' ' + option.deprecated_reason
opt_type = _TYPE_DESCRIPTIONS.get(type(option), 'Unknown')
item = (option.dest,
_sanitize_default(option),
"(%s) %s" % (opt_type, helptext))
items.append(item)
env['items'].append(items)
env['table_label'] = package_name + '-' + cat
ext = 'rst' if output_format == 'rst' else 'xml'
file_path = ("%(target)s/%(package_name)s-%(cat)s.%(ext)s" %
{'target': target, 'package_name': package_name,
'cat': cat, 'ext': ext})
tmpl_file = os.path.join(os.path.dirname(__file__),
'templates/autohelp.%s.j2' % output_format)
with open(tmpl_file) as fd:
template = jinja2.Template(fd.read(), trim_blocks=True)
output = template.render(filename=file_path, **env)
with open(file_path, 'w') as fd:
fd.write(output)
def update_flagmappings(package_name, options, verbose=0):
"""Update flagmappings file.
Update a flagmappings file, adding or removing entries as needed.
This will create a new file $package_name.flagmappings.new with
category information merged from the existing $package_name.flagmappings.
"""
original_flags = {}
try:
with open(package_name + '.flagmappings') as f:
for line in f:
try:
flag, category = line.split(' ', 1)
except ValueError:
flag = line.strip()
category = "Unknown"
original_flags.setdefault(flag, []).append(category.strip())
except IOError:
# If the flags file doesn't exist we'll create it
pass
updated_flags = []
for opt in options.get_option_names():
if len(original_flags.get(opt, [])) == 1:
updated_flags.append((opt, original_flags[opt][0]))
continue
updated_flags.append((opt, 'Unknown'))
with open(package_name + '.flagmappings.new', 'w') as f:
for flag, category in updated_flags:
f.write(flag + ' ' + category + '\n')
if verbose >= 1:
removed_flags = (set(original_flags.keys()) -
set([x[0] for x in updated_flags]))
added_flags = (set([x[0] for x in updated_flags]) -
set(original_flags.keys()))
print("\nRemoved Flags\n")
for line in sorted(removed_flags, OptionsCache._cmpopts):
print(line)
print("\nAdded Flags\n")
for line in sorted(added_flags, OptionsCache._cmpopts):
print(line)
def main():
parser = argparse.ArgumentParser(
description='Manage flag files, to aid in updating documentation.',
usage='%(prog)s <cmd> <package> [options]')
parser.add_argument('subcommand',
help='Action (update, docbook, rst, dump).',
choices=['update', 'docbook', 'rst', 'dump'])
parser.add_argument('package',
help='Name of the top-level package.')
parser.add_argument('-v', '--verbose',
action='count',
default=0,
dest='verbose',
required=False,)
parser.add_argument('-i', '--input',
dest='repos',
help='Path to a python package in which options '
'should be discoverd. Can be used multiple '
'times.',
required=False,
type=str,
action='append')
parser.add_argument('-o', '--output',
dest='target',
help='Directory or file in which data will be saved.\n'
'Defaults to ../../doc/common/tables/ '
'for "docbook".\n'
'Defaults to stdout for "dump"',
required=False,
type=str,)
args = parser.parse_args()
if args.repos is None:
args.repos = ['./sources/%s/%s' % args.package]
for repository in args.repos:
package_name = os.path.basename(repository)
base_path = os.path.dirname(repository)
sys.path.insert(0, base_path)
try:
__import__(package_name)
except ImportError as e:
if args.verbose >= 1:
print(str(e))
print("Failed to import: %s (%s)" % (package_name, e))
import_modules(base_path, package_name, verbose=args.verbose)
sys.path.pop(0)
overrides = _get_overrides(package_name)
options = OptionsCache(overrides, verbose=args.verbose)
options.maybe_load_extensions(args.repos)
if args.verbose > 0:
print("%s options imported from package %s." % (len(options),
str(package_name)))
if args.subcommand == 'update':
update_flagmappings(args.package, options, verbose=args.verbose)
elif args.subcommand in ('docbook', 'rst'):
write_files(args.package, options, args.target, args.subcommand)
elif args.subcommand == 'dump':
options.dump()
if __name__ == "__main__":
main()
|