github.com/jenkins-x/test-infra@v0.0.7/scenarios/kubernetes_execute_bazel.py (about) 1 #!/usr/bin/env python 2 3 # Copyright 2018 The Kubernetes 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 17 """Executes a command, afterwards executes coalesce.py, preserving the return code. 18 19 Also supports configuring bazel remote caching.""" 20 21 import argparse 22 import os 23 import subprocess 24 import sys 25 26 ORIG_CWD = os.getcwd() 27 28 def test_infra(*paths): 29 """Return path relative to root of test-infra repo.""" 30 return os.path.join(ORIG_CWD, os.path.dirname(__file__), '..', *paths) 31 32 def check(*cmd): 33 """Log and run the command, raising on errors.""" 34 print >>sys.stderr, 'Run:', cmd 35 subprocess.check_call(cmd) 36 37 def call(*cmd): 38 """Log and run the command, raising on errors.""" 39 print >>sys.stderr, 'Run:', cmd 40 return subprocess.call(cmd) 41 42 43 def main(cmd): 44 """Run script and preserve return code after running coalesce.py.""" 45 if not cmd: 46 raise ValueError(cmd) 47 # update bazel caching configuration if enabled 48 if os.environ.get('BAZEL_REMOTE_CACHE_ENABLED', 'false') == 'true': 49 print 'Bazel remote cache is enabled, generating .bazelrcs ...' 50 # TODO(bentheelder): consider moving this once we've migrated all users 51 # of the remote cache to this script 52 check(test_infra('images/bootstrap/create_bazel_cache_rcs.sh')) 53 # call the user supplied command 54 return_code = call(*cmd) 55 # Coalesce test results into one file for upload. 56 check(test_infra('hack/coalesce.py')) 57 # preserve the exit code 58 sys.exit(return_code) 59 60 61 if __name__ == '__main__': 62 PARSER = argparse.ArgumentParser() 63 PARSER.add_argument('cmd', nargs=1) 64 PARSER.add_argument('args', nargs='*') 65 ARGS = PARSER.parse_args() 66 main(ARGS.cmd + ARGS.args)