Skip to content

Commit bd17e10

Browse files
authored
Add collector for luarocks ecosystem (#714)
Signed-off-by: Tushar Goel <[email protected]>
1 parent 780e776 commit bd17e10

File tree

2 files changed

+143
-0
lines changed

2 files changed

+143
-0
lines changed

minecode/collectors/luarocks.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#
2+
# Copyright (c) nexB Inc. and others. All rights reserved.
3+
# purldb is a trademark of nexB Inc.
4+
# SPDX-License-Identifier: Apache-2.0
5+
# See http://www.apache.org/licenses/LICENSE-2.0 for the license text.
6+
# See https://github.com/nexB/purldb for support or download.
7+
# See https://aboutcode.org for more information about nexB OSS projects.
8+
#
9+
10+
import logging
11+
from packageurl import PackageURL
12+
import requests
13+
from minecode import priority_router
14+
15+
from packagedb.models import PackageContentType
16+
from packageurl.contrib.purl2url import build_luarocks_download_url
17+
from packagedcode import models as scan_models
18+
19+
logger = logging.getLogger(__name__)
20+
handler = logging.StreamHandler()
21+
logger.addHandler(handler)
22+
logger.setLevel(logging.INFO)
23+
24+
25+
def map_lua_package(package_url, pipelines, priority=0):
26+
"""
27+
Add a lua `package_url` to the PackageDB.
28+
"""
29+
from minecode.model_utils import add_package_to_scan_queue, merge_or_create_package
30+
31+
namespace = package_url.namespace
32+
name = package_url.name
33+
version = package_url.version
34+
35+
download_url = build_luarocks_download_url(str(package_url))
36+
37+
try:
38+
response = requests.head(download_url)
39+
if response.status_code != 200:
40+
error = f"Package does not exist on luarocks.org: {package_url}"
41+
logger.error(error)
42+
return error
43+
except requests.RequestException as e:
44+
error = f"Error checking package existence on luarocks.org: {package_url}, error: {e}"
45+
logger.error(error)
46+
return error
47+
48+
package = scan_models.Package(
49+
type="luarocks",
50+
namespace=namespace,
51+
name=name,
52+
version=version,
53+
download_url=download_url,
54+
homepage_url=f"https://luarocks.org/modules/{namespace}/{name}"
55+
if namespace
56+
else f"https://luarocks.org/modules/{name}",
57+
primary_language="lua",
58+
)
59+
60+
package.extra_data["package_content"] = PackageContentType.SOURCE_ARCHIVE
61+
db_package, _, _, error = merge_or_create_package(package, visit_level=0)
62+
63+
if db_package:
64+
add_package_to_scan_queue(package=db_package, pipelines=pipelines, priority=priority)
65+
66+
return error
67+
68+
69+
@priority_router.route("pkg:luarocks/.*")
70+
def process_request(purl_str, **kwargs):
71+
"""
72+
Process Luarocks Package URL (PURL).
73+
"""
74+
from minecode.model_utils import DEFAULT_PIPELINES
75+
76+
addon_pipelines = kwargs.get("addon_pipelines", [])
77+
pipelines = DEFAULT_PIPELINES + tuple(addon_pipelines)
78+
priority = kwargs.get("priority", 0)
79+
80+
package_url = PackageURL.from_string(purl_str)
81+
error_msg = map_lua_package(package_url, pipelines, priority)
82+
83+
if error_msg:
84+
return error_msg
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#
2+
# Copyright (c) nexB Inc. and others. All rights reserved.
3+
# purldb is a trademark of nexB Inc.
4+
# SPDX-License-Identifier: Apache-2.0
5+
# See http://www.apache.org/licenses/LICENSE-2.0 for the license text.
6+
# See https://github.com/nexB/purldb for support or download.
7+
# See https://aboutcode.org for more information about nexB OSS projects.
8+
#
9+
10+
import pytest
11+
from unittest.mock import patch, MagicMock
12+
13+
from packageurl import PackageURL
14+
import requests
15+
16+
import minecode.collectors.luarocks as luarocks
17+
18+
19+
@pytest.fixture
20+
def package_url():
21+
return PackageURL.from_string("pkg:luarocks/[email protected]")
22+
23+
24+
def test_map_lua_package_success(package_url):
25+
with (
26+
patch("minecode.collectors.luarocks.requests.head") as mock_head,
27+
patch("minecode.model_utils.merge_or_create_package") as mock_merge,
28+
patch("minecode.model_utils.add_package_to_scan_queue") as mock_add,
29+
):
30+
mock_response = MagicMock()
31+
mock_response.status_code = 200
32+
mock_head.return_value = mock_response
33+
34+
mock_merge.return_value = ("db_package", None, None, None)
35+
36+
error = luarocks.map_lua_package(package_url, pipelines=["p1"], priority=1)
37+
38+
assert error is None
39+
mock_head.assert_called_once()
40+
mock_merge.assert_called_once()
41+
mock_add.assert_called_once_with(package="db_package", pipelines=["p1"], priority=1)
42+
43+
44+
def test_map_lua_package_not_found(package_url):
45+
with patch("minecode.collectors.luarocks.requests.head") as mock_head:
46+
mock_response = MagicMock()
47+
mock_response.status_code = 404
48+
mock_head.return_value = mock_response
49+
50+
error = luarocks.map_lua_package(package_url, pipelines=[])
51+
assert "Package does not exist" in error
52+
53+
54+
def test_map_lua_package_network_error(package_url):
55+
with patch("minecode.collectors.luarocks.requests.head") as mock_head:
56+
mock_head.side_effect = requests.RequestException("Network down")
57+
58+
error = luarocks.map_lua_package(package_url, pipelines=[])
59+
assert "Error checking package existence" in error

0 commit comments

Comments
 (0)