2020-06-12 22:28:19 +08:00
|
|
|
#!/usr/bin/env python
|
2020-04-01 00:09:20 +08:00
|
|
|
#===----------------------------------------------------------------------===##
|
|
|
|
#
|
|
|
|
# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
# See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
#
|
|
|
|
#===----------------------------------------------------------------------===##
|
|
|
|
|
|
|
|
"""
|
|
|
|
Runs an executable on a remote host.
|
|
|
|
|
|
|
|
This is meant to be used as an executor when running the C++ Standard Library
|
|
|
|
conformance test suite.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
import os
|
2020-04-01 22:02:55 +08:00
|
|
|
import posixpath
|
2020-10-06 18:38:52 +08:00
|
|
|
import shlex
|
2020-04-01 00:09:20 +08:00
|
|
|
import subprocess
|
|
|
|
import sys
|
2020-04-02 02:52:12 +08:00
|
|
|
import tarfile
|
|
|
|
import tempfile
|
2020-04-01 00:09:20 +08:00
|
|
|
|
2021-11-13 02:53:50 +08:00
|
|
|
from shlex import quote as cmd_quote
|
2021-03-04 16:37:02 +08:00
|
|
|
|
2020-10-06 18:38:52 +08:00
|
|
|
def ssh(args, command):
|
|
|
|
cmd = ['ssh', '-oBatchMode=yes']
|
|
|
|
if args.extra_ssh_args is not None:
|
|
|
|
cmd.extend(shlex.split(args.extra_ssh_args))
|
|
|
|
return cmd + [args.host, command]
|
|
|
|
|
|
|
|
|
|
|
|
def scp(args, src, dst):
|
|
|
|
cmd = ['scp', '-q', '-oBatchMode=yes']
|
|
|
|
if args.extra_scp_args is not None:
|
|
|
|
cmd.extend(shlex.split(args.extra_scp_args))
|
|
|
|
return cmd + [src, '{}:{}'.format(args.host, dst)]
|
|
|
|
|
2020-04-01 00:09:20 +08:00
|
|
|
|
|
|
|
def main():
|
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
parser.add_argument('--host', type=str, required=True)
|
2020-06-11 02:41:47 +08:00
|
|
|
parser.add_argument('--execdir', type=str, required=True)
|
2020-10-21 02:23:41 +08:00
|
|
|
parser.add_argument('--tempdir', type=str, required=False, default='/tmp')
|
2020-10-06 18:38:52 +08:00
|
|
|
parser.add_argument('--extra-ssh-args', type=str, required=False)
|
|
|
|
parser.add_argument('--extra-scp-args', type=str, required=False)
|
2020-04-04 05:50:39 +08:00
|
|
|
parser.add_argument('--codesign_identity', type=str, required=False, default=None)
|
|
|
|
parser.add_argument('--env', type=str, nargs='*', required=False, default=dict())
|
2020-07-21 15:35:47 +08:00
|
|
|
parser.add_argument("command", nargs=argparse.ONE_OR_MORE)
|
|
|
|
args = parser.parse_args()
|
|
|
|
commandLine = args.command
|
2020-04-01 00:09:20 +08:00
|
|
|
|
2020-04-01 22:21:31 +08:00
|
|
|
# Create a temporary directory where the test will be run.
|
2020-06-11 02:41:47 +08:00
|
|
|
# That is effectively the value of %T on the remote host.
|
2020-10-21 02:23:41 +08:00
|
|
|
tmp = subprocess.check_output(ssh(args, 'mktemp -d {}/libcxx.XXXXXXXXXX'.format(args.tempdir)), universal_newlines=True).strip()
|
2020-04-01 23:07:48 +08:00
|
|
|
|
|
|
|
# HACK:
|
|
|
|
# If an argument is a file that ends in `.tmp.exe`, assume it is the name
|
|
|
|
# of an executable generated by a test file. We call these test-executables
|
|
|
|
# below. This allows us to do custom processing like codesigning test-executables
|
|
|
|
# and changing their path when running on the remote host. It's also possible
|
|
|
|
# for there to be no such executable, for example in the case of a .sh.cpp
|
|
|
|
# test.
|
|
|
|
isTestExe = lambda exe: exe.endswith('.tmp.exe') and os.path.exists(exe)
|
2020-04-02 02:52:12 +08:00
|
|
|
pathOnRemote = lambda file: posixpath.join(tmp, os.path.basename(file))
|
2020-04-01 23:07:48 +08:00
|
|
|
|
2020-04-01 22:21:31 +08:00
|
|
|
try:
|
2020-04-01 23:07:48 +08:00
|
|
|
# Do any necessary codesigning of test-executables found in the command line.
|
|
|
|
if args.codesign_identity:
|
|
|
|
for exe in filter(isTestExe, commandLine):
|
2020-04-02 02:52:12 +08:00
|
|
|
subprocess.check_call(['xcrun', 'codesign', '-f', '-s', args.codesign_identity, exe], env={})
|
|
|
|
|
2020-06-11 02:41:47 +08:00
|
|
|
# tar up the execution directory (which contains everything that's needed
|
|
|
|
# to run the test), and copy the tarball over to the remote host.
|
2020-04-06 21:33:08 +08:00
|
|
|
try:
|
|
|
|
tmpTar = tempfile.NamedTemporaryFile(suffix='.tar', delete=False)
|
2020-04-02 02:52:12 +08:00
|
|
|
with tarfile.open(fileobj=tmpTar, mode='w') as tarball:
|
2020-06-11 02:41:47 +08:00
|
|
|
tarball.add(args.execdir, arcname=os.path.basename(args.execdir))
|
2020-04-02 02:52:12 +08:00
|
|
|
|
2020-04-06 21:33:08 +08:00
|
|
|
# Make sure we close the file before we scp it, because accessing
|
|
|
|
# the temporary file while still open doesn't work on Windows.
|
|
|
|
tmpTar.close()
|
2020-04-02 02:52:12 +08:00
|
|
|
remoteTarball = pathOnRemote(tmpTar.name)
|
2020-10-06 18:38:52 +08:00
|
|
|
subprocess.check_call(scp(args, tmpTar.name, remoteTarball))
|
2020-04-06 21:33:08 +08:00
|
|
|
finally:
|
|
|
|
# Make sure we close the file in case an exception happens before
|
|
|
|
# we've closed it above -- otherwise close() is idempotent.
|
|
|
|
tmpTar.close()
|
|
|
|
os.remove(tmpTar.name)
|
2020-04-02 02:52:12 +08:00
|
|
|
|
|
|
|
# Untar the dependencies in the temporary directory and remove the tarball.
|
|
|
|
remoteCommands = [
|
2020-06-11 02:41:47 +08:00
|
|
|
'tar -xf {} -C {} --strip-components 1'.format(remoteTarball, tmp),
|
2020-04-02 02:52:12 +08:00
|
|
|
'rm {}'.format(remoteTarball)
|
|
|
|
]
|
2020-04-01 22:21:31 +08:00
|
|
|
|
2020-04-01 23:07:48 +08:00
|
|
|
# Make sure all test-executables in the remote command line have 'execute'
|
|
|
|
# permissions on the remote host. The host that compiled the test-executable
|
|
|
|
# might not have a notion of 'executable' permissions.
|
2020-04-02 02:52:12 +08:00
|
|
|
for exe in map(pathOnRemote, filter(isTestExe, commandLine)):
|
|
|
|
remoteCommands.append('chmod +x {}'.format(exe))
|
2020-04-01 22:21:31 +08:00
|
|
|
|
|
|
|
# Execute the command through SSH in the temporary directory, with the
|
2020-04-01 23:07:48 +08:00
|
|
|
# correct environment. We tweak the command line to run it on the remote
|
|
|
|
# host by transforming the path of test-executables to their path in the
|
2020-06-11 02:41:47 +08:00
|
|
|
# temporary directory on the remote host.
|
2020-04-18 04:43:35 +08:00
|
|
|
commandLine = (pathOnRemote(x) if isTestExe(x) else x for x in commandLine)
|
2020-05-06 22:35:02 +08:00
|
|
|
remoteCommands.append('cd {}'.format(tmp))
|
|
|
|
if args.env:
|
2021-03-04 16:37:02 +08:00
|
|
|
remoteCommands.append('export {}'.format(cmd_quote(' '.join(args.env))))
|
2020-05-06 22:35:02 +08:00
|
|
|
remoteCommands.append(subprocess.list2cmdline(commandLine))
|
2020-04-02 02:52:12 +08:00
|
|
|
|
|
|
|
# Finally, SSH to the remote host and execute all the commands.
|
2020-10-06 18:38:52 +08:00
|
|
|
rc = subprocess.call(ssh(args, ' && '.join(remoteCommands)))
|
2020-04-01 22:21:31 +08:00
|
|
|
return rc
|
|
|
|
|
|
|
|
finally:
|
|
|
|
# Make sure the temporary directory is removed when we're done.
|
2020-10-06 18:38:52 +08:00
|
|
|
subprocess.check_call(ssh(args, 'rm -r {}'.format(tmp)))
|
2020-04-01 00:09:20 +08:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
exit(main())
|