-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup_parsers.py
executable file
·152 lines (122 loc) · 5.2 KB
/
setup_parsers.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/usr/bin/env python3
"""
Setup script to download tree-sitter grammars for Vulnhound
This script automatically downloads the required tree-sitter parsers
for all supported languages and prepares the environment for code parsing.
"""
import os
import sys
import shutil
import subprocess
from pathlib import Path
from typing import Dict, List, Tuple
# Define the supported language grammars
GRAMMARS = {
'python': 'https://github.com/tree-sitter/tree-sitter-python',
'javascript': 'https://github.com/tree-sitter/tree-sitter-javascript',
'java': 'https://github.com/tree-sitter/tree-sitter-java',
'go': 'https://github.com/tree-sitter/tree-sitter-go',
'ruby': 'https://github.com/tree-sitter/tree-sitter-ruby',
'php': 'https://github.com/tree-sitter/tree-sitter-php',
'c': 'https://github.com/tree-sitter/tree-sitter-c',
'cpp': 'https://github.com/tree-sitter/tree-sitter-cpp',
'c_sharp': 'https://github.com/tree-sitter/tree-sitter-c-sharp',
'typescript': 'https://github.com/tree-sitter/tree-sitter-typescript',
}
def check_git_installed() -> bool:
"""
Check if git is installed on the system
Returns:
bool: True if git is installed, False otherwise
"""
try:
subprocess.run(['git', '--version'], capture_output=True, check=True)
return True
except (subprocess.SubprocessError, FileNotFoundError):
return False
def setup_directories() -> Tuple[Path, Path]:
"""
Create the necessary directories for tree-sitter parsers
Returns:
Tuple of (vendor_dir, build_dir)
"""
# Get the project root directory
root_dir = Path(__file__).parent
# Create vendor directory
vendor_dir = root_dir / 'vendor'
vendor_dir.mkdir(exist_ok=True)
# Create build directory
build_dir = root_dir / 'build'
build_dir.mkdir(exist_ok=True)
return vendor_dir, build_dir
def clone_grammars(vendor_dir: Path) -> List[str]:
"""
Clone tree-sitter grammar repositories
Args:
vendor_dir: Path to vendor directory
Returns:
List of successfully cloned grammar names
"""
success_list = []
for name, url in GRAMMARS.items():
repo_dir = vendor_dir / f'tree-sitter-{name}'
if repo_dir.exists():
print(f"[✓] Grammar '{name}' already exists, updating...")
try:
subprocess.run(
['git', 'pull', 'origin', 'master'],
cwd=repo_dir,
capture_output=True,
check=True
)
success_list.append(name)
print(f"[✓] Successfully updated grammar for {name}")
except subprocess.SubprocessError as e:
print(f"[✗] Error updating grammar for {name}: {e}")
# Keep it in success list if it already exists
success_list.append(name)
else:
print(f"[*] Downloading grammar for {name}...")
try:
subprocess.run(
['git', 'clone', '--depth', '1', url, str(repo_dir)],
check=True,
capture_output=True
)
success_list.append(name)
print(f"[✓] Successfully downloaded grammar for {name}")
except subprocess.SubprocessError as e:
print(f"[✗] Error downloading grammar for {name}: {e}")
return success_list
def main():
"""Set up tree-sitter parsers for Vulnhound"""
print("╔══════════════════════════════════════════════════════╗")
print("║ Setting up tree-sitter parsers for Vulnhound ║")
print("╚══════════════════════════════════════════════════════╝")
# Check if git is installed
if not check_git_installed():
print("[✗] Error: git is not installed or not in PATH")
print(" Please install git and try again.")
return 1
try:
# Setup directories
vendor_dir, build_dir = setup_directories()
# Clone repositories
print("\n[*] Downloading language grammars...")
success_list = clone_grammars(vendor_dir)
if not success_list:
print("\n[✗] Failed to download any grammars. Check your internet connection and try again.")
return 1
# Success message
print("\n[✓] Tree-sitter grammars setup complete!")
print(f"[✓] Successfully set up {len(success_list)} language parsers: {', '.join(success_list)}")
print("[*] Next steps:")
print(" 1. Run 'pip install -r requirements.txt' to install Python dependencies")
print(" 2. Run './vulnhound.py --help' to see available options")
print(" 3. Start scanning with './vulnhound.py /path/to/your/repo'")
return 0
except Exception as e:
print(f"[✗] Error during setup: {str(e)}")
return 1
if __name__ == "__main__":
sys.exit(main())