Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | #!/usr/bin/env python |
michael@0 | 2 | # Copyright (c) 2002-2003 ActiveState Corp. |
michael@0 | 3 | # Author: Trent Mick (TrentM@ActiveState.com) |
michael@0 | 4 | |
michael@0 | 5 | import os |
michael@0 | 6 | import sys |
michael@0 | 7 | import types |
michael@0 | 8 | |
michael@0 | 9 | |
michael@0 | 10 | #---- Support routines |
michael@0 | 11 | |
michael@0 | 12 | def _escapeArg(arg): |
michael@0 | 13 | """Escape the given command line argument for the shell.""" |
michael@0 | 14 | #XXX There is a *lot* more that we should escape here. |
michael@0 | 15 | return arg.replace('"', r'\"') |
michael@0 | 16 | |
michael@0 | 17 | |
michael@0 | 18 | def _joinArgv(argv): |
michael@0 | 19 | r"""Join an arglist to a string appropriate for running. |
michael@0 | 20 | >>> import os |
michael@0 | 21 | >>> _joinArgv(['foo', 'bar "baz']) |
michael@0 | 22 | 'foo "bar \\"baz"' |
michael@0 | 23 | """ |
michael@0 | 24 | cmdstr = "" |
michael@0 | 25 | for arg in argv: |
michael@0 | 26 | if ' ' in arg: |
michael@0 | 27 | cmdstr += '"%s"' % _escapeArg(arg) |
michael@0 | 28 | else: |
michael@0 | 29 | cmdstr += _escapeArg(arg) |
michael@0 | 30 | cmdstr += ' ' |
michael@0 | 31 | if cmdstr.endswith(' '): cmdstr = cmdstr[:-1] # strip trailing space |
michael@0 | 32 | return cmdstr |
michael@0 | 33 | |
michael@0 | 34 | |
michael@0 | 35 | def run(argv): |
michael@0 | 36 | """Prepare and run the given arg vector, 'argv', and return the |
michael@0 | 37 | results. Returns (<stdout lines>, <stderr lines>, <return value>). |
michael@0 | 38 | Note: 'argv' may also just be the command string. |
michael@0 | 39 | """ |
michael@0 | 40 | if type(argv) in (types.ListType, types.TupleType): |
michael@0 | 41 | cmd = _joinArgv(argv) |
michael@0 | 42 | else: |
michael@0 | 43 | cmd = argv |
michael@0 | 44 | if sys.platform.startswith('win'): |
michael@0 | 45 | i, o, e = os.popen3(cmd) |
michael@0 | 46 | output = o.read() |
michael@0 | 47 | error = e.read() |
michael@0 | 48 | i.close() |
michael@0 | 49 | e.close() |
michael@0 | 50 | try: |
michael@0 | 51 | retval = o.close() |
michael@0 | 52 | except IOError: |
michael@0 | 53 | # IOError is raised iff the spawned app returns -1. Go |
michael@0 | 54 | # figure. |
michael@0 | 55 | retval = -1 |
michael@0 | 56 | if retval is None: |
michael@0 | 57 | retval = 0 |
michael@0 | 58 | else: |
michael@0 | 59 | import popen2 |
michael@0 | 60 | p = popen2.Popen3(cmd, 1) |
michael@0 | 61 | i, o, e = p.tochild, p.fromchild, p.childerr |
michael@0 | 62 | output = o.read() |
michael@0 | 63 | error = e.read() |
michael@0 | 64 | i.close() |
michael@0 | 65 | o.close() |
michael@0 | 66 | e.close() |
michael@0 | 67 | retval = (p.wait() & 0xFF00) >> 8 |
michael@0 | 68 | if retval > 2**7: # 8-bit signed 1's-complement conversion |
michael@0 | 69 | retval -= 2**8 |
michael@0 | 70 | return output, error, retval |
michael@0 | 71 | |
michael@0 | 72 | |
michael@0 | 73 | def _rmtreeOnError(rmFunction, filePath, excInfo): |
michael@0 | 74 | if excInfo[0] == OSError: |
michael@0 | 75 | # presuming because file is read-only |
michael@0 | 76 | os.chmod(filePath, 0777) |
michael@0 | 77 | rmFunction(filePath) |
michael@0 | 78 | |
michael@0 | 79 | def rmtree(dirname): |
michael@0 | 80 | import shutil |
michael@0 | 81 | shutil.rmtree(dirname, 0, _rmtreeOnError) |
michael@0 | 82 | |
michael@0 | 83 |