Skip to content

Commit 44dbf0a

Browse files
authored
fix: preserve the order of elements of lists extracted from defaults.ini (#660)
Signed-off-by: Trong Nhan Mai <trong.nhan.mai@oracle.com>
1 parent 8ba2e38 commit 44dbf0a

File tree

4 files changed

+225
-75
lines changed

4 files changed

+225
-75
lines changed

src/macaron/config/defaults.py

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ class ConfigParser(configparser.ConfigParser):
1818
def get_list(
1919
self,
2020
section: str,
21-
item: str,
21+
option: str,
2222
delimiter: str | None = "\n",
2323
fallback: list[str] | None = None,
24-
duplicated_ok: bool = False,
2524
strip: bool = True,
25+
remove_duplicates: bool = True,
2626
) -> list[str]:
27-
r"""Parse and return a list of strings from an item in ``defaults.ini``.
27+
r"""Parse and return a list of strings from an ``option`` for ``section`` in ``defaults.ini``.
2828
2929
This method uses str.split() to split the value into list of strings.
3030
References: https://docs.python.org/3/library/stdtypes.html#str.split.
@@ -37,24 +37,26 @@ def get_list(
3737
If ``strip`` is True (default: True), strings are whitespace-stripped and empty strings
3838
are removed from the final result.
3939
40-
If ``duplicated_ok`` is True (default: False), duplicated values are not removed from the final list.
40+
If `remove_duplicates` is True, duplicated elements which come after the their first instances will
41+
be removed from the list. This operation happens after ``strip`` is handled.
4142
43+
The order of non-empty elements in the list is preserved.
4244
The content of each string in the list is not validated and should be handled separately.
4345
4446
Parameters
4547
----------
4648
section : str
4749
The section in ``defaults.ini``.
48-
item : str
49-
The item to parse the list.
50+
option : str
51+
The option whose values will be split into the a list of strings.
5052
delimiter : str | None
5153
The delimiter used to split the strings.
5254
fallback : list | None
5355
The fallback value in case of errors.
54-
duplicated_ok : bool
55-
If True allow duplicate values.
56-
strip: bool
56+
strip : bool
5757
If True, strings are whitespace-stripped and any empty strings are removed.
58+
remove_duplicates : bool
59+
If True, duplicated elements will be removed from the list.
5860
5961
Returns
6062
-------
@@ -79,20 +81,23 @@ def get_list(
7981
allowed_hosts == ["github.com", "boo.com gitlab.com", "host com"]
8082
"""
8183
try:
82-
value = self.get(section, item)
84+
value = self.get(section, option)
8385
if isinstance(value, str):
8486
content = value.split(sep=delimiter)
8587

8688
if strip:
8789
content = [x.strip() for x in content if x.strip()]
8890

89-
if duplicated_ok:
91+
if not remove_duplicates:
9092
return content
9193

92-
distinct_values = set()
93-
distinct_values.update(content)
94-
return list(distinct_values)
95-
except configparser.NoOptionError as error:
94+
values = []
95+
for ele in content:
96+
if ele in values:
97+
continue
98+
values.append(ele)
99+
return values
100+
except (configparser.NoOptionError, configparser.NoSectionError) as error:
96101
logger.error(error)
97102

98103
return fallback or []

src/macaron/repo_finder/repo_finder_java.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,6 @@ def _create_urls(self, group: str, artifact: str, version: str) -> list[str]:
119119
"repofinder.java",
120120
"artifact_repositories",
121121
fallback=["https://repo.maven.apache.org/maven2"],
122-
duplicated_ok=True,
123122
)
124123
urls = []
125124
for repo in repositories:
@@ -163,7 +162,7 @@ def _read_pom(self, pom: str) -> list[str]:
163162
The extracted contents as a list of strings.
164163
"""
165164
# Retrieve tags
166-
tags = defaults.get_list("repofinder.java", "repo_pom_paths", duplicated_ok=True)
165+
tags = defaults.get_list("repofinder.java", "repo_pom_paths")
167166
if not any(tags):
168167
logger.debug("No POM tags found for URL discovery.")
169168
return []

src/macaron/slsa_analyzer/registry.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -680,8 +680,8 @@ def prepare(self) -> bool:
680680
logger.error("Found circular dependencies in registered checks: %s", str(error))
681681
return False
682682

683-
ex_pats = defaults.get_list(section="analysis.checks", item="exclude", fallback=[])
684-
in_pats = defaults.get_list(section="analysis.checks", item="include", fallback=["*"])
683+
ex_pats = defaults.get_list(section="analysis.checks", option="exclude", fallback=[])
684+
in_pats = defaults.get_list(section="analysis.checks", option="include", fallback=["*"])
685685
try:
686686
checks_to_run = self.get_final_checks(ex_pats, in_pats)
687687
except CheckRegistryError as error:

0 commit comments

Comments
 (0)