From 0e7bb2822483cd33798420a143d107b951154172 Mon Sep 17 00:00:00 2001 From: Doug Hellmann Date: Thu, 26 Apr 2018 16:59:24 -0400 Subject: [PATCH] make the sphinxpolicygen extension handle multiple input/output files Some projects have multiple policy files for different parts of their API. Make the sample generator extension support this by letting the policy_generator_config_file option be set to a list of tuples mapping the config file to the output file base name, as we do in the sample generator in oslo.config. Change-Id: I0c7fa409a1ed0f49d65c9b90b71317066f6d3505 Signed-off-by: Doug Hellmann --- doc/source/user/sphinxpolicygen.rst | 5 +++++ oslo_policy/sphinxpolicygen.py | 16 ++++++++++++--- oslo_policy/tests/test_sphinxpolicygen.py | 24 +++++++++++++++++++++++ 3 files changed, 42 insertions(+), 3 deletions(-) diff --git a/doc/source/user/sphinxpolicygen.rst b/doc/source/user/sphinxpolicygen.rst index bf9fc89c..e39f70a1 100644 --- a/doc/source/user/sphinxpolicygen.rst +++ b/doc/source/user/sphinxpolicygen.rst @@ -27,6 +27,11 @@ where: source directory (``app.srcdir``). If this option is not specified or is invalid then the sample policy file generation will be skipped. + To handle cases where multiple files need to be generated, this + value can be a list of two-part tuples containing the path to the + configuration file and the base name for the output file (in this + case, ``sample_policy_basename`` should not be set). + ``sample_policy_basename`` Base name of the output file. This name will be appended with a ``.policy.yaml.sample`` extension to generate the final output file and the diff --git a/oslo_policy/sphinxpolicygen.py b/oslo_policy/sphinxpolicygen.py index 113e0406..427559f8 100644 --- a/oslo_policy/sphinxpolicygen.py +++ b/oslo_policy/sphinxpolicygen.py @@ -28,9 +28,19 @@ def generate_sample(app): "skipping sample policy generation") return - _generate_sample(app, - app.config.policy_generator_config_file, - app.config.sample_policy_basename) + if isinstance(app.config.policy_generator_config_file, list): + for config_file, base_name in app.config.policy_generator_config_file: + if base_name is None: + base_name = _get_default_basename(config_file) + _generate_sample(app, config_file, base_name) + else: + _generate_sample(app, + app.config.policy_generator_config_file, + app.config.sample_policy_basename) + + +def _get_default_basename(config_file): + return os.path.splitext(os.path.basename(config_file))[0] def _generate_sample(app, policy_file, base_name): diff --git a/oslo_policy/tests/test_sphinxpolicygen.py b/oslo_policy/tests/test_sphinxpolicygen.py index fa9a3a14..5474107f 100644 --- a/oslo_policy/tests/test_sphinxpolicygen.py +++ b/oslo_policy/tests/test_sphinxpolicygen.py @@ -50,3 +50,27 @@ class SingleSampleGenerationTest(base.BaseTestCase): sample.assert_called_once_with(args=[ '--config-file', '/opt/nova/nova.conf', '--output-file', '/opt/nova/sample.policy.yaml']) + + @mock.patch('os.path.isdir') + @mock.patch('os.path.isfile') + @mock.patch('oslo_policy.generator.generate_sample') + def test_sample_gen_with_multiple_config_files(self, sample, isfile, + isdir): + # Tests the scenario that policy_generator_config_file is a list + # of two-item tuples of the config file name and policy basename. + isfile.side_effect = [False, True] * 2 + isdir.return_value = True + + config = mock.Mock(policy_generator_config_file=[ + ('nova.conf', 'nova'), + ('placement.conf', 'placement')]) + app = mock.Mock(srcdir='/opt/nova', config=config) + sphinxpolicygen.generate_sample(app) + + sample.assert_has_calls([ + mock.call(args=[ + '--config-file', '/opt/nova/nova.conf', + '--output-file', '/opt/nova/nova.policy.yaml.sample']), + mock.call(args=[ + '--config-file', '/opt/nova/placement.conf', + '--output-file', '/opt/nova/placement.policy.yaml.sample'])])