Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Config checker #376

Merged
merged 4 commits into from
Apr 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions pacman/config_holder.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,54 @@ def config_options(section):
:param str section: What section to list options for.
"""
return __config.options(section)


def _check_lines(py_path, line, lines, index, method):
"""
Support for check_python_file. Gets section and option name

:param str line: Line with get_config call
:param list(str) lines: All lines in the file
:param int index: index of line with get_config call
:raise Exception: If an unexpected or uncovered get_config found
"""
while ")" not in line:
index += 1
line += lines[index]
parts = line[line.find("(", line.find("get_config")) + 1:
line.find(")")].split(",")
section = parts[0].strip().replace("'", "").replace('"', '')
option = parts[1].strip()
if option[0] == "'":
option = option.replace("'", "")
elif option[0] == '"':
option = option.replace('"', '')
else:
print(line)
return
try:
method(section, option)
except Exception:
raise Exception(f"failed in line:{index} of file: {py_path}")


def check_python_file(py_path):
"""
A testing function to check that all the get_config calls work

:param str py_path: path to file to be checked
:raise Exception: If an unexpected or uncovered get_config found
"""
with open(py_path, 'r') as py_file:
lines = py_file.readlines()
for index, line in enumerate(lines):
if "get_config_bool(" in line:
_check_lines(py_path, line, lines, index, get_config_bool)
if "get_config_float(" in line:
_check_lines(py_path, line, lines, index, get_config_float)
if "get_config_int(" in line:
_check_lines(py_path, line, lines, index, get_config_int)
if "get_config_str(" in line:
_check_lines(py_path, line, lines, index, get_config_str)
if "get_config_str_list(" in line:
_check_lines(py_path, line, lines, index, get_config_str_list)
1 change: 1 addition & 0 deletions pacman/spinnaker.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ write_json_routing_tables = False
write_json_partition_n_keys_map = False
write_compressor_iobuf = False
write_bit_field_compressor_report = False
generate_router_compression_with_bitfield_report = True

# NOTE ***that for bespoke file paths, folders will not be automatically deleted***
# options are DEFAULT or a file path
Expand Down
33 changes: 33 additions & 0 deletions unittests/test_cfg_checker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Copyright (c) 2017-2019 The University of Manchester
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import os
import unittest
from pacman.config_holder import check_python_file


class TestCfgChecker(unittest.TestCase):

def test_import_all(self):
module = __import__("pacman")
path = module.__file__
directory = os.path.dirname(path)
for root, dirs, files in os.walk(directory):
for file_name in files:
if file_name.endswith(".py"):
if file_name == "config_holder.py":
continue
py_path = os.path.join(root, file_name)
check_python_file(py_path)