Python subprocess.SubprocessError() Examples

The following are 30 code examples of subprocess.SubprocessError(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module subprocess , or try the search function .
Example #1
Source File: test_subprocess.py    From ironpython3 with Apache License 2.0 6 votes vote down vote up
def test_surrogates_error_message(self):
        def prepare():
            raise ValueError("surrogate:\uDCff")

        try:
            subprocess.call(
                [sys.executable, "-c", "pass"],
                preexec_fn=prepare)
        except ValueError as err:
            # Pure Python implementations keeps the message
            self.assertIsNone(subprocess._posixsubprocess)
            self.assertEqual(str(err), "surrogate:\uDCff")
        except subprocess.SubprocessError as err:
            # _posixsubprocess uses a default message
            self.assertIsNotNone(subprocess._posixsubprocess)
            self.assertEqual(str(err), "Exception occurred in preexec_fn.")
        else:
            self.fail("Expected ValueError or subprocess.SubprocessError") 
Example #2
Source File: utils.py    From testcontainers-python with Apache License 2.0 6 votes vote down vote up
def default_gateway_ip():
    """
    Returns gateway IP address of the host that testcontainer process is
    running on

    https://github.com/testcontainers/testcontainers-java/blob/3ad8d80e2484864e554744a4800a81f6b7982168/core/src/main/java/org/testcontainers/dockerclient/DockerClientConfigUtils.java#L27
    """
    cmd = ["sh", "-c", "ip route|awk '/default/ { print $3 }'"]
    try:
        process = subprocess.Popen(cmd, stdout=subprocess.PIPE,
                                   stderr=subprocess.PIPE)
        ip_address = process.communicate()[0]
        if ip_address and process.returncode == 0:
            return ip_address.decode('utf-8').strip().strip('\n')
    except subprocess.SubprocessError:
        return None 
Example #3
Source File: notarize.py    From gridsync with GNU General Public License v3.0 6 votes vote down vote up
def notarization_info(uuid: str, username: str, password: str) -> dict:
    completed_process = run(
        [
            altool,
            "--notarization-info",
            uuid,
            f"--username={username}",
            f"--password={password}",
        ],
        capture_output=True,
        text=True,
    )
    stdout = completed_process.stdout.strip()
    stderr = completed_process.stderr.strip()
    if completed_process.returncode or stderr:
        raise SubprocessError(stderr)
    results = {}
    for line in stdout.split("\n"):
        if line:
            split = line.split(":")
            key = split[0].strip()
            value = ":".join(split[1:]).strip()
            if key and value:
                results[key] = value
    return results 
Example #4
Source File: test_subprocess.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 6 votes vote down vote up
def test_surrogates_error_message(self):
        def prepare():
            raise ValueError("surrogate:\uDCff")

        try:
            subprocess.call(
                [sys.executable, "-c", "pass"],
                preexec_fn=prepare)
        except ValueError as err:
            # Pure Python implementations keeps the message
            self.assertIsNone(subprocess._posixsubprocess)
            self.assertEqual(str(err), "surrogate:\uDCff")
        except subprocess.SubprocessError as err:
            # _posixsubprocess uses a default message
            self.assertIsNotNone(subprocess._posixsubprocess)
            self.assertEqual(str(err), "Exception occurred in preexec_fn.")
        else:
            self.fail("Expected ValueError or subprocess.SubprocessError") 
Example #5
Source File: scm_tag_titles_ext.py    From cheroot with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def _get_scm_timestamp_for(commitish, *, scm=None):
    """Retrieve the tag date from SCM."""
    if scm is None:
        scm = 'git'

    try:
        ts = subprocess.check_output(
            _SCM_COMMANDS[scm] + (commitish, ),
            stderr=subprocess.DEVNULL,
            text=True,
        ).strip()
    except subprocess.SubprocessError:
        raise ValueError(
            f'There is no `{commitish}` in {scm.title()}',
        ) from None

    return dateutil.parser.parse(ts) 
Example #6
Source File: bootstrap.py    From opentelemetry-python with Apache License 2.0 6 votes vote down vote up
def _syscall(func):
    def wrapper(package=None):
        try:
            if package:
                return func(package)
            return func()
        except subprocess.SubprocessError as exp:
            cmd = getattr(exp, "cmd", None)
            if cmd:
                msg = 'Error calling system command "{0}"'.format(
                    " ".join(cmd)
                )
            if package:
                msg = '{0} for package "{1}"'.format(msg, package)
            raise RuntimeError(msg)

    return wrapper 
Example #7
Source File: run.py    From apex-sigma-core with GNU General Public License v3.0 6 votes vote down vote up
def install_requirements():
    """
    Tries to install the pip requirements
    if startup fails due to a missing module.
    :return:
    """
    global modules_installed
    if not modules_installed:
        pip_cmd = ['pip', 'install', '-Ur', 'requirements.txt']
        print('Missing required modules, trying to install them...')
        try:
            subprocess.run(pip_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=True)
            modules_installed = True
        except (OSError, subprocess.SubprocessError):
            print('Requirement update failed!')
            exit(errno.EINVAL)
    else:
        print('Trying to install missing requirements did not work, please contact Sigma\'s developers.')
        exit(errno.EINVAL) 
Example #8
Source File: sysexec.py    From apex-sigma-core with GNU General Public License v3.0 6 votes vote down vote up
def sysexec(cmd, pld):
    """
    :param cmd: The command object referenced in the command.
    :type cmd: sigma.core.mechanics.command.SigmaCommand
    :param pld: The payload with execution data and details.
    :type pld: sigma.core.mechanics.payload.CommandPayload
    """
    response = None
    if pld.args:
        try:
            process = subprocess.run(pld.args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=True)
            await pld.msg.add_reaction('✔')
            response = from_output(process.stdout)
        except (OSError, subprocess.SubprocessError) as e:
            cmd.log.error(e)
            await pld.msg.add_reaction('❗')
    else:
        response = 'No input.'
    if response:
        await pld.msg.channel.send(response) 
Example #9
Source File: package.py    From django-leonardo with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def install_package(package, upgrade=True,
                    target=None):
    """Install a package on PyPi. Accepts pip compatible package strings.

    Return boolean if install successful.
    """
    # Not using 'import pip; pip.main([])' because it breaks the logger
    with INSTALL_LOCK:
        if check_package_exists(package, target):
            return True

        _LOGGER.info('Attempting install of %s', package)
        args = [sys.executable, '-m', 'pip', 'install', '--quiet', package]
        if upgrade:
            args.append('--upgrade')
        if target:
            args += ['--target', os.path.abspath(target)]

        try:
            return subprocess.call(args) == 0
        except subprocess.SubprocessError:
            _LOGGER.exception('Unable to install pacakge %s', package)
            return False 
Example #10
Source File: test_subprocess.py    From Fluid-Designer with GNU General Public License v3.0 6 votes vote down vote up
def test_surrogates_error_message(self):
        def prepare():
            raise ValueError("surrogate:\uDCff")

        try:
            subprocess.call(
                [sys.executable, "-c", "pass"],
                preexec_fn=prepare)
        except ValueError as err:
            # Pure Python implementations keeps the message
            self.assertIsNone(subprocess._posixsubprocess)
            self.assertEqual(str(err), "surrogate:\uDCff")
        except subprocess.SubprocessError as err:
            # _posixsubprocess uses a default message
            self.assertIsNotNone(subprocess._posixsubprocess)
            self.assertEqual(str(err), "Exception occurred in preexec_fn.")
        else:
            self.fail("Expected ValueError or subprocess.SubprocessError") 
Example #11
Source File: scaffold_test.py    From httprunner with Apache License 2.0 6 votes vote down vote up
def test_create_scaffold(self):
        project_name = "projectABC"
        create_scaffold(project_name)
        self.assertTrue(os.path.isdir(os.path.join(project_name, "har")))
        self.assertTrue(os.path.isdir(os.path.join(project_name, "testcases")))
        self.assertTrue(os.path.isdir(os.path.join(project_name, "reports")))
        self.assertTrue(os.path.isfile(os.path.join(project_name, "debugtalk.py")))
        self.assertTrue(os.path.isfile(os.path.join(project_name, ".env")))

        # run demo testcases
        try:
            subprocess.check_call(["hrun", project_name])
        except subprocess.SubprocessError:
            raise
        finally:
            shutil.rmtree(project_name) 
Example #12
Source File: datasets.py    From renku-python with Apache License 2.0 6 votes vote down vote up
def _fetch_lfs_files(self, repo_path, paths):
        """Fetch and checkout paths that are tracked by Git LFS."""
        repo_path = str(repo_path)
        try:
            output = run(('git', 'lfs', 'ls-files', '--name-only'),
                         stdout=PIPE,
                         cwd=repo_path,
                         universal_newlines=True)
        except SubprocessError:
            return

        lfs_files = set(output.stdout.split('\n'))
        files = lfs_files & paths
        if not files:
            return

        try:
            for path in files:
                run(['git', 'lfs', 'pull', '--include', path], cwd=repo_path)
        except KeyboardInterrupt:
            raise
        except SubprocessError:
            pass 
Example #13
Source File: TestBackupChecker.py    From k8s-mongo-operator with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_backup_mongo_error(self, subprocess_mock):
        subprocess_mock.side_effect = CalledProcessError(3, "cmd", "output", "error")
        current_date = datetime(2018, 2, 28, 14, 0, 0)

        with self.assertRaises(SubprocessError) as context:
            self.checker.backup(self.cluster_object, current_date)

        self.assertEqual("Could not backup 'mongo-cluster-2.mongo-cluster.mongo-operator-cluster.svc.cluster.local' to "
                         "'/tmp/mongodb-backup-mongo-operator-cluster-mongo-cluster-2018-02-28_140000.archive.gz'. "
                         "Return code: 3\n stderr: 'error'\n stdout: 'output'",
                         str(context.exception))
        self.assertEqual(1, subprocess_mock.call_count) 
Example #14
Source File: player.py    From discord.py with MIT License 5 votes vote down vote up
def _spawn_process(self, args, **subprocess_kwargs):
        process = None
        try:
            process = subprocess.Popen(args, **subprocess_kwargs)
        except FileNotFoundError:
            executable = args.partition(' ')[0] if isinstance(args, str) else args[0]
            raise ClientException(executable + ' was not found.') from None
        except subprocess.SubprocessError as exc:
            raise ClientException('Popen failed: {0.__class__.__name__}: {0}'.format(exc)) from exc
        else:
            return process 
Example #15
Source File: notarize.py    From gridsync with GNU General Public License v3.0 5 votes vote down vote up
def staple(path: str) -> None:
    p = run([stapler, "staple", path])
    if p.returncode:
        raise SubprocessError(f"Error stapling {path}") 
Example #16
Source File: elevated_server.py    From Pyro5 with MIT License 5 votes vote down vote up
def dmesg(self):
        # reading last 20 lines of the kernel's dmesg buffer... (requires root privilege)
        try:
            result = subprocess.check_output(["dmesg", "--nopager", "--level", "info"])
            return result.decode().splitlines()[-20:]
        except subprocess.SubprocessError as x:
            raise OSError("couldn't run the dmesg command in the server: " + str(x)) 
Example #17
Source File: notarize.py    From gridsync with GNU General Public License v3.0 5 votes vote down vote up
def notarize_app(
    path: str, bundle_id: str, username: str, password: str
) -> str:
    completed_process = run(
        [
            altool,
            "--notarize-app",
            f"--file={path}",
            f"--primary-bundle-id={bundle_id}",
            f"--username={username}",
            f"--password={password}",
        ],
        capture_output=True,
        text=True,
    )
    stdout = completed_process.stdout.strip()
    stderr = completed_process.stderr.strip()
    if completed_process.returncode or stderr:
        s = "The software asset has already been uploaded. The upload ID is "
        if s in stderr:
            print(f"{path} has already been uploaded")
            start = stderr.index(s) + len(s)
            uuid = stderr[start : start + 36]
            return uuid
        raise SubprocessError(stderr)
    if stdout.startswith("No errors uploading"):
        uuid = stdout.split()[-1]
        return uuid 
Example #18
Source File: runner_host.py    From popper with MIT License 5 votes vote down vote up
def _exec_cmd(cmd, env=None, cwd=os.getcwd(), pids=set(), logging=True):
        pid = 0
        try:
            with Popen(
                cmd,
                stdout=PIPE,
                stderr=STDOUT,
                universal_newlines=True,
                preexec_fn=os.setsid,
                env=env,
                cwd=cwd,
            ) as p:
                pid = p.pid
                pids.add(p.pid)
                log.debug("Reading process output")

                output = []
                for line in iter(p.stdout.readline, ""):
                    if logging:
                        log.step_info(line)
                    else:
                        output.append(line)

                p.wait()
                ecode = p.poll()

            log.debug(f"Code returned by process: {ecode}")

        except SubprocessError as ex:
            output = ""
            ecode = ex.returncode
            log.step_info(f"Command '{cmd[0]}' failed with: {ex}")
        except Exception as ex:
            output = ""
            ecode = 1
            log.step_info(f"Command raised non-SubprocessError error: {ex}")

        return pid, ecode, "\n".join(output) 
Example #19
Source File: deps.py    From httprunner with Apache License 2.0 5 votes vote down vote up
def install_dependenies(deps: List[str]):
    resp = {"code": 0, "message": "success", "result": {}}
    for dep in deps:
        try:
            p = subprocess.run(["pip", "install", dep])
            assert p.returncode == 0
            resp["result"][dep] = True
        except (AssertionError, subprocess.SubprocessError):
            resp["result"][dep] = False
            resp["code"] = 1
            resp["message"] = "fail"
            logger.error(f"failed to install dependency: {dep}")

    return resp 
Example #20
Source File: test_subprocess.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
def test_preexec_errpipe_does_not_double_close_pipes(self):
        """Issue16140: Don't double close pipes on preexec error."""

        def raise_it():
            raise subprocess.SubprocessError(
                    "force the _execute_child() errpipe_data path.")

        with self.assertRaises(subprocess.SubprocessError):
            self._TestExecuteChildPopen(
                        self, [sys.executable, "-c", "pass"],
                        stdin=subprocess.PIPE, stdout=subprocess.PIPE,
                        stderr=subprocess.PIPE, preexec_fn=raise_it) 
Example #21
Source File: test_subprocess.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
def test_preexec_exception(self):
        def raise_it():
            raise ValueError("What if two swallows carried a coconut?")
        try:
            p = subprocess.Popen([sys.executable, "-c", ""],
                                 preexec_fn=raise_it)
        except subprocess.SubprocessError as e:
            self.assertTrue(
                    subprocess._posixsubprocess,
                    "Expected a ValueError from the preexec_fn")
        except ValueError as e:
            self.assertIn("coconut", e.args[0])
        else:
            self.fail("Exception raised by preexec_fn did not make it "
                      "to the parent process.") 
Example #22
Source File: generate_output.py    From flask-rebar with MIT License 5 votes vote down vote up
def app():
    print("Starting app...")

    app_process = None
    try:
        app_process = subprocess.Popen(
            args=["python", todo_app_filepath],
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
        )

        # Wait for the app to startup by polling the service
        up = False
        while not up:
            try:
                subprocess.check_output(
                    "curl -s http://127.0.0.1:5000/swagger", shell=True
                )
                up = True
            except subprocess.SubprocessError:
                pass

        yield

    finally:
        if app_process:
            app_process.terminate() 
Example #23
Source File: build.py    From sockeye with Apache License 2.0 5 votes vote down vote up
def check_command(cmd):
    try:
        retcode = subprocess.call([cmd, '--version'])
    except FileNotFoundError:
        retcode = None
    if retcode != 0:
        msg = 'Please install {}'.format(cmd)
        raise subprocess.SubprocessError(msg) 
Example #24
Source File: server.py    From opencraft with GNU Affero General Public License v3.0 5 votes vote down vote up
def _delete_ssh_key(self) -> None:
        """
        Delete SSH key from `~/.ssh/known_hosts`.

        We can safely ignore the command's return code, because we just need to be sure that the key has been removed
        for non-existing server - we don't care about non-existing keys.
        """
        self.logger.info('Deleting SSH key of "%s" host.', self.public_ip)
        command = f'ssh-keygen -R {self.public_ip}'
        try:
            subprocess.Popen(command, shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
        except subprocess.SubprocessError as e:
            self.logger.error('Failed to delete SSH key of "%s" host: %s', self.public_ip, e) 
Example #25
Source File: renku.py    From renku-python with Apache License 2.0 5 votes vote down vote up
def _get_file_size(self, remote_client, path):
        # Try to get file size from Git LFS
        try:
            lfs_run = run(('git', 'lfs', 'ls-files', '--name-only', '--size'),
                          stdout=PIPE,
                          cwd=remote_client.path,
                          universal_newlines=True)
        except SubprocessError:
            pass
        else:
            lfs_output = lfs_run.stdout.split('\n')
            # Example line format: relative/path/to/file (7.9 MB)
            pattern = re.compile(r'.*\((.*)\)')
            for line in lfs_output:
                if path not in line:
                    continue
                match = pattern.search(line)
                if not match:
                    continue
                size_info = match.groups()[0].split()
                if len(size_info) != 2:
                    continue
                try:
                    size = float(size_info[0])
                except ValueError:
                    continue
                unit = size_info[1].strip().lower()
                conversions = {'b': 1, 'kb': 1e3, 'mb': 1e6, 'gb': 1e9}
                multiplier = conversions.get(unit, None)
                if multiplier is None:
                    continue
                return size * multiplier

        # Return size of the file on disk
        full_path = remote_client.path / path
        return os.path.getsize(full_path) 
Example #26
Source File: dataset_files.py    From renku-python with Apache License 2.0 5 votes vote down vote up
def _get_lfs_file_sizes(client, records):
    # Try to get file size from Git LFS
    files_sizes = {}

    try:
        lfs_run = run(('git', 'lfs', 'ls-files', '--name-only', '--size'),
                      stdout=PIPE,
                      cwd=client.path,
                      universal_newlines=True)
    except SubprocessError:
        pass
    else:
        lfs_output = lfs_run.stdout.split('\n')
        # Example line format: relative/path/to/file (7.9 MB)
        pattern = re.compile(r'^(.*?)\s*\((.*)\)')

        for line in lfs_output:
            match = pattern.search(line)
            if not match:
                continue
            filepath, size = match.groups()
            # Fix alignment for bytes
            if size.endswith(' B'):
                size = size.replace(' B', '  B')
            files_sizes[filepath] = size

    for record in records:
        size = files_sizes.get(record.path)
        if size is None:
            try:
                path = client.path / record.path
                size = os.path.getsize(path)
                size = humanize.naturalsize(size).upper()
                size = size.replace('BYTES', ' B')
            except OSError:
                pass

        record.size = size 
Example #27
Source File: firewall.py    From openpyn-nordvpn with GNU General Public License v3.0 5 votes vote down vote up
def manage_ipv6(disable: bool) -> None:
    value = 1 if disable else 0
    try:
        subprocess.check_call(
            ["sudo", "sysctl", "-w", "net.ipv6.conf.all.disable_ipv6={}".format(value)],
            stdout=subprocess.DEVNULL)
    except subprocess.SubprocessError:      # in case systemd is not used
        logger.warning("Cant disable/enable ipv6 using sysctl, are you even using systemd?")


# Clears Firewall rules, applies basic rules. 
Example #28
Source File: BackupHelper.py    From k8s-mongo-operator with GNU Affero General Public License v3.0 5 votes vote down vote up
def backup(self, cluster_object: V1MongoClusterConfiguration, now: datetime):
        """
        Creates a new backup for the given cluster saving it in the cloud storage.
        :param cluster_object: The cluster object from the YAML file.
        :param now: The current date, used in the date format.
        """
        backup_file = "/tmp/" + self.BACKUP_FILE_FORMAT.format(namespace=cluster_object.metadata.namespace,
                                                               name=cluster_object.metadata.name,
                                                               date=now.strftime("%Y-%m-%d_%H%M%S"))

        pod_index = cluster_object.spec.mongodb.replicas - 1  # take last pod
        hostname = MongoResources.getMemberHostname(pod_index, cluster_object.metadata.name,
                                                    cluster_object.metadata.namespace)

        logging.info("Backing up cluster %s @ ns/%s from %s to %s.", cluster_object.metadata.name,
                     cluster_object.metadata.namespace, hostname, backup_file)

        try:
            backup_output = check_output(["mongodump", "--host", hostname, "--gzip", "--archive=" + backup_file])
        except CalledProcessError as err:
            raise SubprocessError("Could not backup '{}' to '{}'. Return code: {}\n stderr: '{}'\n stdout: '{}'"
                                  .format(hostname, backup_file, err.returncode, err.stderr, err.stdout))

        logging.debug("Backup output: %s", backup_output)

        self._uploadBackup(cluster_object, backup_file)
        os.remove(backup_file) 
Example #29
Source File: file_analysis.py    From lbry-sdk with MIT License 5 votes vote down vote up
def _execute(command, environment):
        # log.debug("Executing: %s", command)
        try:
            with subprocess.Popen(
                    shlex.split(command) if platform.system() != 'Windows' else command,
                    stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=environment
            ) as process:
                (stdout, stderr) = process.communicate()  # blocks until the process exits
                return stdout.decode(errors='replace') + stderr.decode(errors='replace'), process.returncode
        except subprocess.SubprocessError as e:
            return str(e), -1

    # This create_subprocess_exec call is broken in Windows Python 3.7, but it's prettier than what's here.
    # The recommended fix is switching to ProactorEventLoop, but that breaks UDP in Linux Python 3.7.
    # We work around that issue here by using run_in_executor. Check it again in Python 3.8. 
Example #30
Source File: icmp_sweep.py    From TOOLS with GNU General Public License v3.0 5 votes vote down vote up
def _icmp(self, ip_address):
        try:
            run(f'ping -c 1 -W 0.51 {ip_address}',
                shell=True, check=True, stdout=DEVNULL, stderr=DEVNULL)
        except SubprocessError:
            pass
        else:
            self._reachable_hosts.append(ip_address)

        with self._counter_lock:
            self._completed_count += 1
            self._progress()