-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_projects.py
executable file
·144 lines (127 loc) · 5.09 KB
/
generate_projects.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
# Copyright 2015 gRPC authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import argparse
import glob
import multiprocessing
import os
import pickle
import shutil
import sys
import tempfile
from typing import Dict, List, Union
import _utils
import yaml
PROJECT_ROOT = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..",
"..")
os.chdir(PROJECT_ROOT)
# TODO(lidiz) find a better way for plugins to reference each other
sys.path.append(os.path.join(PROJECT_ROOT, 'tools', 'buildgen', 'plugins'))
# from tools.run_tests.python_utils import jobset
jobset = _utils.import_python_module(
os.path.join(PROJECT_ROOT, 'tools', 'run_tests', 'python_utils',
'jobset.py'))
PREPROCESSED_BUILD = '.preprocessed_build'
test = {} if os.environ.get('TEST', 'false') == 'true' else None
assert sys.argv[1:], 'run generate_projects.sh instead of this directly'
parser = argparse.ArgumentParser()
parser.add_argument('build_files',
nargs='+',
default=[],
help="build files describing build specs")
parser.add_argument('--templates',
nargs='+',
default=[],
help="mako template files to render")
parser.add_argument('--output_merged',
'-m',
default='',
type=str,
help="merge intermediate results to a file")
parser.add_argument('--jobs',
'-j',
default=multiprocessing.cpu_count(),
type=int,
help="maximum parallel jobs")
parser.add_argument('--base',
default='.',
type=str,
help="base path for generated files")
args = parser.parse_args()
def preprocess_build_files() -> _utils.Bunch:
"""Merges build yaml into a one dictionary then pass it to plugins."""
build_spec = dict()
for build_file in args.build_files:
with open(build_file, 'r') as f:
_utils.merge_json(build_spec,
yaml.load(f.read(), Loader=yaml.FullLoader))
# Executes plugins. Plugins update the build spec in-place.
for py_file in sorted(glob.glob('tools/buildgen/plugins/*.py')):
plugin = _utils.import_python_module(py_file)
plugin.mako_plugin(build_spec)
if args.output_merged:
with open(args.output_merged, 'w') as f:
f.write(yaml.dump(build_spec))
# Makes build_spec sort of immutable and dot-accessible
return _utils.to_bunch(build_spec)
def generate_template_render_jobs(templates: List[str]) -> List[jobset.JobSpec]:
"""Generate JobSpecs for each one of the template rendering work."""
jobs = []
base_cmd = [sys.executable, 'tools/buildgen/_mako_renderer.py']
for template in sorted(templates, reverse=True):
root, f = os.path.split(template)
if os.path.splitext(f)[1] == '.template':
out_dir = args.base + root[len('templates'):]
out = os.path.join(out_dir, os.path.splitext(f)[0])
if not os.path.exists(out_dir):
os.makedirs(out_dir)
cmd = base_cmd[:]
cmd.append('-P')
cmd.append(PREPROCESSED_BUILD)
cmd.append('-o')
if test is None:
cmd.append(out)
else:
tf = tempfile.mkstemp()
test[out] = tf[1]
os.close(tf[0])
cmd.append(test[out])
cmd.append(args.base + '/' + root + '/' + f)
jobs.append(jobset.JobSpec(cmd, shortname=out,
timeout_seconds=None))
return jobs
def main() -> None:
templates = args.templates
if not templates:
for root, _, files in os.walk('templates'):
for f in files:
templates.append(os.path.join(root, f))
build_spec = preprocess_build_files()
with open(PREPROCESSED_BUILD, 'wb') as f:
pickle.dump(build_spec, f)
err_cnt, _ = jobset.run(generate_template_render_jobs(templates),
maxjobs=args.jobs)
if err_cnt != 0:
print('ERROR: %s error(s) found while generating projects.' % err_cnt,
file=sys.stderr)
sys.exit(1)
if test is not None:
for s, g in test.items():
if os.path.isfile(g):
assert 0 == os.system('diff %s %s' % (s, g)), s
os.unlink(g)
else:
assert 0 == os.system('diff -r %s %s' % (s, g)), s
shutil.rmtree(g, ignore_errors=True)
if __name__ == "__main__":
main()