Python subprocess.Popen() Examples

The following are code examples for showing how to use subprocess.Popen(). 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: pyblish-win   Author: pyblish   File: test_threading.py    GNU Lesser General Public License v3.0 11 votes vote down vote up
def _run_and_join(self, script):
        script = """if 1:
            import sys, os, time, threading

            # a thread, which waits for the main program to terminate
            def joiningfunc(mainthread):
                mainthread.join()
                print 'end of thread'
        \n""" + script

        p = subprocess.Popen([sys.executable, "-c", script], stdout=subprocess.PIPE)
        rc = p.wait()
        data = p.stdout.read().replace('\r', '')
        p.stdout.close()
        self.assertEqual(data, "end of main\nend of thread\n")
        self.assertFalse(rc == 2, "interpreter was blocked")
        self.assertTrue(rc == 0, "Unexpected error") 
Example 2
Project: fs_image   Author: facebookincubator   File: yum_dnf_from_snapshot.py    MIT License 10 votes vote down vote up
def _repo_server(
    repo_server_bin: Path,
    sock: socket.socket,
    storage_cfg: str,
    snapshot_dir: Path
):
    '''
    Invokes `repo-server` with the given storage & snapshot; passes it
    ownership of the bound TCP socket -- it listens & accepts connections.
    '''
    # This could be a thread, but it's probably not worth the risks
    # involved in mixing threads & subprocess (yes, lots of programs do,
    # but yes, far fewer do it safely).
    with sock, subprocess.Popen([
        repo_server_bin,
        '--socket-fd', str(sock.fileno()),
        '--storage', storage_cfg,
        '--snapshot-dir', snapshot_dir,
    ], pass_fds=[sock.fileno()]) as server_proc:
        try:
            yield server_proc
        finally:
            server_proc.kill()  # It's a read-only proxy, abort ASAP 
Example 3
Project: NordVPN-NetworkManager-Gui   Author: vfosterm   File: nord_nm_gui.py    GNU General Public License v3.0 8 votes vote down vote up
def check_sudo(self):
        """
        Checks validity of sudo password

        :return: True if valid False if invalid
        """
        self.sudo_password = self.sudo_dialog.sudo_password.text()
        try:
            p1 = subprocess.Popen(['echo', self.sudo_password], stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE)
            p2 = subprocess.Popen(['sudo', '-S', 'whoami'], stdin=p1.stdout, stdout=subprocess.PIPE, encoding='utf-8', stderr=subprocess.PIPE)
            p1.stdout.close()
            output = p2.communicate()[0].strip()
            p2.stdout.close()

            if "root" in output:
                self.sudo_dialog.close()
                print('True')
                return True
            else:
                error = QtWidgets.QErrorMessage(self.sudo_dialog)
                error.showMessage("Invalid Password")
                return False
        except Exception as ex:
            print('failed', ex)
            self.sudo_password = None 
Example 4
Project: badge-o-matic   Author: markuslindenberg   File: online.py    BSD 2-Clause "Simplified" License 7 votes vote down vote up
def main():
	pdf = BytesIO()

	# Find all IPv6 addresses with global scope
	ips = [dict(a['attrs'])['IFA_ADDRESS'] for a in IPRoute().get_addr() if a['scope'] == 0 and a['family'] == 10]

	if ips:
		c = Canvas(pdf, pagesize=(PAGE_W, PAGE_H))
		hello(c, 'http://[%s]/badge' % ips[0])
	else:
		# ToDo: Legacy IP bild
		c = Canvas(pdf, pagesize=(PAGE_W, PAGE_H))
		
	c.showPage()
	c.save()
	lpr = subprocess.Popen(['lpr', '-P', PRINTER], stdin=subprocess.PIPE)
	lpr.communicate(pdf.getvalue())
	if lpr.returncode != 0:
		pass 
Example 5
Project: NordVPN-NetworkManager-Gui   Author: vfosterm   File: nord_nm_gui.py    GNU General Public License v3.0 7 votes vote down vote up
def enable_ipv6(self):
        """
        Re-enables ipv6 system wide
        """
        if self.sudo_password:
            try:
                p1 = subprocess.Popen(['echo', self.sudo_password], stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
                p2 = subprocess.Popen(['sudo', '-S', 'sysctl', '-w', 'net.ipv6.conf.all.disable_ipv6=0', '&&', 'sysctl', '-w', 'net.ipv6.conf.default.disable_ipv6=0'], stdin=p1.stdout, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
                p1.stdout.close()
                p2.stdout.close()
            except subprocess.CalledProcessError:
                self.statusbar.showMessage("ERROR: disabling IPV6 failed", 2000)
        else:
            self.sudo_dialog = self.get_sudo()
            self.sudo_dialog.text_label.setText("<html><head/><body><p>VPN Network Manager requires <span style=\" font-weight:600;\">sudo</span> permissions in order to enable IPV6. Please input the <span style=\" font-weight:600;\">sudo</span> Password or run the program with elevated priveledges.</p></body></html>")
            self.sudo_dialog.exec_()

            if self.sudo_password:
                try:
                    p1 = subprocess.Popen(['echo', self.sudo_password], stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
                    p2 = subprocess.Popen(['sudo', '-S', 'sysctl', '-w', 'net.ipv6.conf.all.disable_ipv6=0', '&&', 'sysctl', '-w', 'net.ipv6.conf.default.disable_ipv6=0'], stdin=p1.stdout, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
                    p1.stdout.close()
                    p2.stdout.close()
                except subprocess.CalledProcessError:
                    self.statusbar.showMessage("ERROR: Enabling IPV6 failed", 2000) 
Example 6
Project: fs_image   Author: facebookincubator   File: common.py    MIT License 6 votes vote down vote up
def check_popen_returncode(proc: subprocess.Popen):
    if proc.returncode != 0:  # pragma: no cover
        # Providing a meaningful coverage test for this is annoying, so I just
        # tested manually:
        #   >>> import subprocess
        #   >>> raise subprocess.CalledProcessError(returncode=5, cmd=['a'])
        #   Traceback (most recent call last):
        #     File "<stdin>", line 1, in <module>
        #   subprocess.CalledProcessError: Command '['a']' returned non-zero
        #   exit status 5.
        raise subprocess.CalledProcessError(
            returncode=proc.returncode, cmd=proc.args,
        ) 
Example 7
Project: fs_image   Author: facebookincubator   File: test_unshare.py    MIT License 6 votes vote down vote up
def _popen_sleep_forever(self, unshare: Unshare):
        # We need the ready signal to know when we've actually executed the
        # payload -- otherwise, we might try to interact with it while we're
        # still at `nsenter`.
        proc = subprocess.Popen(nsenter_as_user(
            unshare, 'bash', '-uec', 'echo ready $$ ; exec sleep infinity',
        ), stdout=subprocess.PIPE)

        # Wait for the child to start
        ready_and_pid = proc.stdout.readline().split(b' ')
        self.assertEqual(b'ready', ready_and_pid[0])

        proc.stdout.close()  # `sudo` keeps stdout open, but will not write.
        # Returning the PID lets us clean up the `sleep infinity` when it is
        # not inside a PID namespace.
        return proc, int(ready_and_pid[1]) 
Example 8
Project: webnull   Author: macrael   File: test_integration.py    BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def run_test_command(self, cmd):
        observer = FSEventsObserver()
        shared_queue = Queue.Queue()
        handler = ChangeHandler(shared_queue)
        observer.schedule(handler, self.temp_dir)
        observer.start()

        args = ['./webnull.py'] + cmd
        env = os.environ.copy()
        if 'DEV_MODE' in env:
            del env['DEV_MODE']
        env['HOSTFILE_PATH'] = self.hostfile_path
        env['TEST_DURATION'] = '0.02'
        process = subprocess.Popen(args, env=env)
        process.wait()
        time.sleep(.09) # if we just quit, the observer doesn't see the final file action.

        bodies = []
        while not shared_queue.empty():
            bodies.append(shared_queue.get())

        observer.stop()
        observer.join()
        return bodies 
Example 9
Project: autofff   Author: ChiefGokhlayeh   File: scanner.py    MIT License 6 votes vote down vote up
def _preprocess_file(self, filename: str, cpp_path: str = 'cpp', cpp_args: str = '') -> str:
        path_list = [cpp_path]
        if isinstance(cpp_args, list):
            path_list += cpp_args
        elif cpp_args != '':
            path_list += [cpp_args]
        path_list += [filename]

        try:
            # Note the use of universal_newlines to treat all newlines
            # as \n for Python's purpose
            #
            pipe = subprocess.Popen(path_list,
                                    stdout=subprocess.PIPE,
                                    universal_newlines=True)
            text = pipe.communicate()[0]
        except OSError as e:
            raise RuntimeError("Unable to invoke 'cpp'.  " +
                               'Make sure its path was passed correctly\n' +
                               ('Original error: %s' % e))

        filteredText = self.ignorePattern.sub('', text)

        return filteredText 
Example 10
Project: pyblish-win   Author: pyblish   File: patchcheck.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def changed_files():
    """Get the list of changed or added files from the VCS."""
    if os.path.isdir(os.path.join(SRCDIR, '.hg')):
        vcs = 'hg'
        cmd = 'hg status --added --modified --no-status'
        if mq_patches_applied():
            cmd += ' --rev qparent'
    elif os.path.isdir('.svn'):
        vcs = 'svn'
        cmd = 'svn status --quiet --non-interactive --ignore-externals'
    else:
        sys.exit('need a checkout to get modified files')

    st = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE)
    try:
        st.wait()
        if vcs == 'hg':
            return [x.decode().rstrip() for x in st.stdout]
        else:
            output = (x.decode().rstrip().rsplit(None, 1)[-1]
                      for x in st.stdout if x[0] in 'AM')
        return set(path for path in output if os.path.isfile(path))
    finally:
        st.stdout.close() 
Example 11
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 12
Project: pyblish-win   Author: pyblish   File: __init__.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def wait(self, timeout=None):
        """Wait for process to terminate and, if process is a children
        of os.getpid(), also return its exit code, else None.

        If the process is already terminated immediately return None
        instead of raising NoSuchProcess.

        If timeout (in seconds) is specified and process is still alive
        raise TimeoutExpired.

        To wait for multiple Process(es) use psutil.wait_procs().
        """
        if timeout is not None and not timeout >= 0:
            raise ValueError("timeout must be a positive integer")
        return self._proc.wait(timeout)


# =====================================================================
# --- Popen class
# ===================================================================== 
Example 13
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 14
Project: pyblish-win   Author: pyblish   File: test_gdb.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def run_gdb(*args, **env_vars):
    """Runs gdb in batch mode with the additional arguments given by *args.

    Returns its (stdout, stderr)
    """
    if env_vars:
        env = os.environ.copy()
        env.update(env_vars)
    else:
        env = None
    # -nx: Do not execute commands from any .gdbinit initialization files
    #      (issue #22188)
    base_cmd = ('gdb', '--batch', '-nx')
    if (gdb_major_version, gdb_minor_version) >= (7, 4):
        base_cmd += ('-iex', 'add-auto-load-safe-path ' + checkout_hook_path)
    out, err = subprocess.Popen(base_cmd + args,
        stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env,
        ).communicate()
    return out, err

# Verify that "gdb" was built with the embedded python support enabled: 
Example 15
Project: pyblish-win   Author: pyblish   File: test_threading.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def test_join_nondaemon_on_shutdown(self):
        # Issue 1722344
        # Raising SystemExit skipped threading._shutdown
        p = subprocess.Popen([sys.executable, "-c", """if 1:
                import threading
                from time import sleep

                def child():
                    sleep(1)
                    # As a non-daemon thread we SHOULD wake up and nothing
                    # should be torn down yet
                    print "Woke up, sleep function is:", sleep

                threading.Thread(target=child).start()
                raise SystemExit
            """],
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE)
        self.addCleanup(p.stdout.close)
        self.addCleanup(p.stderr.close)
        stdout, stderr = p.communicate()
        self.assertEqual(stdout.strip(),
            "Woke up, sleep function is: <built-in function sleep>")
        stderr = re.sub(r"^\[\d+ refs\]", "", stderr, re.MULTILINE).strip()
        self.assertEqual(stderr, "") 
Example 16
Project: pyblish-win   Author: pyblish   File: script_helper.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def _assert_python(expected_success, *args, **env_vars):
    cmd_line = [sys.executable]
    if not env_vars:
        cmd_line.append('-E')
    cmd_line.extend(args)
    # Need to preserve the original environment, for in-place testing of
    # shared library builds.
    env = os.environ.copy()
    env.update(env_vars)
    p = subprocess.Popen(cmd_line, stdin=subprocess.PIPE,
                         stdout=subprocess.PIPE, stderr=subprocess.PIPE,
                         env=env)
    try:
        out, err = p.communicate()
    finally:
        subprocess._cleanup()
        p.stdout.close()
        p.stderr.close()
    rc = p.returncode
    err =  strip_python_stderr(err)
    if (rc and expected_success) or (not rc and not expected_success):
        raise AssertionError(
            "Process return code is %d, "
            "stderr follows:\n%s" % (rc, err.decode('ascii', 'ignore')))
    return rc, out, err 
Example 17
Project: pyblish-win   Author: pyblish   File: test_unicodedata.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def test_failed_import_during_compiling(self):
        # Issue 4367
        # Decoding \N escapes requires the unicodedata module. If it can't be
        # imported, we shouldn't segfault.

        # This program should raise a SyntaxError in the eval.
        code = "import sys;" \
            "sys.modules['unicodedata'] = None;" \
            """eval("u'\N{SOFT HYPHEN}'")"""
        args = [sys.executable, "-c", code]
        # We use a subprocess because the unicodedata module may already have
        # been loaded in this process.
        popen = subprocess.Popen(args, stderr=subprocess.PIPE)
        popen.wait()
        self.assertEqual(popen.returncode, 1)
        error = "SyntaxError: (unicode error) \N escapes not supported " \
            "(can't load unicodedata module)"
        self.assertIn(error, popen.stderr.read()) 
Example 18
Project: pyblish-win   Author: pyblish   File: test_sys.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def test_ioencoding(self):
        import subprocess
        env = dict(os.environ)

        # Test character: cent sign, encoded as 0x4A (ASCII J) in CP424,
        # not representable in ASCII.

        env["PYTHONIOENCODING"] = "cp424"
        p = subprocess.Popen([sys.executable, "-c", 'print unichr(0xa2)'],
                             stdout = subprocess.PIPE, env=env)
        out = p.communicate()[0].strip()
        self.assertEqual(out, unichr(0xa2).encode("cp424"))

        env["PYTHONIOENCODING"] = "ascii:replace"
        p = subprocess.Popen([sys.executable, "-c", 'print unichr(0xa2)'],
                             stdout = subprocess.PIPE, env=env)
        out = p.communicate()[0].strip()
        self.assertEqual(out, '?') 
Example 19
Project: pyblish-win   Author: pyblish   File: test_sys.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def test_executable(self):
        # sys.executable should be absolute
        self.assertEqual(os.path.abspath(sys.executable), sys.executable)

        # Issue #7774: Ensure that sys.executable is an empty string if argv[0]
        # has been set to an non existent program name and Python is unable to
        # retrieve the real program name
        import subprocess
        # For a normal installation, it should work without 'cwd'
        # argument. For test runs in the build directory, see #7774.
        python_dir = os.path.dirname(os.path.realpath(sys.executable))
        p = subprocess.Popen(
            ["nonexistent", "-c", 'import sys; print repr(sys.executable)'],
            executable=sys.executable, stdout=subprocess.PIPE, cwd=python_dir)
        executable = p.communicate()[0].strip()
        p.wait()
        self.assertIn(executable, ["''", repr(sys.executable)]) 
Example 20
Project: badge-o-matic   Author: markuslindenberg   File: webapp.py    BSD 2-Clause "Simplified" License 5 votes vote down vote up
def _print(pdfdata):
    if app.config['DEBUG']:
        app.logger.info('printing to /tmp/out.pdf')
        open('/tmp/out.pdf', 'wb').write(pdfdata)
    else:
        lpr = subprocess.Popen(['lpr', '-P', PRINTER], stdin=subprocess.PIPE)
        lpr.communicate(pdfdata) 
Example 21
Project: UrsaRobotics_SmartHome   Author: stav98   File: speech_recogn.py    GNU General Public License v3.0 5 votes vote down vote up
def speakSpeechFromText(phrase):
    if voice == 'ONLINE':
        googleSpeechURL = getGoogleSpeechURL(phrase)
        print(googleSpeechURL)
        subprocess.call(["mplayer", "-af", "volume=5", googleSpeechURL], shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        #subprocess.Popen(["mplayer", "-really-quiet", googleSpeechURL], shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    elif voice == 'OFFLINE':
        subprocess.call(["espeak", "-vel+m5", "-s 130", phrase], shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE) 
Example 22
Project: UrsaRobotics_SmartHome   Author: stav98   File: tts.py    GNU General Public License v3.0 5 votes vote down vote up
def speakSpeechFromText(phrase):
    #Αν είναι ONLINE στέλνει ένα URL στην Google και αυτή επιστρέφει αρχείο ήχου, το οποίο παίζει με το Mplayer
    if voice == 'ONLINE':
        googleSpeechURL = getGoogleSpeechURL(phrase)
        #print(googleSpeechURL)  #Debug
        subprocess.call(["mplayer", "-af", "volume=5", googleSpeechURL], shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        #subprocess.Popen(["mplayer", "-really-quiet", googleSpeechURL], shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    #Αν είναι OFFLINE τότε καλεί το espeak
    elif voice == 'OFFLINE':
        subprocess.call(["espeak", "-vel+m5", "-s 130", phrase], shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

#Για δημοσίευση εντολών 
Example 23
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 24
Project: NordVPN-NetworkManager-Gui   Author: vfosterm   File: nord_nm_gui.py    GNU General Public License v3.0 5 votes vote down vote up
def set_auto_connect(self):
        """
        Generates auto_connect bash script and moves it to the NetworkManager
        """
        self.config.read(self.conf_path)
        interfaces = self.get_interfaces()
        if interfaces:
            interface_string = '|'.join(interfaces)
            script =(
            '#!/bin/bash\n\n'
            'if [[ "$1" =~ ' + interface_string + ' ]] && [[ "$2" =~ up|connectivity-change ]]; then\n'
            '  nmcli con up id "' + self.generate_connection_name() + '"\n'
            'fi\n'
            )
        try:
            with open(os.path.join(self.scripts_path, 'auto_connect'), 'w') as auto_connect:
                print(script, file=auto_connect)
        except Exception as ex:
            print(ex)
            self.statusbar.showMessage("ERROR building script file")
        try:
            p1 = subprocess.Popen(['echo', self.sudo_password], stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE)
            p2 = subprocess.Popen(['sudo', '-S', 'mv', self.scripts_path + '/auto_connect', self.network_manager_path + 'auto_connect'], stdin=p1.stdout, stdout=subprocess.PIPE)
            p1.stdout.close()
            p2.stdout.close()
            p3 = subprocess.Popen(['echo', self.sudo_password], stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE)
            p4 = subprocess.Popen(['sudo', '-S', 'chown', 'root:root', self.network_manager_path + 'auto_connect'], stdin=p3.stdout, stdout=subprocess.PIPE)
            p3.stdout.close()
            p4.stdout.close()
            p5 = subprocess.Popen(['echo', self.sudo_password], stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE)
            p6 = subprocess.Popen(['sudo', '-S', 'chmod', '744', self.network_manager_path + 'auto_connect'], stdin=p5.stdout, stdout=subprocess.PIPE)
            p5.stdout.close()
            p6.stdout.close()
            self.config['SETTINGS']['auto_connect'] = 'True'
            self.write_conf()
        except Exception as ex:
            print(ex) 
Example 25
Project: fs_image   Author: facebookincubator   File: send_fds_and_run.py    MIT License 5 votes vote down vote up
def popen_and_inject_fds_after_sudo(cmd, fds, popen, *, set_listen_fds):
    '''
    This is a context manager intended to let you imitate the as-CLI
    behavior documented in the module docblock.  See that docblock to
    understand the process-spawning chain.

      - `cmd` is the post-sudo command, to which you want to pass the FDs.
        This may invoke `systemd-nspawn`, in which case you will want to set
        `set_listen_fds` to `True` if you need the FDs to be delivered
        inside the container.
      - `fds` are arbitrary FDs that you must not close before this context
        manager produces a context variable.
      - `popen` is a callback returning a context manager that works just
        like `subprocess.Popen`.  The only difference is that normally this
        callback will prepend its arguments with a `['sudo', '--some',
        '--args', '--']` invocation.
    '''
    with listen_temporary_unix_socket() as (lsock_path, lsock), popen([
        # The wrapper is Python.  In @mode/dev this can end up writing
        # bytecode as `root` into `buck-out`, which would break Buck's
        # garbage-collection.  The magic environment variable fixes that.
        # This doesn't affect @mode/opt since that is precompiled anyway.
        'env', 'PYTHONDONTWRITEBYTECODE=1',
        # The wrapper is part of this library's `resources`, so this will
        # work in @mode/opt with ZIP-PAR or XAR packaging.
        os.path.join(os.path.dirname(__file__), 'recv-fds-and-run'),
        # Although the permissions of lsock_path restrict it to the repo
        # user, the wrapper runs as `root`, so it can connect.
        '--unix-sock', lsock_path,
        '--num-fds', str(len(fds)),
        *([] if set_listen_fds else ['--no-set-listen-fds']),
        '--', *cmd,
    ]) as proc:
        log.info(f'Sending FDS {fds} to {cmd} via wrapper')
        lsock.settimeout(FD_UNIX_SOCK_TIMEOUT)
        with lsock.accept()[0] as csock:
            csock.settimeout(FD_UNIX_SOCK_TIMEOUT)
            send_fds(csock, fds)
        yield proc 
Example 26
Project: fs_image   Author: facebookincubator   File: send_fds_and_run.py    MIT License 5 votes vote down vote up
def send_fds_and_popen(opts, **popen_kwargs):
    return popen_and_inject_fds_after_sudo(
        opts.cmd,
        opts.fd,
        lambda wrapped_cmd: subprocess.Popen([
            *(['sudo', *opts.sudo_arg, '--'] if opts.sudo else []),
            *wrapped_cmd,
        ], **popen_kwargs),
        set_listen_fds=opts.set_listen_fds,
    )


# The CLI functionality is pretty well-covered in `test_send_fds_and_run.py`.
# Here is a manual smoke test that should print nothing and exit with code 1.
#     buck run //fs_image:send-fds-and-run -- --no-set-listen-fds -- \
#         printenv LISTEN_FDS LISTEN_PID ; echo $? 
Example 27
Project: fs_image   Author: facebookincubator   File: test_open_url.py    MIT License 5 votes vote down vote up
def test_open_http_url(self):
        with temp_dir() as server_dir:
            hello_path = server_dir / 'hello'
            with open(hello_path, 'w') as out_f:
                out_f.write('world')

            # First, check file:// URLs
            with open_url(hello_path.file_url()) as in_f:
                self.assertEqual(b'world', in_f.read())

            # Now, http:// URLs
            with subprocess.Popen([
                sys.executable, '-c', '''
import http.server as hs
with hs.HTTPServer(('localhost', 0), hs.SimpleHTTPRequestHandler) as httpd:
    print('http://{}:{}/'.format(*httpd.socket.getsockname()), flush=True)
    httpd.serve_forever()
                ''',
            ], cwd=server_dir, stdout=subprocess.PIPE) as proc:
                try:
                    with open_url(
                        proc.stdout.readline().decode().rstrip('\n') + 'hello'
                    ) as in_f:
                        self.assertEqual(b'world', in_f.read())
                finally:
                    proc.kill() 
Example 28
Project: fs_image   Author: facebookincubator   File: subvol_utils.py    MIT License 5 votes vote down vote up
def _mark_readonly_and_send(
        self, *, stdout, no_data: bool=False, parent: 'Subvol'=None,
    ) -> Iterator[subprocess.Popen]:
        self.set_readonly(True)

        # Btrfs bug #25329702: in some cases, a `send` without a sync will
        # violate read-after-write consistency and send a "past" view of the
        # filesystem.  Do this on the read-only filesystem to improve
        # consistency.
        self.sync()

        # Btrfs bug #25379871: our 4.6 kernels have an experimental xattr
        # caching patch, which is broken, and results in xattrs not showing
        # up in the `send` stream unless that metadata is `fsync`ed.  For
        # some dogscience reason, `getfattr` on a file actually triggers
        # such an `fsync`.  We do this on a read-only filesystem to improve
        # consistency. Coverage: manually tested this on a 4.11 machine:
        #   platform.uname().release.startswith('4.11.')
        if platform.uname().release.startswith('4.6.'):  # pragma: no cover
            self.run_as_root([
                # Symlinks can point outside of the subvol, don't follow them
                'getfattr', '--no-dereference', '--recursive', self.path()
            ])

        with self.popen_as_root([
            'btrfs', 'send',
            *(['--no-data'] if no_data else []),
            *(['-p', parent.path()] if parent else []),
            self.path(),
        ], stdout=stdout) as proc:
            yield proc 
Example 29
Project: fs_image   Author: facebookincubator   File: test_fs_utils.py    MIT License 5 votes vote down vote up
def test_open_for_read_decompress(self):
        # The goal is that our stream should be bigger than any buffers
        # involved (so we get to test edge effects), but not so big that the
        # test takes more than 1-2 seconds.
        n_bytes = 12 << 20  # 12MiB
        my_line = b'kitteh' * 700 + b'\n'  # ~ 4KiB
        for compress, ext in [('gzip', 'gz'), ('zstd', 'zst')]:
            filename = 'kitteh.' + ext
            with temp_dir() as td, open(td / filename, 'wb') as outf:
                with subprocess.Popen(
                    [compress, '-'], stdin=subprocess.PIPE, stdout=outf,
                ) as proc:
                    for _ in range(n_bytes // len(my_line)):
                        proc.stdin.write(my_line)
                check_popen_returncode(proc)

                with open_for_read_decompress(td / filename) as infile:
                    for l in infile:
                        self.assertEqual(my_line, l)

        # Test uncompressed
        with temp_dir() as td:
            with open(td / 'kitteh', 'wb') as outfile:
                outfile.write(my_line + b'meow')
            with open_for_read_decompress(td / 'kitteh') as infile:
                self.assertEqual(my_line + b'meow', infile.read())

        # Test decompression error
        with temp_dir() as td:
            with open(td / 'kitteh.gz', 'wb') as outfile:
                outfile.write(my_line)
            with self.assertRaises(subprocess.CalledProcessError), \
                    open_for_read_decompress(td / 'kitteh.gz') as infile:
                infile.read() 
Example 30
Project: fs_image   Author: facebookincubator   File: package_image.py    MIT License 5 votes vote down vote up
def package_full(self, subvol: Subvol, output_path: str, opts: _Opts):
        with create_ro(output_path, 'wb') as outfile, subprocess.Popen(
            ['zstd', '--stdout'], stdin=subprocess.PIPE, stdout=outfile
        ) as zst, subvol.mark_readonly_and_write_sendstream_to_file(zst.stdin):
            pass
        check_popen_returncode(zst) 
Example 31
Project: PEAKachu   Author: tbischler   File: adaptive.py    ISC License 5 votes vote down vote up
def _blockbuster_worker(self, blockbuster_path, replicon):
        p = Popen(
            ["blockbuster.x", "-minBlockHeight", "10", "-print", "1",
             "-distance", "1",
             "{}/{}_sorted_reads_for_blockbuster.bed".format(
                 self._blockbuster_input_folder, replicon)], stdout=PIPE,
            stderr=PIPE, universal_newlines=True)
        output, error = p.communicate()
        returncode = p.returncode
        return {"output": output, "error": error, "returncode": returncode} 
Example 32
Project: incubator-spot   Author: apache   File: utils.py    Apache License 2.0 5 votes vote down vote up
def popen(cls, cmd, cwd=None, raises=False):
        '''
            Execute the given command string in a new process. Send data to stdin and
        read data from stdout and stderr, until end-of-file is reached.

        :param cls     : The class as implicit first argument.
        :param cwd     : If it is set, then the child's current directory will be change
                         to `cwd` before it is executed.
        :param raises  : If ``True`` and stderr has data, it raises an ``OSError`` exception.
        :returns       : The output of the given command; pair of (stdout, stderr).
        :rtype         : ``tuple``
        :raises OSError: If `raises` and stderr has data.
        '''
        parser   = lambda x: [] if x == '' else [y.strip() for y in x.strip().split('\n')]
        process  = subprocess.Popen(cmd, shell=True, universal_newlines=True,
                    stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        out, err = process.communicate()

        # .............................trim lines and remove the empty ones
        _stdout  = [x for x in parser(out) if bool(x)]
        _stderr  = [x for x in parser(err) if bool(x)]

        if _stderr and raises:
            raise OSError('\n'.join(_stderr))

        return _stdout, _stderr 
Example 33
Project: godot-mono-builds   Author: godotengine   File: patch_mono.py    MIT License 5 votes vote down vote up
def main(raw_args):
    import cmd_utils
    import os
    import os.path
    from os_utils import get_emsdk_root

    parser = cmd_utils.build_arg_parser(description='Apply patches to the Mono source tree')

    default_help = 'default: %(default)s'

    mono_sources_default = os.environ.get('MONO_SOURCE_ROOT', '')

    if mono_sources_default:
        parser.add_argument('--mono-sources', default=mono_sources_default, help=default_help)
    else:
        parser.add_argument('--mono-sources', required=True)

    args = parser.parse_args(raw_args)

    this_script_dir = os.path.dirname(os.path.realpath(__file__))
    patches_dir = os.path.join(this_script_dir, 'files', 'patches')

    mono_source_root = args.mono_sources

    patches = [
        'fix-mono-android-tkill.diff'
    ]

    from subprocess import Popen
    for patch in patches:
        proc = Popen('bash -c \'patch -N -p1 < %s; exit 0\'' % os.path.join(patches_dir, patch), cwd=mono_source_root, shell=True) 
Example 34
Project: godot-mono-builds   Author: godotengine   File: patch_emscripten.py    MIT License 5 votes vote down vote up
def main(raw_args):
    import os
    import cmd_utils
    from os_utils import get_emsdk_root

    parser = cmd_utils.build_arg_parser(description='Apply patches to the active Emscripten SDK')

    default_help = 'default: %(default)s'

    mono_sources_default = os.environ.get('MONO_SOURCE_ROOT', '')

    if mono_sources_default:
        parser.add_argument('--mono-sources', default=mono_sources_default, help=default_help)
    else:
        parser.add_argument('--mono-sources', required=True)

    args = parser.parse_args(raw_args)

    mono_source_root = args.mono_sources
    emsdk_root = get_emsdk_root()

    patches = [
        '%s/sdks/builds/fix-emscripten-8511.diff' % mono_source_root,
        '%s/sdks/builds/emscripten-pr-8457.diff' % mono_source_root
    ]

    from subprocess import Popen
    for patch in patches:
        proc = Popen('bash -c \'patch -N -p1 < %s; exit 0\'' % patch, cwd=emsdk_root, shell=True) 
Example 35
Project: godot-mono-builds   Author: godotengine   File: os_utils.py    MIT License 5 votes vote down vote up
def source(script: str, cwd=None) -> dict:
    popen_args = {}
    if cwd is not None:
        popen_args['cwd'] = cwd

    import subprocess
    proc = subprocess.Popen('bash -c \'source %s; env -0\'' % script, stdout=subprocess.PIPE, shell=True, **popen_args)
    output = proc.communicate()[0]
    return dict(line.split('=', 1) for line in output.decode().split('\x00') if line)


# Creates the directory if no other file or directory with the same path exists 
Example 36
Project: autofff   Author: ChiefGokhlayeh   File: scanner.py    MIT License 5 votes vote down vote up
def _read_symbols(self, pathToObj: str) -> SymbolTable:
        path_list = ['objdump', '-t']
        path_list += [pathToObj]

        try:
            pipe = subprocess.Popen(path_list,
                                    stdout=subprocess.PIPE,
                                    universal_newlines=True)
            text = pipe.communicate()[0]
            matches = re.finditer(
                r"(?P<object>.*):\s+file format.*\s+SYMBOL TABLE:\n(?P<symbols>(?:.+(\n|$))*)", text, re.MULTILINE)
            tables = list()
            for match in matches:
                print(match)
                objectFile = match.group('object') or pathToObj
                symbols = match.group('symbols')
                symMatch = re.search(
                    r"\*ABS\*\s+[0-9a-fA-F]*\s+(?P<source>.*)", symbols, re.MULTILINE)
                sourceFile = symMatch.group('source')
                fuNMatches = re.finditer(r"", symbols, re.MULTILINE)

                tables.append(SymbolTable(objectFile, None))
                print(objectFile)
                print(sourceFile)
                print(symbols)
        except OSError as e:
            raise RuntimeError("Unable to invoke 'readelf'.  " +
                               'Make sure its path was passed correctly\n' +
                               ('Original error: %s' % e)) 
Example 37
Project: alfred-yubikey-otp   Author: robertoriv   File: yubi.py    MIT License 5 votes vote down vote up
def execute(cmd_list):
    new_env = os.environ.copy()
    new_env['PATH'] = '/usr/local/bin:%s' % new_env['PATH']
    cmd, err = subprocess.Popen(cmd_list,
                                stdout=subprocess.PIPE,
                                stderr=subprocess.PIPE,
                                env=new_env).communicate()
    if err:
        return err
    return cmd 
Example 38
Project: alfred-yubikey-otp   Author: robertoriv   File: yubi.py    MIT License 5 votes vote down vote up
def touch(name):
    new_env = os.environ.copy()
    new_env['PATH'] = '/usr/local/bin:%s' % new_env['PATH']
    process = subprocess.Popen(['ykman', 'oath', 'code', name], stdout=subprocess.PIPE, env=new_env)
    counter = 0
    for i in iter(process.stdout.readline,'b'):
        code_search = re.search('(.*)((\d{6,8}))', i, re.IGNORECASE)
        if code_search:
            wf.add_item(
                name,
                'Copy %s to clipboard' % code_search.group(2).strip(),
                arg=code_search.group(2).strip(),
                icon=get_icon_for_service(name), 
                valid=True,
            )
            wf.send_feedback()
            break
        counter += 1
        if counter == 3:
            wf.add_item(
                name,
                'Timeout',
                arg=name,
                icon=get_icon_for_service(name), 
                valid=True,
            )
            wf.send_feedback()
            break
    process.kill() 
Example 39
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 40
Project: phrydy   Author: Josef-Friedrich   File: versioneer.py    MIT License 5 votes vote down vote up
def run_command(commands, args, cwd=None, verbose=False, hide_stderr=False,
                env=None):
    """Call the given command(s)."""
    assert isinstance(commands, list)
    p = None
    for c in commands:
        try:
            dispcmd = str([c] + args)
            # remember shell=False, so use git.cmd on windows, not just git
            p = subprocess.Popen([c] + args, cwd=cwd, env=env,
                                 stdout=subprocess.PIPE,
                                 stderr=(subprocess.PIPE if hide_stderr
                                         else None))
            break
        except EnvironmentError:
            e = sys.exc_info()[1]
            if e.errno == errno.ENOENT:
                continue
            if verbose:
                print("unable to run %s" % dispcmd)
                print(e)
            return None, None
    else:
        if verbose:
            print("unable to find command, tried %s" % (commands,))
        return None, None
    stdout = p.communicate()[0].strip()
    if sys.version_info[0] >= 3:
        stdout = stdout.decode()
    if p.returncode != 0:
        if verbose:
            print("unable to run %s (error)" % dispcmd)
            print("stdout was %s" % stdout)
        return None, p.returncode
    return stdout, p.returncode 
Example 41
Project: phrydy   Author: Josef-Friedrich   File: _version.py    MIT License 5 votes vote down vote up
def run_command(commands, args, cwd=None, verbose=False, hide_stderr=False,
                env=None):
    """Call the given command(s)."""
    assert isinstance(commands, list)
    p = None
    for c in commands:
        try:
            dispcmd = str([c] + args)
            # remember shell=False, so use git.cmd on windows, not just git
            p = subprocess.Popen([c] + args, cwd=cwd, env=env,
                                 stdout=subprocess.PIPE,
                                 stderr=(subprocess.PIPE if hide_stderr
                                         else None))
            break
        except EnvironmentError:
            e = sys.exc_info()[1]
            if e.errno == errno.ENOENT:
                continue
            if verbose:
                print("unable to run %s" % dispcmd)
                print(e)
            return None, None
    else:
        if verbose:
            print("unable to find command, tried %s" % (commands,))
        return None, None
    stdout = p.communicate()[0].strip()
    if sys.version_info[0] >= 3:
        stdout = stdout.decode()
    if p.returncode != 0:
        if verbose:
            print("unable to run %s (error)" % dispcmd)
            print("stdout was %s" % stdout)
        return None, p.returncode
    return stdout, p.returncode 
Example 42
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 43
Project: pyblish-win   Author: pyblish   File: patchcheck.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def mq_patches_applied():
    """Check if there are any applied MQ patches."""
    cmd = 'hg qapplied'
    st = subprocess.Popen(cmd.split(),
                          stdout=subprocess.PIPE,
                          stderr=subprocess.PIPE)
    try:
        bstdout, _ = st.communicate()
        return st.returncode == 0 and bstdout
    finally:
        st.stdout.close()
        st.stderr.close() 
Example 44
Project: pyblish-win   Author: pyblish   File: imaplib.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def open(self, host = None, port = None):
        """Setup a stream connection.
        This connection will be used by the routines:
            read, readline, send, shutdown.
        """
        self.host = None        # For compatibility with parent class
        self.port = None
        self.sock = None
        self.file = None
        self.process = subprocess.Popen(self.command,
            stdin=subprocess.PIPE, stdout=subprocess.PIPE,
            shell=True, close_fds=True)
        self.writefile = self.process.stdin
        self.readfile = self.process.stdout 
Example 45
Project: pyblish-win   Author: pyblish   File: os.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def popen2(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) are returned."""
            import warnings
            msg = "os.popen2 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,
                                 close_fds=True)
            return p.stdin, p.stdout 
Example 46
Project: pyblish-win   Author: pyblish   File: __init__.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def __init__(self, *args, **kwargs):
        # Explicitly avoid to raise NoSuchProcess in case the process
        # spawned by subprocess.Popen terminates too quickly, see:
        # https://github.com/giampaolo/psutil/issues/193
        self.__subproc = subprocess.Popen(*args, **kwargs)
        self._init(self.__subproc.pid, _ignore_nsp=True) 
Example 47
Project: pyblish-win   Author: pyblish   File: __init__.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def __dir__(self):
        return sorted(set(dir(Popen) + dir(subprocess.Popen))) 
Example 48
Project: pyblish-win   Author: pyblish   File: __init__.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def wait(self, timeout=None):
        if self.__subproc.returncode is not None:
            return self.__subproc.returncode
        ret = super(Popen, self).wait(timeout)
        self.__subproc.returncode = ret
        return ret


# =====================================================================
# --- system processes related functions
# ===================================================================== 
Example 49
Project: pyblish-win   Author: pyblish   File: _pssunos.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def _get_unix_sockets(self, pid):
        """Get UNIX sockets used by process by parsing 'pfiles' output."""
        # TODO: rewrite this in C (...but the damn netstat source code
        # does not include this part! Argh!!)
        cmd = "pfiles %s" % pid
        p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE,
                             stderr=subprocess.PIPE)
        stdout, stderr = p.communicate()
        if PY3:
            stdout, stderr = [x.decode(sys.stdout.encoding)
                              for x in (stdout, stderr)]
        if p.returncode != 0:
            if 'permission denied' in stderr.lower():
                raise AccessDenied(self.pid, self._name)
            if 'no such process' in stderr.lower():
                raise NoSuchProcess(self.pid, self._name)
            raise RuntimeError("%r command error\n%s" % (cmd, stderr))

        lines = stdout.split('\n')[2:]
        for i, line in enumerate(lines):
            line = line.lstrip()
            if line.startswith('sockname: AF_UNIX'):
                path = line.split(' ', 2)[2]
                type = lines[i - 2].strip()
                if type == 'SOCK_STREAM':
                    type = socket.SOCK_STREAM
                elif type == 'SOCK_DGRAM':
                    type = socket.SOCK_DGRAM
                else:
                    type = -1
                yield (-1, socket.AF_UNIX, type, path, "", _common.CONN_NONE) 
Example 50
Project: pyblish-win   Author: pyblish   File: msvc9compiler.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def query_vcvarsall(version, arch="x86"):
    """Launch vcvarsall.bat and read the settings from its environment
    """
    vcvarsall = find_vcvarsall(version)
    interesting = set(("include", "lib", "libpath", "path"))
    result = {}

    if vcvarsall is None:
        raise DistutilsPlatformError("Unable to find vcvarsall.bat")
    log.debug("Calling 'vcvarsall.bat %s' (version=%s)", arch, version)
    popen = subprocess.Popen('"%s" %s & set' % (vcvarsall, arch),
                             stdout=subprocess.PIPE,
                             stderr=subprocess.PIPE)
    try:
        stdout, stderr = popen.communicate()
        if popen.wait() != 0:
            raise DistutilsPlatformError(stderr.decode("mbcs"))

        stdout = stdout.decode("mbcs")
        for line in stdout.split("\n"):
            line = Reg.convert_mbcs(line)
            if '=' not in line:
                continue
            line = line.strip()
            key, value = line.split('=', 1)
            key = key.lower()
            if key in interesting:
                if value.endswith(os.pathsep):
                    value = value[:-1]
                result[key] = removeDuplicates(value)

    finally:
        popen.stdout.close()
        popen.stderr.close()

    if len(result) != len(interesting):
        raise ValueError(str(list(result.keys())))

    return result

# More globals 
Example 51
Project: pyblish-win   Author: pyblish   File: test_tool.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_stdin_stdout(self):
        proc = subprocess.Popen(
                (sys.executable, '-m', 'json.tool'),
                stdin=subprocess.PIPE, stdout=subprocess.PIPE)
        out, err = proc.communicate(self.data.encode())
        self.assertEqual(out.splitlines(), self.expect.encode().splitlines())
        self.assertEqual(err, None) 
Example 52
Project: pyblish-win   Author: pyblish   File: webbrowser.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def open(self, url, new=0, autoraise=True):
        cmdline = [self.name] + [arg.replace("%s", url)
                                 for arg in self.args]
        try:
            if sys.platform[:3] == 'win':
                p = subprocess.Popen(cmdline)
            else:
                setsid = getattr(os, 'setsid', None)
                if not setsid:
                    setsid = getattr(os, 'setpgrp', None)
                p = subprocess.Popen(cmdline, close_fds=True, preexec_fn=setsid)
            return (p.poll() is None)
        except OSError:
            return False 
Example 53
Project: pyblish-win   Author: pyblish   File: regrtest.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def work():
            # A worker thread.
            try:
                while True:
                    try:
                        test, args_tuple = next(pending)
                    except StopIteration:
                        output.put((None, None, None, None))
                        return
                    # -E is needed by some tests, e.g. test_import
                    popen = Popen(base_cmd + ['--slaveargs', json.dumps(args_tuple)],
                                   stdout=PIPE, stderr=PIPE,
                                   universal_newlines=True,
                                   close_fds=(os.name != 'nt'))
                    stdout, stderr = popen.communicate()
                    # Strip last refcount output line if it exists, since it
                    # comes from the shutdown of the interpreter in the subcommand.
                    stderr = debug_output_pat.sub("", stderr)
                    stdout, _, result = stdout.strip().rpartition("\n")
                    if not result:
                        output.put((None, None, None, None))
                        return
                    result = json.loads(result)
                    output.put((test, stdout.rstrip(), stderr.rstrip(), result))
            except BaseException:
                output.put((None, None, None, None))
                raise 
Example 54
Project: pyblish-win   Author: pyblish   File: test_threading.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def assertScriptHasOutput(self, script, expected_output):
        p = subprocess.Popen([sys.executable, "-c", script],
                             stdout=subprocess.PIPE)
        rc = p.wait()
        data = p.stdout.read().decode().replace('\r', '')
        self.assertEqual(rc, 0, "Unexpected error")
        self.assertEqual(data, expected_output) 
Example 55
Project: pyblish-win   Author: pyblish   File: test_threading.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_recursion_limit(self):
        # Issue 9670
        # test that excessive recursion within a non-main thread causes
        # an exception rather than crashing the interpreter on platforms
        # like Mac OS X or FreeBSD which have small default stack sizes
        # for threads
        script = """if True:
            import threading

            def recurse():
                return recurse()

            def outer():
                try:
                    recurse()
                except RuntimeError:
                    pass

            w = threading.Thread(target=outer)
            w.start()
            w.join()
            print('end of main thread')
            """
        expected_output = "end of main thread\n"
        p = subprocess.Popen([sys.executable, "-c", script],
                             stdout=subprocess.PIPE)
        stdout, stderr = p.communicate()
        data = stdout.decode().replace('\r', '')
        self.assertEqual(p.returncode, 0, "Unexpected error")
        self.assertEqual(data, expected_output) 
Example 56
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 57
Project: pyblish-win   Author: pyblish   File: test_warnings.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_comma_separated_warnings(self):
        newenv = os.environ.copy()
        newenv["PYTHONWARNINGS"] = ("ignore::DeprecationWarning,"
                                    "ignore::UnicodeWarning")
        p = subprocess.Popen([sys.executable,
                "-c", "import sys; sys.stdout.write(str(sys.warnoptions))"],
                stdout=subprocess.PIPE, env=newenv)
        self.assertEqual(p.communicate()[0],
                "['ignore::DeprecationWarning', 'ignore::UnicodeWarning']")
        self.assertEqual(p.wait(), 0) 
Example 58
Project: pyblish-win   Author: pyblish   File: test_warnings.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_envvar_and_command_line(self):
        newenv = os.environ.copy()
        newenv["PYTHONWARNINGS"] = "ignore::DeprecationWarning"
        p = subprocess.Popen([sys.executable, "-W" "ignore::UnicodeWarning",
                "-c", "import sys; sys.stdout.write(str(sys.warnoptions))"],
                stdout=subprocess.PIPE, env=newenv)
        self.assertEqual(p.communicate()[0],
                "['ignore::UnicodeWarning', 'ignore::DeprecationWarning']")
        self.assertEqual(p.wait(), 0) 
Example 59
Project: pyblish-win   Author: pyblish   File: test_tcl.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def testLoadWithUNC(self):
        # Build a UNC path from the regular path.
        # Something like
        #   \\%COMPUTERNAME%\c$\python27\python.exe

        fullname = os.path.abspath(sys.executable)
        if fullname[1] != ':':
            self.skipTest('unusable path: %r' % fullname)
        unc_name = r'\\%s\%s$\%s' % (os.environ['COMPUTERNAME'],
                                    fullname[0],
                                    fullname[3:])

        with test_support.EnvironmentVarGuard() as env:
            env.unset("TCL_LIBRARY")
            cmd = '%s -c "import Tkinter; print Tkinter"' % (unc_name,)

            try:
                p = Popen(cmd, stdout=PIPE, stderr=PIPE)
            except WindowsError as e:
                if e.winerror == 5:
                    self.skipTest('Not permitted to start the child process')
                else:
                    raise

            out_data, err_data = p.communicate()

            msg = '\n\n'.join(['"Tkinter.py" not in output',
                               'Command:', cmd,
                               'stdout:', out_data,
                               'stderr:', err_data])

            self.assertIn('Tkinter.py', out_data, msg)

            self.assertEqual(p.wait(), 0, 'Non-zero exit code') 
Example 60
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 61
Project: pyblish-win   Author: pyblish   File: test_xpickle.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def send_to_worker(self, python, obj, proto):
        """Bounce a pickled object through another version of Python.

        This will pickle the object, send it to a child process where it will be
        unpickled, then repickled and sent back to the parent process.

        Args:
            python: the name of the Python binary to start.
            obj: object to pickle.
            proto: pickle protocol number to use.

        Returns:
            The pickled data received from the child process.
        """
        # Prevent the subprocess from picking up invalid .pyc files.
        target = __file__
        if target[-1] in ("c", "o"):
            target = target[:-1]

        data = self.module.dumps((proto, obj), proto)
        worker = subprocess.Popen([python, target, "worker"],
                                  stdin=subprocess.PIPE,
                                  stdout=subprocess.PIPE,
                                  stderr=subprocess.PIPE)
        stdout, stderr = worker.communicate(data)
        if worker.returncode != 0:
            raise RuntimeError(stderr)
        return stdout 
Example 62
Project: pyblish-win   Author: pyblish   File: test_support.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def strip_python_stderr(stderr):
    """Strip the stderr of a Python process from potential debug output
    emitted by the interpreter.

    This will typically be run on the result of the communicate() method
    of a subprocess.Popen object.
    """
    stderr = re.sub(br"\[\d+ refs\]\r?\n?$", b"", stderr).strip()
    return stderr 
Example 63
Project: pyblish-win   Author: pyblish   File: test_sysconfig.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_symlink(self):
        # Issue 7880
        symlink = get_attribute(os, "symlink")
        def get(python):
            cmd = [python, '-c',
                   'import sysconfig; print sysconfig.get_platform()']
            p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
            return p.communicate()
        real = os.path.realpath(sys.executable)
        link = os.path.abspath(TESTFN)
        symlink(real, link)
        try:
            self.assertEqual(get(real), get(link))
        finally:
            unlink(link) 
Example 64
Project: pyblish-win   Author: pyblish   File: test_file2k.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_unicode(self):
        import subprocess

        def get_message(encoding, *code):
            code = '\n'.join(code)
            env = os.environ.copy()
            env['PYTHONIOENCODING'] = encoding
            process = subprocess.Popen([sys.executable, "-c", code],
                                       stdout=subprocess.PIPE, env=env)
            stdout, stderr = process.communicate()
            self.assertEqual(process.returncode, 0)
            return stdout

        def check_message(text, encoding, expected):
            stdout = get_message(encoding,
                "import sys",
                "sys.stdout.write(%r)" % text,
                "sys.stdout.flush()")
            self.assertEqual(stdout, expected)

        # test the encoding
        check_message(u'15\u20ac', "iso-8859-15", "15\xa4")
        check_message(u'15\u20ac', "utf-8", '15\xe2\x82\xac')
        check_message(u'15\u20ac', "utf-16-le", '1\x005\x00\xac\x20')

        # test the error handler
        check_message(u'15\u20ac', "iso-8859-1:ignore", "15")
        check_message(u'15\u20ac', "iso-8859-1:replace", "15?")
        check_message(u'15\u20ac', "iso-8859-1:backslashreplace", "15\\u20ac")

        # test the buffer API
        for objtype in ('buffer', 'bytearray'):
            stdout = get_message('ascii',
                'import sys',
                r'sys.stdout.write(%s("\xe9"))' % objtype,
                'sys.stdout.flush()')
            self.assertEqual(stdout, "\xe9") 
Example 65
Project: pyblish-win   Author: pyblish   File: test_subprocess.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_invalid_args(self):
        # Popen() called with invalid arguments should raise TypeError
        # but Popen.__del__ should not complain (issue #12085)
        with test_support.captured_stderr() as s:
            self.assertRaises(TypeError, subprocess.Popen, invalid_arg_name=1)
            argcount = subprocess.Popen.__init__.__code__.co_argcount
            too_many_args = [0] * (argcount + 1)
            self.assertRaises(TypeError, subprocess.Popen, *too_many_args)
        self.assertEqual(s.getvalue(), '') 
Example 66
Project: pyblish-win   Author: pyblish   File: test_subprocess.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_stdin_none(self):
        # .stdin is None when not redirected
        p = subprocess.Popen([sys.executable, "-c", 'print "banana"'],
                         stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        self.addCleanup(p.stdout.close)
        self.addCleanup(p.stderr.close)
        p.wait()
        self.assertEqual(p.stdin, None) 
Example 67
Project: pyblish-win   Author: pyblish   File: test_subprocess.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_stderr_none(self):
        # .stderr is None when not redirected
        p = subprocess.Popen([sys.executable, "-c", 'print "banana"'],
                         stdin=subprocess.PIPE, stdout=subprocess.PIPE)
        self.addCleanup(p.stdout.close)
        self.addCleanup(p.stdin.close)
        p.wait()
        self.assertEqual(p.stderr, None) 
Example 68
Project: pyblish-win   Author: pyblish   File: test_subprocess.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_executable_with_cwd(self):
        python_dir = os.path.dirname(os.path.realpath(sys.executable))
        p = subprocess.Popen(["somethingyoudonthave", "-c",
                              "import sys; sys.exit(47)"],
                             executable=sys.executable, cwd=python_dir)
        p.wait()
        self.assertEqual(p.returncode, 47) 
Example 69
Project: pyblish-win   Author: pyblish   File: test_subprocess.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_executable_without_cwd(self):
        # For a normal installation, it should work without 'cwd'
        # argument.  For test runs in the build directory, see #7774.
        p = subprocess.Popen(["somethingyoudonthave", "-c",
                              "import sys; sys.exit(47)"],
                             executable=sys.executable)
        p.wait()
        self.assertEqual(p.returncode, 47) 
Example 70
Project: pyblish-win   Author: pyblish   File: test_subprocess.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_stdin_pipe(self):
        # stdin redirection
        p = subprocess.Popen([sys.executable, "-c",
                         'import sys; sys.exit(sys.stdin.read() == "pear")'],
                        stdin=subprocess.PIPE)
        p.stdin.write("pear")
        p.stdin.close()
        p.wait()
        self.assertEqual(p.returncode, 1) 
Example 71
Project: NordVPN-NetworkManager-Gui   Author: vfosterm   File: nord_nm_gui.py    GNU General Public License v3.0 4 votes vote down vote up
def disable_auto_connect(self):
        """
        Handles the enabling and disabling of auto-connect depending on UI state
        Called everytime the auto-connect box is clicked
        """
        self.config.read(self.conf_path)

        if not self.auto_connect_box.isChecked() and not self.sudo_password and self.config.getboolean('SETTINGS', 'auto_connect'):
            self.sudo_dialog = self.get_sudo()
            self.sudo_dialog.text_label.setText("<html><head/><body><p>VPN Network Manager requires <span style=\" font-weight:600;\">sudo</span> permissions in order to remove the auto-connect script from the Network Manager directory. Please input the <span style=\" font-weight:600;\">sudo</span> Password or run the program with elevated priveledges.</p></body></html>")
            self.sudo_dialog.exec_()
            if not self.sudo_password:  # dialog was canceled
                return False
            try:
                p1 = subprocess.Popen(['echo', self.sudo_password], stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE)
                p2 = subprocess.Popen(['sudo', '-S', 'rm', self.network_manager_path + 'auto_connect'], stdin=p1.stdout, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
                p1.stdout.close()
                p2.stdout.close()
                self.config['SETTINGS']['auto_connect'] = 'False'
                self.write_conf()
            except Exception as ex:
                print(ex)

        elif not self.auto_connect_box.isChecked() and self.sudo_password and self.config.getboolean('SETTINGS', 'auto_connect'):

            try:
                p1 = subprocess.Popen(['echo', self.sudo_password], stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE)
                p2 = subprocess.Popen(['sudo', '-S', 'rm', self.network_manager_path + 'auto_connect'], stdin=p1.stdout, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
                p1.stdout.close()
                p2.stdout.close()
                self.config['SETTINGS']['auto_connect'] = 'False'
                self.write_conf()
            except Exception as ex:
                print(ex)

        elif self.auto_connect_box.isChecked() and self.get_active_vpn() and self.sudo_password:
            self.set_auto_connect()

        elif self.auto_connect_box.isChecked() and self.get_active_vpn() and not self.sudo_password:
            self.sudo_dialog = self.get_sudo()
            self.sudo_dialog.exec_()
            if self.sudo_password:
                self.set_auto_connect()
            else:
                self.auto_connect_box.setChecked(False)
                return False 
Example 72
Project: NordVPN-NetworkManager-Gui   Author: vfosterm   File: nord_nm_gui.py    GNU General Public License v3.0 4 votes vote down vote up
def set_kill_switch(self):
        """
        Generates bash killswitch script and moves it to the NetworkManager
        """
        script = (
            '#!/bin/bash\n'
            'PERSISTENCE_FILE=' + os.path.join(self.scripts_path, '.killswitch_data') + '\n\n'
            'case $2 in'
            '  vpn-up)\n'
            '    nmcli -f type,device connection | awk \'$1~/^vpn$/ && $2~/[^\-][^\-]/ { print $2; }\' > "${PERSISTENCE_FILE}"\n'
            '  ;;\n'
            '  vpn-down)\n'
            '    xargs -n 1 -a "${PERSISTENCE_FILE}" nmcli device disconnect\n'
            '  ;;\n'
            'esac\n'
        )

        try:
            with open(os.path.join(self.scripts_path, 'kill_switch'), 'w') as kill_switch:
                print(script, file=kill_switch)

            p1 = subprocess.Popen(['echo', self.sudo_password], stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
            p2 = subprocess.Popen(['sudo', '-S', 'mv', self.scripts_path + '/kill_switch', self.network_manager_path + 'kill_switch'], stdin=p1.stdout, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
            p1.stdout.close()
            p2.stdout.close()
            time.sleep(0.5)
            p3 = subprocess.Popen(['echo', self.sudo_password], stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
            p4 = subprocess.Popen(['sudo', '-S', 'chown', 'root:root', self.network_manager_path + 'kill_switch'], stdin=p3.stdout, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
            time.sleep(0.5)
            p3.stdout.close()
            p4.stdout.close()
            p5 = subprocess.Popen(['echo', self.sudo_password], stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
            p6 = subprocess.Popen(['sudo', '-S', 'chmod', '744', self.network_manager_path + 'kill_switch'], stdin=p5.stdout, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
            p5.stdout.close()
            p6.stdout.close()
            self.config['SETTINGS']['kill_switch'] = 'True'
            self.write_conf()

            self.statusbar.showMessage('Kill switch activated', 2000)
            self.repaint()
        except Exception as ex:
            print(ex) 
Example 73
Project: NordVPN-NetworkManager-Gui   Author: vfosterm   File: nord_nm_gui.py    GNU General Public License v3.0 4 votes vote down vote up
def disable_kill_switch(self):
        """
        Enables or disables Killswitch depending on UI state
        Called everytime the Killswitch button is pressed
        """
        if not self.killswitch_btn.isChecked() and not self.sudo_password and self.config.getboolean('SETTINGS', 'kill_switch'):
            self.sudo_dialog = self.get_sudo()
            self.sudo_dialog.text_label.setText("<html><head/><body><p>VPN Network Manager requires <span style=\" font-weight:600;\">sudo</span> permissions in order to remove the kill switch script from the Network Manager directory. Please input the <span style=\" font-weight:600;\">sudo</span> Password or run the program with elevated priveledges.</p></body></html>")
            self.sudo_dialog.exec_()

            if not self.sudo_password:  # dialog was canceled
                self.killswitch_btn.setChecked(False)
                return False
            try:
                p1 = subprocess.Popen(['echo', self.sudo_password], stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
                p2 = subprocess.Popen(['sudo', '-S', 'rm', self.network_manager_path + 'kill_switch'], stdin=p1.stdout, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
                p1.stdout.close()
                p2.stdout.close()
                self.statusbar.showMessage('Kill switch disabled', 2000)
                self.repaint()
                self.config['SETTINGS']['kill_switch'] = 'False'
                self.write_conf()

            except subprocess.CalledProcessError:
                self.statusbar.showMessage('ERROR disabling kill switch', 2000)
                self.repaint()

        elif not self.killswitch_btn.isChecked() and self.sudo_password and self.config.getboolean('SETTINGS', 'kill_switch'):

            try:
                p1 = subprocess.Popen(['echo', self.sudo_password], stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
                p2 = subprocess.Popen(['sudo', '-S', 'rm', self.network_manager_path + 'kill_switch'], stdin=p1.stdout, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
                p1.stdout.close()
                p2.stdout.close()
                self.statusbar.showMessage('Kill switch disabled', 2000)
                self.repaint()
                self.config['SETTINGS']['kill_switch'] = 'False'
                self.write_conf()

            except subprocess.CalledProcessError:
                self.statusbar.showMessage('ERROR disabling kill switch', 2000)
                self.repaint()

        elif self.killswitch_btn.isChecked() and self.get_active_vpn() and self.sudo_password:
            self.set_kill_switch()

        elif self.killswitch_btn.isChecked() and self.get_active_vpn() and not self.sudo_password:
            self.sudo_dialog = self.get_sudo()
            self.sudo_dialog.text_label.setText("<html><head/><body><p>VPN Network Manager requires <span style=\" font-weight:600;\">sudo</span> permissions in order to move the kill switch script to the Network Manager directory. Please input the <span style=\" font-weight:600;\">sudo</span> Password or run the program with elevated priveledges.</p></body></html>")
            self.sudo_dialog.exec_()
            if self.sudo_password:
                self.set_kill_switch()
            else:
                self.killswitch_btn.setChecked(False)
                return False 
Example 74
Project: fs_image   Author: facebookincubator   File: subvol_utils.py    MIT License 4 votes vote down vote up
def path(
        self, path_in_subvol: AnyStr=b'.', *, no_dereference_leaf=False,
    ) -> Path:
        '''
        The only safe way to access paths inside the subvolume.  Do NOT
        `os.path.join(subvol.path('a/path'), 'more/path')`, since that skips
        crucial safety checks.  Instead: `subvol.path(os.path.join(...))`.

        This code has checks to mitigate two risks:
          - `path_in_subvol` is relative, and exits the subvolume via '..'
          - Some component of the path is a symlink, and this symlink, when
            interpreted by a non-chrooted tool, will attempt to access
            something outside of the subvolume.

        At present, the above check fail on attempting to traverse an
        in-subvolume symlink that is an absolute path to another directory
        within the subvolume, but support could easily be added.  It is not
        supported now because at present, I believe that the right idiom is
        to encourage image authors to manipulate the "real" locations of
        files, and not to manipulate paths through symlinks.

        In the rare case that you need to manipulate a symlink itself (e.g.
        remove or rename), you will want to pass `no_dereference_leaf`.

        Future: consider using a file descriptor to refer to the subvolume
        root directory to better mitigate races due to renames in its path.
        '''
        # The `btrfs` CLI is not very flexible, so it will try to name a
        # subvol '.' if we do not normalize `/subvol/.`.
        result_path = os.path.normpath(os.path.join(
            self._path,
            # Without the lstrip, we would lose the subvolume prefix if the
            # supplied path is absolute.
            byteme(path_in_subvol).lstrip(b'/'),
        ))
        # Paranoia: Make sure that, despite any symlinks in the path, the
        # resulting path is not outside of the subvolume root.
        #
        # NB: This will prevent us from even accessing symlinks created
        # inside the subvolume.  To fix this, we should add an OPTION not to
        # follow the LAST component of the path.
        root_relative = os.path.relpath((
            os.path.join(
                os.path.realpath(os.path.dirname(result_path)),
                os.path.basename(result_path),
            ) if no_dereference_leaf else os.path.realpath(result_path)
        ), os.path.realpath(self._path))
        if root_relative.startswith(b'../') or root_relative == b'..':
            raise AssertionError(f'{path_in_subvol} is outside the subvol')
        return Path(result_path)

    # This differs from the regular `subprocess.Popen` interface in these ways:
    #   - stdout maps to stderr by default (to protect the caller's stdout),
    #   - `check` is supported, and default to `True`,
    #   - `cwd` is prohibited.
    #
    # `_subvol_exists` is a private kwarg letting us `run_as_root` to create
    # new subvolumes, and not just touch existing ones. 
Example 75
Project: fs_image   Author: facebookincubator   File: test_send_fds_and_run.py    MIT License 4 votes vote down vote up
def test_send_fds(self):
        for sudo_args in [[], ['--sudo'], ['--sudo', '--sudo-arg', 'moo=cow']]:
            # Check basic wrapper execution, except for FD passing (we
            # pass 0 FDs), and `--no-set-listen-fds`.
            ret, out, err = _run([
                *sudo_args, '--', 'bash', '-c',
                'echo $SUDO_COMMAND ; echo $$ ; '
                    'printenv LISTEN_FDS LISTEN_PID ; exit 37',
            ])
            self.assertEqual([], _clean_err(err))
            self.assertEqual(37, ret)
            sudo, sh_pid, listen_fds, listen_pid = out.rstrip('\n').split('\n')
            self.assertEqual('0', listen_fds)
            self.assertEqual(sh_pid, listen_pid)
            self.assertEqual(bool(sudo), bool(sudo_args), f'{out} {sudo_args}')

            # Exercise `--no-set-listen-fds` and exit code 0.
            ret, out, err = _run([
                *sudo_args, '--no-set-listen-fds', '--', 'bash', '-c',
                'echo "$LISTEN_FDS/$LISTEN_PID"',
            ])
            self.assertEqual((0, '/\n', []), (ret, out, _clean_err(err)))

            # Exercise actual FD passing, both input and output
            with pipe() as (r_fd4, w_fd4), pipe() as (r_fd3, w_fd3), \
                    subprocess.Popen(['echo', 'hi-diddly-ho'], stdout=w_fd4), \
                    tempfile.TemporaryFile() as tf:
                w_fd4.close()  # or the Flanders cat might wait forever
                with subprocess.Popen(
                    ['cat'], stdin=r_fd3, stdout=tf.fileno(),
                ) as homer_proc:
                    try:
                        ret, out, err = _run([
                            *sudo_args,
                            '--fd', str(w_fd3.fileno()),
                            '--fd', str(r_fd4.fileno()),
                            '--', 'bash', '-c',
                            'echo "$LISTEN_FDS" ; cat <&4 ; echo doh >&3',
                        ])
                    finally:
                        w_fd3.close()  # or the Homer cat might wait forever
                self.assertEqual([], _clean_err(err))
                self.assertEqual('2\nhi-diddly-ho\n', out)
                self.assertEqual(0, ret)
                tf.seek(0)
                self.assertEqual(b'doh\n', tf.read())
                homer_proc.wait() 
Example 76
Project: dcos-proxy   Author: dparrish   File: build-config.py    Apache License 2.0 4 votes vote down vote up
def main(argv):
    try:
        old_config = None
        while True:
            params = {
                'vhosts': {},
            }

            s = requests.Session()
            apps = json.loads(s.get('http://master.mesos:8080/v2/apps').text)
            for app in apps['apps']:
                try:
                    vhost = app['labels']['VIRTUAL_HOST']
                except KeyError:
                    continue
                tasks = json.loads(s.get('http://master.mesos:8080/v2/apps%s/tasks' % app['id'],
                                         headers={'Accept': 'application/json'}).text)
                backends = []
                for task in tasks['tasks']:
                    try:
                        ip = socket.gethostbyname(task['host'])
                    except socket.gaierror:
                        print "Can't look up host %s" % task['host']
                        continue
                    backends.append('%s:%s' % (ip, task['ports'][0]))
                if backends:
                    params['vhosts'][vhost] = {
                        'backends': backends,
                    }

            template = Template(TEMPLATE)
            new_config = template.render(params)
            if new_config != old_config:
                with file('/etc/nginx/sites-available/default', 'w') as fh:
                    fh.write(new_config)
                test = subprocess.Popen(['/usr/sbin/nginx', '-t'], stderr=subprocess.PIPE)
                output = test.communicate()
                if test.returncode != 0:
                    if old_config:
                        print 'Error generating new NGINX configuration, not reloading'
                        return
                    else:
                        raise RuntimeError('Error generating NGINX configuration')
                subprocess.call(['/usr/sbin/nginx', '-s', 'reload'])
                old_config = new_config
            time.sleep(10)
    except KeyboardInterrupt:
        return 1 
Example 77
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 78
Project: pyblish-win   Author: pyblish   File: webbrowser.py    GNU Lesser General Public License v3.0 4 votes vote down vote up
def _invoke(self, args, remote, autoraise):
        raise_opt = []
        if remote and self.raise_opts:
            # use autoraise argument only for remote invocation
            autoraise = int(autoraise)
            opt = self.raise_opts[autoraise]
            if opt: raise_opt = [opt]

        cmdline = [self.name] + raise_opt + args

        if remote or self.background:
            inout = file(os.devnull, "r+")
        else:
            # for TTY browsers, we need stdin/out
            inout = None
        # if possible, put browser in separate process group, so
        # keyboard interrupts don't affect browser as well as Python
        setsid = getattr(os, 'setsid', None)
        if not setsid:
            setsid = getattr(os, 'setpgrp', None)

        p = subprocess.Popen(cmdline, close_fds=True, stdin=inout,
                             stdout=(self.redirect_stdout and inout or None),
                             stderr=inout, preexec_fn=setsid)
        if remote:
            # wait five seconds. If the subprocess is not finished, the
            # remote invocation has (hopefully) started a new instance.
            time.sleep(1)
            rc = p.poll()
            if rc is None:
                time.sleep(4)
                rc = p.poll()
                if rc is None:
                    return True
            # if remote call failed, open() will try direct invocation
            return not rc
        elif self.background:
            if p.poll() is None:
                return True
            else:
                return False
        else:
            return not p.wait() 
Example 79
Project: pyblish-win   Author: pyblish   File: webbrowser.py    GNU Lesser General Public License v3.0 4 votes vote down vote up
def open(self, url, new=0, autoraise=True):
        # XXX Currently I know no way to prevent KFM from opening a new win.
        if new == 2:
            action = "newTab"
        else:
            action = "openURL"

        devnull = file(os.devnull, "r+")
        # if possible, put browser in separate process group, so
        # keyboard interrupts don't affect browser as well as Python
        setsid = getattr(os, 'setsid', None)
        if not setsid:
            setsid = getattr(os, 'setpgrp', None)

        try:
            p = subprocess.Popen(["kfmclient", action, url],
                                 close_fds=True, stdin=devnull,
                                 stdout=devnull, stderr=devnull)
        except OSError:
            # fall through to next variant
            pass
        else:
            p.wait()
            # kfmclient's return code unfortunately has no meaning as it seems
            return True

        try:
            p = subprocess.Popen(["konqueror", "--silent", url],
                                 close_fds=True, stdin=devnull,
                                 stdout=devnull, stderr=devnull,
                                 preexec_fn=setsid)
        except OSError:
            # fall through to next variant
            pass
        else:
            if p.poll() is None:
                # Should be running now.
                return True

        try:
            p = subprocess.Popen(["kfm", "-d", url],
                                 close_fds=True, stdin=devnull,
                                 stdout=devnull, stderr=devnull,
                                 preexec_fn=setsid)
        except OSError:
            return False
        else:
            return (p.poll() is None) 
Example 80
Project: pyblish-win   Author: pyblish   File: test_sysconfig.py    GNU Lesser General Public License v3.0 4 votes vote down vote up
def test_platform_in_subprocess(self):
        my_platform = sysconfig.get_platform()

        # Test without MACOSX_DEPLOYMENT_TARGET in the environment

        env = os.environ.copy()
        if 'MACOSX_DEPLOYMENT_TARGET' in env:
            del env['MACOSX_DEPLOYMENT_TARGET']

        with open('/dev/null', 'w') as devnull_fp:
            p = subprocess.Popen([
                    sys.executable, '-c',
                   'import sysconfig; print(sysconfig.get_platform())',
                ],
                stdout=subprocess.PIPE,
                stderr=devnull_fp,
                env=env)
        test_platform = p.communicate()[0].strip()
        test_platform = test_platform.decode('utf-8')
        status = p.wait()

        self.assertEqual(status, 0)
        self.assertEqual(my_platform, test_platform)


        # Test with MACOSX_DEPLOYMENT_TARGET in the environment, and
        # using a value that is unlikely to be the default one.
        env = os.environ.copy()
        env['MACOSX_DEPLOYMENT_TARGET'] = '10.1'

        p = subprocess.Popen([
                sys.executable, '-c',
                'import sysconfig; print(sysconfig.get_platform())',
            ],
            stdout=subprocess.PIPE,
            stderr=open('/dev/null'),
            env=env)
        test_platform = p.communicate()[0].strip()
        test_platform = test_platform.decode('utf-8')
        status = p.wait()

        self.assertEqual(status, 0)
        self.assertEqual(my_platform, test_platform)