-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathconfiguration_loader.py
More file actions
82 lines (68 loc) · 3.75 KB
/
configuration_loader.py
File metadata and controls
82 lines (68 loc) · 3.75 KB
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
#
# Sonar Scanner Python
# Copyright (C) 2011-2026 SonarSource Sàrl
# mailto:info AT sonarsource DOT com
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
import logging
from pathlib import Path
from typing import Any
from pysonar_scanner.configuration.cli import CliConfigurationLoader
from pysonar_scanner.configuration.coveragerc_loader import CoverageRCConfigurationLoader
from pysonar_scanner.configuration.pyproject_toml import TomlConfigurationLoader
from pysonar_scanner.configuration.properties import SONAR_PROJECT_KEY, SONAR_TOKEN, SONAR_PROJECT_BASE_DIR, Key
from pysonar_scanner.configuration.properties import PROPERTIES
from pysonar_scanner.configuration import sonar_project_properties, environment_variables, dynamic_defaults_loader
from pysonar_scanner.exceptions import MissingProperty, MissingPropertyException
def get_static_default_properties() -> dict[Key, Any]:
return {prop.name: prop.default_value for prop in PROPERTIES if prop.default_value is not None}
class ConfigurationLoader:
@staticmethod
def load() -> dict[Key, Any]:
logging.debug("Loading configuration properties...")
# each property loader is required to return NO default values.
# E.g. if no property has been set, an empty dict must be returned.
# Default values should be set through the get_static_default_properties() method
cli_properties = CliConfigurationLoader.load()
# CLI properties have a higher priority than properties file,
# but we need to resolve them first to load the properties file
base_dir = Path(cli_properties.get(SONAR_PROJECT_BASE_DIR, "."))
toml_path_property = cli_properties.get("toml-path", ".")
toml_path = Path(toml_path_property) if "toml-path" in cli_properties else base_dir
toml_properties = TomlConfigurationLoader.load(toml_path)
coverage_properties = CoverageRCConfigurationLoader.load_exclusion_properties(base_dir)
resolved_properties = get_static_default_properties()
resolved_properties.update(dynamic_defaults_loader.load())
resolved_properties.update(coverage_properties)
resolved_properties.update(toml_properties.project_properties)
resolved_properties.update(sonar_project_properties.load(base_dir))
resolved_properties.update(toml_properties.sonar_properties)
resolved_properties.update(environment_variables.load())
resolved_properties.update(cli_properties)
return resolved_properties
@staticmethod
def check_configuration(config: dict[Key, Any]) -> None:
missing_keys = []
if SONAR_TOKEN not in config:
missing_keys.append(MissingProperty(SONAR_TOKEN, "--token"))
if SONAR_PROJECT_KEY not in config:
missing_keys.append(MissingProperty(SONAR_PROJECT_KEY, "--project-key"))
if len(missing_keys) > 0:
raise MissingPropertyException.from_missing_keys(*missing_keys)
def get_token(config: dict[Key, Any]) -> str:
if SONAR_TOKEN not in config:
raise MissingPropertyException(f'Missing property "{SONAR_TOKEN}"')
return config[SONAR_TOKEN]