Python subprocess32.CalledProcessError() Examples

The following are 30 code examples of subprocess32.CalledProcessError(). 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 subprocess32 , or try the search function .
Example #1
Source File: netns.py    From libcalico with Apache License 2.0 6 votes vote down vote up
def veth_exists(veth_name_host):
    """
    Check if the veth exists on the host.
    :param veth_name_host: The name of the veth interface.
    :return: True if veth exists, False if veth does not exist
    """
    # Suppress output
    with open(os.devnull, 'w') as fnull:
        try:
            check_call(["ip", "link", "show", veth_name_host],
                       stderr=fnull,
                       stdout=fnull)
            return True
        except CalledProcessError:
            # veth does not exist
            return False 
Example #2
Source File: netns.py    From libcalico with Apache License 2.0 6 votes vote down vote up
def create_veth(veth_name_host, veth_name_ns_temp):
    """
    Create the veth (pair).
    :param veth_name_host: The name of the veth interface
    :param veth_name_ns_temp: The temporary interface name of the veth that will be
    moved into the namespace.
    :return: None. Raises CalledProcessError on error.
    """
    # Create the veth
    _log.debug("Creating veth %s in temp_ns: %s", veth_name_host, veth_name_ns_temp)
    check_output(['ip', 'link',
                'add', veth_name_host,
                'type', 'veth',
                'peer', 'name', veth_name_ns_temp],
               timeout=IP_CMD_TIMEOUT)

    # Set the host end of the veth to 'up' so felix notices it.
    check_output(['ip', 'link', 'set', veth_name_host, 'up'],
               timeout=IP_CMD_TIMEOUT) 
Example #3
Source File: archives.py    From instaclone with Apache License 2.0 6 votes vote down vote up
def _autodetect_unzip_command():
  unzip_cmd = None
  unzip_output = None
  try:
    unzip_output = subprocess.check_output(["unzip", "-v"])
    unzip_cmd = "unzip -q $ARCHIVE"
  except subprocess.CalledProcessError as e:
    pass

  # On MacOS Yosemite, unzip does not support Zip64, but ditto is available.
  # See: https://github.com/vivlabs/instaclone/issues/1
  if not unzip_cmd or not unzip_output or unzip_output.find("ZIP64_SUPPORT") < 0:
    log.debug("did not find 'unzip' with Zip64 support; trying ditto")
    try:
      # ditto has no simple flag to check its version and exit with 0 status code.
      subprocess.check_call(["ditto", "-c", "/dev/null", tempfile.mktemp()])
      unzip_cmd = "ditto -x -k $ARCHIVE ."
    except subprocess.CalledProcessError as e:
      log.debug("did not find ditto")

  if not unzip_cmd:
    raise ArchiveError("Archive handling requires 'unzip' or 'ditto' in path")

  log.debug("unzip command: %s", unzip_cmd)
  return unzip_cmd 
Example #4
Source File: ioskit.py    From ATX with Apache License 2.0 6 votes vote down vote up
def screenshot(self, filename=None):
        """
        Return:
            PIL.Image
            
        Raises:
            EnvironmentError
        """
        tmpfile = tempfile.mktemp(prefix='atx-screencap-', suffix='.tiff')
        try:
            idevice("screenshot", "--udid", self.udid, tmpfile)
        except subprocess.CalledProcessError as e:
            sys.exit(e.message)

        try:
            image = Image.open(tmpfile)
            image.load()
            if filename:
                image.save(filename)
            return image
        finally:
            if os.path.exists(tmpfile):
                os.unlink(tmpfile) 
Example #5
Source File: kube_plugin_test.py    From k8s-exec-plugin with Apache License 2.0 6 votes vote down vote up
def test_create_error(self):
        """Test Pod Creation Hook Failure"""
        with patch_object(self.plugin, '_configure_interface',
                          autospec=True) as m_configure_interface:
            # Set up mock objects
            m_configure_interface.side_effect = CalledProcessError(1,'','')
            self.plugin.delete = MagicMock(spec=self.plugin.delete)

            # Set up args
            namespace = 'ns'
            pod_name = 'pod1'
            docker_id = 13

            # Call method under test
            assert_raises(SystemExit, self.plugin.create, namespace, pod_name, docker_id)
            self.plugin.delete.assert_called_once_with(namespace, pod_name, docker_id) 
Example #6
Source File: kube_plugin_test.py    From k8s-exec-plugin with Apache License 2.0 6 votes vote down vote up
def test_create_error_delete_error(self):
        """Test Error in Pod Creation Hook Failure
        Tests that we handle errors when cleaning up gracefully.
        """
        with patch_object(self.plugin, '_configure_interface',
                          autospec=True) as m_configure_interface:
            # Set up mock objects
            m_configure_interface.side_effect = CalledProcessError(1,'','')
            self.plugin.delete = MagicMock(spec=self.plugin.delete)
            self.plugin.delete.side_effect = KeyError

            # Set up args
            namespace = 'ns'
            pod_name = 'pod1'
            docker_id = 13

            # Call method under test
            assert_raises(SystemExit, self.plugin.create, namespace, pod_name, docker_id)
            self.plugin.delete.assert_called_once_with(namespace, pod_name, docker_id) 
Example #7
Source File: execActuator.py    From sensorReporter with Apache License 2.0 6 votes vote down vote up
def on_message(self, client, userdata, msg):
        """Process a message"""
        self.logger.info('Received command on {0}: {1}'.format(self.cmdTopic, msg.payload))
        
        inArgs = msg.payload.split(' ')
        cmdArgs = []
        for arg in self.command.split(' '):
          if arg.find(';') == -1 or arg.find('|') == -1 or arg.find('//') == -1:
            cmdArgs.append(arg)
        for arg in inArgs:
          if arg != 'NA' and arg.find(';') == -1 and arg.find('|') == -1 and arg.find('//') == -1:
            cmdArgs.append(arg)

        self.logger.info('Executing command with the following arguments: {0}'.format(cmdArgs))
        try:
          output = subprocess.check_output(cmdArgs, shell=False, universal_newlines=True)
          self.logger.info('Command results to be published to {0}\n{1}'.format(self.pubTopic, output))
          self.publishImpl(output, self.pubTopic)
        except subprocess.CalledProcessError as e:
          self.logger.info('Command returned an error code: {0}\n{1}'.format(e.returncode, e.output))
          self.publishImpl('ERROR', self.pubTopic) 
Example #8
Source File: ioskit.py    From ATX with Apache License 2.0 5 votes vote down vote up
def check_output(cmds, shell=False):
    try:
        output = subprocess.check_output(cmds, stderr=subprocess.STDOUT, shell=shell)
        return output
    except subprocess.CalledProcessError:
        # logger.warn('Failed to run command: %s', ' '.join(cmds))
        # logger.warn('Error output:\n%s', e.output)
        raise 
Example #9
Source File: netns.py    From libcalico with Apache License 2.0 5 votes vote down vote up
def remove_veth(veth_name_host):
    """
    Remove the veth (pair).
    :param veth_name_host: The name of the veth interface.
    :return: True if veth was removed.  False if veth does not exist.
             Raises CalledProcessError on error.
    """
    # The veth removal is best effort. If it fails then just log.
    if not veth_exists(veth_name_host):
        return False
    check_output(['ip', 'link', 'del', veth_name_host],
               timeout=IP_CMD_TIMEOUT)
    return True 
Example #10
Source File: flite.py    From epitran with MIT License 5 votes vote down vote up
def english_g2p(self, text):
        text = self.normalize(text)
        try:
            arpa_text = subprocess.check_output(['t2p', '"{}"'.format(text)])
            arpa_text = arpa_text.decode('utf-8')
        except OSError:
            logging.warning('t2p (from flite) is not installed.')
            arpa_text = ''
        except subprocess.CalledProcessError:
            logging.warning('Non-zero exit status from t2p.')
            arpa_text = ''
        return self.arpa_to_ipa(arpa_text) 
Example #11
Source File: flite.py    From epitran with MIT License 5 votes vote down vote up
def english_g2p(self, text):
        text = self.normalize(text).lower()
        try:
            arpa_text = subprocess.check_output(['lex_lookup', text])
            arpa_text = arpa_text.decode('utf-8')
        except OSError:
            logging.warning('lex_lookup (from flite) is not installed.')
            arpa_text = ''
        except subprocess.CalledProcessError:
            logging.warning('Non-zero exit status from lex_lookup.')
            arpa_text = ''
        # Split on newlines and take the first element (in case lex_lookup
        # returns multiple lines).
        arpa_text = arpa_text.splitlines()[0]
        return self.arpa_to_ipa(arpa_text) 
Example #12
Source File: concurrency.py    From petridishnn with MIT License 5 votes vote down vote up
def subproc_call(cmd, timeout=None):
    """
    Execute a command with timeout, and return both STDOUT/STDERR.

    Args:
        cmd(str): the command to execute.
        timeout(float): timeout in seconds.

    Returns:
        output(bytes), retcode(int). If timeout, retcode is -1.
    """
    try:
        output = subprocess.check_output(
            cmd, stderr=subprocess.STDOUT,
            shell=True, timeout=timeout)
        return output, 0
    except subprocess.TimeoutExpired as e:
        logger.warn("Command '{}' timeout!".format(cmd))
        logger.warn(e.output.decode('utf-8'))
        return e.output, -1
    except subprocess.CalledProcessError as e:
        logger.warn("Command '{}' failed, return code={}".format(cmd, e.returncode))
        logger.warn(e.output.decode('utf-8'))
        return e.output, e.returncode
    except Exception:
        logger.warn("Command '{}' failed to run.".format(cmd))
        return "", -2 
Example #13
Source File: zip_getter.py    From depsy with MIT License 5 votes vote down vote up
def _grep_for_dep_lines(self, query_str, include_globs, exclude_globs):
        arg_list =['zipgrep', query_str, self.temp_file_name]
        arg_list += include_globs
        arg_list.append("-x")
        arg_list += exclude_globs
        start = time()

        try:
            print "Running zipgrep: '{}'".format(" ".join(arg_list))
            self.dep_lines = subprocess32.check_output(
                arg_list,
                timeout=90
            )

        except subprocess32.CalledProcessError:
            # heroku throws an error here when there are no dep lines to find.
            # but it's fine. there just aren't no lines.
            pass

        except subprocess32.TimeoutExpired:
            # too many files, we'll skip it and move on.
            self.error = "grep_timeout"
            pass

        finally:
            self.grep_elapsed = elapsed(start, 4)
            #print "found these dep lines: {}".format(self.dep_lines)
            print "finished dep lines search in {} sec".format(self.grep_elapsed) 
Example #14
Source File: execSensor.py    From sensorReporter with Apache License 2.0 5 votes vote down vote up
def checkState(self):
        """calls the script and publishes the result"""

        self.logger.info('Executing script with the following arguments: {0}'.format(self.cmdArgs))
        try:
            self.results = subprocess.check_output(self.cmdArgs, shell=False, universal_newlines=True)
            self.logger.info('Command results to be published to {0}\n{1}'.format(self.dest, self.results))
        except subprocess.CalledProcessError as e:
            self.logger.warn('Command returned an error code: {0}\n{1}'.format(e.returncode, e.output))
            self.results = 'Error'

        self.publishState() 
Example #15
Source File: netns.py    From libcalico with Apache License 2.0 5 votes vote down vote up
def set_veth_mac(veth_name_host, mac):
    """
    Set the veth MAC address.
    :param veth_name_host: The name of the veth.
    :param mac: The MAC address.
    :return: None. Raises CalledProcessError on error.
    """
    #TODO MAC should be an EUI object.
    check_output(['ip', 'link', 'set',
                'dev', veth_name_host,
                'address', mac],
               timeout=IP_CMD_TIMEOUT) 
Example #16
Source File: ioskit.py    From ATX with Apache License 2.0 5 votes vote down vote up
def idevice(name, *args):
    exec_name = 'idevice' + name
    exec_path = look_exec(exec_name)
    if not exec_path:
        raise EnvironmentError('Necessary binary ("%s") not found.' % exec_name)

    cmds = [exec_path] + list(args)
    try:
        output = subprocess.check_output(cmds, stderr=subprocess.STDOUT, shell=False)
        return output
    except subprocess.CalledProcessError:
        raise 
Example #17
Source File: install.py    From ATX with Apache License 2.0 5 votes vote down vote up
def get_file_size(adb, remote_path):
    try:
        output = adb.run_cmd('shell', 'ls', '-l', remote_path)
        m = re.search(r'\s(\d+)', output)
        if not m:
            return 0
        return int(m.group(1))
    except subprocess.CalledProcessError as e:
        log.warn("call error: %s", e)
        time.sleep(.1)
        return 0 
Example #18
Source File: concurrency.py    From Distributed-BA3C with Apache License 2.0 5 votes vote down vote up
def subproc_call(cmd, timeout=None):
    try:
        output = subprocess.check_output(
                cmd, stderr=subprocess.STDOUT,
                shell=True, timeout=timeout)
        return output
    except subprocess.TimeoutExpired as e:
        logger.warn("Command timeout!")
        logger.warn(e.output)
    except subprocess.CalledProcessError as e:
        logger.warn("Commnad failed: {}".format(e.returncode))
        logger.warn(e.output) 
Example #19
Source File: concurrency.py    From ternarynet with Apache License 2.0 5 votes vote down vote up
def subproc_call(cmd, timeout=None):
    try:
        output = subprocess.check_output(
                cmd, stderr=subprocess.STDOUT,
                shell=True, timeout=timeout)
        return output
    except subprocess.TimeoutExpired as e:
        logger.warn("Command timeout!")
        logger.warn(e.output)
    except subprocess.CalledProcessError as e:
        logger.warn("Commnad failed: {}".format(e.returncode))
        logger.warn(e.output) 
Example #20
Source File: netns.py    From libcalico with Apache License 2.0 5 votes vote down vote up
def increment_metrics(namespace):
    """
    If any default route has a metric of 0, increase the metric of
    all default routes by 1, so long as it can be done without breaking
    uniqueness or surpassing the max metric value.
    :param namespace: The Networking namespace of the container.
    :return: None. Raises CalledProcessError on error.
    """
    with NamedNamespace(namespace) as ns:
        # Gather all default routes
        routes = ns.check_output(["ip", "route"]).split("\n")
        default_routes = {}
        for route in routes:
            route = Route(route)
            if route.default:
                default_routes[route.metric] = route

        # Increment default routes (if a 0-metric default exists)
        if 0 in default_routes:
            # Order routes descending by metric so, while incrementing,
            # no 2 routes temporarily have the same metric.
            descending_routes = sorted(default_routes.items(),
                                       key=lambda metric: -metric[0])
            assigned_metrics = []
            for metric, route in descending_routes:
                if metric + 1 >= MAX_METRIC or metric + 1 in assigned_metrics:
                    # Don't increment this metric
                    assigned_metrics.append(metric)
                else:
                    # Increment this metric.
                    original_route = copy(route)
                    route.metric += 1

                    ns.check_output(["ip", "route", "add"] + str(route).split())
                    ns.check_output(["ip", "route", "del"] +
                                    str(original_route).split())
                    assigned_metrics.append(metric + 1) 
Example #21
Source File: tune_ray.py    From blueoil with Apache License 2.0 5 votes vote down vote up
def subproc_call(cmd, timeout=None):
    """Execute a command with timeout, and return both STDOUT/STDERR.

    Args:
        cmd (str): the command to execute.
        timeout (float): timeout in seconds.

    Returns:
        output (bytes), retcode(int): If timeout, retcode is -1.

    """
    try:
        output = subprocess.check_output(
            cmd, stderr=subprocess.STDOUT,
            shell=True, timeout=timeout)
        return output, 0
    except subprocess.TimeoutExpired as e:
        print("Command '{}' timeout!".format(cmd))
        print(e.output.decode('utf-8'))
        return e.output, -1
    except subprocess.CalledProcessError as e:
        print("Command '{}' failed, return code={}".format(cmd, e.returncode))
        print(e.output.decode('utf-8'))
        return e.output, e.returncode
    except Exception:
        print("Command '{}' failed to run.".format(cmd))
        return "", -2 
Example #22
Source File: _compat.py    From poetry with MIT License 5 votes vote down vote up
def check_returncode(self):
            """Raise CalledProcessError if the exit code is non-zero."""
            if self.returncode:
                raise CalledProcessError(
                    self.returncode, self.args, self.stdout, self.stderr
                ) 
Example #23
Source File: subproc.py    From treadmill with Apache License 2.0 5 votes vote down vote up
def check_output(cmdline, environ=(), **kwargs):
    """Runs command wrapping subprocess.check_output.

    :param cmdline:
        Command to run
    :type cmdline:
        ``list``
    :param environ:
        *optional* Environ variable to set prior to running the command
    :type environ:
        ``dict``
    """
    _LOGGER.debug('check_output environ: %r, %r', environ, cmdline)
    args = _alias_command(cmdline)

    # Setup a copy of the environ with the provided overrides
    cmd_environ = dict(os.environ.items())
    cmd_environ.update(environ)

    try:
        res = subprocess.check_output(args,
                                      close_fds=_CLOSE_FDS,
                                      env=cmd_environ,
                                      **kwargs)

        _LOGGER.debug('Finished.')
    except CalledProcessError as exc:
        _LOGGER.error('Command failed: rc:%d: %s', exc.returncode, exc.output)
        raise

    # Decode output back into unicode
    res = res.decode()

    return res 
Example #24
Source File: archives.py    From instaclone with Apache License 2.0 5 votes vote down vote up
def _autodetect_zip_command():
  try:
    zip_output = subprocess.check_output(["zip", "-v"])
    zip_cmd = "zip -q -r $ARCHIVE $DIR"
  except subprocess.CalledProcessError as e:
    raise ArchiveError("Archive handling requires 'zip' in path: %s" % e)

  if zip_output.find("ZIP64_SUPPORT") < 0:
    log.warn("installed 'zip' doesn't have Zip64 support so will fail for large archives")
  log.debug("zip command: %s", zip_cmd)
  return zip_cmd 
Example #25
Source File: concurrency.py    From DDRL with Apache License 2.0 5 votes vote down vote up
def subproc_call(cmd, timeout=None):
    try:
        output = subprocess.check_output(
                cmd, stderr=subprocess.STDOUT,
                shell=True, timeout=timeout)
        return output
    except subprocess.TimeoutExpired as e:
        logger.warn("Command timeout!")
        logger.warn(e.output)
    except subprocess.CalledProcessError as e:
        logger.warn("Commnad failed: {}".format(e.returncode))
        logger.warn(e.output) 
Example #26
Source File: subproc.py    From treadmill with Apache License 2.0 5 votes vote down vote up
def check_call(cmdline, environ=(), runas=None, **kwargs):
    """Runs command wrapping subprocess.check_call.

    :param cmdline:
        Command to run
    :type cmdline:
        ``list``
    :param environ:
        *optional* Environ variable to set prior to running the command
    :type environ:
        ``dict``
    :param runas:
        *optional* Run as user.
    :type runas:
        ``str``
    """
    _LOGGER.debug('check_call environ: %r, runas: %r, %r',
                  environ, runas, cmdline)

    args = _alias_command(cmdline)
    if runas:
        s6_setguid = _resolve('s6_setuidgid')
        args = s6_setguid + [runas] + args

    # Setup a copy of the environ with the provided overrides
    cmd_environ = dict(os.environ.items())
    cmd_environ.update(environ)

    try:
        rc = subprocess.check_call(args, close_fds=_CLOSE_FDS, env=cmd_environ,
                                   **kwargs)
        _LOGGER.debug('Finished, rc: %d', rc)
        return rc
    except CalledProcessError as exc:
        _LOGGER.error('Command failed: rc:%d', exc.returncode)
        raise 
Example #27
Source File: calico_kubernetes.py    From k8s-exec-plugin with Apache License 2.0 5 votes vote down vote up
def _remove_endpoint(self, endpoint):
        """
        Remove the provided endpoint on this host from Calico networking.
        - Removes any IP address assignments.
        - Removes the veth interface for this endpoint.
        - Removes the endpoint object from etcd.
        """
        # Remove any IP address assignments that this endpoint has
        ip_set = set()
        for net in endpoint.ipv4_nets | endpoint.ipv6_nets:
            ip_set.add(net.ip)
        logger.info("Removing IP addresses %s from endpoint %s",
                    ip_set, endpoint.name)
        self._datastore_client.release_ips(ip_set)

        # Remove the veth interface from endpoint
        logger.info("Removing veth interfaces")
        try:
            netns.remove_veth(endpoint.name)
        except CalledProcessError:
            logger.exception("Could not remove veth interface from "
                             "endpoint %s", endpoint.name)

        # Remove endpoint from the datastore.
        try:
            self._datastore_client.remove_workload(
                HOSTNAME, ORCHESTRATOR_ID, self.docker_id)
        except KeyError:
            logger.exception("Error removing workload.")
        logger.info("Removed Calico endpoint %s", endpoint.endpoint_id) 
Example #28
Source File: kube_plugin_test.py    From k8s-exec-plugin with Apache License 2.0 5 votes vote down vote up
def test_remove_endpoint_with_exceptions(self, m_remove_veth):
        """Test Container Remove Exception Handling

        Failures in remove_veth and remove_workload should gently raise exceptions without exit.
        """
        # Raise errors under test.
        m_remove_veth.side_effect = CalledProcessError(1, '', '')
        self.m_datastore_client.remove_workload.side_effect = KeyError

        self.plugin._remove_endpoint(MagicMock()) 
Example #29
Source File: kube_plugin_test.py    From k8s-exec-plugin with Apache License 2.0 5 votes vote down vote up
def test_log_error(self):
        with patch('calico_kubernetes.tests.kube_plugin_test.'
                   'calico_kubernetes.check_output',
                   autospec=True) as m_check_output:
            # Mock to throw Exception
            m_check_output.side_effect = CalledProcessError

            # Call function, assert Exception is caught.
            _log_interfaces("12345") 
Example #30
Source File: stage_wrapper.py    From SVE with GNU General Public License v3.0 5 votes vote down vote up
def run(self,run_id,inputs={}):
        in_file = ''
        
        #retrieve all the parameters and map them to the series of calls to be executed...
        command = ['ls','-als']
        self.db_start(run_id,in_file,self.params)
        
        #workflow is to run through the stage correctly and then check for error handles
        #[3a]execute the command here----------------------------------------------------
        output,err = '',{}
        try:
            output = subprocess.check_output(command,stderr=subprocess.STDOUT)
        except subprocess.CalledProcessError as E:
            print('ouput: '+E.output)             #what you would see in the term
            err['output'] = E.output
            #the python exception issues (shouldn't have any...
            print('message: '+E.message)          #?? empty
            err['message'] = E.message
            #return codes used for failure....
            print('code: '+str(E.returncode))     #return 1 for a fail in art?
            err['code'] = E.returncode
        except OSError as E:
            print('ouput: '+E.strerror)             #what you would see in the term
            err['output'] = E.strerror
            #the python exception issues (shouldn't have any...
            print('message: '+E.message)          #?? empty
            err['message'] = E.message
            #the error num
            print('code: '+str(E.errno))
            err['code'] = E.errno
        print('output:\n'+output)
        #[3a]execute the command here----------------------------------------------------
        #[3b]do a os directory/data check or a ls type command to check the size
        #[3b]of the produced data to ensure that everything is fine...        
        if err == {}:
            self.db_stop(run_id,{'output':output},'',True)
            return output
        else:
            self.db_stop(run_id,{'output':output},err['message'],False)
            return None