simulateuser.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 import sys
00004 import os
00005 import tempfile
00006 import subprocess
00007 
00008 # When executed from the command line
00009 if __name__ == "__main__":
00010     # Input arguments
00011     asebatest_bin = None
00012     input_script = None
00013     if len(sys.argv) == 3:
00014         asebatest_bin = sys.argv[1]
00015         input_script = sys.argv[2]
00016     else:
00017         print >> sys.stderr, "Wrong number of arguments.\n"
00018         print >> sys.stderr, "Usage:"
00019         print >> sys.stderr, "  {} asebatest_bin input_script".format(sys.argv[0])
00020         exit(1)
00021 
00022 try:
00023     f = open(input_script, 'rb')
00024 except IOError, e:
00025     print "Can not find the script file {}. Error.".format(input_script)
00026     exit(2)
00027 
00028 # read the script
00029 script = f.read()
00030 f.close()
00031 
00032 # open a tempory file
00033 tmp = tempfile.NamedTemporaryFile(mode='wb', prefix='tmp', delete=False)
00034 tmp.close()
00035 
00036 # write the script, character by character
00037 tmp_script = ''
00038 failed = False
00039 for c in script:
00040     # generate the incremental script
00041     tmp_script += c
00042     # write to temp file
00043     tmp = open(tmp.name, 'wb')
00044     tmp.write(script)
00045     tmp.close()
00046     # run the compiler on it
00047     retcode = subprocess.call([asebatest_bin, tmp.name], stdout=open(os.devnull), stderr=open(os.devnull))
00048     if retcode < 0:
00049         # oops, received a signal (sigsev?)
00050         print >> sys.stderr, "Compiler crached for the following code:"
00051         print >> sys.stderr, tmp_script
00052         failed = True
00053         break
00054 
00055 # remove the temp file
00056 os.unlink(tmp.name)
00057 
00058 if failed == True:
00059     exit(retcode)
00060 else:
00061     exit(0)
00062 


aseba
Author(s): Stéphane Magnenat
autogenerated on Sun Oct 5 2014 23:46:39