-
Notifications
You must be signed in to change notification settings - Fork 123
/
cpp.py
92 lines (69 loc) 路 3.11 KB
/
cpp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
"""Dependency file parsers for C/C++ projects."""
import re
from typing import List
from readmeai.core.parsers import BaseFileParser
class CMakeParser(BaseFileParser):
"""Parser for CMake dependency files."""
def __init__(self) -> None:
"""Initializes the handler with given configuration."""
super().__init__()
def parse(self, content: str) -> List[str]:
"""Extracts dependencies, libs, and software from a CMakeLists.txt."""
try:
extracted_dependencies = []
for line in content.splitlines():
line = line.strip()
if (
line.startswith("find_package")
or "target_link_libraries" in line
):
dependencies = re.findall(
r"(?:find_package\(|target_link_libraries\()\s*(\w+)",
line,
)
extracted_dependencies.extend(dependencies)
if line.startswith("target_link_libraries") or line.startswith(
"find_package"
):
libs = re.findall(
r"target_link_libraries\([^)]+\s+([^)]+)\)", line
)
extracted_dependencies.extend(libs)
return list(set(extracted_dependencies))
except re.error as exc:
return self.handle_parsing_error(f"CMakeLists.txt: {str(exc)}")
class ConfigureAcParser(BaseFileParser):
"""Parser for configure.ac dependency files."""
def parse(self, content: str) -> List[str]:
"""Extracts package names from a configure.ac file."""
try:
regex = re.compile(r"AC_CHECK_LIB\([^)]+\s+([^)]+)\)")
return regex.findall(content)
except re.error as exc:
return self.handle_parsing_error(f"configure.ac: {str(exc)}")
class MakefileAmParser(BaseFileParser):
"""Parser for Makefile dependency files."""
def parse(self, content: str) -> List[str]:
"""Extracts dependencies from Makefile.am files."""
try:
extracted_packages = []
bin_programs_regex = r"bin_PROGRAMS\s*=\s*(.*)"
match = re.search(bin_programs_regex, content)
if match:
programs = match.group(1).split()
extracted_packages.extend(programs)
lib_sources_regex = r"libfoo_la_SOURCES\s*=\s*(.*)"
match = re.search(lib_sources_regex, content)
if match:
sources = match.group(1).split()
extracted_packages.extend(sources)
check_programs_regex = r"check_PROGRAMS\s*=\s*(.*)"
match = re.search(check_programs_regex, content)
if match:
programs = match.group(1).split()
extracted_packages.extend(programs)
check_libs_regex = r"check_LTLIBRARIES\s*=\s*(.*)"
match = re.search(check_libs_regex, content)
return list(set(extracted_packages))
except re.error as exc:
return self.handle_parsing_error(f"Makefile.am: {str(exc)}")