-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfigure.py
executable file
·236 lines (202 loc) · 9.45 KB
/
configure.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
227
228
229
230
231
232
233
234
235
236
#!/usr/bin/env python3
"""Main script for BAP building"""
from __future__ import annotations
from typing import TYPE_CHECKING
import os
import sys
import json
import argparse
import subprocess
if TYPE_CHECKING:
from typing import Sequence, List, Callable, TypeVar, NewType
CommandLineArgument = TypeVar('CommandLineArgument', bound=str)
class ConfigureApp:
"""Application for BAP building"""
_targets: List[Callable[["ConfigureApp"], None]]
def __init__(self, python_path: str, mypy_config_file: str, manifest_path: str,
srcdir: str, ballistica_path: str, git_path: str, make_path: str,
ballistica_repo_url: str, tooldir: str, tool_manifest_path: str,
pylint_rcfile: str) -> None:
self._python_path = python_path
self._mypy_config_file = mypy_config_file
self._pylint_rcfile = pylint_rcfile
self._manifest_path = manifest_path
self._srcdir = srcdir
self._ballistica_path = ballistica_path
self._git_path = git_path
self._ballistica_repo_url = ballistica_repo_url
self._tooldir = tooldir
self._tool_manifest_path = tool_manifest_path
self._make_path = make_path
self._project_files: List[str]
self._tool_files: List[str]
self._get_project_files()
self._get_tool_files()
def _get_project_files(self) -> None:
if not os.path.exists(self._manifest_path):
self._update_manifest(initial=True)
with open(self._manifest_path) as manifest:
self._project_files = json.loads(manifest.read())
def _get_tool_files(self) -> None:
if not os.path.exists(self._tool_manifest_path):
self._update_tool_manifest(initial=True)
with open(self._tool_manifest_path) as manifest:
self._tool_files = json.loads(manifest.read())
def _update_manifest(self, initial: bool = False) -> None:
with open(self._manifest_path, 'w') as manifest:
projectfiles: List[str] = []
for root, dirs, files in os.walk(self._srcdir):
del dirs # unused.
for filename in files:
if filename.endswith('.py'):
projectfiles.append(os.path.join(root, filename))
if not initial:
newfiles = set(projectfiles) - set(self._project_files)
if newfiles:
print(f'New project files detected: {", ".join(newfiles)}!')
else:
print('Project manifest are up to date!')
manifest.write(json.dumps(projectfiles, indent=2))
def _update_tool_manifest(self, initial: bool = False) -> None:
with open(self._tool_manifest_path, 'w') as manifest:
toolfiles: List[str] = []
for root, dirs, files in os.walk(self._tooldir):
del dirs # unused.
for filename in files:
if filename.endswith('.py'):
toolfiles.append(os.path.join(root, filename))
toolfiles.append(os.path.basename(sys.argv[0]))
if not initial:
newfiles = set(toolfiles) - set(self._tool_files)
if newfiles:
print(f'New tool files detected: {", ".join(newfiles)}!')
else:
print('Tool manifest are up to date!')
manifest.write(json.dumps(toolfiles, indent=2))
def _sync_ba(self, check: bool = True) -> None:
print('Syning git repository...')
if not os.path.exists(os.path.join(self._ballistica_path, '.git')):
os.makedirs(os.path.dirname(self._ballistica_path), exist_ok=True)
subprocess.run([
self._git_path, 'clone', self._ballistica_repo_url,
self._ballistica_path
], check=check)
else:
subprocess.run([
self._git_path, '-C', self._ballistica_path, 'pull'
], check=check)
def _remove_broken_symlinks(self) -> None:
for root, dirs, files in reversed(list(os.walk(os.path.join(self._ballistica_path,
'assets/src/ba_data')))):
for dest in files:
dest = os.path.join(root, dest)
if not os.path.exists(dest) and os.path.islink(dest):
print(f'removing broken symlink {dest}...')
os.remove(dest)
for directory in dirs:
directory = os.path.join(root, directory)
if not os.listdir(directory):
print(f'removing empty directory {directory}...')
os.rmdir(directory)
def _create_symlinks(self) -> None:
print('Creating symlinks...')
self._remove_broken_symlinks()
for projectfile in self._project_files:
assert projectfile.startswith(self._srcdir)
projectfile = projectfile[len(self._srcdir) + 1:]
dest = os.path.join(self._ballistica_path, 'assets/src/ba_data',
projectfile)
if not os.path.exists(dest):
os.makedirs(os.path.dirname(dest), exist_ok=True)
os.symlink(os.path.abspath(os.path.join(
self._srcdir, projectfile)), dest)
print(f'Created symlink to {dest}')
def _run_ba_update(self, check: bool = True) -> None:
print('Updating Ballistica source...')
subprocess.run([
self._make_path, 'update'
], cwd=self._ballistica_path, check=check)
def _mypy(self, filenames: List[str], check: bool, use_ba_tools: bool = True) -> None:
if use_ba_tools:
subprocess.run([
self._make_path, 'mypy'
], cwd=self._ballistica_path, check=check)
else:
try:
subprocess.run([
self._python_path, '-m', 'mypy', '--pretty', '--config-file',
self._mypy_config_file,
] + filenames, check=check)
except subprocess.CalledProcessError:
print('Mypy: fail')
else:
print('Mypy: success')
def _pylint(self, filenames: List[str], check: bool, use_ba_tools: bool = True) -> None:
if use_ba_tools:
subprocess.run([
self._make_path, 'pylint'
], cwd=self._ballistica_path, check=check)
else:
try:
subprocess.run([
self._python_path, '-m', 'pylint', '--rcfile',
self._pylint_rcfile, '--output-format=colorized'
] + filenames, check=check)
except subprocess.CalledProcessError:
print('Pylint: fail')
else:
print('Pylint: success')
def _build(self, buildtype: str, check: bool = True) -> None:
subprocess.run([
self._make_path, f'prefab-{buildtype}-build'
], cwd=self._ballistica_path, check=check)
def mypy(self, check: bool = True) -> None:
self._mypy(self._tool_files, check=check, use_ba_tools=False)
self._mypy(self._project_files, check=check, use_ba_tools=True)
def pylint(self, check: bool = True) -> None:
self._pylint(self._tool_files, check=check, use_ba_tools=False)
self._pylint(self._project_files, check=check, use_ba_tools=True)
def update(self) -> None:
self._update_manifest()
self._update_tool_manifest()
def sync(self) -> None:
self._sync_ba()
self._create_symlinks()
self._run_ba_update()
def build(self) -> None:
self._build('debug')
@staticmethod
def parse_args(args: Sequence[CommandLineArgument]) -> None:
targets = ('mypy', 'update', 'sync', 'pylint', 'build')
assert __doc__
parser = argparse.ArgumentParser(
description=__doc__.split('\n')[0])
parser.add_argument('target', choices=targets)
parser.add_argument('--python-path', default=sys.executable)
parser.add_argument('--git-path', default='git')
parser.add_argument('--make-path', default='make')
parser.add_argument('--manifest-path', default='.manifest.json')
parser.add_argument('--tool-manifest-path', default='.manifest-tools.json')
parser.add_argument('--srcdir', default='src')
parser.add_argument('--tooldir', default='tools')
parser.add_argument('--ballistica-path', default='build/ballistica')
parser.add_argument('--ballistica-repo-url',
default='https://github.com/efroemling/ballistica.git')
parser.add_argument('--mypy-config', default='config/mypy.ini', dest='mypy_config_file')
parser.add_argument('--pylintrc', default='config/pylintrc', dest='pylint_rcfile')
ns = parser.parse_args(args[1:])
app = ConfigureApp(
python_path=ns.python_path,
mypy_config_file=ns.mypy_config_file,
manifest_path=ns.manifest_path,
srcdir=os.path.join(ns.srcdir), # exclude / on path end
ballistica_path=ns.ballistica_path,
git_path=ns.git_path,
ballistica_repo_url=ns.ballistica_repo_url,
tooldir=ns.tooldir,
tool_manifest_path=ns.tool_manifest_path,
make_path=ns.make_path,
pylint_rcfile=ns.pylint_rcfile)
getattr(app, ns.target)()
if __name__ == '__main__':
ConfigureApp.parse_args(sys.argv)