Пример #1
0
 def simple_execute_command(self, cmd, **kwargs):
     import subprocess
     try:
         pipe = subprocess.Popen(cmd,
                                 stdout=subprocess.PIPE,
                                 stdin=subprocess.PIPE,
                                 stderr=subprocess.PIPE,
                                 **kwargs)
     except OSError as e:
         raise CompilerError(
             "Compiler failed to execute. (%s)" % e,
             command=cmd,
             error_output=
             "Executing the compiler resulted in an %s from the system.\n\nThis is most likely due to the `browserify` executable not being found in your PATH (if it is installed globally), or a misconfigured BROWSERIFY_BINARY setting (if you are using a non-global install)."
             % repr(e))
     stdout, stderr = pipe.communicate()
     if self.verbose:
         print(stdout)
         print(stderr)
     if pipe.returncode != 0:
         raise CompilerError("Compiler returned non-zero exit status %i" %
                             pipe.returncode,
                             command=cmd,
                             error_output=stderr)
     return stdout.decode()
Пример #2
0
    def execute_command(self, command, content=None, cwd=None):
        pipe = subprocess.Popen(command,
                                shell=True,
                                cwd=cwd,
                                stdout=subprocess.PIPE,
                                stdin=subprocess.PIPE,
                                stderr=subprocess.PIPE)
        if content:
            content = smart_bytes(content)
        stdout, stderr = pipe.communicate(content)

        if isinstance(stderr, bytes):
            try:
                stderr = stderr.decode()
            except UnicodeDecodeError:
                pass

        if stderr.strip():
            raise CompilerError(stderr, error_output=stderr)

        if self.verbose:
            print(stderr)

        if pipe.returncode != 0:
            msg = "Command '{0}' returned non-zero exit status {1}".format(
                command, pipe.returncode)
            raise CompilerError(msg, error_output=msg)

        return stdout
Пример #3
0
    def execute_command(self, command, cwd=None, stdout_captured=None):
        """Execute a command at cwd, saving its normal output at
        stdout_captured. Errors, defined as nonzero return code or a failure
        to start execution, will raise a CompilerError exception with a
        description of the cause. They do not write output.

        This is file-system safe (any valid file names are allowed, even with
        spaces or crazy characters) and OS agnostic (existing and future OSes
        that Python supports should already work).

        The only thing weird here is that any incoming command arg item may
        itself be a tuple. This allows compiler implementations to look clean
        while supporting historical string config settings and maintaining
        backwards compatibility. Thus, we flatten one layer deep.
         ((env, foocomp), infile, (-arg,)) -> (env, foocomp, infile, -arg)
        """
        argument_list = []
        for flattening_arg in command:
            if isinstance(flattening_arg, string_types):
                argument_list.append(flattening_arg)
            else:
                argument_list.extend(flattening_arg)

        try:
            # We always catch stdout in a file, but we may not have a use for it.
            temp_file_container = cwd or os.path.dirname(stdout_captured
                                                         or "") or os.getcwd()
            with NamedTemporaryFile(delete=False,
                                    dir=temp_file_container) as stdout:
                compiling = subprocess.Popen(argument_list,
                                             cwd=cwd,
                                             stdout=stdout,
                                             stderr=subprocess.PIPE)
                _, stderr = compiling.communicate()

            if compiling.returncode != 0:
                stdout_captured = None  # Don't save erroneous result.
                raise CompilerError("{0!r} exit code {1}\n{2}".format(
                    argument_list, compiling.returncode, stderr))

            # User wants to see everything that happened.
            if self.verbose:
                with open(stdout.name) as out:
                    print(out.read())
                print(stderr)
        except OSError as e:
            stdout_captured = None  # Don't save erroneous result.
            raise CompilerError(e)
        finally:
            # Decide what to do with captured stdout.
            if stdout_captured:
                os.rename(stdout.name,
                          os.path.join(cwd or os.curdir, stdout_captured))
            else:
                os.remove(stdout.name)
Пример #4
0
 def compile_file(self, infile, outfile, outdated=False, force=False):
     if not outdated and not force:
         return
     try:
         return self.transformer.transform(infile, outfile)
     except TransformError as e:
         raise CompilerError(str(e))
Пример #5
0
 def execute_command(self, command, content=None, cwd=None):
     import subprocess
     pipe = subprocess.Popen(command, shell=True, cwd=cwd,
                             stdout=subprocess.PIPE, stdin=subprocess.PIPE,
                             stderr=subprocess.PIPE)
     if content:
         content = smart_bytes(content)
     stdout, stderr = pipe.communicate(content)
     if pipe.returncode != 0:
         if stderr.strip():
             raise CompilerError(stderr)
         elif stdout.strip():
             raise CompilerError(stdout)
         else:
             raise CompilerError("Command failed with no output")
     if self.verbose:
         print(stderr)
     return stdout
Пример #6
0
 def execute_command(self, command, content=None, cwd=None):
     import subprocess
     pipe = subprocess.Popen(command,
                             shell=True,
                             cwd=cwd,
                             stdout=subprocess.PIPE,
                             stdin=subprocess.PIPE,
                             stderr=subprocess.PIPE)
     if content:
         content = smart_bytes(content)
     stdout, stderr = pipe.communicate(content)
     if stderr.strip():
         raise CompilerError(stderr)
     if self.verbose:
         print(stderr)
     if pipe.returncode != 0:
         raise CompilerError(
             "Command '{0}' returned non-zero exit status {1}".format(
                 command, pipe.returncode))
     return stdout
Пример #7
0
 def execute_command(self, command, content=None, cwd=None):
     pipe = subprocess.Popen(command, shell=True, cwd=cwd,
                             stdout=subprocess.PIPE, stdin=subprocess.PIPE,
                             stderr=subprocess.PIPE)
     if content:
         content = smart_bytes(content)
     stdout, stderr = pipe.communicate(content)
     if stderr.strip():
         raise CompilerError(stderr)
     if self.verbose:
         print(stderr)
     return stdout
Пример #8
0
    def compile_file(self, infile, outfile, outdated=False, force=False):
        # noinspection PyPackageRequirements,PyUnresolvedReferences
        from pipeline.exceptions import CompilerError

        command = ([settings.TYPESCRIPT_BINARY] +
                   settings.TYPESCRIPT_ARGUMENTS + ["-out", outfile, infile])
        try:
            p = subprocess.Popen(
                command,
                stdin=subprocess.PIPE,
                stdout=subprocess.PIPE,
                stderr=subprocess.PIPE,
            )
            stdout, __ = p.communicate(b"")
            if p.returncode != 0:
                raise CompilerError(
                    "Unable to execute TypeScript",
                    command=command,
                    error_output=stdout.decode(),
                )
        except Exception as e:
            raise CompilerError(e, command=command, error_output=str(e))
Пример #9
0
 def execute_command(self, command, content=None, cwd=None):
     """This is like the one in SubProcessCompiler, except it checks the exit code."""
     import subprocess
     pipe = subprocess.Popen(command,
                             shell=True,
                             cwd=cwd,
                             stdout=subprocess.PIPE,
                             stdin=subprocess.PIPE,
                             stderr=subprocess.PIPE)
     if content:
         content = smart_bytes(content)
     stdout, stderr = pipe.communicate(content)
     if self.verbose:
         print(stderr)
     if pipe.returncode != 0:
         raise CompilerError(stderr)
     return stdout
Пример #10
0
    def compile_file(self, infile, outfile, outdated=False, force=False):
        if not outdated and not force:
            return

        try:
            harmony = settings.REACT_HARMONY
        except KeyError:
            harmony = False

        try:
            strip_types = settings.REACT_STRIP_TYPES
        except KeyError:
            strip_types = False

        try:
            return self.transformer.transform(
                infile,
                outfile,
                harmony=harmony,
                strip_types=strip_types,
            )
        except TransformError as e:
            raise CompilerError(str(e))