Bug 458256. Use LoadLibraryW instead of LoadLibrary (patch by DougT). r+sr=vlad
[wine-gecko.git] / tools / relic / test / testsupport.py
blobe2e5107e31a35bb4be890c1e3cbc814ad3cb2bd4
1 #!/usr/bin/env python
3 import os
4 import sys
5 import types
6 import shutil
9 #---- Support routines
11 def _escapeArg(arg):
12 """Escape the given command line argument for the shell."""
13 #XXX There is a *lot* more that we should escape here.
14 return arg.replace('"', r'\"')
17 def _joinArgv(argv):
18 r"""Join an arglist to a string appropriate for running.
19 >>> import os
20 >>> _joinArgv(['foo', 'bar "baz'])
21 'foo "bar \\"baz"'
22 """
23 cmdstr = ""
24 for arg in argv:
25 if ' ' in arg:
26 cmdstr += '"%s"' % _escapeArg(arg)
27 else:
28 cmdstr += _escapeArg(arg)
29 cmdstr += ' '
30 if cmdstr.endswith(' '): cmdstr = cmdstr[:-1] # strip trailing space
31 return cmdstr
34 def run(argv):
35 """Prepare and run the given arg vector, 'argv', and return the
36 results. Returns (<stdout lines>, <stderr lines>, <return value>).
37 Note: 'argv' may also just be the command string.
38 """
39 if type(argv) in (types.ListType, types.TupleType):
40 cmd = _joinArgv(argv)
41 else:
42 cmd = argv
43 if sys.platform.startswith('win'):
44 i, o, e = os.popen3(cmd)
45 output = o.read()
46 error = e.read()
47 i.close()
48 e.close()
49 try:
50 retval = o.close()
51 except IOError:
52 # IOError is raised iff the spawned app returns -1. Go
53 # figure.
54 retval = -1
55 if retval is None:
56 retval = 0
57 else:
58 import popen2
59 p = popen2.Popen3(cmd, 1)
60 i, o, e = p.tochild, p.fromchild, p.childerr
61 output = o.read()
62 error = e.read()
63 i.close()
64 o.close()
65 e.close()
66 retval = (p.wait() & 0xFF00) >> 8
67 if retval > 2**7: # 8-bit signed 1's-complement conversion
68 retval -= 2**8
69 return output, error, retval
72 def _rmtreeOnError(rmFunction, filePath, excInfo):
73 if excInfo[0] == OSError:
74 # presuming because file is read-only
75 os.chmod(filePath, 0777)
76 rmFunction(filePath)
78 def rmtree(dirname):
79 import shutil
80 shutil.rmtree(dirname, 0, _rmtreeOnError)