Python subprocess.PIPE() Examples

The following are code examples for showing how to use subprocess.PIPE(). 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: badge-o-matic   Author: markuslindenberg   File: online.py    BSD 2-Clause "Simplified" License 6 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 2
Project: NordVPN-NetworkManager-Gui   Author: vfosterm   File: nord_nm_gui.py    GNU General Public License v3.0 6 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 3
Project: NordVPN-NetworkManager-Gui   Author: vfosterm   File: nord_nm_gui.py    GNU General Public License v3.0 6 votes vote down vote up
def get_interfaces(self):
        """
        Gets current Network interfaces

        :return: List of Network interfaces
        """
        try:
            output = subprocess.run(['nmcli', '--mode', 'tabular', '--terse', '--fields', 'TYPE,DEVICE', 'device', 'status'], stdout=subprocess.PIPE)
            output.check_returncode()

            lines = output.stdout.decode('utf-8').split('\n')
            interfaces = []

            for line in lines:
                if line:
                    elements = line.strip().split(':')

                    if (elements[0] == 'wifi') or (elements[0] == 'ethernet'):
                        interfaces.append(elements[1])

            return interfaces

        except subprocess.CalledProcessError:
            self.statusbar.showMessage("ERROR Fetching interfaces") 
Example 4
Project: NordVPN-NetworkManager-Gui   Author: vfosterm   File: nord_nm_gui.py    GNU General Public License v3.0 6 votes vote down vote up
def disable_ipv6(self):
        """
        Disables 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=1', '&&', 'sysctl', '-w', 'net.ipv6.conf.default.disable_ipv6=1'], 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 disable 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=1', '&&', '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) 
Example 5
Project: NordVPN-NetworkManager-Gui   Author: vfosterm   File: nord_nm_gui.py    GNU General Public License v3.0 6 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: 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 7
Project: fs_image   Author: facebookincubator   File: test_nspawn_in_subvol.py    MIT License 6 votes vote down vote up
def test_boot_cmd_is_system_running(self):
        ret = self._nspawn_in('bootable-systemd-os', [
            '--boot',
            # This needs to be root because we don't yet create a proper
            # login session for non-privileged users when we execute commands.
            # Systemctl will try and connect to the user session
            # when it's run as non-root.
            '--user=root',
            '--',
            '/usr/bin/systemctl', 'is-system-running', '--wait',
        ], stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=False)

        # The assertions here are validating that the systemd instance inside
        # the container completed its boot but *not* that it successfully
        # started every service.  This reason for this is that it's not
        # a gauranteed property of the image build system that the image
        # successfully boot systemd, but simply that systemd can be properly
        # started and 'complete' a boot.  The success of the boot is really
        # something that can only be properly evaulated by unit tests for
        # a specific image.
        self.assertIn(ret.returncode, [0, 1], msg=ret.stderr.strip())
        self.assertIn(ret.stdout.strip(),
                [b'running', b'degraded'], msg=ret.stderr.strip())
        # T48760757
        # self.assertEqual(b'', ret.stderr) 
Example 8
Project: fs_image   Author: facebookincubator   File: test_nspawn_in_subvol.py    MIT License 6 votes vote down vote up
def test_boot_env_clean(self):
        ret = self._nspawn_in('bootable-systemd-os', [
            '--boot',
            '--',
            '/bin/env',
        ], stdout=subprocess.PIPE, check=True)
        self.assertEqual(0, ret.returncode)

        # Verify we aren't getting anything in from the outside we don't want
        self.assertNotIn(b'BUCK_BUILD_ID', ret.stdout)

        # Verify we get what we expect
        self.assertIn(b'HOME', ret.stdout)
        self.assertIn(b'PATH', ret.stdout)
        self.assertIn(b'LOGNAME', ret.stdout)
        self.assertIn(b'USER', ret.stdout)
        self.assertIn(b'TERM', ret.stdout) 
Example 9
Project: fs_image   Author: facebookincubator   File: test_execute_installed.py    MIT License 6 votes vote down vote up
def test_execute(self):
        for print_ok in [
            '/foo/bar/installed/print-ok',
            '/foo/bar/installed/print-ok-too',
        ]:
            ret = self._nspawn_in(
                'exe-layer', [
                    # Workaround: When the test is compiled with LLVM
                    # profiling, then `print-ok` would try to write to
                    # `/default.profraw`, which is not permitted to the test
                    # user `nobody`.  This would print errors to stderr and
                    # cause our assertion below to fail.
                    'env', 'LLVM_PROFILE_FILE=/tmp/default.profraw',
                    print_ok,
                ],
                stdout=subprocess.PIPE, stderr=subprocess.PIPE,
            )
            self.assertEqual(b'ok\n', ret.stdout)
            # systemd-nspawn >= 242 dumps out an error about capabilities when
            # run in an environment that drops cap_sys_boot (such as the FB
            # container runtime).
            # T48760757
            # self.assertEqual(b'', ret.stderr) 
Example 10
Project: fs_image   Author: facebookincubator   File: fs_utils.py    MIT License 6 votes vote down vote up
def open_for_read_decompress(path):
    'Wraps `open(path, "rb")` to add transparent `.zst` or `.gz` decompression.'
    path = Path(path)
    if path.endswith(b'.zst'):
        decompress = 'zstd'
    elif path.endswith(b'.gz') or path.endswith(b'.tgz'):
        decompress = 'gzip'
    else:
        with open(path, 'rb') as f:
            yield f
        return
    with subprocess.Popen([
        decompress, '--decompress', '--stdout', path,
    ], stdout=subprocess.PIPE) as proc:
        yield proc.stdout
    check_popen_returncode(proc) 
Example 11
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 12
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 13
Project: pyblish-win   Author: pyblish   File: os.py    GNU Lesser General Public License v3.0 6 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 14
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 15
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 16
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 17
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 18
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 19
Project: pyblish-win   Author: pyblish   File: test_winsound.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def _have_soundcard():
    """Return True iff this computer has a soundcard."""
    global __have_soundcard_cache
    if __have_soundcard_cache is None:
        cscript_path = _get_cscript_path()
        if cscript_path is None:
            # Could not find cscript.exe to run our VBScript helper. Default
            # to True: most computers these days *do* have a soundcard.
            return True

        check_script = os.path.join(os.path.dirname(__file__),
                                    "check_soundcard.vbs")
        p = subprocess.Popen([cscript_path, check_script],
                             stdout=subprocess.PIPE)
        __have_soundcard_cache = not p.wait()
    return __have_soundcard_cache 
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: speech_recogn.py    GNU General Public License v3.0 5 votes vote down vote up
def pubCommand(topic, msg):
    subprocess.call(["mosquitto_pub", "-d", "-t", topic, "-m", msg], shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE) 
Example 23
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 24
Project: UrsaRobotics_SmartHome   Author: stav98   File: tts.py    GNU General Public License v3.0 5 votes vote down vote up
def pubCommand(topic, msg):
    subprocess.call(["mosquitto_pub", "-d", "-t", topic, "-m", msg], shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

#Αν δεχθεί μήνυμα σε κάποιο topic που έχει κάνει subscribe 
Example 25
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 26
Project: NordVPN-NetworkManager-Gui   Author: vfosterm   File: nord_nm_gui.py    GNU General Public License v3.0 5 votes vote down vote up
def randomize_mac(self):
        """
        Takes down network interface and brings it back with a new MAC Address
        """
        try:
            self.statusbar.showMessage("Randomizing MAC Address", 2000)
            self.repaint()
            output = subprocess.run(['nmcli', '--mode', 'tabular', '--terse', '--fields', 'TYPE,UUID', 'connection', 'show', '--active'], stdout=subprocess.PIPE)
            output.check_returncode()
            lines = output.stdout.decode('utf-8').split('\n')

            for line in lines:
                if line:
                    elements = line.strip().split(':')
                    uuid = elements[1]
                    connection_type = elements[0]
                    if type != 'vpn':
                        subprocess.run(['nmcli', 'connection', 'down', uuid])
                        subprocess.run(['nmcli', 'connection', 'modify', '--temporary', uuid, connection_type+'.cloned-mac-address', 'random'])
                        subprocess.run(['nmcli', 'connection', 'up', uuid])

            self.statusbar.showMessage("Random MAC Address assigned", 2000)
            self.write_conf()
            self.repaint()
        except subprocess.CalledProcessError:
            self.statusbar.showMessage("ERROR: Randomizer failed", 2000)
            self.repaint() 
Example 27
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 28
Project: fs_image   Author: facebookincubator   File: demo_sendstreams.py    MIT License 5 votes vote down vote up
def _populate_sendstream_dict(d):
    d['build_start_time'] = _float_to_sec_nsec_tuple(time.time())
    yield d
    d['dump'] = subprocess.run(
        ['btrfs', 'receive', '--dump'],
        input=d['sendstream'], stdout=subprocess.PIPE, check=True,
        # split into lines to make the `pretty` output prettier
    ).stdout.rstrip(b'\n').split(b'\n')
    d['build_end_time'] = _float_to_sec_nsec_tuple(time.time())


# Takes `path_in_repo` because this is part of the library interface, and
# thus must work in @mode/opt, and thus we cannot use `__file__` here. 
Example 29
Project: fs_image   Author: facebookincubator   File: rpm_metadata.py    MIT License 5 votes vote down vote up
def _repo_query(
        self,
        db_path: Path,
        package_name: str,
        package_path: Path,
    ) -> "RpmMetadata":
        query_args = [
            "rpm",
            "--query",
            "--queryformat",
            "'%{NAME}:%{epochnum}:%{VERSION}:%{RELEASE}'",
        ]

        if db_path and package_name and (package_path is None):
            query_args += ["--dbpath", db_path, package_name]
        elif package_path and (db_path is None and package_name is None):
            query_args += ["--package", package_path]
        else:
            raise ValueError(
                "Must pass only (--dbpath and --package_name) or --package"
            )

        try:
            result = subprocess.check_output(
                query_args,
                stderr=subprocess.PIPE,
            ).decode().strip("'\"")
        except subprocess.CalledProcessError as e:
            raise RuntimeError(f"Error querying RPM: {e.stdout}, {e.stderr}")

        n, e, v, r = result.split(":")
        return RpmMetadata(name=n, epoch=int(e), version=v, release=r)


# Returns  1 if the version of a is newer than b
# Returns  0 if the versions match
# Returns -1 if the version of a is older than b
#
# Referenced from:
# github.com/rpm-software-management/yum/blob/master/rpmUtils/miscutils.py 
Example 30
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 31
Project: fs_image   Author: facebookincubator   File: subvol_utils.py    MIT License 5 votes vote down vote up
def _path_is_btrfs_subvol(path):
    'Ensure that there is a btrfs subvolume at this path. As per @kdave at '
    'https://stackoverflow.com/a/32865333'
    # You'd think I could just `os.statvfs`, but no, not until Py3.7
    # https://bugs.python.org/issue32143
    fs_type = subprocess.run(
        ['stat', '-f', '--format=%T', path],
        stdout=subprocess.PIPE,
    ).stdout.decode().strip()
    ino = os.stat(path).st_ino
    return fs_type == 'btrfs' and ino == 256 
Example 32
Project: fs_image   Author: facebookincubator   File: subvol_utils.py    MIT License 5 votes vote down vote up
def run_as_root(
        self, args, timeout=None, input=None, _subvol_exists=True,
        check=True, **kwargs,
    ):
        '''
        Run a command against an image.  IMPORTANT: You MUST wrap all image
        paths with `Subvol.path`, see that function's docblock.

        Mostly API-compatible with subprocess.run, except that:
            - `check` defaults to True instead of False,
            - `stdout` is redirected to stderr by default,
            - `cwd` is prohibited.
        '''
        # IMPORTANT: Any logic that CAN go in popen_as_root, MUST go there.
        if input:
            assert 'stdin' not in kwargs
            kwargs['stdin'] = subprocess.PIPE
        with self.popen_as_root(
            args, _subvol_exists=_subvol_exists, check=check, **kwargs,
        ) as proc:
            stdout, stderr = proc.communicate(timeout=timeout, input=input)
        return subprocess.CompletedProcess(
            args=proc.args,
            returncode=proc.returncode,
            stdout=stdout,
            stderr=stderr,
        )

    # Future: run_in_image()

    # From here on out, every public method directly maps to the btrfs API.
    # For now, we shell out, but in the future, we may talk to a privileged
    # `btrfsutil` helper, or use `guestfs`. 
Example 33
Project: fs_image   Author: facebookincubator   File: test_subvol_utils.py    MIT License 5 votes vote down vote up
def test_run_as_root_return(self):
        args = ['bash', '-c', 'echo -n my out; echo -n my err >&2']
        r = Subvol('/dev/null/no-such-dir').run_as_root(
            args,
            stdout=subprocess.PIPE, stderr=subprocess.PIPE,
            _subvol_exists=False,
        )
        self.assertEqual(['sudo', 'TMP=', '--'] + args, r.args)
        self.assertEqual(0, r.returncode)
        self.assertEqual(b'my out', r.stdout)
        self.assertEqual(b'my err', r.stderr) 
Example 34
Project: fs_image   Author: facebookincubator   File: test_nspawn_in_subvol.py    MIT License 5 votes vote down vote up
def test_redirects(self):
        cmd = ['--', 'sh', '-c', 'echo ohai && echo abracadabra >&2']
        ret = self._nspawn_in(
            'host', cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
        )
        self.assertEqual(b'ohai\n' + self.maybe_extra_ending, ret.stdout)

        # stderr is not just a clean `abracadabra\n` because we don't
        # suppress nspawn's debugging output, hence the 'assertIn'.
        self.assertIn(b'abracadabra\n', ret.stderr)

        # The same test with `--quiet` is much simpler.
        ret = self._nspawn_in(
            'host', ['--quiet'] + cmd,
            stdout=subprocess.PIPE, stderr=subprocess.PIPE,
        )
        self.assertEqual(b'ohai\n', ret.stdout)

        # Nspawn as of version 242 started dumping a message to stderr when
        # trying to set capabilities that are not currently in the bounding
        # set. In FB production, we drop `cap_sys_boot` and because we do that
        # nspawn (as of version 242) will inform us of that this capability
        # cannot be set.  Since we don't drop `cap_sys_boot` in normal cases
        # we need the 'assertIn' so that test can pass when not run in a
        # container that drops `cap_sys_boot`.
        # T48760757
        self.assertIn(b'abracadabra\n', ret.stderr) 
Example 35
Project: fs_image   Author: facebookincubator   File: test_nspawn_in_subvol.py    MIT License 5 votes vote down vote up
def test_logs_directory(self):
        # The log directory is on by default.
        ret = self._nspawn_in('host', [
            '--', 'sh', '-c',
            'touch /logs/foo && stat --format="%U %G %a" /logs && whoami',
        ], stdout=subprocess.PIPE)
        self.assertEqual(0, ret.returncode)
        self.assertEqual(
            b'nobody nobody 755\nnobody\n' + self.maybe_extra_ending,
            ret.stdout
        )
        # And the option prevents it from being created.
        self.assertEqual(0, self._nspawn_in('host', [
            '--no-logs-tmpfs', '--', 'test', '!', '-e', '/logs',
        ]).returncode) 
Example 36
Project: fs_image   Author: facebookincubator   File: test_nspawn_in_subvol.py    MIT License 5 votes vote down vote up
def test_hostname(self):
        ret = self._nspawn_in('host', [
            '--hostname=test-host.com',
            '--',
            '/bin/hostname',
        ], stdout=subprocess.PIPE, check=True)
        self.assertEqual(b'test-host.com\n', ret.stdout) 
Example 37
Project: fs_image   Author: facebookincubator   File: test_nspawn_in_subvol.py    MIT License 5 votes vote down vote up
def test_tls_environment(self):
        ret = self._nspawn_in('host', [
            '--forward-tls-env', '--',
            'printenv', 'THRIFT_TLS_KITTEH', 'UNENCRYPTED_KITTEH',
        ], stdout=subprocess.PIPE, check=False)
        self.assertNotEqual(0, ret.returncode)  # UNENCRYPTED_KITTEH is unset
        self.assertEqual(b'meow\n' + self.maybe_extra_ending, ret.stdout) 
Example 38
Project: fs_image   Author: facebookincubator   File: test_nspawn_in_subvol.py    MIT License 5 votes vote down vote up
def test_xar(self):
        'Make sure that XAR binaries work in vanilla `buck run` containers'
        ret = self._nspawn_in('host-hello-xar', [
            '--', '/hello.xar',
        ], stdout=subprocess.PIPE, check=True)
        self.assertEqual(b'hello world\n' + self.maybe_extra_ending, ret.stdout) 
Example 39
Project: fs_image   Author: facebookincubator   File: test_nspawn_in_subvol.py    MIT License 5 votes vote down vote up
def test_mknod(self):
        'CAP_MKNOD is dropped by our runtime.'
        ret = self._nspawn_in('host', [
            '--user', 'root', '--quiet', '--', 'mknod', '/foo', 'c', '1', '3',
        ], stderr=subprocess.PIPE, check=False)
        self.assertNotEqual(0, ret.returncode)
        # Note this should be `assertEquals`, but T48760757
        self.assertIn(
            b"mknod: '/foo': Operation not permitted\n", ret.stderr,
        ) 
Example 40
Project: fs_image   Author: facebookincubator   File: test_nspawn_in_subvol.py    MIT License 5 votes vote down vote up
def test_boot_forward_fd(self):
        with tempfile.TemporaryFile() as tf:
            tf.write(b'hello')
            tf.seek(0)
            ret = self._nspawn_in('bootable-systemd-os', [
                '--boot',
                '--forward-fd', str(tf.fileno()),
                '--',
                '/usr/bin/sh',
                '-c',
                '/usr/bin/cat <&3 && /usr/bin/echo goodbye >&3',
            ], stdout=subprocess.PIPE, check=True)
            self.assertEqual(b'hello', ret.stdout)
            tf.seek(0)
            self.assertEqual(b'hellogoodbye\n', tf.read()) 
Example 41
Project: fs_image   Author: facebookincubator   File: test_nspawn_in_subvol.py    MIT License 5 votes vote down vote up
def test_boot_unprivileged_user(self):
        ret = self._nspawn_in('bootable-systemd-os', [
            '--boot',
            '--',
            '/bin/whoami',
        ], stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=True)
        self.assertEqual(0, ret.returncode)
        self.assertEqual(b'nobody\n', ret.stdout)
        self.assertEqual(b'', ret.stderr) 
Example 42
Project: fs_image   Author: facebookincubator   File: test_nspawn_in_subvol.py    MIT License 5 votes vote down vote up
def test_boot_proc_results(self):
        ret = self._nspawn_in('bootable-systemd-os', [
            '--boot',
            '--',
            '/bin/true',
        ], stdout=subprocess.PIPE, check=True)
        self.assertEqual(0, ret.returncode)

        self.assertIsNotNone(ret.boot)
        self.assertIsNotNone(ret.boot.returncode)
        self.assertEqual(0, ret.boot.returncode)
        self.assertIn(b'Welcome to', ret.boot.stdout)
        self.assertIn(b'Reached target', ret.boot.stdout)
        self.assertIn(b'Stopped target', ret.boot.stdout) 
Example 43
Project: fs_image   Author: facebookincubator   File: test_fs_utils.py    MIT License 5 votes vote down vote up
def test_path_from_argparse(self):
        res = subprocess.run([
            sys.executable, '-c', 'import sys;print(repr(sys.argv[1]))',
            _BAD_UTF,
        ], stdout=subprocess.PIPE)
        # Demangle non-UTF bytes in the same way that `sys.argv` mangles them.
        self.assertEqual(_BAD_UTF, Path.from_argparse(
            ast.literal_eval(res.stdout.rstrip(b'\n').decode())
        )) 
Example 44
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 45
Project: fs_image   Author: facebookincubator   File: test_send_fds_and_run.py    MIT License 5 votes vote down vote up
def _run(argv):
    env = os.environ
    env.pop('SUDO_COMMAND', None)  # So we can assert whether `sudo` was used
    with send_fds_and_popen(
        parse_opts(argv),
        env=env, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
    ) as proc:
        stdout, stderr = proc.communicate()
    return proc.returncode, stdout.decode(), stderr.decode() 
Example 46
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 47
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 48
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 49
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 50
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))