Python subprocess.STDOUT() Examples

The following are code examples for showing how to use subprocess.STDOUT(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: clikit   Author: sdispater   File: terminal.py    MIT License 8 votes vote down vote up
def _get_terminal_size_tput(self):
        # get terminal width
        # src: http://stackoverflow.com/questions/263890/how-do-i-find-the-width-height-of-a-terminal-window
        try:
            cols = int(
                subprocess.check_output(
                    shlex.split("tput cols"), stderr=subprocess.STDOUT
                )
            )
            rows = int(
                subprocess.check_output(
                    shlex.split("tput lines"), stderr=subprocess.STDOUT
                )
            )

            return (cols, rows)
        except:
            pass 
Example 2
Project: wechat-alfred-workflow   Author: TKkk-iOSer   File: notify.py    MIT License 7 votes vote down vote up
def convert_image(inpath, outpath, size):
    """Convert an image file using ``sips``.

    Args:
        inpath (str): Path of source file.
        outpath (str): Path to destination file.
        size (int): Width and height of destination image in pixels.

    Raises:
        RuntimeError: Raised if ``sips`` exits with non-zero status.
    """
    cmd = [
        b'sips',
        b'-z', str(size), str(size),
        inpath,
        b'--out', outpath]
    # log().debug(cmd)
    with open(os.devnull, 'w') as pipe:
        retcode = subprocess.call(cmd, stdout=pipe, stderr=subprocess.STDOUT)

    if retcode != 0:
        raise RuntimeError('sips exited with %d' % retcode) 
Example 3
Project: pyblish-win   Author: pyblish   File: os.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def popen4(cmd, mode="t", bufsize=-1):
            """Execute the shell command 'cmd' in a sub-process.  On UNIX, 'cmd'
            may be a sequence, in which case arguments will be passed directly to
            the program without shell intervention (as with os.spawnv()).  If 'cmd'
            is a string it will be passed to the shell (as with os.system()). If
            'bufsize' is specified, it sets the buffer size for the I/O pipes.  The
            file objects (child_stdin, child_stdout_stderr) are returned."""
            import warnings
            msg = "os.popen4 is deprecated.  Use the subprocess module."
            warnings.warn(msg, DeprecationWarning, stacklevel=2)

            import subprocess
            PIPE = subprocess.PIPE
            p = subprocess.Popen(cmd, shell=isinstance(cmd, basestring),
                                 bufsize=bufsize, stdin=PIPE, stdout=PIPE,
                                 stderr=subprocess.STDOUT, close_fds=True)
            return p.stdin, p.stdout 
Example 4
Project: pyblish-win   Author: pyblish   File: test_sysconfig.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def test_customize_compiler_before_get_config_vars(self):
        # Issue #21923: test that a Distribution compiler
        # instance can be called without an explicit call to
        # get_config_vars().
        with open(TESTFN, 'w') as f:
            f.writelines(textwrap.dedent('''\
                from distutils.core import Distribution
                config = Distribution().get_command_obj('config')
                # try_compile may pass or it may fail if no compiler
                # is found but it should not raise an exception.
                rc = config.try_compile('int x;')
                '''))
        p = subprocess.Popen([str(sys.executable), TESTFN],
                stdout=subprocess.PIPE,
                stderr=subprocess.STDOUT,
                universal_newlines=True)
        outs, errs = p.communicate()
        self.assertEqual(0, p.returncode, "Subprocess failed: " + outs) 
Example 5
Project: pyblish-win   Author: pyblish   File: test_pdb.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def run_pdb(self, script, commands):
        """Run 'script' lines with pdb and the pdb 'commands'."""
        filename = 'main.py'
        with open(filename, 'w') as f:
            f.write(textwrap.dedent(script))
        self.addCleanup(test_support.unlink, filename)
        cmd = [sys.executable, '-m', 'pdb', filename]
        stdout = stderr = None
        proc = subprocess.Popen(cmd, stdout=subprocess.PIPE,
                                   stdin=subprocess.PIPE,
                                   stderr=subprocess.STDOUT,
                                   )
        stdout, stderr = proc.communicate(commands)
        proc.stdout.close()
        proc.stdin.close()
        return stdout, stderr 
Example 6
Project: django-click   Author: GaretJax   File: conftest.py    MIT License 6 votes vote down vote up
def manage():
    def call(*args, **kwargs):
        ignore_errors = kwargs.pop('ignore_errors', False)
        assert not kwargs
        cmd = [
            sys.executable,
            os.path.join(os.path.dirname(__file__), 'testprj', 'manage.py'),
        ] + list(args)
        try:
            return subprocess.check_output(cmd, stderr=subprocess.STDOUT)
        except subprocess.CalledProcessError as e:
            if not ignore_errors:
                raise
            return e.output

    return call 
Example 7
Project: git-deployment-scripts   Author: codechick   File: deployment.py    MIT License 6 votes vote down vote up
def __checkout_new_version(self):
        exit_code = 0
        self.__print('Checking out the new version...')

        #we are talking about bare repositories. Hence we have to specify where the HEAD points
        cmd = "git symbolic-ref HEAD refs/heads/%s;" % self.branch
        cmd += self.__cmd_prefix + 'checkout -f;'
        cmd += self.__cmd_prefix + 'submodule init;'
        cmd += self.__cmd_prefix + 'submodule sync;'
        cmd += self.__cmd_prefix + 'submodule update;'

        exit_code = subprocess.call(cmd, stderr=subprocess.STDOUT, shell=True)

        if exit_code == 0:
            self.__print('Checkout done')
            return True
        else:
            self.__print('Checkout met some errors. Proceding anyway', 'ERR')
            return False 
Example 8
Project: git-deployment-scripts   Author: codechick   File: deployment.py    MIT License 6 votes vote down vote up
def __unstash_it(self):
        self.__print('Unstashing...')
        cmd = self.__cmd_prefix + 'stash show'
        exit_code = subprocess.call(cmd, stderr=subprocess.STDOUT, shell=True)

        if exit_code > 0:
            self.__print('No stashing to apply. Skipping...')
            return True

        cmd = self.__cmd_prefix + 'stash pop'
        exit_code = subprocess.call(cmd, stderr=subprocess.STDOUT, shell=True)

        if exit_code == 0:
            self.__print('Unstashed.')
            return True
        else:
            self.__print('Stashing met some errors. Proceding anyway', 'ERR')
            return False 
Example 9
Project: Autoline   Author: zjh1218   File: runner.py    Apache License 2.0 6 votes vote down vote up
def debug(self):
        try:
            output_dir = os.getcwd() + "/logs/%s/%s" % (self.project_id, self.build_no)
            output_dir = output_dir.replace("\\", "/")
            # -x result/output.xml -l result/log.html -r result/report.html
            command = ["pybot", "-d", "%s" % output_dir, "--dryrun", "-N", "调试输出", "%s/testcase.robot" % output_dir]
            self._out_fd = open(output_dir + "/debug.log", "a+")
            self._process = subprocess.Popen(command, shell=False, stdout=self._out_fd, stderr=subprocess.STDOUT)
            while True:
                if self._process.poll() == 0:  # 判断子进程是否结束
                    break
                else:
                    time.sleep(0.2)

        except Exception as e:
            print(str(e))
            pass

        return {"status": "success",
                "msg": "任务启动成功",
                "project_id": self.project_id,
                "build_no": self.build_no} 
Example 10
Project: flasky   Author: RoseOu   File: os.py    MIT License 6 votes vote down vote up
def popen4(cmd, mode="t", bufsize=-1):
            """Execute the shell command 'cmd' in a sub-process.  On UNIX, 'cmd'
            may be a sequence, in which case arguments will be passed directly to
            the program without shell intervention (as with os.spawnv()).  If 'cmd'
            is a string it will be passed to the shell (as with os.system()). If
            'bufsize' is specified, it sets the buffer size for the I/O pipes.  The
            file objects (child_stdin, child_stdout_stderr) are returned."""
            import warnings
            msg = "os.popen4 is deprecated.  Use the subprocess module."
            warnings.warn(msg, DeprecationWarning, stacklevel=2)

            import subprocess
            PIPE = subprocess.PIPE
            p = subprocess.Popen(cmd, shell=isinstance(cmd, basestring),
                                 bufsize=bufsize, stdin=PIPE, stdout=PIPE,
                                 stderr=subprocess.STDOUT, close_fds=True)
            return p.stdin, p.stdout 
Example 11
Project: flasky   Author: RoseOu   File: firefox_binary.py    MIT License 6 votes vote down vote up
def _start_from_profile_path(self, path):
        self._firefox_env["XRE_PROFILE_PATH"] = path

        if platform.system().lower() == 'linux':
            self._modify_link_library_path()
        command = [self._start_cmd, "-silent"]
        if self.command_line is not None:
            for cli in self.command_line:
                command.append(cli)

        Popen(command, stdout=self._log_file, stderr=STDOUT,
              env=self._firefox_env).communicate()
        command[1] = '-foreground'
        self.process = Popen(
            command, stdout=self._log_file, stderr=STDOUT,
            env=self._firefox_env) 
Example 12
Project: dynamic-training-with-apache-mxnet-on-aws   Author: awslabs   File: straight_dope_test_utils.py    Apache License 2.0 6 votes vote down vote up
def _run_command(cmd, timeout_secs=300):
    """ Runs a command with a specified timeout.

    Args:
        cmd : list of string
            The command with arguments to run.
        timeout_secs: integer
            The timeout in seconds

    Returns:
        Returns the process and the output as a pair.
    """
    proc = subprocess.Popen(
        cmd,
        stdout=subprocess.PIPE,
        stderr=subprocess.STDOUT)

    for i in range(timeout_secs):
        sleep(1)
        if proc.poll() is not None:
            (out, _) = proc.communicate()
            return proc, out.decode('utf-8')

    proc.kill()
    return proc, "Timeout of %s secs exceeded." % timeout_secs 
Example 13
Project: helloworld   Author: pip-uninstaller-python   File: learning.py    GNU General Public License v2.0 6 votes vote down vote up
def do_POST(self):
        self.close_connection = True
        if self.path != '/run':
            return self.send_error(400)
        print('Prepare code...')
        body = self.rfile.read(int(self.headers['Content-length']))
        qs = parse.parse_qs(body.decode('utf-8'))
        if not 'code' in qs:
            return self.send_error(400)
        code = qs['code'][0]
        r = dict()
        try:
            fpath = write_py(get_name(), code)
            print('Execute: %s %s' % (EXEC, fpath))
            r['output'] = decode(subprocess.check_output([EXEC, fpath], stderr=subprocess.STDOUT, timeout=5))
        except subprocess.CalledProcessError as e:
            r = dict(error='Exception', output=decode(e.output))
        except subprocess.TimeoutExpired as e:
            r = dict(error='Timeout', output='执行超时')
        except subprocess.CalledProcessError as e:
            r = dict(error='Error', output='执行错误')
        print('Execute done.')
        self._sendHttpHeader()
        self._sendHttpBody(r) 
Example 14
Project: alfred-urban-dictionary   Author: xilopaint   File: notify.py    MIT License 6 votes vote down vote up
def convert_image(inpath, outpath, size):
    """Convert an image file using ``sips``.

    Args:
        inpath (str): Path of source file.
        outpath (str): Path to destination file.
        size (int): Width and height of destination image in pixels.

    Raises:
        RuntimeError: Raised if ``sips`` exits with non-zero status.
    """
    cmd = [
        b'sips',
        b'-z', str(size), str(size),
        inpath,
        b'--out', outpath]
    # log().debug(cmd)
    with open(os.devnull, 'w') as pipe:
        retcode = subprocess.call(cmd, stdout=pipe, stderr=subprocess.STDOUT)

    if retcode != 0:
        raise RuntimeError('sips exited with %d' % retcode) 
Example 15
Project: Stegall   Author: mrT4ntr4   File: wavest.py    MIT License 6 votes vote down vote up
def launch(arg2):
	test = sb.Popen(['python3','--version'], stdout=sb.PIPE, stdin=sb.PIPE, stderr=sb.STDOUT) 
	output = test.communicate()[0]
	res = 'Python 3.' in output
	if not res:
		print "You need to have Python ver_3.* to run WavSteg"
		yn1 = 'y'
		while yn1:
			while (yn1):
				yn1 = raw_input('Do you want to install it now (y/n)')
				if yn1=='y':
					stg.prog_install('python3.6')
					execute(arg2)
				elif yn1=='n':
					print stg.color.FAIL
					exit("Program Exited")
				else :
					print("Please enter a vaild choice")
			continue
	else:
		execute(arg2) 
Example 16
Project: Stegall   Author: mrT4ntr4   File: steghide.py    MIT License 6 votes vote down vote up
def sh(arg2):

 dotLoc = arg2.find('.')
 ext = arg2[dotLoc+1::]	


 try:
	test = sb.Popen([prog,'extract','-sf',arg2,'-p','testpass'], stdout=sb.PIPE, stdin=sb.PIPE, stderr=sb.STDOUT) 
	output = test.communicate()[0]
	res = output.find('could not open the file')

 	if res==10:
		stg.io_error()
		exit()
		return

	elif ext in ['jpg','bmp','jpeg']:
  		launch(arg2)
  	else:
  		stg.io_error()
  		exit()

 except OSError as e:
	if e.errno == os.errno.ENOENT:
		prog_install(prog) 
Example 17
Project: mx   Author: graalvm   File: mx.py    GNU General Public License v2.0 6 votes vote down vote up
def root(self, directory, abortOnError=True):
        metadata = VC._find_metadata_dir(directory, '.hg')
        if metadata:
            try:
                out = _check_output_str(['hg', 'root'], cwd=directory, stderr=subprocess.STDOUT)
                return out.strip()
            except subprocess.CalledProcessError:
                if abortOnError:
                    self.check_for_hg()
                    abort('hg root failed')
                else:
                    return None
        else:
            if abortOnError:
                abort('No .hg directory')
            else:
                return None 
Example 18
Project: mx   Author: graalvm   File: mx_native.py    GNU General Public License v2.0 6 votes vote down vote up
def _ninja_deps(cls):  # pylint: disable=no-self-argument
        deps = []

        try:
            subprocess.check_output(['ninja', '--version'], stderr=subprocess.STDOUT)
        except OSError:
            dep = mx.library('NINJA', False)
            if dep:
                deps.append(dep.qualifiedName())
                Ninja.binary = mx.join(dep.get_path(False), 'ninja')
            else:
                # necessary until GR-13214 is resolved
                mx.warn('Make `ninja` binary available via PATH to build native projects.')

        try:
            import ninja_syntax  # pylint: disable=unused-variable, unused-import
        except ImportError:
            dep = mx.library('NINJA_SYNTAX')
            deps.append(dep.qualifiedName())
            module_path = mx.join(dep.get_path(False), 'ninja_syntax-{}'.format(dep.version))
            mx.ensure_dir_exists(module_path)  # otherwise, import machinery will ignore it
            sys.path.append(module_path)

        return deps 
Example 19
Project: ngo-addons-backport   Author: camptocamp   File: suite.py    GNU Affero General Public License v3.0 6 votes vote down vote up
def run(self, result):
        try:
            subprocess.call(['phantomjs', '-v'],
                            stdout=open(os.devnull, 'w'),
                            stderr=subprocess.STDOUT)
        except OSError:
            test = QUnitTest('phantomjs', 'javascript tests')
            result.startTest(test)
            result.startTest(test)
            result.addSkip(test , "phantomjs command not found")
            result.stopTest(test)
            return

        result._exc_info_to_string = _exc_info_to_string
        try:
            self._run(result)
        finally:
            del result._exc_info_to_string 
Example 20
Project: UrsaRobotics_SmartHome   Author: stav98   File: radio.py    GNU General Public License v3.0 5 votes vote down vote up
def play_radio():
    global pid
    with open(os.devnull, 'wb') as nul:
        pid = subprocess.Popen(['mplayer', '-really-quiet', station], stdin=nul,  stdout=subprocess.PIPE)
        #pid = subprocess.Popen(['mplayer','-msglevel',  'all=4',  station], stdin=nul,  stdout=subprocess.PIPE,  stderr=subprocess.STDOUT)

#Αν δεχθεί μήνυμα σε κάποιο topic που έχει κάνει subscribe 
Example 21
Project: ubittool   Author: carlosperate   File: test_system_cli.py    MIT License 5 votes vote down vote up
def _run_cli_cmd(cmd_list):
    """Run a shell command and return the output.

    :param cmd_list: A list of strings that make up the command to execute.
    """
    try:
        return subprocess.check_output(cmd_list, stderr=subprocess.STDOUT)
    except subprocess.CalledProcessError as e:
        return e.output 
Example 22
Project: alfred-yubikey-otp   Author: robertoriv   File: notify.py    MIT License 5 votes vote down vote up
def convert_image(inpath, outpath, size):
    """Convert an image file using ``sips``.

    Args:
        inpath (str): Path of source file.
        outpath (str): Path to destination file.
        size (int): Width and height of destination image in pixels.

    Raises:
        RuntimeError: Raised if ``sips`` exits with non-zero status.
    """
    cmd = [
        b'sips',
        b'-z', str(size), str(size),
        inpath,
        b'--out', outpath]
    # log().debug(cmd)
    with open(os.devnull, 'w') as pipe:
        retcode = subprocess.call(cmd, stdout=pipe, stderr=subprocess.STDOUT)

    if retcode != 0:
        raise RuntimeError('sips exited with %d' % retcode) 
Example 23
Project: alfred-yubikey-otp   Author: robertoriv   File: workflow.py    MIT License 5 votes vote down vote up
def _call_security(self, action, service, account, *args):
        """Call ``security`` CLI program that provides access to keychains.

        May raise `PasswordNotFound`, `PasswordExists` or `KeychainError`
        exceptions (the first two are subclasses of `KeychainError`).

        :param action: The ``security`` action to call, e.g.
                           ``add-generic-password``
        :type action: ``unicode``
        :param service: Name of the service.
        :type service: ``unicode``
        :param account: name of the account the password is for, e.g.
            "Pinboard"
        :type account: ``unicode``
        :param password: the password to secure
        :type password: ``unicode``
        :param *args: list of command line arguments to be passed to
                      ``security``
        :type *args: `list` or `tuple`
        :returns: ``(retcode, output)``. ``retcode`` is an `int`, ``output`` a
                  ``unicode`` string.
        :rtype: `tuple` (`int`, ``unicode``)

        """
        cmd = ['security', action, '-s', service, '-a', account] + list(args)
        p = subprocess.Popen(cmd, stdout=subprocess.PIPE,
                             stderr=subprocess.STDOUT)
        stdout, _ = p.communicate()
        if p.returncode == 44:  # password does not exist
            raise PasswordNotFound()
        elif p.returncode == 45:  # password already exists
            raise PasswordExists()
        elif p.returncode > 0:
            err = KeychainError('Unknown Keychain error : %s' % stdout)
            err.retcode = p.returncode
            raise err
        return stdout.strip().decode('utf-8') 
Example 24
Project: pyblish-win   Author: pyblish   File: _launcher.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def main(root, program, args=None):
    """Command-line entry point

    Arguments:
        root (str): Directory from which executable is launched
        program (str): Name of program, e.g. pyblish-qml
        async (bool, optional): Run asynchronously, default is False
        console (bool, optional): Run with console, default is False
        args (list, optional): Additional arguments passed to program

    """

    setup(root)

    args = [sys.executable,
            "-u",  # Unbuffered, for outputting to stdout below
            "-m", program] + args or []

    CREATE_NO_WINDOW = 0x08000000
    popen = subprocess.Popen(args,
                             creationflags=CREATE_NO_WINDOW,
                             stdout=subprocess.PIPE,
                             stderr=subprocess.STDOUT)

    for line in iter(popen.stdout.readline, b""):
        sys.stdout.write(line)

    popen.communicate()  # Block until done 
Example 25
Project: pyblish-win   Author: pyblish   File: test_hash.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def get_hash(self, repr_, seed=None):
        env = os.environ.copy()
        if seed is not None:
            env['PYTHONHASHSEED'] = str(seed)
        else:
            env.pop('PYTHONHASHSEED', None)
        cmd_line = [sys.executable, '-c', self.get_hash_command(repr_)]
        p = subprocess.Popen(cmd_line, stdin=subprocess.PIPE,
                             stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
                             env=env)
        out, err = p.communicate()
        out = test_support.strip_python_stderr(out)
        return int(out.strip()) 
Example 26
Project: pyblish-win   Author: pyblish   File: script_helper.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def spawn_python(*args, **kwargs):
    cmd_line = [sys.executable, '-E']
    cmd_line.extend(args)
    return subprocess.Popen(cmd_line, stdin=subprocess.PIPE,
                            stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
                            **kwargs) 
Example 27
Project: pyblish-win   Author: pyblish   File: test_subprocess.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_check_output_stderr(self):
        # check_output() function stderr redirected to stdout
        output = subprocess.check_output(
                [sys.executable, "-c", "import sys; sys.stderr.write('BDFL')"],
                stderr=subprocess.STDOUT)
        self.assertIn('BDFL', output) 
Example 28
Project: pyblish-win   Author: pyblish   File: test_subprocess.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_stdout_stderr_pipe(self):
        # capture stdout and stderr to the same pipe
        p = subprocess.Popen([sys.executable, "-c",
                          'import sys;'
                          'sys.stdout.write("apple");'
                          'sys.stdout.flush();'
                          'sys.stderr.write("orange")'],
                         stdout=subprocess.PIPE,
                         stderr=subprocess.STDOUT)
        self.addCleanup(p.stdout.close)
        self.assertStderrEqual(p.stdout.read(), "appleorange") 
Example 29
Project: pyblish-win   Author: pyblish   File: test_bz2.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def decompress(self, data):
            pop = subprocess.Popen("bunzip2", shell=True,
                                   stdin=subprocess.PIPE,
                                   stdout=subprocess.PIPE,
                                   stderr=subprocess.STDOUT)
            pop.stdin.write(data)
            pop.stdin.close()
            ret = pop.stdout.read()
            pop.stdout.close()
            if pop.wait() != 0:
                ret = bz2.decompress(data)
            return ret 
Example 30
Project: pyblish-win   Author: pyblish   File: _install.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def has_requirements():
    try:
        popen = subprocess.Popen(
            ["where", "git"],
            stdout=subprocess.PIPE,
            stderr=subprocess.STDOUT)
        popen.communicate()
        assert popen.returncode == 0
    except:
        print("")
        print("Error: Installation requires Git")
        print("See installation guide for details, or use binary installer")
        print("https://github.com/pyblish/pyblish-win/wiki")
        return False
    return True 
Example 31
Project: logging-test-case   Author: chadrosenquist   File: loggingtestcase_test.py    MIT License 5 votes vote down vote up
def run_test(command):
    """Runs a test case and returns the output."""
    new_command = 'python -m unittest ' + command
    new_command = new_command.split()
    try:
        output = subprocess.check_output(new_command,
                                         stderr=subprocess.STDOUT,
                                         universal_newlines=True)
    except subprocess.CalledProcessError as error:
        output = error.output

    return output 
Example 32
Project: wechat-alfred-workflow   Author: TKkk-iOSer   File: workflow.py    MIT License 5 votes vote down vote up
def _call_security(self, action, service, account, *args):
        """Call ``security`` CLI program that provides access to keychains.

        May raise `PasswordNotFound`, `PasswordExists` or `KeychainError`
        exceptions (the first two are subclasses of `KeychainError`).

        :param action: The ``security`` action to call, e.g.
                           ``add-generic-password``
        :type action: ``unicode``
        :param service: Name of the service.
        :type service: ``unicode``
        :param account: name of the account the password is for, e.g.
            "Pinboard"
        :type account: ``unicode``
        :param password: the password to secure
        :type password: ``unicode``
        :param *args: list of command line arguments to be passed to
                      ``security``
        :type *args: `list` or `tuple`
        :returns: ``(retcode, output)``. ``retcode`` is an `int`, ``output`` a
                  ``unicode`` string.
        :rtype: `tuple` (`int`, ``unicode``)

        """
        cmd = ['security', action, '-s', service, '-a', account] + list(args)
        p = subprocess.Popen(cmd, stdout=subprocess.PIPE,
                             stderr=subprocess.STDOUT)
        stdout, _ = p.communicate()
        if p.returncode == 44:  # password does not exist
            raise PasswordNotFound()
        elif p.returncode == 45:  # password already exists
            raise PasswordExists()
        elif p.returncode > 0:
            err = KeychainError('Unknown Keychain error : %s' % stdout)
            err.retcode = p.returncode
            raise err
        return stdout.strip().decode('utf-8') 
Example 33
Project: kipet   Author: salvadorgarciamunoz   File: validate_tut_problems.py    GNU General Public License v3.0 5 votes vote down vote up
def _schedule(self, examples_dir):
        examples_tutorial_dir = os.path.join(examples_dir)
        examples_tutorial=[f for f in os.listdir(examples_dir) if os.path.isfile(os.path.join(examples_tutorial_dir,f)) and f.endswith('.py')]
        
        #####################################
        ###Tutorial-Examples:##
        #####################################
        #else:
        flagpy = 0
        countpy = 0
        flagps = 0
        for f in examples_tutorial:
            flagpy = subprocess.call([sys.executable,os.path.join(examples_tutorial_dir,f),'1'],
                                stdout=self.std_out,
                                stderr=subprocess.STDOUT)
            if flagpy!=0: 
                print("running tutorial examples:",f,"failed")
                countpy = countpy + 1
                flagpy=1
                flagps=1

            else:
                print("running tutorial examples:",f,"passed")
            continue
        print(countpy,"files in",examples_tutorial_dir,"failed")
        return {self.assertEqual(int(flagpy),0),self.assertEqual(int(flagps),0)} 
Example 34
Project: git-deployment-scripts   Author: codechick   File: deployment.py    MIT License 5 votes vote down vote up
def __stash_modules_changes(self, op='save'):
        exit_code = 0

        if op not in ('save', 'pop'):
            raise ValueError("Bad argument: %s" % op)

        try:
            if op == 'save':
                self.__print('Stashing all the modules...')
                cmd = self.__cmd_prefix + "submodule foreach 'git stash save --all';"
                exit_code = subprocess.call(cmd, stderr=subprocess.STDOUT, shell=True)
            else:
                self.__print('Reapplying stash to the modules...')
                cmd = self.__cmd_prefix + "submodule foreach 'git stash pop ||:'"
                exit_code = subprocess.call(cmd, stderr=subprocess.STDOUT, shell=True)
        except subprocess.CalledProcessError as e:
            self.__print('An error occoured and stashing has terminated unexpectedly.', 'ERR')
            ret = e.output
            exit_code = 1

        if exit_code == 0:
            self.__print('Done')
        else:
            self.__print('Stashing met some errors. Proceding anyway', 'ERR')

#        self.__output_history.append({'op': 'stash_modules_changes', 'out': ret})

        if exit_code == 0:
            return True
        else:
            return False 
Example 35
Project: git-deployment-scripts   Author: codechick   File: deployment.py    MIT License 5 votes vote down vote up
def __stash_it(self):
        exit_code = 0
        self.__print('Stashing the website...')

        cmd = self.__cmd_prefix + "stash save --all"
        exit_code = subprocess.call(cmd, stderr=subprocess.STDOUT, shell=True)

        if exit_code == 0:
            self.__print('Done')
            return True
        else:
            self.__print('Stashing met some errors. Proceding anyway', 'ERR')
            return False 
Example 36
Project: utilities   Author: czbiohub   File: run_star_and_htseq.py    MIT License 5 votes vote down vote up
def run_htseq(dest_dir, sjdb_gtf, id_attr, logger):
    command = [
        HTSEQ,
        "-r",
        "name",
        "-s",
        "no",
        "-f",
        "bam",
        f"--idattr={id_attr}",
        "-m",
        "intersection-nonempty",
        os.path.join(dest_dir, "results", "Pass1", "Aligned.out.sorted-byname.bam"),
        sjdb_gtf,
        ">",
        "htseq-count.txt",
    ]
    failed = ut_log.log_command(
        logger,
        command,
        stdout=subprocess.PIPE,
        stderr=subprocess.STDOUT,
        shell=True,
        cwd=os.path.join(dest_dir, "results"),
    )

    return failed 
Example 37
Project: Autoline   Author: zjh1218   File: runner.py    Apache License 2.0 5 votes vote down vote up
def auto_run(self):
        try:
            user_id = User.query.filter_by(username="AutoExecutor").first().id
            name = AutoProject.query.filter_by(id=self.project_id).first().name
            task = AutoTask(project_id=self.project_id,
                            build_no=self.build_no,
                            status="running",
                            create_author_id=user_id,
                            create_timestamp=datetime.now())
            db.session.add(task)
            db.session.commit()

            output_dir = os.getcwd() + "/logs/%s/%s" % (self.project_id, self.build_no)
            output_dir = output_dir.replace("\\", "/")

            # -x result/output.xml -l result/log.html -r result/report.html
            shell = False
            if "Windows" in platform.platform():
                self._out_fd = codecs.open(output_dir + "/logs.log", "a+", "cp936")
                command = "pybot -d %s -L DEBUG -N %s %s/testcase.robot" % (output_dir, name, output_dir)
                shell = True
            else:
                self._out_fd = codecs.open(output_dir + "/logs.log", "a+", "utf-8")
                command = ["pybot", "-d", "%s" % output_dir, "-L", "DEBUG", "-N", "%s" % name, "%s/testcase.robot" % output_dir]
                #print(command)

            self._process = subprocess.Popen(command, shell=shell, stdout=self._out_fd, stderr=subprocess.STDOUT)
            #self._process = Process(command)

            #self._process.start()

        except Exception as e:
            print(str(e))
            pass

        return {"status": "success",
                "msg": "任务启动成功",
                "project_id": self.project_id,
                "build_no": self.build_no} 
Example 38
Project: Autoline   Author: zjh1218   File: runner.py    Apache License 2.0 5 votes vote down vote up
def run(self):
        #
        try:
            name = AutoProject.query.filter_by(id=self.project_id).first().name
            task = AutoTask(project_id=self.project_id,
                            build_no=self.build_no,
                            status="running",
                            create_author_id=current_user.get_id(),
                            create_timestamp=datetime.now())
            db.session.add(task)
            db.session.commit()

            output_dir = os.getcwd() + "/logs/%s/%s" % (self.project_id, self.build_no)
            output_dir = output_dir.replace("\\", "/")

            shell = False
            if "Windows" in platform.platform():
                self._out_fd = codecs.open(output_dir + "/logs.log", "a+", "cp936")
                command = "pybot -d %s -L DEBUG -N %s %s/testcase.robot" % (output_dir, name, output_dir)
                shell = True
            else:
                self._out_fd = codecs.open(output_dir + "/logs.log", "a+", "utf-8")
                command = ["pybot", "-d", "%s" % output_dir, "-L", "DEBUG", "-N", "%s" % name,
                           "%s/testcase.robot" % output_dir]
                # print(command)

            self._process = subprocess.Popen(command, shell=shell, stdout=self._out_fd, stderr=subprocess.STDOUT)

        except Exception as e:
            print(str(e))
            pass

        return {"status": "success",
                "msg": "任务启动成功",
                "project_id": self.project_id,
                "build_no": self.build_no} 
Example 39
Project: Autoline   Author: zjh1218   File: process.py    Apache License 2.0 5 votes vote down vote up
def start(self):
        self._out_fd, self._out_path = tempfile.mkstemp(prefix='rfproc_', suffix='.txt', text=True)
        self._out_file = open(self._out_path)
        try:
            self._process = subprocess.Popen(self._command, stdout=self._out_fd,
                                             stderr=subprocess.STDOUT)
        except OSError as err:
            self._error = str(err) 
Example 40
Project: PyPadPlusPlus   Author: bitagoras   File: pyPadHost.py    GNU General Public License v3.0 5 votes vote down vote up
def startNewKernel(self):
        self.proc = subprocess.Popen(self.StartCommand,
                        stdin=subprocess.PIPE,
                        stdout=subprocess.PIPE,
                        stderr=subprocess.STDOUT,
                        universal_newlines=True)
        time.sleep(0.1)
        self.buffer = []
        self.kernelAlive.set() 
Example 41
Project: PyPadPlusPlus   Author: bitagoras   File: pyPadRemoteHost.py    GNU General Public License v3.0 5 votes vote down vote up
def pipeStream(StartCommand):
    proc = subprocess.Popen(self.StartCommand,
                        stdin=subprocess.PIPE,
                        stdout=subprocess.PIPE,
                        stderr=subprocess.STDOUT,
                        universal_newlines=True)
    return proc 
Example 42
Project: PyPadPlusPlus   Author: bitagoras   File: pyPadRemoteHost.py    GNU General Public License v3.0 5 votes vote down vote up
def startNewKernel(self):
        #self.proc = socketStream(host="127.0.0.5", port=8888)
        self.proc = socketStream()
        # self.proc = subprocess.Popen(self.StartCommand,
                        # stdin=subprocess.PIPE,
                        # stdout=subprocess.PIPE,
                        # stderr=subprocess.STDOUT,
                        # universal_newlines=True)
        time.sleep(0.1)
        self.buffer = []
        self.kernelAlive.set() 
Example 43
Project: dynamic-training-with-apache-mxnet-on-aws   Author: awslabs   File: benchmark.py    Apache License 2.0 5 votes vote down vote up
def run(self):
        LOGGER = logging.getLogger('benchmark')
        LOGGER.info('started running %s', ' '.join(self.cmd_args))
        log_fd = open(self.logfile, 'w')
        self.process = subprocess.Popen(self.cmd_args, stdout=log_fd, stderr=subprocess.STDOUT, universal_newlines=True)
        for line in self.process.communicate():
            LOGGER.debug(line)
        log_fd.close()
        LOGGER.info('finished running %s', ' '.join(self.cmd_args)) 
Example 44
Project: dynamic-training-with-apache-mxnet-on-aws   Author: awslabs   File: benchmark.py    Apache License 2.0 5 votes vote down vote up
def stop_old_processes(hosts_file, prog_name):
    stop_args = ['python', '../../tools/kill-mxnet.py', hosts_file, 'python', prog_name]
    stop_args_str = ' '.join(stop_args)
    LOGGER.info('killing old remote processes\n %s', stop_args_str)
    stop = subprocess.check_output(stop_args, stderr=subprocess.STDOUT)
    LOGGER.debug(stop)
    time.sleep(1) 
Example 45
Project: dynamic-training-with-apache-mxnet-on-aws   Author: awslabs   File: test_docker_cache.py    Apache License 2.0 5 votes vote down vote up
def call(self, *popenargs, **kwargs):
        """
        Replace subprocess.call
        :param popenargs:
        :param timeout:
        :param kwargs:
        :return:
        """
        kwargs['stderr'] = subprocess.STDOUT
        kwargs['stdout'] = self.buf_output
        return self.old_method(*popenargs, **kwargs) 
Example 46
Project: alfred-urban-dictionary   Author: xilopaint   File: workflow.py    MIT License 5 votes vote down vote up
def _call_security(self, action, service, account, *args):
        """Call ``security`` CLI program that provides access to keychains.

        May raise `PasswordNotFound`, `PasswordExists` or `KeychainError`
        exceptions (the first two are subclasses of `KeychainError`).

        :param action: The ``security`` action to call, e.g.
                           ``add-generic-password``
        :type action: ``unicode``
        :param service: Name of the service.
        :type service: ``unicode``
        :param account: name of the account the password is for, e.g.
            "Pinboard"
        :type account: ``unicode``
        :param password: the password to secure
        :type password: ``unicode``
        :param *args: list of command line arguments to be passed to
                      ``security``
        :type *args: `list` or `tuple`
        :returns: ``(retcode, output)``. ``retcode`` is an `int`, ``output`` a
                  ``unicode`` string.
        :rtype: `tuple` (`int`, ``unicode``)

        """
        cmd = ['security', action, '-s', service, '-a', account] + list(args)
        p = subprocess.Popen(cmd, stdout=subprocess.PIPE,
                             stderr=subprocess.STDOUT)
        stdout, _ = p.communicate()
        if p.returncode == 44:  # password does not exist
            raise PasswordNotFound()
        elif p.returncode == 45:  # password already exists
            raise PasswordExists()
        elif p.returncode > 0:
            err = KeychainError('Unknown Keychain error : %s' % stdout)
            err.retcode = p.returncode
            raise err
        return stdout.strip().decode('utf-8') 
Example 47
Project: Stegall   Author: mrT4ntr4   File: stegall.py    MIT License 5 votes vote down vote up
def suggest(arg2):
	test = sb.Popen(['ls',arg2], stdout=sb.PIPE, stdin=sb.PIPE, stderr=sb.STDOUT) 
	output = test.communicate()[0]
	res = output.find('cannot access')
	if res==4:
		io_error()
		exit()

	dotLoc = arg2.find('.')
	ext = arg2[dotLoc+1::]
	print "\nSuggested Tools for "+ext+ " extension :"
	print color.OKBLUE
	if ext in ['jpeg','jpg']:
		print "  steghide  ( -sh )\n  stegsolve  ( -ss )"
	elif ext in ['png','bmp']:
		print "  zsteg  ( -zs )\n  stegsolve  ( -ss )\n  pngcheck & pngcsum ( -pc )"	
	elif ext in ['mp3','m4a','wav','ogg','flac']:
		print "  sox  ( -sx )\n  audacity  ( -ad )\n  sonicVisualizer  ( -sv )"
	elif ext == 'wav':
		print "  wavsteg  ( -ws )"	
	print color.END

	print "Universal Tools : "
	print color.OKBLUE
	print "  binwalk  ( -bw )\n  strings  ( -str )\n  exiftool  ( -ef )\n  hexcurse ( -he )"
	print color.END 
Example 48
Project: Stegall   Author: mrT4ntr4   File: zsteg.py    MIT License 5 votes vote down vote up
def launch(arg2):
	test = sb.Popen([prog,arg2], stdout=sb.PIPE, stdin=sb.PIPE, stderr=sb.STDOUT) 
	output = test.communicate()[0]
	res = 'No such file or directory' in output
	if res:
		stg.io_error()
		exit()
		return
	else:
		dotLoc = arg2.find('.')
		ext = arg2[dotLoc+1::]
		if ext in ['bmp','png']:
			yn1 = 'y'
			while yn1:
				while (yn1):
					print stg.color.OKBLUE
					yn1 = raw_input('Do you want run zsteg with additional flags (y/n)')
					print stg.color.END
					if yn1=='y':
						try:
							flags=raw_input("\nEnter the flags in a one liner\n")
							os.system(prog +' ' + arg2 +' ' + flags)	
							exit()
						except OSError as e:
							if e.errno == os.errno.ENOENT:
								stg.prog_install(prog2)
								flags=raw_input("\nEnter the flags in a one liner\n")
								os.system(prog +' ' + arg2 +' ' + flags)	
								fire()
								exit()
					elif yn1=='n':
						os.system(prog+' '+arg2)
						exit()
						
					else :
						print("Please enter a vaild choice")
				continue
		else:
			stg.io_error()
			exit() 
Example 49
Project: Stegall   Author: mrT4ntr4   File: pngcheck.py    MIT License 5 votes vote down vote up
def launch(arg2):
	test = sb.Popen([prog,arg2], stdout=sb.PIPE, stdin=sb.PIPE, stderr=sb.STDOUT) 
	output = test.communicate()[0]
	res = 'No such file or directory' in output
	if res:
		stg.io_error()
		exit()
		return
	else:
		try:
			print output
			prog2 = 'pngcsum'
			p = sb.Popen(['./'+prog2], stdout=sb.PIPE, stdin=sb.PIPE, stderr=sb.STDOUT) 
			yn1 = 'y'
			while yn1:
				while (yn1):
					yn1 = raw_input('Do you want to use '+prog2+' to correct any checksum error if present (y/n)')

					if yn1=='y':
						try:
							target_img=raw_input('\nEnter the Target Corrected Image name\n')
							os.system('./'+prog2 +' ' + arg2 +' ' + target_img)	
							exit()
						except OSError as e:
							if e.errno == os.errno.ENOENT:
								dwn_pngcsum()
								target_img=raw_input('\nEnter the Target Corrected Image name\n')
								os.system('./'+prog2 +' ' + arg2 +' ' + target_img)	
								fire()
								exit()
					elif yn1=='n':
						print stg.color.FAIL
						exit("Program Exited")
					else :
						print("Please enter a vaild choice")
				continue

		except OSError as e:
			if e.errno == os.errno.ENOENT:
				dwn_pngcsum('pngcsum') 
Example 50
Project: Stegall   Author: mrT4ntr4   File: steghide.py    MIT License 5 votes vote down vote up
def launch(arg2):
	yn1 = 'y'
	while yn1 == 'y':
		print stg.color.OKBLUE	+  "Enter the Password to demystify the image" +stg.color.END
		Pass = raw_input()
		
		try:
			p = sb.Popen([prog,'extract','-sf',arg2,'-p',Pass,'-f'], stdout=sb.PIPE, stdin=sb.PIPE, stderr=sb.STDOUT) 
			output = p.communicate()[0]
			outlist = str(output).split()
			catter = outlist[4][1:-2] 
			try: 
				with open(catter,'r') as fl:
					print stg.color.OKBLUE + "=======================================\n"
					print stg.color.OKGREEN + fl.read() + stg.color.OKBLUE + "\n=======================================" + stg.color.END
				break
			except IOError:
					print stg.color.FAIL + "	 Wrong Password :( 		"  + stg.color.END
					while (yn1!='y' or yn1!='n'):
						yn1 = raw_input('Do you want to enter password once more (y/n)')
						if yn1=='y':
							break
						elif yn1=='n':
							scrk(arg2)
							exit
						else :
							print("Please enter a vaild choice")
						continue	

		except sb.CalledProcessError as e:
			print stg.color.FAIL + "Error Hit", e.output 
Example 51
Project: Stegall   Author: mrT4ntr4   File: exiftool.py    MIT License 5 votes vote down vote up
def lauch(arg2):
	test = sb.Popen([prog,arg2], stdout=sb.PIPE, stdin=sb.PIPE, stderr=sb.STDOUT) 
	output = test.communicate()[0]
	res = output.find('File not found')
	if res==7:
		stg.io_error()
		exit()
		return
	else:
		test = sb.call([prog,arg2]) 
Example 52
Project: Stegall   Author: mrT4ntr4   File: strings.py    MIT License 5 votes vote down vote up
def strings(arg2):
	prog = 'strings'
	try :
		test = sb.Popen([prog,arg2], stdout=sb.PIPE, stdin=sb.PIPE, stderr=sb.STDOUT) 
		output = test.communicate()[0]
		res = output.find('No such file')
		if res>22:
			stg.io_error()
			exit()
			return
  		else:
			yn1 = 'y'
			while yn1:
				while (yn1):
					print stg.color.OKBLUE
					yn1 = raw_input('Do you want to search for some specific strings (y/n)')
					print stg.color.END
					if yn1=='y':
							grep_string=raw_input("\nEnter the Pattern/String to search for \n")
							os.system('strings '+arg2+' | grep '+grep_string)
							exit()
					elif yn1=='n':
						sb.call([prog,arg2])
						exit()
						
					else :
						print("Please enter a vaild choice")
				continue			 			 
		
	except OSError as e:
		if e.errno == os.errno.ENOENT:
			stg.prog_install('binutils') 
Example 53
Project: bigquerylayers   Author: smandaric   File: _cloud_sdk.py    GNU General Public License v3.0 5 votes vote down vote up
def get_project_id():
    """Gets the project ID from the Cloud SDK.

    Returns:
        Optional[str]: The project ID.
    """
    if os.name == 'nt':
        command = _CLOUD_SDK_WINDOWS_COMMAND
    else:
        command = _CLOUD_SDK_POSIX_COMMAND

    try:
        output = subprocess.check_output(
            (command,) + _CLOUD_SDK_CONFIG_COMMAND,
            stderr=subprocess.STDOUT)
    except (subprocess.CalledProcessError, OSError, IOError):
        return None

    try:
        configuration = json.loads(output.decode('utf-8'))
    except ValueError:
        return None

    try:
        return configuration['configuration']['properties']['core']['project']
    except KeyError:
        return None 
Example 54
Project: zabbix   Author: xiaomatech   File: zabbix-varnish-cache.py    MIT License 5 votes vote down vote up
def execute(command, stdin=None):
    child = subprocess.Popen(
        command,
        shell=True,
        stdout=subprocess.PIPE,
        stdin=subprocess.PIPE,
        stderr=subprocess.STDOUT)
    output = child.communicate(input=stdin)[0]
    return child.returncode, output

###############################################################################
## MAIN
############################################################################### 
Example 55
Project: chess_book_learning   Author: Aloril   File: analyse_games.py    GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, engine_executable, log_filename, syzygy_path, nodes):
        self.engine = subprocess.Popen([engine_executable], bufsize=1, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True)
        self.nodes = nodes
        if log_filename:
            self.log_out = open(log_filename, "a")
        else:
            self.log_out = None
        self.write("uci\n")
        self.write("setoption name Hash value %i\n" % HASH_SIZE)
        self.write("setoption name SyzygyPath value %s\n" % syzygy_path) 
Example 56
Project: photo4D   Author: ArcticSnow   File: Utils.py    MIT License 5 votes vote down vote up
def exec_mm3d(command, display=True):
    """
    launch a MicMac command.
    As MicMac handle errors and wait for user's input, we needed a way to carry on when a command fails
    This function will kill the process if an error happens, allowing further process to be done
    :param command: MicMac command line, string beginning with mm3d
     to see every command allowed, see https://micmac.ensg.eu/index.php/Accueil, or Github
    :param display: display or not the logs from MicMac, boolean
    :return:
    """
    if(command[:5] != "mm3d "):
        print("WARNING The command must begin with mm3d\n")
        return 0, None

    print('\n\n================================================')
    print(command)
    print('================================================\n\n')
    process = Popen(command.split(" "), stdout=PIPE, stderr=STDOUT)
    for line_bin in iter(process.stdout.readline, b''):
        try:
            line = line_bin.decode(sys.stdout.encoding)
            if display:
                sys.stdout.write(line)
            # if waiting for input, which means an error was generated
            if '(press enter)' in line:
                print("Error in MicMac process, abort process")
                process.kill()
                return 1, None
            elif 'Warn tape enter to continue' in line:
                print("Value error in Tapas, abort process")
                process.kill()
                return 1,'Value error in Tapas'
        except UnicodeDecodeError:
            sys.stdout.write('---cannot decode this line---')

    # if no error occurs
    return 0, None 
Example 57
Project: photo4D   Author: ArcticSnow   File: Utils.py    MIT License 5 votes vote down vote up
def exec_mm3d(command, display=True):
    """
    launch a MicMac command.
    As MicMac handle errors and wait for user's input, we needed a way to carry on when a command fails
    This function will kill the process if an error happens, allowing further process to be done
    :param command: MicMac command line, string beginning with mm3d
     to see every command allowed, see https://micmac.ensg.eu/index.php/Accueil, or Github
    :param display: display or not the logs from MicMac, boolean
    :return:
    """
    if(command[:5] != "mm3d "):
        print("WARNING The command must begin with mm3d\n")
        return 0, None

    process = Popen(command.split(" "), stdout=PIPE, stderr=STDOUT)
    for line_bin in iter(process.stdout.readline, b''):
        try:
            line = line_bin.decode(sys.stdout.encoding)
            if display:
                sys.stdout.write(line)
            # if waiting for input, which means an error was generated
            if '(press enter)' in line:
                print("Error in MicMac process, abort process")
                process.kill()
                return 1, None
            elif 'Warn tape enter to continue' in line:
                print("Value error in Tapas, abort process")
                process.kill()
                return 1,'Value error in Tapas'
        except UnicodeDecodeError:
            sys.stdout.write('---cannot decode this line---')

    # if no error occurs
    return 0, None 
Example 58
Project: calico-upgrade   Author: projectcalico   File: utils.py    Apache License 2.0 5 votes vote down vote up
def log_and_run(command, raise_exception_on_failure=True):
    def log_output(results):
        if results is None:
            logger.info("  # <no output>")

        lines = results.split("\n")
        for line in lines:
            logger.info("  # %s", line.rstrip())

    try:
        logger.info("%s", command)
        try:
            results = check_output(command, shell=True, stderr=STDOUT).rstrip()
        finally:
            # Restore terminal settings in case the command we ran manipulated
            # them. Note: under concurrent access, this is still not a perfect
            # solution since another thread's child process may break the
            # settings again before we log below.
            termios.tcsetattr(sys.stdin.fileno(), termios.TCSADRAIN, _term_settings)
        log_output(results)
        return results
    except CalledProcessError as e:
        # Wrap the original exception with one that gives a better error
        # message (including command output).
        logger.info("  # Return code: %s", e.returncode)
        log_output(e.output)
        if raise_exception_on_failure:
            raise e 
Example 59
Project: danube-delta   Author: honzajavorek   File: helpers.py    MIT License 5 votes vote down vote up
def run(command, env=None, bg=False, capture=False):
    options = {
        'env': dict(os.environ, **env) if env else os.environ,
        'universal_newlines': True,
    }
    if capture:
        options['stdout'] = subprocess.PIPE
        options['stderr'] = subprocess.STDOUT
    if bg:
        return subprocess.Popen(shlex.split(command), **options)

    options['check'] = True
    proc = subprocess.run(shlex.split(command), **options)
    return proc.stdout 
Example 60
Project: Old-school-processing   Author: cianfrocco-lab   File: apParam.py    MIT License 5 votes vote down vote up
def killVirtualFrameBuffer(port=None):
	### port is unknown kill all virtual frame buffers
	if port is None:
		xvfbcmd = "killall Xvfb\n"
		proc = subprocess.Popen(xvfbcmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
		proc.wait()
		return

	### find specific virtual frame buffer
	xvfbcmd = "ps -ef | grep -i xvfb | grep %d"%(port)
	proc = subprocess.Popen(xvfbcmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
	stdout = proc.stdout
	proc.wait()
	lines = proc.stdout.readlines()
	for line in lines:
		if 'Xvfb' in line:
			bits = line.strip().split()
			if len(bits) > 0:
				### kill the frame buffer
				pid = int(bits[1])
				xvfbcmd = "kill %d"%(pid)
				proc = subprocess.Popen(xvfbcmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
				proc.wait()
				### delete this file can cause problems with user permissions
				rmxfile = "rm -fv /tmp/.X11-unix/X%d"%(port)
				proc = subprocess.Popen(rmxfile, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
				proc.wait()
				apDisplay.printMsg("Killed Xvfb on port %d"%(port))
				return
	return

#===================== 
Example 61
Project: tools_python   Author: xingag   File: replace_source.py    Apache License 2.0 5 votes vote down vote up
def __re_sign(self):
        """
        重新签名
        :return:
        """
        # 重新签名
        cmd = 'jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore **.keystore -storepass ** %s **' % self.target_apk_name
        p = Popen(cmd, stdout=PIPE, stdin=PIPE, stderr=STDOUT, shell=True)

        # 输入参数
        p.communicate(input=b'nantian')
        print('第4步:重新签名成功~') 
Example 62
Project: mx   Author: graalvm   File: mx.py    GNU General Public License v2.0 5 votes vote down vote up
def _has_gzip():
        if AbstractTARDistribution.__gzip_binary is None:
            # Probe for pigz (parallel gzip) first and then try common gzip
            for binary_name in ["pigz", "gzip"]:
                gzip_ret_code = None
                try:
                    gzip_ret_code = run([binary_name, '-V'], nonZeroIsFatal=False, err=subprocess.STDOUT, out=OutputCapture())
                except OSError as e:
                    gzip_ret_code = e
                if gzip_ret_code == 0:
                    AbstractTARDistribution.__gzip_binary = binary_name
                    break
        return AbstractTARDistribution.__gzip_binary is not None 
Example 63
Project: mx   Author: graalvm   File: mx.py    GNU General Public License v2.0 5 votes vote down vote up
def root(self, directory, abortOnError=True):
        if VC._find_metadata_dir(directory, '.git'):
            if self.check_for_git(abortOnError=abortOnError):
                try:
                    out = _check_output_str(['git', 'rev-parse', '--show-toplevel'], cwd=directory, stderr=subprocess.STDOUT)
                    return out.strip()
                except subprocess.CalledProcessError:
                    if abortOnError:
                        abort('`git rev-parse --show-toplevel` (root) failed')
        elif abortOnError:
            abort('No .git directory')
        return None 
Example 64
Project: mx   Author: graalvm   File: mx.py    GNU General Public License v2.0 5 votes vote down vote up
def getKnownJavacLints(self):
        """
        Gets the lint warnings supported by this JDK.
        """
        if self._knownJavacLints is None:
            try:
                out = _check_output_str([self.javac, '-X'], stderr=subprocess.STDOUT)
            except subprocess.CalledProcessError as e:
                if e.output:
                    log(e.output)
                raise e
            if self.javaCompliance < JavaCompliance(9):
                lintre = re.compile(r"-Xlint:\{([a-z-]+(?:,[a-z-]+)*)\}")
                m = lintre.search(out)
                if not m:
                    self._knownJavacLints = []
                else:
                    self._knownJavacLints = m.group(1).split(',')
            else:
                self._knownJavacLints = []
                lines = out.split(os.linesep)
                inLintSection = False
                for line in lines:
                    if not inLintSection:
                        if '-Xmodule' in line:
                            self._javacXModuleOptionExists = True
                        elif line.strip() in ['-Xlint:key,...', '-Xlint:<key>(,<key>)*']:
                            inLintSection = True
                    else:
                        if line.startswith('         '):
                            warning = line.split()[0]
                            self._knownJavacLints.append(warning)
                            self._knownJavacLints.append('-' + warning)
                        elif line.strip().startswith('-X'):
                            return self._knownJavacLints
                warn('Did not find lint warnings in output of "javac -X"')
        return self._knownJavacLints 
Example 65
Project: mx   Author: graalvm   File: mx.py    GNU General Public License v2.0 5 votes vote down vote up
def gmake_cmd():
    global _gmake_cmd
    if _gmake_cmd == '<uninitialized>':
        for a in ['make', 'gmake', 'gnumake']:
            try:
                output = _check_output_str([a, '--version'], stderr=subprocess.STDOUT)
                if 'GNU' in output:
                    _gmake_cmd = a
                    break
            except:
                pass
        if _gmake_cmd == '<uninitialized>':
            abort('Could not find a GNU make executable on the current path.')
    return _gmake_cmd 
Example 66
Project: wemoo-center   Author: wemoo   File: shell.py    MIT License 5 votes vote down vote up
def execute(cmd, timeout=5):
    try:
        output = check_output(cmd, stderr=STDOUT, timeout=timeout, shell=True)
        return output
    except TimeoutExpired:
        return False 
Example 67
Project: presto-admin   Author: prestosql   File: release.py    Apache License 2.0 5 votes vote down vote up
def _run_pypi_command(self, command):
        try:
            output = subprocess.check_output(command, stderr=subprocess.STDOUT)
        except subprocess.CalledProcessError as e:
            print e.output
            raise
        if PypiReleaser._check_pypi_success(output):
            return True
        else:
            print output
            return False 
Example 68
Project: presto-admin   Author: prestosql   File: test_authentication.py    Apache License 2.0 5 votes vote down vote up
def test_prestoadmin_no_sudo_popen(self):
        self.upload_topology()
        self.setup_for_catalog_add()

        # We use Popen because docker-py loses the first 8 characters of TTY
        # output.
        args = ['docker', 'exec', '-t', self.cluster.master, 'sudo',
                '-u', 'app-admin', get_presto_admin_path(),
                'topology show']
        proc = subprocess.Popen(args, stdout=subprocess.PIPE,
                                stderr=subprocess.STDOUT)
        self.assertRegexpMatchesLineByLine(
            'Please run presto-admin with sudo.\n'
            '\\[Errno 13\\] Permission denied: \'.*/.prestoadmin/log'
            'presto-admin.log\'', proc.stdout.read()) 
Example 69
Project: cloudygo   Author: sethtroisi   File: sgf_utils.py    Apache License 2.0 5 votes vote down vote up
def board_png(board_size, setup, data, filename=None,
              include_move=True, is_pv=False, force_refresh=False):
    sgf = commented_squares(board_size, setup, data, include_move, is_pv)
    if filename:
        if force_refresh or not os.path.exists(filename):
            try:
                p = Popen(['sgftopng', filename, '-coord', '-nonrs'],
                          stdout=PIPE, stdin=PIPE, stderr=STDOUT)
                sgf_to_png_stdout = p.communicate(input=sgf.encode('utf-8'))[0]
            except FileNotFoundError:
                # sgftopng not found.
                # model page and model-graphs won't have pretty images.
                pass
    return sgf 
Example 70
Project: operator-courier   Author: operator-framework   File: test_verify.py    Apache License 2.0 5 votes vote down vote up
def test_verify_valid_sources(source_dir):
    process = subprocess.Popen(f'operator-courier verify {source_dir}',
                               shell=True,
                               stdout=subprocess.PIPE,
                               stderr=subprocess.STDOUT)
    exit_code = process.wait()
    assert exit_code == 0 
Example 71
Project: operator-courier   Author: operator-framework   File: test_verify.py    Apache License 2.0 5 votes vote down vote up
def test_verify_valid_nested_sources(source_dir):
    process = subprocess.Popen(f'operator-courier verify {source_dir}',
                               shell=True,
                               stdout=subprocess.PIPE,
                               stderr=subprocess.STDOUT)
    exit_code = process.wait()
    assert exit_code == 0 
Example 72
Project: operator-courier   Author: operator-framework   File: test_verify.py    Apache License 2.0 5 votes vote down vote up
def test_verify_invalid_nested_sources(source_dir):
    process = subprocess.Popen(f'operator-courier verify {source_dir}',
                               shell=True,
                               stdout=subprocess.PIPE,
                               stderr=subprocess.STDOUT)
    exit_code = process.wait()
    assert exit_code != 0 
Example 73
Project: operator-courier   Author: operator-framework   File: test_verify.py    Apache License 2.0 5 votes vote down vote up
def test_verify_valid_nested_sources_with_output(source_dir):
    with TemporaryDirectory() as temp_dir:
        outfile_path = join(temp_dir, "output.json")
        process = subprocess.Popen(f'operator-courier verify {source_dir} '
                                   f'--validation-output {outfile_path}',
                                   shell=True,
                                   stdout=subprocess.PIPE,
                                   stderr=subprocess.STDOUT)
        exit_code = process.wait()
        assert isfile(outfile_path)
        with open(outfile_path, 'r') as f:
            validation_json = loads(f.read())

    assert exit_code == 0
    assert not validation_json['errors'] 
Example 74
Project: pyblish-win   Author: pyblish   File: platform.py    GNU Lesser General Public License v3.0 4 votes vote down vote up
def _syscmd_file(target,default=''):

    """ Interface to the system's file command.

        The function uses the -b option of the file command to have it
        ommit the filename in its output and if possible the -L option
        to have the command follow symlinks. It returns default in
        case the command should fail.

    """

    # We do the import here to avoid a bootstrap issue.
    # See c73b90b6dadd changeset.
    #
    # [..]
    # ranlib libpython2.7.a
    # gcc   -o python \
    #        Modules/python.o \
    #        libpython2.7.a -lsocket -lnsl -ldl    -lm
    # Traceback (most recent call last):
    #  File "./setup.py", line 8, in <module>
    #    from platform import machine as platform_machine
    #  File "[..]/build/Lib/platform.py", line 116, in <module>
    #    import sys,string,os,re,subprocess
    #  File "[..]/build/Lib/subprocess.py", line 429, in <module>
    #    import select
    # ImportError: No module named select

    import subprocess

    if sys.platform in ('dos','win32','win16','os2'):
        # XXX Others too ?
        return default
    target = _follow_symlinks(target)
    try:
        proc = subprocess.Popen(['file', target],
                stdout=subprocess.PIPE, stderr=subprocess.STDOUT)

    except (AttributeError,os.error):
        return default
    output = proc.communicate()[0]
    rc = proc.wait()
    if not output or rc:
        return default
    else:
        return output

### Information about the used architecture

# Default values for architecture; non-empty strings override the
# defaults given as parameters 
Example 75
Project: utilities   Author: czbiohub   File: velocyto.py    MIT License 4 votes vote down vote up
def run_sample(
    sample_key,
    mask_path,
    gtf_path,
    s3_input_bucket,
    s3_output_bucket,
    s3_output_prefix,
    run_dir,
    logger,
):

    t_config = TransferConfig(num_download_attempts=25)

    sample_name = os.path.basename(sample_key)
    sample_id = sample_name.split(".")[0]  # this is brittle!
    local_sample = os.path.join(run_dir, "input", sample_name)

    s3c.download_file(
        Bucket=s3_input_bucket, Key=sample_key, Filename=local_sample, Config=t_config
    )

    veloctyo_command = [
        "velocyto",
        "run-smartseq2",
        "-o",
        run_dir,
        "-m",
        mask_path,
        "-e",
        sample_id,
        local_sample,
        gtf_path,
    ]

    if ut_log.log_command(
        logger,
        veloctyo_command,
        stdout=subprocess.PIPE,
        stderr=subprocess.STDOUT,
        shell=True,
    ):
        logger.info(f"velocyto failed on {sample_id}")
        os.remove(local_sample)
        return

    output_file = os.path.join(run_dir, f"{sample_id}.loom")

    logger.info("Uploading {}".format(output_file))
    time.sleep(10)
    s3c.upload_file(
        Filename=output_file,
        Bucket=s3_output_bucket,
        Key=os.path.join(s3_output_prefix, f"{sample_id}.loom"),
        Config=t_config,
    )

    os.remove(local_sample)
    os.remove(output_file) 
Example 76
Project: sync_github_bot   Author: Ladsgroup   File: sync_github_bot.py    MIT License 4 votes vote down vote up
def create_commit(self, pull_request, password):
        folder_name = '%s-%s' % (
            pull_request.project.gerrit_name,
            pull_request.number)
        folder_name = folder_name.replace('/', '-')
        print('ssh://%[email protected]:29418/%s.git'
              % (bot.username, pull_request.project.gerrit_name))
        p = Popen(
            ['git',
             'clone',
             'ssh://%[email protected]:29418/%s.git'
                % (bot.username, pull_request.project.gerrit_name),
             '%s/%s' % (_program_dir, folder_name)],
            cwd=_program_dir,
            stdout=PIPE, stdin=PIPE, stderr=STDOUT)
        out = p.communicate(input=bytes(password, 'utf-8'))[0]
        print(out)
        patch_url = pull_request.patch_url
        patch_filename, headers = urlretrieve(patch_url)
        p2 = Popen(
            ['git',
             'apply',
             os.path.join(_program_dir, patch_filename)],
            cwd=os.path.join(_program_dir, folder_name),
            stdout=PIPE, stderr=PIPE).stdout.read()
        print(p2)
        commit_message = '%s\n\n%s\n[Duplicated from: %s by bot]' \
            % (pull_request.title, pull_request.body, pull_request.html_url)
        with open('%s/commit_message' % _program_dir, 'w') as f:
            f.write(commit_message)
        p3 = Popen(
            ['git', 'commit', '-a', '-F', '../commit_message'],
            cwd=os.path.join(_program_dir, folder_name),
            stdout=PIPE, stderr=PIPE).stdout.read()
        print(p3)
        p31 = Popen(
            ['git', 'config', '--global', 'gitreview.username', bot.username],
            cwd=_program_dir,
            stdout=PIPE, stderr=PIPE).stdout.read()
        print(p31)
        p32 = Popen(
            ['git', 'config', '--global',
             'user.email', '[email protected]'],
            cwd=_program_dir,
            stdout=PIPE, stderr=PIPE).stdout.read()
        print(p32)
        p4 = Popen(
            ['git', 'review', '-R'],
            cwd=os.path.join(_program_dir, folder_name),
            stdout=PIPE, stdin=PIPE, stderr=PIPE)
        p4 = p4.communicate(input=bytes('yes', 'utf-8'))
        print(p4) 
Example 77
Project: Stegall   Author: mrT4ntr4   File: binw.py    MIT License 4 votes vote down vote up
def launch(arg2):
	test = sb.Popen([prog,arg2], stdout=sb.PIPE, stdin=sb.PIPE, stderr=sb.STDOUT) 
	output = test.communicate()[0]
	res = output.find('Cannot open file')
	if res==16:
		stg.io_error()
		exit()
		return
	else:
		test = sb.call([prog,'-e',arg2])
		print "Embedded Files :"
		folder = '_'+arg2+'.extracted'
		overwrite = '_'+arg2+'-0.extracted'
		if os.path.isdir(overwrite):
			os.system('mv '+overwrite+'/* '+folder+'/ && rm -rf '+overwrite)
		if os.path.isdir(folder):
			os.system('cd '+folder+'&&'+'file *')

		print "" ;stg.spinning_cursor(); print ""
		print "Contents have been saved to " + stg.color.OKYELLOW +folder+ stg.color.END + " in my directory :)\n"

		if os.path.isdir(folder):
		 for file in os.listdir(folder):
			path = folder+'/'+file
			chk_ext = magic.from_file(path, mime=True)
			if (chk_ext=='text/plain'):
				print stg.color.OKBLUE + "Reading txt's if present" + stg.color.END
				print "----------"+file+"----------";os.system('cat ' + path);print "\n---------------------------\n";
			elif (chk_ext=='application/zip'):
				zf = zipfile.ZipFile(path)
				try:
					zf.testzip()
				except RuntimeError as e:
					if 'encrypted' in str(e):
						print 'H0ly Sh1t !!, '+file+' is encrypted!'
						try:
							prog2 = 'fcrackzip'
							p = sb.Popen([prog2,'--version'], stdout=sb.PIPE, stdin=sb.PIPE, stderr=sb.STDOUT) 
							yn1 = 'y'
							while yn1:
								while (yn1):
									yn1 = raw_input('Do you want to crack this zip with fcrackzip (y/n)')
									if yn1=='y':
											wordlist=raw_input('\nEnter the location of your wordlist\n')
											os.system(prog2 + ' -u -D -p '+ wordlist +' ' + path)	
											exit()
									elif yn1=='n':
										print stg.color.FAIL
										exit("Program Exited")
									else :
										print("Please enter a vaild choice")
								continue
						except OSError as e:
							if e.errno == os.errno.ENOENT:
								stg.prog_install(prog2)

		else:
		 print "\nNo Embedded Files Found.." 
Example 78
Project: obi   Author: felipemfp   File: testes.py    MIT License 4 votes vote down vote up
def get_tests_results(question):
    global cache

    main_folder = question
    script = '{0}.py'.format(question)
    hasher = hashlib.md5()
    with open(script, 'rb') as f:
        hasher.update(f.read())
    hsh = hasher.digest()
    hashes.append(hsh)
    if hsh in cache:
        return cache[hsh]

    tests = os.listdir(main_folder)
    right_answers = 0
    total_answers = 0

    for test_folder in tests:
        test_folder_name = '{0}/{1}'.format(main_folder, test_folder)
        tests_cases = [case for case in os.listdir(test_folder_name)]
        for index in range(int(len(tests_cases) / 2)):
            case_input_name = '{0}/{1}{2}'.format(
                test_folder_name, 'in', index + 1)
            case_output_name = '{0}/{1}{2}'.format(
                test_folder_name, 'out', index + 1)
            input_file = open(case_input_name)
            proc = subprocess.Popen(
                ['python', script], stdin=input_file,
                stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
            result = proc.communicate()[0].decode('utf-8').strip()
            output_file = open(case_output_name).read().strip()

            if result == output_file:
                right_answers += 1
                sys.stdout.write('.')
            else:
                sys.stdout.write('F')
            total_answers += 1
            sys.stdout.flush()

    cache[hsh] = (right_answers, total_answers)
    return cache[hsh] 
Example 79
Project: GCN-SeA   Author: sumanbanerjee1   File: bleu.py    Apache License 2.0 4 votes vote down vote up
def moses_multi_bleu(hypotheses, references, lowercase=False):
  """Calculate the bleu score for hypotheses and references
  using the MOSES ulti-bleu.perl script.

  Args:
    hypotheses: A numpy array of strings where each string is a single example.
    references: A numpy array of strings where each string is a single example.
    lowercase: If true, pass the "-lc" flag to the multi-bleu script

  Returns:
    The BLEU score as a float32 value.
  """

  if np.size(hypotheses) == 0:
    return np.float32(0.0)

  # Get MOSES multi-bleu script
  try:
    multi_bleu_path, _ = urllib.request.urlretrieve(
        "https://raw.githubusercontent.com/moses-smt/mosesdecoder/"
        "master/scripts/generic/multi-bleu.perl")
    os.chmod(multi_bleu_path, 0o755)
  except: #pylint: disable=W0702
    tf.logging.info("Unable to fetch multi-bleu.perl script, using local.")
    metrics_dir = os.path.dirname(os.path.realpath(__file__))
    bin_dir = os.path.abspath(os.path.join(metrics_dir, "..", "..", "bin"))
    multi_bleu_path = os.path.join(bin_dir, "tools/multi-bleu.perl")

  # Dump hypotheses and references to tempfiles
  hypothesis_file = tempfile.NamedTemporaryFile()
  hypothesis_file.write("\n".join(hypotheses).encode("utf-8"))
  hypothesis_file.write(b"\n")
  hypothesis_file.flush()
  reference_file = tempfile.NamedTemporaryFile()
  reference_file.write("\n".join(references).encode("utf-8"))
  reference_file.write(b"\n")
  reference_file.flush()

  # Calculate BLEU using multi-bleu script
  with open(hypothesis_file.name, "r") as read_pred:
    bleu_cmd = [multi_bleu_path]
    if lowercase:
      bleu_cmd += ["-lc"]
    bleu_cmd += [reference_file.name]
    try:
      bleu_out = subprocess.check_output(
          bleu_cmd, stdin=read_pred, stderr=subprocess.STDOUT)
      bleu_out = bleu_out.decode("utf-8")
      bleu_score = re.search(r"BLEU = (.+?),", bleu_out).group(1)
      bleu_score = float(bleu_score)
    except subprocess.CalledProcessError as error:
      if error.output is not None:
        tf.logging.warning("multi-bleu.perl script returned non-zero exit code")
        tf.logging.warning(error.output)
      bleu_score = np.float32(0.0)

  # Close temp files
  hypothesis_file.close()
  reference_file.close()

  return np.float32(bleu_score) 
Example 80
Project: deb-python-cassandra-driver   Author: openstack   File: setup.py    Apache License 2.0 4 votes vote down vote up
def run(self):
        if self.test:
            path = "docs/_build/doctest"
            mode = "doctest"
        else:
            path = "docs/_build/%s" % __version__
            mode = "html"

            try:
                os.makedirs(path)
            except:
                pass

        if has_subprocess:
            # Prevent run with in-place extensions because cython-generated objects do not carry docstrings
            # http://docs.cython.org/src/userguide/special_methods.html#docstrings
            import glob
            for f in glob.glob("cassandra/*.so"):
                print("Removing '%s' to allow docs to run on pure python modules." %(f,))
                os.unlink(f)

            # Build io extension to make import and docstrings work
            try:
                output = subprocess.check_output(
                    ["python", "setup.py", "build_ext", "--inplace", "--force", "--no-murmur3", "--no-cython"],
                    stderr=subprocess.STDOUT)
            except subprocess.CalledProcessError as exc:
                raise RuntimeError("Documentation step '%s' failed: %s: %s" % ("build_ext", exc, exc.output))
            else:
                print(output)

            try:
                output = subprocess.check_output(
                    ["sphinx-build", "-b", mode, "docs", path],
                    stderr=subprocess.STDOUT)
            except subprocess.CalledProcessError as exc:
                raise RuntimeError("Documentation step '%s' failed: %s: %s" % (mode, exc, exc.output))
            else:
                print(output)

            print("")
            print("Documentation step '%s' performed, results here:" % mode)
            print("   file://%s/%s/index.html" % (os.path.dirname(os.path.realpath(__file__)), path))