Logo Search packages:      
Sourcecode: tailor version File versions  Download package

def vcpx::shwrap::ExternalCommand::execute (   self,
  args,
  kwargs 
)

Execute the command.

Definition at line 132 of file shwrap.py.

00132                                       :
        """Execute the command."""

        from sys import stderr
        from locale import getpreferredencoding
        from os import environ, getcwd
        from cStringIO import StringIO

        self.exit_status = None

        self._last_command = [chunk % kwargs for chunk in self.command]
        if len(args) == 1 and type(args[0]) == type([]):
            self._last_command.extend(args[0])
        else:
            self._last_command.extend(args)

        self.log.info(self)

        if self.DRY_RUN:
            return

        if not kwargs.has_key('cwd') and self.cwd:
            kwargs['cwd'] = self.cwd

        self.log.debug("Executing %r (%r)", self, kwargs.get('cwd', getcwd()))

        if not kwargs.has_key('env'):
            env = kwargs['env'] = {}
            env.update(environ)

            for v in ['LANG', 'TZ', 'PATH']:
                if kwargs.has_key(v):
                    env[v] = kwargs[v]
            # Override also LC_ALL that has a higher priority over LANG,
            # and LC_MESSAGES as well.
            if kwargs.has_key('LANG'):
                env['LC_ALL'] = kwargs['LANG']
                env['LC_MESSAGES'] = kwargs['LANG']

        input = kwargs.get('input')
        output = kwargs.get('stdout')
        error = kwargs.get('stderr')

        # When not in debug, redirect stderr and stdout to /dev/null
        # when the caller didn't ask for them.
        if not self.DEBUG:
            try:
                from os import devnull
            except ImportError:
                devnull = '/dev/null'
            if output is None:
                output = open(devnull, 'w')
            if error is None:
                error = open(devnull, 'w')
        try:
            process = Popen(self._last_command,
                            stdin=input and PIPE or None,
                            stdout=output,
                            stderr=error,
                            env=kwargs.get('env'),
                            cwd=kwargs.get('cwd'),
                            universal_newlines=True)
        except OSError, e:
            from errno import ENOENT

            if e.errno == ENOENT:
                raise OSError("'%s' does not exist!" % self._last_command[0])
            else:
                raise

        if input and isinstance(input, unicode):
            input = input.encode(self.FORCE_ENCODING or getpreferredencoding())

        out, err = process.communicate(input=input)

        self.exit_status = process.returncode
        if not self.exit_status:
            self.log.info("[Ok]")
        else:
            self.log.warning("[Status %s]", self.exit_status)

        # For debug purposes, copy the output to our stderr when hidden above
        if self.DEBUG:
            if out and output == PIPE:
                stderr.write('Output stream:\n')
                stderr.write(out)
            if err and error == PIPE:
                stderr.write('Error stream:\n')
                stderr.write(err)

        if out is not None:
            out = StringIO(out)
        if err is not None:
            err = StringIO(err)

        return out, err
        return out, err


Generated by  Doxygen 1.6.0   Back to index