1 # Copyright 2018 Red Hat, Inc.
4 # Licensed under the Apache License, Version 2.0 (the "License"); you may
5 # not use this file except in compliance with the License. You may obtain
6 # a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 # License for the specific language governing permissions and limitations
18 from six.moves import configparser
19 from sphinx.util import logging
24 logger = logging.getLogger(__name__)
27 def _find_setup_cfg(srcdir):
28 """Find the 'setup.cfg' file, if it exists.
30 This assumes we're using 'doc/source' for documentation, but also allows
31 for single level 'doc' paths.
33 # TODO(stephenfin): Are we sure that this will always exist, e.g. for
34 # an sdist or wheel? Perhaps we should check for 'PKG-INFO' or
35 # 'METADATA' files, a la 'pbr.packaging._get_version_from_pkg_metadata'
37 os.path.join(srcdir, os.pardir, 'setup.cfg'),
38 os.path.join(srcdir, os.pardir, os.pardir, 'setup.cfg')]:
39 if os.path.exists(path):
45 def _get_project_name(srcdir):
46 """Return string name of project name, or None.
48 This extracts metadata from 'setup.cfg'. We don't rely on
49 distutils/setuptools as we don't want to actually install the package
55 parser = configparser.ConfigParser()
57 path = _find_setup_cfg(srcdir)
58 if not path or not parser.read(path):
59 logger.info('Could not find a setup.cfg to extract project name '
64 # for project name we use the name in setup.cfg, but if the
65 # length is longer then 32 we use summary. Otherwise thAe
66 # menu rendering looks brolen
67 project = parser.get('metadata', 'name')
68 if len(project.split()) == 1 and len(project) > 32:
69 project = parser.get('metadata', 'summary')
70 except configparser.Error:
71 logger.info('Could not extract project metadata from setup.cfg')
79 def _builder_inited(app):
80 # TODO(stephenfin): Once Sphinx 1.8 is released, we should move the below
81 # to a 'config-inited' handler
83 project_name = _get_project_name(app.srcdir)
85 version_info = pbr.version.VersionInfo(project_name)
89 if version_info and not app.config.version and not app.config.release:
90 app.config.version = version_info.canonical_version_string()
91 app.config.release = version_info.version_string_with_vcs()
95 app.connect('builder-inited', _builder_inited)
97 'parallel_read_safe': True,
98 'parallel_write_safe': True,