Add package manager parsers (npm, pip, go, cargo)
Some checks failed
CI / test (push) Has been cancelled
Some checks failed
CI / test (push) Has been cancelled
This commit is contained in:
59
src/depcheck/parsers/go.py
Normal file
59
src/depcheck/parsers/go.py
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
"""Go go.mod parser."""
|
||||||
|
|
||||||
|
import re
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
from depcheck.models import Dependency, PackageManager
|
||||||
|
from depcheck.parsers import Parser
|
||||||
|
from depcheck.utils import parse_version_string
|
||||||
|
|
||||||
|
|
||||||
|
class GoParser(Parser):
|
||||||
|
"""Parser for Go go.mod files."""
|
||||||
|
|
||||||
|
package_manager = PackageManager.GO
|
||||||
|
|
||||||
|
def supports_file(self, file_path: Path) -> bool:
|
||||||
|
return file_path.name == "go.mod"
|
||||||
|
|
||||||
|
def get_file_patterns(self) -> list[str]:
|
||||||
|
return ["go.mod"]
|
||||||
|
|
||||||
|
def parse(self, file_path: Path) -> list[Dependency]:
|
||||||
|
dependencies: list[Dependency] = []
|
||||||
|
|
||||||
|
try:
|
||||||
|
content = file_path.read_text()
|
||||||
|
except OSError:
|
||||||
|
return dependencies
|
||||||
|
|
||||||
|
in_require_block = False
|
||||||
|
for line in content.splitlines():
|
||||||
|
line = line.strip()
|
||||||
|
|
||||||
|
if line.startswith("require ("):
|
||||||
|
in_require_block = True
|
||||||
|
continue
|
||||||
|
elif line == ")":
|
||||||
|
in_require_block = False
|
||||||
|
continue
|
||||||
|
|
||||||
|
if in_require_block or line.startswith("require "):
|
||||||
|
match = re.match(r"^(?:require\s+)?([^\s]+)\s+(.+)$", line)
|
||||||
|
if match:
|
||||||
|
name = match.group(1)
|
||||||
|
version = match.group(2).strip()
|
||||||
|
|
||||||
|
version = parse_version_string(version)
|
||||||
|
if version and not name.startswith("="):
|
||||||
|
dependencies.append(
|
||||||
|
Dependency(
|
||||||
|
name=name,
|
||||||
|
current_version=version,
|
||||||
|
package_manager=self.package_manager,
|
||||||
|
category="dependencies",
|
||||||
|
source_file=str(file_path),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
return dependencies
|
||||||
Reference in New Issue
Block a user