Skip to content

Commit 029eb0e

Browse files
author
Trong Nhan Mai
committed
refactor: refactor the function to get the purl-based directory path to a separated module and add tests for it
1 parent 936b52f commit 029eb0e

File tree

5 files changed

+119
-10
lines changed

5 files changed

+119
-10
lines changed

src/macaron/database/table_definitions.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
For table associated with a check see the check module.
1212
"""
1313
import logging
14-
import os
1514
import string
1615
from datetime import datetime
1716
from pathlib import Path
@@ -36,6 +35,7 @@
3635
from macaron.database.database_manager import ORMBase
3736
from macaron.database.db_custom_types import ProvenancePayload, RFC3339DateTime
3837
from macaron.errors import InvalidPURLError
38+
from macaron.path_utils.purl_based_path import get_purl_based_dir
3939
from macaron.repo_finder.repo_finder_enums import CommitFinderInfo, RepoFinderInfo
4040
from macaron.slsa_analyzer.provenance.intoto import InTotoPayload, ProvenanceSubjectPURLMatcher
4141
from macaron.slsa_analyzer.slsa_req import ReqName
@@ -256,15 +256,11 @@ def report_dir_name(self) -> str:
256256
str
257257
The report directory name.
258258
"""
259-
# Sanitize the path and make sure it's a valid file name.
260-
# A purl string is an ASCII URL string that can allow uppercase letters for
261-
# certain parts. So we shouldn't change uppercase letters with lower case
262-
# to avoid merging results for two distinct PURL strings.
263-
allowed_chars = string.ascii_letters + string.digits + "-"
264-
p_type = "".join(c if c in allowed_chars else "_" for c in self.type)
265-
p_namespace = "".join(c if c in allowed_chars else "_" for c in self.namespace) if self.namespace else ""
266-
p_name = "".join(c if c in allowed_chars else "_" for c in self.name)
267-
return os.path.join(p_type, p_namespace, p_name)
259+
return get_purl_based_dir(
260+
purl_name=self.name,
261+
purl_namespace=self.namespace,
262+
purl_type=self.type,
263+
)
268264

269265

270266
class Repository(ORMBase):

src/macaron/path_utils/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Copyright (c) 2025 - 2025, Oracle and/or its affiliates. All rights reserved.
2+
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Copyright (c) 2025 - 2025, Oracle and/or its affiliates. All rights reserved.
2+
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/.
3+
4+
"""This module contains functions to manage PackageURL-based paths."""
5+
6+
import os
7+
import string
8+
9+
10+
def get_purl_based_dir(
11+
purl_type: str,
12+
purl_name: str,
13+
purl_namespace: str | None = None,
14+
) -> str:
15+
"""Return a directory path according to components of a PackageURL.
16+
17+
Parameters
18+
----------
19+
purl_type: str
20+
The type component of the PackageURL as string.
21+
purl_name:str
22+
The name component of the PackageURL as string.
23+
purl_namespace: str | None = None
24+
The namespace component of the PackageURL as string (optional).
25+
26+
Returns
27+
-------
28+
str
29+
The directory path.
30+
31+
Examples
32+
--------
33+
>>> get_purl_based_dir(purl_type="maven", purl_name="macaron", purl_namespace="oracle")
34+
'maven/oracle/macaron'
35+
"""
36+
# Sanitize the path and make sure it's a valid file name.
37+
# A purl string is an ASCII URL string that can allow uppercase letters for
38+
# certain parts. So we shouldn't change uppercase letters with lower case
39+
# to avoid merging results for two distinct PURL strings.
40+
allowed_chars = string.ascii_letters + string.digits + "-"
41+
p_type = "".join(c if c in allowed_chars else "_" for c in purl_type)
42+
p_namespace = "".join(c if c in allowed_chars else "_" for c in purl_namespace) if purl_namespace else ""
43+
p_name = "".join(c if c in allowed_chars else "_" for c in purl_name)
44+
return os.path.join(p_type, p_namespace, p_name)

tests/path_utils/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Copyright (c) 2025 - 2025, Oracle and/or its affiliates. All rights reserved.
2+
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/.
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Copyright (c) 2025 - 2025, Oracle and/or its affiliates. All rights reserved.
2+
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/.
3+
4+
"""This module tests the purl_based_path module."""
5+
6+
import pytest
7+
8+
from macaron.path_utils.purl_based_path import get_purl_based_dir
9+
10+
11+
@pytest.mark.parametrize(
12+
("purl_type", "purl_namespace", "purl_name", "expected"),
13+
[
14+
pytest.param(
15+
"maven",
16+
"oracle",
17+
"macaron",
18+
"maven/oracle/macaron",
19+
id="simple_case_with_no_special_characters",
20+
),
21+
pytest.param(
22+
"maven",
23+
None,
24+
"macaron",
25+
"maven/macaron",
26+
id="no_namespace",
27+
),
28+
pytest.param(
29+
"maven",
30+
"boo#bar",
31+
"macaron@oracle",
32+
"maven/boo_bar/macaron_oracle",
33+
id="handle_non_allow_chars",
34+
),
35+
pytest.param(
36+
"maven",
37+
"boo123bar",
38+
"macaron123oracle",
39+
"maven/boo123bar/macaron123oracle",
40+
id="digits_are_allowed",
41+
),
42+
pytest.param(
43+
"maven",
44+
"boo-bar",
45+
"macaron-oracle",
46+
"maven/boo-bar/macaron-oracle",
47+
id="dashes_are_allowed",
48+
),
49+
],
50+
)
51+
def test_get_purl_based_dir(
52+
purl_type: str,
53+
purl_namespace: str,
54+
purl_name: str,
55+
expected: str,
56+
) -> None:
57+
"""Test the get_purl_based_dir function."""
58+
assert (
59+
get_purl_based_dir(
60+
purl_type=purl_type,
61+
purl_name=purl_name,
62+
purl_namespace=purl_namespace,
63+
)
64+
== expected
65+
)

0 commit comments

Comments
 (0)