コード例 #1
0
    def run_shell_exec(self, cmd, track=True):
        """
        Run the shell exec command.

        @param cmd String Command to be run
        @return int, string
        """
        cmd = 'ulimit -f ' + escape_shellarg(background_size_limit) + ';' + \
            'ulimit -v ' + escape_shellarg(background_memory_limit) + ';' + \
            'ulimit -t ' + escape_shellarg(background_time_limit) + ';' + \
            'ulimit -a;' + \
            'nice -n ' + escape_shellarg(background_priority) + ' ' + cmd + \
            ' 2>&1'

        # Adapted from https://gist.github.com/marazmiki/3015621
        process = subprocess.Popen(cmd,
                                   stdin=None,
                                   stdout=subprocess.PIPE,
                                   stderr=subprocess.STDOUT,
                                   universal_newlines=True,
                                   shell=True)

        re_duration = re.compile(r'Duration: (\d{2}:\d{2}:\d{2})')
        re_position = re.compile(r'time=(\d{2}:\d{2}:\d{2})', re.I)

        duration = None
        position = None
        newpercentage = percentage = -1

        while process.poll() is None:
            # for line in process.stdout.readlines():
            # http://bugs.python.org/issue3907
            while True:
                line = process.stdout.readline()
                if not line:
                    break
                print line,

                if track:
                    if duration is None:
                        duration_match = re_duration.search(line)
                        if duration_match:
                            duration = time_to_seconds(duration_match.group(1))
                    else:
                        position_match = re_position.search(line)
                        if position_match:
                            position = time_to_seconds(position_match.group(1))
                            if duration and position:
                                newpercentage = min(
                                    int(math.floor(100 * position / duration)),
                                    100)

                    if newpercentage != percentage:
                        percentage = newpercentage
                        self.statuscallback(None, percentage)

            time.sleep(2)

        return process.returncode, ''
コード例 #2
0
    def run_shell_exec(self, cmd, track=True):
        """
        Run the shell exec command.

        @param cmd String Command to be run
        @return int, string
        """
        cmd = 'ulimit -f ' + escape_shellarg(background_size_limit) + ';' + \
            'ulimit -v ' + escape_shellarg(background_memory_limit) + ';' + \
            'ulimit -t ' + escape_shellarg(background_time_limit) + ';' + \
            'ulimit -a;' + \
            'nice -n ' + escape_shellarg(background_priority) + ' ' + cmd + \
            ' 2>&1'

        # Adapted from https://gist.github.com/marazmiki/3015621
        process = subprocess.Popen(
            cmd, stdin=None, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
            universal_newlines=True, shell=True
        )

        re_duration = re.compile(r'Duration: (\d{2}:\d{2}:\d{2})')
        re_position = re.compile(r'time=(\d{2}:\d{2}:\d{2})', re.I)

        duration = None
        position = None
        newpercentage = percentage = -1

        while process.poll() is None:
            # for line in process.stdout.readlines():
            # http://bugs.python.org/issue3907
            while True:
                line = process.stdout.readline()
                if not line:
                    break
                print line,

                if track:
                    if duration is None:
                        duration_match = re_duration.search(line)
                        if duration_match:
                            duration = time_to_seconds(duration_match.group(1))
                    else:
                        position_match = re_position.search(line)
                        if position_match:
                            position = time_to_seconds(position_match.group(1))
                            if duration and position:
                                newpercentage = min(int(
                                    math.floor(100 * position / duration)
                                ), 100)

                    if newpercentage != percentage:
                        percentage = newpercentage
                        self.statuscallback(None, percentage)

            time.sleep(2)

        return process.returncode, ''
コード例 #3
0
    def run_shell_exec(self, cmd, track=True):
        """
        Run the shell exec command.

        @param cmd String Command to be run
        @return int, string
        """
        cmd = (
            "ulimit -f "
            + escape_shellarg(background_size_limit)
            + ";"
            + "ulimit -v "
            + escape_shellarg(background_memory_limit)
            + ";"
            + "ulimit -t "
            + escape_shellarg(background_time_limit)
            + ";"
            + "ulimit -a;"
            + "nice -n "
            + escape_shellarg(background_priority)
            + " "
            + cmd
            + " 2>&1"
        )

        # Adapted from https://gist.github.com/marazmiki/3015621
        process = subprocess.Popen(
            cmd,
            stdin=None,
            stdout=subprocess.PIPE,
            stderr=subprocess.STDOUT,
            universal_newlines=True,
            shell=True,
            preexec_fn=os.setsid,
        )

        re_duration = re.compile(r"Duration: (\d{2}:\d{2}:\d{2})")
        re_position = re.compile(r"time=(\d{2}:\d{2}:\d{2})", re.I)

        duration = None
        position = None
        newpercentage = percentage = -1

        while process.poll() is None:
            # for line in process.stdout.readlines():
            # http://bugs.python.org/issue3907
            while True:
                line = process.stdout.readline()
                if not line:
                    break
                print line,

                if track:
                    if duration is None:
                        duration_match = re_duration.search(line)
                        if duration_match:
                            duration = time_to_seconds(duration_match.group(1))
                    else:
                        position_match = re_position.search(line)
                        if position_match:
                            position = time_to_seconds(position_match.group(1))
                            if duration and position:
                                newpercentage = min(int(math.floor(100 * position / duration)), 100)

                    if newpercentage != percentage:
                        percentage = newpercentage
                        try:
                            self.statuscallback(None, percentage)
                        except TaskAbort:
                            os.killpg(os.getpgid(process.pid), signal.SIGTERM)
                            raise

            time.sleep(2)

        return process.returncode, ""