1#!/usr/bin/env python2.7 2 3# Copyright 2015 gRPC authors. 4# 5# 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 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# 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. 16 17import argparse 18import glob 19import os 20import shutil 21import sys 22import tempfile 23import multiprocessing 24sys.path.append( 25 os.path.join( 26 os.path.dirname(sys.argv[0]), '..', 'run_tests', 'python_utils')) 27 28assert sys.argv[1:], 'run generate_projects.sh instead of this directly' 29 30import jobset 31 32os.chdir(os.path.join(os.path.dirname(sys.argv[0]), '..', '..')) 33 34argp = argparse.ArgumentParser() 35argp.add_argument('build_files', nargs='+', default=[]) 36argp.add_argument('--templates', nargs='+', default=[]) 37argp.add_argument('--output_merged', default=None, type=str) 38argp.add_argument('--jobs', '-j', default=multiprocessing.cpu_count(), type=int) 39argp.add_argument('--base', default='.', type=str) 40args = argp.parse_args() 41 42json = args.build_files 43 44test = {} if 'TEST' in os.environ else None 45 46plugins = sorted(glob.glob('tools/buildgen/plugins/*.py')) 47 48templates = args.templates 49if not templates: 50 for root, dirs, files in os.walk('templates'): 51 for f in files: 52 templates.append(os.path.join(root, f)) 53 54pre_jobs = [] 55base_cmd = [sys.executable, 'tools/buildgen/mako_renderer.py'] 56cmd = base_cmd[:] 57for plugin in plugins: 58 cmd.append('-p') 59 cmd.append(plugin) 60for js in json: 61 cmd.append('-d') 62 cmd.append(js) 63cmd.append('-w') 64preprocessed_build = '.preprocessed_build' 65cmd.append(preprocessed_build) 66if args.output_merged is not None: 67 cmd.append('-M') 68 cmd.append(args.output_merged) 69pre_jobs.append( 70 jobset.JobSpec(cmd, shortname='preprocess', timeout_seconds=None)) 71 72jobs = [] 73for template in reversed(sorted(templates)): 74 root, f = os.path.split(template) 75 if os.path.splitext(f)[1] == '.template': 76 out_dir = args.base + root[len('templates'):] 77 out = out_dir + '/' + os.path.splitext(f)[0] 78 if not os.path.exists(out_dir): 79 os.makedirs(out_dir) 80 cmd = base_cmd[:] 81 cmd.append('-P') 82 cmd.append(preprocessed_build) 83 cmd.append('-o') 84 if test is None: 85 cmd.append(out) 86 else: 87 tf = tempfile.mkstemp() 88 test[out] = tf[1] 89 os.close(tf[0]) 90 cmd.append(test[out]) 91 cmd.append(args.base + '/' + root + '/' + f) 92 jobs.append(jobset.JobSpec(cmd, shortname=out, timeout_seconds=None)) 93 94jobset.run(pre_jobs, maxjobs=args.jobs) 95jobset.run(jobs, maxjobs=args.jobs) 96 97if test is not None: 98 for s, g in test.iteritems(): 99 if os.path.isfile(g): 100 assert 0 == os.system('diff %s %s' % (s, g)), s 101 os.unlink(g) 102 else: 103 assert 0 == os.system('diff -r %s %s' % (s, g)), s 104 shutil.rmtree(g, ignore_errors=True) 105