-
Notifications
You must be signed in to change notification settings - Fork 116
/
build_helpers.py
226 lines (190 loc) · 6.82 KB
/
build_helpers.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import codecs
import distutils.log
import errno
import os
import re
import shutil
import subprocess
import sys
from functools import partial
from pathlib import Path
from typing import List, Optional
from setuptools import Command
from setuptools.command import build_py, develop, sdist
class ANTLRCommand(Command): # type: ignore # pragma: no cover
"""Generate parsers using ANTLR."""
description = "Run ANTLR"
user_options: List[str] = []
def run(self) -> None:
"""Run command."""
build_dir = Path(__file__).parent.absolute()
project_root = build_dir.parent
for grammar in [
"OmegaConfGrammarLexer.g4",
"OmegaConfGrammarParser.g4",
]:
command = [
"java",
"-jar",
str(build_dir / "bin" / "antlr-4.11.1-complete.jar"),
"-Dlanguage=Python3",
"-o",
str(project_root / "omegaconf" / "grammar" / "gen"),
"-Xexact-output-dir",
"-visitor",
str(project_root / "omegaconf" / "grammar" / grammar),
]
self.announce(
f"Generating parser for Python3: {command}",
level=distutils.log.INFO,
)
subprocess.check_call(command)
self.announce(
"Fixing imports for generated parsers",
level=distutils.log.INFO,
)
self._fix_imports()
def initialize_options(self) -> None:
pass
def finalize_options(self) -> None:
pass
def _fix_imports(self) -> None:
"""Fix imports from the generated parsers to use the vendored antlr4 instead"""
build_dir = Path(__file__).parent.absolute()
project_root = build_dir.parent
lib = "antlr4"
pkgname = 'omegaconf.vendor'
replacements = [
partial( # import antlr4 -> import omegaconf.vendor.antlr4
re.compile(r'(^\s*)import {}\n'.format(lib), flags=re.M).sub,
r'\1from {} import {}\n'.format(pkgname, lib)
),
partial( # from antlr4 -> from fomegaconf.vendor.antlr4
re.compile(r'(^\s*)from {}(\.|\s+)'.format(lib), flags=re.M).sub,
r'\1from {}.{}\2'.format(pkgname, lib)
),
]
path = project_root / "omegaconf" / "grammar" / "gen"
for item in path.iterdir():
if item.is_file() and item.name.endswith(".py"):
text = item.read_text('utf8')
for replacement in replacements:
text = replacement(text)
item.write_text(text, 'utf8')
class BuildPyCommand(build_py.build_py): # pragma: no cover
def run(self) -> None:
if not self.dry_run:
self.run_command("clean")
run_antlr(self)
build_py.build_py.run(self)
class CleanCommand(Command): # type: ignore # pragma: no cover
"""
Our custom command to clean out junk files.
"""
description = "Cleans out generated and junk files we don't want in the repo"
dry_run: bool
user_options: List[str] = []
def run(self) -> None:
root = Path(__file__).parent.parent.absolute()
files = find(
root=root,
include_files=["^omegaconf/grammar/gen/.*"],
include_dirs=[
"^omegaconf\\.egg-info$",
"\\.eggs$",
"^\\.mypy_cache$",
"^\\.pytest_cache$",
".*/__pycache__$",
"^__pycache__$",
"^build$",
],
scan_exclude=["^.git$", "^.nox/.*$"],
excludes=[".*\\.gitignore$", ".*/__init__.py"],
)
if self.dry_run:
print("Dry run! Would clean up the following files and dirs:")
print("\n".join(sorted(map(str, files))))
else:
for f in files:
if f.exists():
if f.is_dir():
shutil.rmtree(f, ignore_errors=True)
else:
f.unlink()
def initialize_options(self) -> None:
pass
def finalize_options(self) -> None:
pass
class DevelopCommand(develop.develop): # pragma: no cover
def run(self) -> None: # type: ignore
if not self.dry_run:
run_antlr(self)
develop.develop.run(self)
class SDistCommand(sdist.sdist): # pragma: no cover
def run(self) -> None:
if not self.dry_run: # type: ignore
self.run_command("clean")
run_antlr(self)
sdist.sdist.run(self)
def find(
root: Path,
include_files: List[str],
include_dirs: List[str],
excludes: List[str],
rbase: Optional[Path] = None,
scan_exclude: Optional[List[str]] = None,
) -> List[Path]:
if rbase is None:
rbase = Path()
if scan_exclude is None:
scan_exclude = []
files = []
scan_root = root / rbase
for entry in scan_root.iterdir():
path = rbase / entry.name
if matches(scan_exclude, path):
continue
if entry.is_dir():
if matches(include_dirs, path):
if not matches(excludes, path):
files.append(path)
else:
ret = find(
root=root,
include_files=include_files,
include_dirs=include_dirs,
excludes=excludes,
rbase=path,
scan_exclude=scan_exclude,
)
files.extend(ret)
else:
if matches(include_files, path) and not matches(excludes, path):
files.append(path)
return files
def find_version(*file_paths: str) -> str:
root = Path(__file__).parent.parent.absolute()
with codecs.open(root / Path(*file_paths), "r") as fp: # type: ignore
version_file = fp.read()
version_match = re.search(r"^__version__ = ['\"]([^'\"]*)['\"]", version_file, re.M)
if version_match:
return version_match.group(1)
raise RuntimeError("Unable to find version string.") # pragma: no cover
def matches(patterns: List[str], path: Path) -> bool:
string = str(path).replace(os.sep, "/") # for Windows
for pattern in patterns:
if re.match(pattern, string):
return True
return False
def run_antlr(cmd: Command) -> None: # pragma: no cover
try:
cmd.announce("Generating parsers with antlr4", level=distutils.log.INFO)
cmd.run_command("antlr")
except OSError as e:
if e.errno == errno.ENOENT:
msg = f"| Unable to generate parsers: {e} |"
msg = "=" * len(msg) + "\n" + msg + "\n" + "=" * len(msg)
cmd.announce(f"{msg}", level=distutils.log.FATAL)
sys.exit(1)
else:
raise