Skip to content

Commit

Permalink
Address PR comments #247
Browse files Browse the repository at this point in the history
Reference: #305
Reference: #247

Signed-off-by: John M. Horan <[email protected]>
  • Loading branch information
johnmhoran committed Mar 7, 2024
1 parent bd3b2ca commit 4020e1b
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 45 deletions.
43 changes: 26 additions & 17 deletions purldb-toolkit/src/purldb_toolkit/purlcli.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,13 @@ def check_metadata_purl(purl):


def normalize_purls(purls, unique):
"""
If the command includes the `--unique` flag, take the list of input PURLs,
remove the portion of the PURL that starts with a PURL separator (`@`, `?`
or `#`), and return a deduplicated list of the resulting PURLs (in
`input_purls`) and a list of tuples of each pair of the original input PURL
and the normalized PURL (in `normalized_purls`).
"""
input_purls = []
normalized_purls = []
if unique:
Expand Down Expand Up @@ -220,9 +227,11 @@ def construct_headers(
headers_content["purls"] = purls

if (command_name in ["metadata", "urls", "validate", "versions"]) and unique:
for purl in normalized_purls:
if purl[0] != purl[1]:
warnings.append(f"input PURL: '{purl[0]}' normalized to '{purl[1]}'")
for input_purl, normalized_purl in normalized_purls:
if input_purl != normalized_purl:
warnings.append(
f"input PURL: '{input_purl}' normalized to '{normalized_purl}'"
)

for purl in purls:
if not purl:
Expand All @@ -240,11 +249,12 @@ def construct_headers(
if command_name in ["metadata", "urls", "validate", "versions"]:
purl_warning = purl_warnings.get(purl, None)
if purl_warning:
warnings.append(warning_text[purl_warning])
print(warning_text[purl_warning])
warning = warning_text[purl_warning]
warnings.append(warning)
print(warning)
continue

log_file = Path("purldb-toolkit/src/purldb_toolkit/app.log")
log_file = Path(log_file=os.path.join(os.path.expanduser("~"), "app.log"))
if log_file.is_file():
with open(log_file, "r") as f:
for line in f:
Expand Down Expand Up @@ -331,19 +341,19 @@ def get_urls_details(purls, output, file, unique, head, command_name):
if not purl:
continue

urls_purl = check_urls_purl(purl)
purl_status = check_urls_purl(purl)

if command_name == "urls" and urls_purl in [
if command_name == "urls" and purl_status in [
"validation_error",
"not_valid",
"valid_but_not_supported",
"not_in_upstream_repo",
]:
urls_warnings[purl] = urls_purl
urls_warnings[purl] = purl_status
continue

if command_name == "urls" and urls_purl in ["valid_but_not_fully_supported"]:
urls_warnings[purl] = urls_purl
if command_name == "urls" and purl_status in ["valid_but_not_fully_supported"]:
urls_warnings[purl] = purl_status

# Add the URLs.
url_purl = PackageURL.from_string(purl)
Expand Down Expand Up @@ -647,7 +657,7 @@ def validate_purl(purl):
print(f"validate_purl(): json.decoder.JSONDecodeError for '{purl}': {e}")

logging.basicConfig(
filename="purldb-toolkit/src/purldb_toolkit/app.log",
filename=os.path.join(os.path.expanduser("~"), "app.log"),
level=logging.ERROR,
format="%(levelname)s - %(message)s",
filemode="w",
Expand Down Expand Up @@ -740,7 +750,7 @@ def get_versions_details(purls, output, file, unique, command_name):

for package_version in list(versions(purl)):
purl_version_data = {}
purl_version = package_version.to_dict()["value"]
purl_version = package_version.value

# We use `versions()` from fetchcode/package_versions.py, which
# keeps the version (if any) of the input PURL in its output, so
Expand All @@ -752,9 +762,7 @@ def get_versions_details(purls, output, file, unique, command_name):

purl_version_data["purl"] = nested_purl
purl_version_data["version"] = f"{purl_version}"
purl_version_data["release_date"] = (
f'{package_version.to_dict()["release_date"]}'
)
purl_version_data["release_date"] = f"{package_version.release_date}"

purl_data["versions"].append(purl_version_data)

Expand Down Expand Up @@ -834,7 +842,8 @@ def check_for_duplicate_input_sources(purls, file):


def clear_log_file():
log_file = Path("purldb-toolkit/src/purldb_toolkit/app.log")
log_file = Path(log_file=os.path.join(os.path.expanduser("~"), "app.log"))

if log_file.is_file():
os.remove(log_file)

Expand Down
24 changes: 12 additions & 12 deletions purldb-toolkit/tests/data/purlcli/expected_versions_output.json
Original file line number Diff line number Diff line change
Expand Up @@ -113,62 +113,62 @@
{
"purl": "pkg:hex/[email protected]",
"version": "0.8.0",
"release_date": "2023-09-22T18:28:36.224103+00:00"
"release_date": "2023-09-22 18:28:36.224103+00:00"
},
{
"purl": "pkg:hex/[email protected]",
"version": "0.5.2",
"release_date": "2018-09-03T23:52:38.161321+00:00"
"release_date": "2018-09-03 23:52:38.161321+00:00"
},
{
"purl": "pkg:hex/[email protected]",
"version": "0.5.1",
"release_date": "2018-08-28T01:33:14.565151+00:00"
"release_date": "2018-08-28 01:33:14.565151+00:00"
},
{
"purl": "pkg:hex/[email protected]",
"version": "0.5.0",
"release_date": "2017-08-02T06:23:12.948525+00:00"
"release_date": "2017-08-02 06:23:12.948525+00:00"
},
{
"purl": "pkg:hex/[email protected]",
"version": "0.4.0",
"release_date": "2017-07-03T21:55:56.591426+00:00"
"release_date": "2017-07-03 21:55:56.591426+00:00"
},
{
"purl": "pkg:hex/[email protected]",
"version": "0.3.1",
"release_date": "2016-11-27T05:30:34.553920+00:00"
"release_date": "2016-11-27 05:30:34.553920+00:00"
},
{
"purl": "pkg:hex/[email protected]",
"version": "0.3.0",
"release_date": "2016-08-28T19:04:10.794525+00:00"
"release_date": "2016-08-28 19:04:10.794525+00:00"
},
{
"purl": "pkg:hex/[email protected]",
"version": "0.2.0",
"release_date": "2016-07-30T21:07:45.377540+00:00"
"release_date": "2016-07-30 21:07:45.377540+00:00"
},
{
"purl": "pkg:hex/[email protected]",
"version": "0.1.3",
"release_date": "2016-07-19T03:33:09.185782+00:00"
"release_date": "2016-07-19 03:33:09.185782+00:00"
},
{
"purl": "pkg:hex/[email protected]",
"version": "0.1.2",
"release_date": "2016-07-12T18:41:27.084599+00:00"
"release_date": "2016-07-12 18:41:27.084599+00:00"
},
{
"purl": "pkg:hex/[email protected]",
"version": "0.1.1",
"release_date": "2016-07-11T13:56:26.388096+00:00"
"release_date": "2016-07-11 13:56:26.388096+00:00"
},
{
"purl": "pkg:hex/[email protected]",
"version": "0.1.0",
"release_date": "2016-07-11T06:52:43.545719+00:00"
"release_date": "2016-07-11 06:52:43.545719+00:00"
}
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,62 +77,62 @@
{
"purl": "pkg:hex/[email protected]",
"version": "0.8.0",
"release_date": "2023-09-22T18:28:36.224103+00:00"
"release_date": "2023-09-22 18:28:36.224103+00:00"
},
{
"purl": "pkg:hex/[email protected]",
"version": "0.5.2",
"release_date": "2018-09-03T23:52:38.161321+00:00"
"release_date": "2018-09-03 23:52:38.161321+00:00"
},
{
"purl": "pkg:hex/[email protected]",
"version": "0.5.1",
"release_date": "2018-08-28T01:33:14.565151+00:00"
"release_date": "2018-08-28 01:33:14.565151+00:00"
},
{
"purl": "pkg:hex/[email protected]",
"version": "0.5.0",
"release_date": "2017-08-02T06:23:12.948525+00:00"
"release_date": "2017-08-02 06:23:12.948525+00:00"
},
{
"purl": "pkg:hex/[email protected]",
"version": "0.4.0",
"release_date": "2017-07-03T21:55:56.591426+00:00"
"release_date": "2017-07-03 21:55:56.591426+00:00"
},
{
"purl": "pkg:hex/[email protected]",
"version": "0.3.1",
"release_date": "2016-11-27T05:30:34.553920+00:00"
"release_date": "2016-11-27 05:30:34.553920+00:00"
},
{
"purl": "pkg:hex/[email protected]",
"version": "0.3.0",
"release_date": "2016-08-28T19:04:10.794525+00:00"
"release_date": "2016-08-28 19:04:10.794525+00:00"
},
{
"purl": "pkg:hex/[email protected]",
"version": "0.2.0",
"release_date": "2016-07-30T21:07:45.377540+00:00"
"release_date": "2016-07-30 21:07:45.377540+00:00"
},
{
"purl": "pkg:hex/[email protected]",
"version": "0.1.3",
"release_date": "2016-07-19T03:33:09.185782+00:00"
"release_date": "2016-07-19 03:33:09.185782+00:00"
},
{
"purl": "pkg:hex/[email protected]",
"version": "0.1.2",
"release_date": "2016-07-12T18:41:27.084599+00:00"
"release_date": "2016-07-12 18:41:27.084599+00:00"
},
{
"purl": "pkg:hex/[email protected]",
"version": "0.1.1",
"release_date": "2016-07-11T13:56:26.388096+00:00"
"release_date": "2016-07-11 13:56:26.388096+00:00"
},
{
"purl": "pkg:hex/[email protected]",
"version": "0.1.0",
"release_date": "2016-07-11T06:52:43.545719+00:00"
"release_date": "2016-07-11 06:52:43.545719+00:00"
}
]
}
Expand Down
8 changes: 4 additions & 4 deletions purldb-toolkit/tests/test_purlcli.py
Original file line number Diff line number Diff line change
Expand Up @@ -2022,17 +2022,17 @@ def test_versions_cli_unique(self):
{
"purl": "pkg:pypi/[email protected]",
"version": "0.1.0",
"release_date": "2021-08-25T15:15:15.265015+00:00",
"release_date": "2021-08-25 15:15:15.265015+00:00",
},
{
"purl": "pkg:pypi/[email protected]",
"version": "0.2.0",
"release_date": "2022-09-14T16:36:02.242182+00:00",
"release_date": "2022-09-14 16:36:02.242182+00:00",
},
{
"purl": "pkg:pypi/[email protected]",
"version": "0.3.0",
"release_date": "2023-12-18T20:49:45.840364+00:00",
"release_date": "2023-12-18 20:49:45.840364+00:00",
},
],
}
Expand Down Expand Up @@ -2064,7 +2064,7 @@ def test_versions_cli_unique(self):
{
"purl": "pkg:gem/[email protected]",
"version": "0.1.2",
"release_date": "2013-12-11T00:27:10.097000+00:00",
"release_date": "2013-12-11 00:27:10.097000+00:00",
}
],
}
Expand Down

0 comments on commit 4020e1b

Please sign in to comment.