1#===----------------------------------------------------------------------===## 2# 3# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4# See https://llvm.org/LICENSE.txt for license information. 5# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6# 7#===----------------------------------------------------------------------===## 8 9""" 10Runs an executable on a remote host. 11 12This is meant to be used as an executor when running the C++ Standard Library 13conformance test suite. 14""" 15 16import argparse 17import os 18import posixpath 19import subprocess 20import sys 21import tarfile 22import tempfile 23 24 25def main(): 26 parser = argparse.ArgumentParser() 27 parser.add_argument('--host', type=str, required=True) 28 parser.add_argument('--codesign_identity', type=str, required=False, default=None) 29 parser.add_argument('--dependencies', type=str, nargs='*', required=False, default=[]) 30 parser.add_argument('--env', type=str, nargs='*', required=False, default=dict()) 31 (args, remaining) = parser.parse_known_args(sys.argv[1:]) 32 33 if len(remaining) < 2: 34 sys.stderr.write('Missing actual commands to run') 35 return 1 36 37 commandLine = remaining[1:] # Skip the '--' 38 39 ssh = lambda command: ['ssh', '-oBatchMode=yes', args.host, command] 40 scp = lambda src, dst: ['scp', '-q', '-oBatchMode=yes', src, '{}:{}'.format(args.host, dst)] 41 42 # Create a temporary directory where the test will be run. 43 tmp = subprocess.check_output(ssh('mktemp -d /tmp/libcxx.XXXXXXXXXX'), universal_newlines=True).strip() 44 45 # HACK: 46 # If an argument is a file that ends in `.tmp.exe`, assume it is the name 47 # of an executable generated by a test file. We call these test-executables 48 # below. This allows us to do custom processing like codesigning test-executables 49 # and changing their path when running on the remote host. It's also possible 50 # for there to be no such executable, for example in the case of a .sh.cpp 51 # test. 52 isTestExe = lambda exe: exe.endswith('.tmp.exe') and os.path.exists(exe) 53 pathOnRemote = lambda file: posixpath.join(tmp, os.path.basename(file)) 54 55 try: 56 # Do any necessary codesigning of test-executables found in the command line. 57 if args.codesign_identity: 58 for exe in filter(isTestExe, commandLine): 59 subprocess.check_call(['xcrun', 'codesign', '-f', '-s', args.codesign_identity, exe], env={}) 60 61 # Ensure the test dependencies exist, tar them up and copy the tarball 62 # over to the remote host. 63 try: 64 tmpTar = tempfile.NamedTemporaryFile(suffix='.tar', delete=False) 65 with tarfile.open(fileobj=tmpTar, mode='w') as tarball: 66 for dep in args.dependencies: 67 if not os.path.exists(dep): 68 sys.stderr.write('Missing file or directory "{}" marked as a dependency of a test'.format(dep)) 69 return 1 70 tarball.add(dep, arcname=os.path.basename(dep)) 71 72 # Make sure we close the file before we scp it, because accessing 73 # the temporary file while still open doesn't work on Windows. 74 tmpTar.close() 75 remoteTarball = pathOnRemote(tmpTar.name) 76 subprocess.check_call(scp(tmpTar.name, remoteTarball)) 77 finally: 78 # Make sure we close the file in case an exception happens before 79 # we've closed it above -- otherwise close() is idempotent. 80 tmpTar.close() 81 os.remove(tmpTar.name) 82 83 # Untar the dependencies in the temporary directory and remove the tarball. 84 remoteCommands = [ 85 'tar -xf {} -C {}'.format(remoteTarball, tmp), 86 'rm {}'.format(remoteTarball) 87 ] 88 89 # Make sure all test-executables in the remote command line have 'execute' 90 # permissions on the remote host. The host that compiled the test-executable 91 # might not have a notion of 'executable' permissions. 92 for exe in map(pathOnRemote, filter(isTestExe, commandLine)): 93 remoteCommands.append('chmod +x {}'.format(exe)) 94 95 # Execute the command through SSH in the temporary directory, with the 96 # correct environment. We tweak the command line to run it on the remote 97 # host by transforming the path of test-executables to their path in the 98 # temporary directory, where we know they have been copied when we handled 99 # test dependencies above. 100 commandLine = (pathOnRemote(x) if isTestExe(x) else x for x in commandLine) 101 remoteCommands.append('cd {}'.format(tmp)) 102 if args.env: 103 remoteCommands.append('export {}'.format(' '.join(args.env))) 104 remoteCommands.append(subprocess.list2cmdline(commandLine)) 105 106 # Finally, SSH to the remote host and execute all the commands. 107 rc = subprocess.call(ssh(' && '.join(remoteCommands))) 108 return rc 109 110 finally: 111 # Make sure the temporary directory is removed when we're done. 112 subprocess.check_call(ssh('rm -r {}'.format(tmp))) 113 114 115if __name__ == '__main__': 116 exit(main()) 117