blob: f885b266972d0de49228835931d0b3fe81ec9b01 [file] [log] [blame]
Craig Tiller1a718112015-07-03 12:07:06 -07001#!/usr/bin/env python2.7
2
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003# Copyright 2015 gRPC authors.
Craig Tiller1a718112015-07-03 12:07:06 -07004#
Jan Tattermusch7897ae92017-06-07 22:57:36 +02005# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
Craig Tiller1a718112015-07-03 12:07:06 -07008#
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009# http://www.apache.org/licenses/LICENSE-2.0
Craig Tiller1a718112015-07-03 12:07:06 -070010#
Jan Tattermusch7897ae92017-06-07 22:57:36 +020011# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
Craig Tiller1a718112015-07-03 12:07:06 -070016
Craig Tiller0fe5ee72015-12-22 12:50:36 -080017import argparse
Craig Tiller1a718112015-07-03 12:07:06 -070018import glob
19import os
Craig Tiller259e6272015-08-31 16:58:18 -070020import shutil
Craig Tiller1a718112015-07-03 12:07:06 -070021import sys
22import tempfile
David Garcia Quintas985f22e2015-10-04 23:14:37 -070023import multiprocessing
Jan Tattermusch5c79a312016-12-20 11:02:50 +010024sys.path.append(os.path.join(os.path.dirname(sys.argv[0]), '..', 'run_tests', 'python_utils'))
Craig Tiller1a718112015-07-03 12:07:06 -070025
26assert sys.argv[1:], 'run generate_projects.sh instead of this directly'
27
28import jobset
29
30os.chdir(os.path.join(os.path.dirname(sys.argv[0]), '..', '..'))
Craig Tiller0fe5ee72015-12-22 12:50:36 -080031
32argp = argparse.ArgumentParser()
Craig Tiller560c9012016-02-24 16:34:38 -080033argp.add_argument('build_files', nargs='+', default=[])
Craig Tiller0fe5ee72015-12-22 12:50:36 -080034argp.add_argument('--templates', nargs='+', default=[])
Craig Tiller3ab2fe02016-04-11 20:11:18 -070035argp.add_argument('--output_merged', default=None, type=str)
Craig Tiller12971702016-01-21 13:38:35 -080036argp.add_argument('--jobs', '-j', default=multiprocessing.cpu_count(), type=int)
Craig Tiller0fe5ee72015-12-22 12:50:36 -080037args = argp.parse_args()
38
Craig Tiller560c9012016-02-24 16:34:38 -080039json = args.build_files
Craig Tiller1a718112015-07-03 12:07:06 -070040
41test = {} if 'TEST' in os.environ else None
42
43plugins = sorted(glob.glob('tools/buildgen/plugins/*.py'))
44
Craig Tiller0fe5ee72015-12-22 12:50:36 -080045templates = args.templates
46if not templates:
47 for root, dirs, files in os.walk('templates'):
48 for f in files:
49 templates.append(os.path.join(root, f))
50
Craig Tiller560c9012016-02-24 16:34:38 -080051pre_jobs = []
52base_cmd = ['python2.7', 'tools/buildgen/mako_renderer.py']
53cmd = base_cmd[:]
54for plugin in plugins:
55 cmd.append('-p')
56 cmd.append(plugin)
57for js in json:
58 cmd.append('-d')
59 cmd.append(js)
60cmd.append('-w')
61preprocessed_build = '.preprocessed_build'
62cmd.append(preprocessed_build)
Craig Tiller3ab2fe02016-04-11 20:11:18 -070063if args.output_merged is not None:
64 cmd.append('-M')
65 cmd.append(args.output_merged)
Craig Tiller560c9012016-02-24 16:34:38 -080066pre_jobs.append(jobset.JobSpec(cmd, shortname='preprocess', timeout_seconds=None))
67
Craig Tiller1a718112015-07-03 12:07:06 -070068jobs = []
Craig Tiller560c9012016-02-24 16:34:38 -080069for template in reversed(sorted(templates)):
Craig Tiller0fe5ee72015-12-22 12:50:36 -080070 root, f = os.path.split(template)
71 if os.path.splitext(f)[1] == '.template':
72 out_dir = '.' + root[len('templates'):]
73 out = out_dir + '/' + os.path.splitext(f)[0]
74 if not os.path.exists(out_dir):
75 os.makedirs(out_dir)
Craig Tiller560c9012016-02-24 16:34:38 -080076 cmd = base_cmd[:]
77 cmd.append('-P')
78 cmd.append(preprocessed_build)
Craig Tiller0fe5ee72015-12-22 12:50:36 -080079 cmd.append('-o')
80 if test is None:
81 cmd.append(out)
82 else:
83 tf = tempfile.mkstemp()
84 test[out] = tf[1]
85 os.close(tf[0])
86 cmd.append(test[out])
87 cmd.append(root + '/' + f)
Craig Tiller590105a2016-01-19 13:03:46 -080088 jobs.append(jobset.JobSpec(cmd, shortname=out, timeout_seconds=None))
Craig Tiller1a718112015-07-03 12:07:06 -070089
Craig Tiller560c9012016-02-24 16:34:38 -080090jobset.run(pre_jobs, maxjobs=args.jobs)
Craig Tiller12971702016-01-21 13:38:35 -080091jobset.run(jobs, maxjobs=args.jobs)
Craig Tiller1a718112015-07-03 12:07:06 -070092
93if test is not None:
Nicolas "Pixel" Noble22232ae2015-07-11 22:40:22 +020094 for s, g in test.iteritems():
Craig Tiller259e6272015-08-31 16:58:18 -070095 if os.path.isfile(g):
Nicolas "Pixel" Noble9baaead2015-10-03 02:25:02 +020096 assert 0 == os.system('diff %s %s' % (s, g)), s
Craig Tiller259e6272015-08-31 16:58:18 -070097 os.unlink(g)
98 else:
Nicolas "Pixel" Noble9baaead2015-10-03 02:25:02 +020099 assert 0 == os.system('diff -r %s %s' % (s, g)), s
Craig Tiller259e6272015-08-31 16:58:18 -0700100 shutil.rmtree(g, ignore_errors=True)