horizon/tools/find_settings.py
Radomir Dopieralski d7d6c0577c Add a upgrade_check management command
This command checks the configuration for invalid and deprecated
settings, as described in
https://governance.openstack.org/tc/goals/stein/upgrade-checkers.html

There is also a script in tools/find_settings.py that scans all python
files for the potential new settings, which is supposed to make it
easier to update the lists that the checks use.

Change-Id: Ie85cf4be3da1ab446c10883a4580e20ea154b67c
Story: 2003657
Task: 26132
2019-03-01 15:43:13 +01:00

59 lines
2.0 KiB
Python
Executable File

#!/usr/bin/env python3
# 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 script that scans the source code looking for any use of Django
settings, and compiles a list of possible settings options.
"""
import os
import re
SETTINGS_RE = re.compile(r"""
# Straight use of a setting.
\bsettings[.](\w+)
|
# Using getattr with a default value.
\bgetattr[(]settings,[ ]['"](\w+)['"]
|
# Using a helper function. The first parameter is the request.
\bget_config_value[(][^,]*, ['"](\w+)['"]
""", re.VERBOSE | re.UNICODE)
settings = set()
# Scan all files above the tools directory.
root_path = os.path.normpath(
os.path.join(os.path.dirname(os.path.realpath(__file__)), ".."))
for directory, subdirs, files in os.walk(root_path):
for filename in files:
# Exclude all directories that start with a dot.
subdirs[:] = (
subdir for subdir in subdirs
if not subdir.startswith(".")
)
# Only look at python files.
if not filename.endswith(".py"):
continue
path = os.path.join(directory, filename)
with open(path, 'r', encoding='utf-8') as f:
data = f.read()
# Look at all matches.
for match in SETTINGS_RE.finditer(data):
# Look at all captured groups.
for group in match.groups():
# Settings have to be non-empty and uppercase.
if group and group.isupper():
settings.add(group)
print(sorted(settings))