4 # Sample run-on-target script
5 # This is a script that can be used as cross-execute parameter to samba
6 # configuration process, running the command on a remote target for which
7 # the cross-compiled configure test was compiled.
12 # '--cross-execute=./buildtools/example/run_on_target.py --host=<host>'
14 # A more elaborate example:
17 # '--cross-execute=./buildtools/example/run_on_target.py --host=<host> --user=<user> "--ssh=ssh -i <some key file>" --destdir=/path/to/dir'
19 # Typically this is to be used also with --cross-answers, so that the
20 # cross answers file gets built and further builds can be made without
21 # the help of a remote target.
23 # The following assumptions are made:
24 # 1. rsync is available on build machine and target machine
25 # 2. A running ssh service on target machine with password-less shell login
26 # 3. A directory writable by the password-less login user
27 # 4. The tests on the target can run and provide reliable results
28 # from the login account's home directory. This is significant
29 # for example in locking tests which
30 # create files in the current directory. As a workaround to this
31 # assumption, the TESTDIR environment variable can be set on the target
32 # (using ssh command line or server config) and the tests shall
33 # chdir to that directory.
39 from optparse
import OptionParser
41 # those are defaults, but can be overidden using command line
47 def xfer_files(ssh
, srcdir
, host
, user
, targ_destdir
):
48 """Transfer executable files to target
50 Use rsync to copy the directory containing program to run
51 INTO a destination directory on the target. An exact copy
52 of the source directory is created on the target machine,
53 possibly deleting files on the target machine which do not
54 exist on the source directory.
56 The idea is that the test may include files in addition to
57 the compiled binary, and all of those files reside alongside
58 the binary in a source directory.
60 For example, if the test to run is /foo/bar/test and the
61 destination directory on the target is /tbaz, then /tbaz/bar
62 on the target shall be an exact copy of /foo/bar on the source,
63 including deletion of files inside /tbaz/bar which do not exist
69 userhost
= '%s@%s' % (user
, host
)
71 cmd
= 'rsync --verbose -rl --ignore-times --delete -e "%s" %s %s:%s/' % \
72 (ssh
, srcdir
, userhost
, targ_destdir
)
73 p
= subprocess
.Popen(cmd
, shell
=True, stdout
=subprocess
.PIPE
,
74 stderr
=subprocess
.PIPE
)
75 (out
, err
) = p
.communicate()
77 raise Exception('failed syncing files\n stdout:\n%s\nstderr:%s\n'
81 def exec_remote(ssh
, host
, user
, destdir
, targdir
, prog
, args
):
82 """Run a test on the target
84 Using password-less ssh, run the compiled binary on the target.
86 An assumption is that there's no need to cd into the target dir,
87 same as there's no need to do it on a native build.
91 userhost
= '%s@%s' % (user
, host
)
93 cmd
= '%s %s %s/%s/%s' % (ssh
, userhost
, destdir
, targdir
, prog
)
95 cmd
= cmd
+ ' ' + ' '.join(args
)
96 p
= subprocess
.Popen(cmd
, shell
=True, stdout
=subprocess
.PIPE
,
97 stderr
=subprocess
.PIPE
)
98 (out
, err
) = p
.communicate()
99 return (p
.returncode
, out
)
103 usage
= "usage: %prog [options] <prog> [args]"
104 parser
= OptionParser(usage
)
106 parser
.add_option('--ssh', help="SSH client and additional flags",
108 parser
.add_option('--host', help="target host name or IP address",
110 parser
.add_option('--user', help="login user on target",
112 parser
.add_option('--destdir', help="work directory on target",
115 (options
, args
) = parser
.parse_args(argv
)
117 parser
.error("please supply test program to run")
121 # assume that a test that was not compiled fails (e.g. getconf)
122 if progpath
[0] != '/':
125 progdir
= os
.path
.dirname(progpath
)
126 prog
= os
.path
.basename(progpath
)
127 targ_progdir
= os
.path
.basename(progdir
)
136 (rc
, out
) = exec_remote(options
.ssh
,
145 if __name__
== '__main__':
146 (rc
, out
) = main(sys
.argv
[1:])
147 sys
.stdout
.write(out
)