[PATCH v2 3/3] waf: example cross-execute script for running on target
Uri Simchoni
urisimchoni at gmail.com
Sun May 3 15:38:52 MDT 2015
This is an example script that can be the cross-execute program,
but instead of running the test in an emulator, it runs it on the
target.
Signed-off-by: Uri Simchoni <urisimchoni at gmail.com>
---
buildtools/examples/run_on_target.py | 105 +++++++++++++++++++++++++++++++++++
1 file changed, 105 insertions(+)
create mode 100755 buildtools/examples/run_on_target.py
diff --git a/buildtools/examples/run_on_target.py b/buildtools/examples/run_on_target.py
new file mode 100755
index 0000000..e3afe70
--- /dev/null
+++ b/buildtools/examples/run_on_target.py
@@ -0,0 +1,105 @@
+#!/usr/bin/env python
+
+'''
+This is an example script that serves as a cross-execute, but instead of running
+the code by an emulator, copies the test to the target platform and runs it there.
+
+This example uses rsync for copying the compiled tests and telnet for running the test.
+
+Typical usage is:
+./configure --cross-compile --cross-execute=/path-to-samba/buildtools/examples/run_on_target.py
+'''
+
+#Log of telnet session to help undestand the script:
+'''
+Trying 1.2.3.4...
+Connected to my-awsome-nas.my-company.local.
+Escape character is '^]'.
+
+my-awsome-nas login: root
+Password:
+
+
+BusyBox v1.13.4 (2015-04-27 07:00:33 IDT) built-in shell (ash)
+Enter 'help' for a list of built-in commands.
+
+my-awsome-nas:~ # cd /shares/share1/default/
+my-awsome-nas:/shares/share1/default # ./testprog
+65my-awsome-nas:/shares/share1/default # echo $?
+0
+my-awsome-nas:/shares/share1/default # exit
+Connection closed by foreign host.
+'''
+
+import sys,os,re,telnetlib,subprocess
+
+console_user='root'
+console_password='myawsomepassword'
+rsync_user='admin'
+rsync_password='adminspassword'
+host='my-awsome-nas'
+share='share1'
+
+def xfer_files(srcdir,host,share,user,pw):
+ cmd='rsync -rl --delete --password-file=- %s rsync://%s@%s/%s/' % (srcdir,user,host,share)
+ p = subprocess.Popen(cmd,shell=True,stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
+ (out,err) = p.communicate(pw)
+ if p.returncode != 0:
+ raise Exception('failed syncing files\n stdout:\n%s\nstderr:%s\n' % (out,err))
+
+def expect(tn,r):
+ '''wait until input matches some regex,
+ return match object'''
+ idx, m, out = tn.expect([r],10)
+ if not m:
+ raise Exception('expected and not received "%s"' % str(r))
+ return m
+
+def cmd(tn,prompt,txt):
+ '''type a command, return the output'''
+ tn.write(txt + '\n')
+
+ #support $ and ? in command
+ sanitized_txt = txt.replace('$','\$')
+ sanitized_txt = sanitized_txt.replace('?','\?')
+
+ r = re.compile('%s\r\n(.*)%s:[^#]*# ' % (sanitized_txt,prompt), re.S)
+ m = expect(tn,r)
+ return m.group(1)
+
+def login(host,user,password):
+ '''connect, login, return telent object and prompt'''
+ tn = telnetlib.Telnet(host)
+ expect(tn,'login: ')
+ tn.write(user + '\n')
+ expect(tn,'Password: ')
+ tn.write(password + '\n')
+ m = expect(tn,'\n([^\n:]+):.*# ')
+ return (tn,m.group(1))
+
+def main(argv):
+ progpath=argv[1]
+
+ #assume that a test that was not compiled fails (e.g. getconf)
+ if progpath[0] != '/':
+ return (1,"")
+
+ progdir=os.path.dirname(progpath)
+ prog=os.path.basename(progpath)
+ targ_progdir=os.path.basename(progdir)
+
+ xfer_files(progdir,host,share,rsync_user,rsync_password)
+ tn,prompt = login(host,console_user,console_password)
+ cmd(tn,prompt,"cd /shares/%s/%s" % (share,targ_progdir))
+ args = [ './%s' % prog ]
+ args.extend(argv[2:])
+ out=cmd(tn,prompt,' '.join(args))
+ rc=cmd(tn,prompt,'echo $?')
+ return (int(rc),out)
+
+
+if __name__ == '__main__':
+ (rc,out) = main(sys.argv)
+ sys.stdout.write(out)
+ sys.exit(rc)
+
--
1.9.1
More information about the samba-technical
mailing list