forked from PerchSecurity/dendrol
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompile_stix_grammar.py
executable file
·146 lines (112 loc) · 4.03 KB
/
compile_stix_grammar.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
#!/usr/bin/env python
import subprocess
import sys
from pathlib import Path
from typing import Iterable, Union
import click
import requests
from tqdm import tqdm
@click.command()
@click.option('-o', '--output',
type=click.Path(exists=True, file_okay=False, resolve_path=True),
default='./dendrol/lang/',
help='Directory to save the generated Python modules into')
@click.option('--force-antlr-download',
default=False,
help='Force the ANTLR .jar to be downloaded, even if it already exists')
@click.option('--progress',
default=True)
def compile_stix_grammar(output, force_antlr_download: bool, progress: bool):
output = Path(output)
if not is_java_installed():
click.echo('Java could not be found. It is required to run the ANTLR '
'compiler. Please install it, and ensure the "java" '
'executable is on your PATH.', color='red')
sys.exit(2)
jar_path = get_antlr_jar_path(output)
will_download_antlr = False
if force_antlr_download:
will_download_antlr = True
elif not is_antlr_jar_saved(output):
will_download_antlr = True
click.echo(f'ANTLR .jar not found at: {jar_path}', color='orange')
if will_download_antlr:
click.echo(f'Downloading ANTLR .jar to: {jar_path}')
save_antlr_jar(directory=output, display_progress=progress)
compile_grammar(output_dir=output)
click.echo(f'Successfully compiled grammar to: {output}', color='green')
def is_java_installed() -> bool:
"""Simple check to detect existence of Java."""
try:
retcode = subprocess.check_call(
['java', '-version'],
stderr=subprocess.DEVNULL,
)
except (subprocess.CalledProcessError, OSError):
return False
else:
return retcode == 0
def compile_grammar(output_dir: Path = None) -> bool:
"""Generate lexer, parser, listener, and visitor classes for the STIX grammar
If output_dir is not passed, the directory of this module is used.
"""
jar_path = get_antlr_jar_path(output_dir)
retcode = subprocess.check_call(
cwd=output_dir,
env={'CLASSPATH': str(jar_path)},
args=[
'java',
'org.antlr.v4.Tool',
'-Dlanguage=Python3',
'-visitor',
'-listener',
'-package', get_antlr_jar_path.__module__,
'-o', output_dir,
'STIXPattern.g4',
],
)
return retcode == 0
def save_antlr_jar(directory: Path, display_progress=False) -> Path:
"""Download and save the ANTLR .jar
"""
path = get_antlr_jar_path(directory)
content = stream_antlr_jar()
if display_progress:
url = get_antlr_jar_download_url()
head_res = requests.head(url)
head_res.raise_for_status()
file_size = int(head_res.headers['Content-Length'])
pbar = tqdm(
iterable=content,
total=file_size,
unit='B',
unit_scale=True,
desc=path.name,
)
content = _iter_with_progress(pbar)
with path.open('wb') as fp:
for chunk in content:
fp.write(chunk)
return path
def _iter_with_progress(pbar):
for chunk in pbar:
if chunk:
pbar.update(len(chunk))
yield chunk
pbar.close()
def stream_antlr_jar(chunk_size=1024) -> Iterable[bytes]:
"""Retrieve the ANTLR .jar in chunks from the internet
"""
url = get_antlr_jar_download_url()
response = requests.get(url, stream=True)
response.raise_for_status()
for chunk in response.iter_content(chunk_size=chunk_size):
yield chunk
def get_antlr_jar_path(directory: Path) -> Path:
return directory / 'antlr-4.7.1-complete.jar'
def is_antlr_jar_saved(directory: Path) -> bool:
return get_antlr_jar_path(directory).exists()
def get_antlr_jar_download_url() -> str:
return 'https://www.antlr.org/download/antlr-4.7.1-complete.jar'
if __name__ == '__main__':
compile_stix_grammar()