示例#1
0
    def run(self):
        """Run command"""
        if len(self.cmd) == 0:
            return

        Debug.msg(1, "gcmd.CommandThread(): %s" % " ".join(self.cmd))

        self.startTime = time.time()

        # TODO: replace ugly hack below
        # this cannot be replaced it can be only improved
        # also unifying this with 3 other places in code would be nice
        # changing from one chdir to get_real_command function
        args = self.cmd
        if sys.platform == "win32":
            if os.path.splitext(args[0])[1] == SCT_EXT:
                args[0] = args[0][:-3]
            # using Python executable to run the module if it is a script
            # expecting at least module name at first position
            # cannot use make_command for this now because it is used in GUI
            # The same code is in grass.script.core already twice.
            args[0] = grass.get_real_command(args[0])
            if args[0].endswith(".py"):
                args.insert(0, sys.executable)

        try:
            self.module = Popen(
                args,
                stdin=subprocess.PIPE,
                stdout=subprocess.PIPE,
                stderr=subprocess.PIPE,
                shell=sys.platform == "win32",
                env=self.env,
            )

        except OSError as e:
            self.error = str(e)
            print(e, file=sys.stderr)
            return 1

        if self.stdin:  # read stdin if requested ...
            self.module.stdin.write(self.stdin)
            self.module.stdin.close()

        # redirect standard outputs...
        self._redirect_stream()
示例#2
0
    def run(self):
        """Run command"""
        if len(self.cmd) == 0:
            return

        Debug.msg(1, "gcmd.CommandThread(): %s" % ' '.join(self.cmd))

        self.startTime = time.time()

        # TODO: replace ugly hack below
        # this cannot be replaced it can be only improved
        # also unifying this with 3 other places in code would be nice
        # changing from one chdir to get_real_command function
        args = self.cmd
        if sys.platform == 'win32':
            if os.path.splitext(args[0])[1] == globalvar.SCT_EXT:
                args[0] = args[0][:-3]
            # using Python executable to run the module if it is a script
            # expecting at least module name at first position
            # cannot use make_command for this now because it is used in GUI
            # The same code is in grass.script.core already twice.
            args[0] = grass.get_real_command(args[0])
            if args[0].endswith('.py'):
                args.insert(0, sys.executable)
 
        try:
            self.module = Popen(args,
                                stdin = subprocess.PIPE,
                                stdout = subprocess.PIPE,
                                stderr = subprocess.PIPE,
                                shell = sys.platform == "win32",
                                env = self.env)
            
        except OSError as e:
            self.error = str(e)
            print >> sys.stderr, e
            return 1
        
        if self.stdin: # read stdin if requested ...
            self.module.stdin.write(self.stdin)
            self.module.stdin.close()
            
        # redirect standard outputs...
        self._redirect_stream()