Ticket #2293: W32_POSIX_SHELL.patch

File W32_POSIX_SHELL.patch, 3.2 KB (added by LRN, 8 years ago)
  • runprocess.py

    old new  
    2525import subprocess
    2626import traceback
    2727import stat
     28import tempfile
    2829from collections import deque
    2930
    3031from twisted.python import runtime, log
     
    148149        self.pending_stdin = ""
    149150        self.stdin_finished = False
    150151        self.killed = False
     152        self.scriptfile = ""
    151153
    152154    def setStdin(self, data):
    153155        assert not self.connected
     
    198200                rc = 1
    199201            else:
    200202                rc = -1
     203        if self.scriptfile:
     204            try:
     205                os.remove (self.scriptfile)
     206            except:
     207                pass
    201208        self.command.finished(sig, rc)
    202209
    203210
     
    408415
    409416        if type(self.command) in types.StringTypes:
    410417            if runtime.platformType  == 'win32':
    411                 argv = os.environ['COMSPEC'].split() # allow %COMSPEC% to have args
    412                 if '/c' not in argv: argv += ['/c']
    413                 argv += [self.command]
     418                if os.environ['BUILDSLAVE_SHELL']:
     419                    argv = os.environ['BUILDSLAVE_SHELL'].split() # allow %COMSPEC% to have args
     420                    argv += [self.command]
     421                else:
     422                    argv = os.environ['COMSPEC'].split() # allow %COMSPEC% to have args
     423                    if '/c' not in argv:
     424                        argv += ['/c']
     425                    argv += [self.command]
    414426            else:
    415427                # for posix, use /bin/sh. for other non-posix, well, doesn't
    416428                # hurt to try
     
    424436            # handle path searching, etc.
    425437            if runtime.platformType == 'win32' and not \
    426438                    (self.command[0].lower().endswith(".exe") and os.path.isabs(self.command[0])):
    427                 argv = os.environ['COMSPEC'].split() # allow %COMSPEC% to have args
    428                 if '/c' not in argv: argv += ['/c']
    429                 argv += list(self.command)
     439                if os.environ['BUILDSLAVE_SHELL']:
     440                    argv = os.environ['BUILDSLAVE_SHELL'].split()
     441                    # Create a temporary script file that changes current directory
     442                    # and runs the command we want
     443                    # It will be deleted after command is finished running (see RunProcessPP)
     444                    tf, tf_name = tempfile.mkstemp ()
     445                    f = os.fdopen (tf, 'wb')
     446                    fcontents = '#!/bin/sh\ncd {}\n{}'.format (
     447                        re.sub(r'(?<!\\) ','\\ ', self.workdir.replace('\\','/')),
     448                        ' '.join (self.command))
     449                    f.write (fcontents)
     450
     451                    f.close ()
     452                    self.pp.scriptfile = tf_name
     453                    argv += [tf_name.replace('\\','/')]
     454                else:
     455                    argv = os.environ['COMSPEC'].split() # allow %COMSPEC% to have args
     456                    if '/c' not in argv:
     457                      argv += ['/c']
     458                    argv += list(self.command)
    430459            else:
    431460                argv = self.command
    432461            # Attempt to format this for use by a shell, although the process isn't perfect