Python subprocess32.check_output() Examples

The following are 30 code examples of subprocess32.check_output(). 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: afl_sancov.py    From orthrus with GNU General Public License v3.0 6 votes vote down vote up
def parent_identical_or_crashes(self, crash, parent):

        # Base names
        cbasename = os.path.basename(crash)
        pbasename = os.path.basename(parent)

        ## Filter queue filenames with sig info
        if self.find_crash_parent_regex.match(pbasename):
            self.logr("Parent ({}) looks like crashing input!".format(pbasename))
            return True

        try:
            diff_out = subprocess.check_output("diff -q {} {}".format(crash, parent),
                                               stderr=subprocess.STDOUT, shell=True)
        except Exception, e:
            diff_out = e.output 
Example #2
Source File: bwa_split.py    From SVE with GNU General Public License v3.0 6 votes vote down vote up
def fastq_bwa_mem_piped(fastqs,i,j,t,rg,out_dir,ref_path):
    output = ''
    bam = out_dir+'/'+fastqs[0].rsplit('/')[-1].rsplit('.fq')[0].rsplit('_')[0]+'.%s.bam'%j
    piped_mem = [bwa_mem,'-M','-t %s'%t,'-R',r"'%s'"%rg,ref_path,
                 '<(%s -f %s -i %s -j %s)'%(route,fastqs[0],i,j),
                 '<(%s -f %s -i %s -j %s)'%(route,fastqs[1],i,j),
                 '|',samtools_view,'-Sb','-','-@ %s'%t,
                 '|',sambamba_sort,'-t %s'%t,'--tmpdir=%s/temp'%out_dir,'-o',bam,'/dev/stdin']  #-@ for threads here
    try:#bwa mem call here-------------------------------------------
        output += subprocess.check_output(' '.join(piped_mem),
                                          stderr=subprocess.STDOUT,
                                          executable='/bin/bash',
                                          shell=True)
        output += subprocess.check_output(['rm','-rf','%s/temp'%out_dir])
    except Exception as E:
        output += str(E) #error on the call-------------------------
    return bam #list of bam files to merge into next step

    
#|| by number of fastq files presented 
Example #3
Source File: test_subscription_transport.py    From graphql-python-subscriptions with MIT License 6 votes vote down vote up
def test_should_trigger_on_connect_with_correct_cxn_params(server_with_mocks):
    node_script = '''
        module.paths.push('{0}')
        WebSocket = require('ws')
        const SubscriptionClient =
        require('subscriptions-transport-ws').SubscriptionClient
        const connectionParams = {{test: true}}
        new SubscriptionClient('ws://localhost:{1}/socket', {{
        connectionParams,
        }})
    '''.format(
        os.path.join(os.path.dirname(__file__), 'node_modules'), TEST_PORT)
    try:
        subprocess.check_output(
            ['node', '-e', node_script], stderr=subprocess.STDOUT, timeout=.2)
    except:
        mock = server_with_mocks.get_nowait()
        assert mock.name == 'on_connect'
        mock.assert_called_once()
        mock.assert_called_with({'test': True}) 
Example #4
Source File: netns.py    From libcalico with Apache License 2.0 6 votes vote down vote up
def move_veth_into_ns(namespace, veth_name_ns_temp, veth_name_ns):
    """
    Move the veth into the namespace.

    :param namespace: The Namespace to move the veth into.
    :type namespace Namespace
    :param veth_name_ns_temp: The temporary interface name of the veth that will be
    moved into the namespace.
    :param veth_name_ns: The name of the interface in the namespace.
    :return: None. Raises CalledProcessError on error.
    """
    with NamedNamespace(namespace) as ns:
        _log.debug("Moving temp interface %s into ns %s.", veth_name_ns_temp, ns.name)
        # Create the veth pair and move one end into container:
        check_output(["ip", "link", "set", veth_name_ns_temp,
                    "netns", ns.name],
                   timeout=IP_CMD_TIMEOUT)
        ns.check_output(["ip", "link", "set", "dev", veth_name_ns_temp,
                       "name", veth_name_ns])
        ns.check_output(["ip", "link", "set", veth_name_ns, "up"]) 
Example #5
Source File: test_subscription_transport.py    From graphql-python-subscriptions with MIT License 6 votes vote down vote up
def test_should_trigger_on_connect_if_client_connect_valid(server_with_mocks):
    node_script = '''
        module.paths.push('{0}')
        WebSocket = require('ws')
        const SubscriptionClient =
        require('subscriptions-transport-ws').SubscriptionClient
        new SubscriptionClient('ws://localhost:{1}/socket')
    '''.format(
        os.path.join(os.path.dirname(__file__), 'node_modules'), TEST_PORT)
    try:
        subprocess.check_output(
            ['node', '-e', node_script], stderr=subprocess.STDOUT, timeout=.2)
    except:
        mock = server_with_mocks.get_nowait()
        assert mock.name == 'on_connect'
        mock.assert_called_once() 
Example #6
Source File: CheckGenerateRG.py    From SVE with GNU General Public License v3.0 6 votes vote down vote up
def CheckRG(samtools,bam,out_name,result):
    file = out_name+'.bam.header'
    command = [samtools,'view','-SH',bam,'-o',file]
    print (' '.join(command))
    subprocess.check_output(' '.join(command),stderr=subprocess.STDOUT,shell=True)

    result=[]

    with open(file,'r') as f:
        header = f.readlines()
    for l in range(len(header)):
        i = 0
        if header[l][0:3] == "@RG":
          RG = {x.split(':')[0]:x.split(':')[-1].replace('\n','') for x in header[l].split('@RG')[-1].split('\t')[1:]}
          result.append(RG)

    clean = ['rm','-f',file]
    subprocess.check_output(' '.join(clean),stderr=subprocess.STDOUT,shell=True)
    return result 
Example #7
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 #8
Source File: netns.py    From libcalico with Apache License 2.0 6 votes vote down vote up
def check_output(self, command):
        """
        Run a command within the named namespace.
        :param command: The command to run.
        :param shell: Whether this is a shell command.
        :param timeout: Command timeout in seconds.
        """
        command = self._get_nets_command(command)
        _log.debug("Run command: %s", command)
        # Note: we must not capture the stderr here (as with
        # stderr=STDOUT), because otherwise successful 'ip netns exec <ns>
        # <command> ...' call will write error messages to stderr if there are
        # any bogus namespaces on the system.  See
        # https://github.com/projectcalico/libcalico/issues/148 for detailed
        # discussion.
        return check_output(command, timeout=IP_CMD_TIMEOUT) 
Example #9
Source File: arpScanner.py    From sensorReporter with Apache License 2.0 6 votes vote down vote up
def checkState(self):
        """Detects and publishes any state change"""
        found = False
        state = 0
        arpList = subprocess.check_output(['arp', '-n']).split('\n')
        
        # Remove blank line
        arpList.pop()
        
        # Remove title
        del arpList[0]
        
        for entry in arpList:
            if entry.split()[2].lower() == self.address:
                found = True
                break

        if found == True:
            state = 1
        else:
            state = 0
            
        if state != self.state:
            self.state = state
            self.publishState() 
Example #10
Source File: calico_kubernetes.py    From k8s-exec-plugin with Apache License 2.0 6 votes vote down vote up
def _log_interfaces(namespace):
    """
    Log interface state in namespace and default namespace.

    :param namespace
    :type namespace str
    """
    try:
        if logger.isEnabledFor(logging.DEBUG):
            interfaces = check_output(['ip', 'addr'])
            logger.debug("Interfaces in default namespace:\n%s", interfaces)

            namespaces = check_output(['ip', 'netns', 'list'])
            logger.debug("Namespaces:\n%s", namespaces)

            cmd = ['ip', 'netns', 'exec', str(namespace), 'ip', 'addr']
            namespace_interfaces = check_output(cmd)

            logger.debug("Interfaces in namespace %s:\n%s",
                         namespace, namespace_interfaces)
    except BaseException:
        # Don't exit if we hit an error logging out the interfaces.
        logger.exception("Ignoring error logging interfaces") 
Example #11
Source File: afl-sancov.py    From afl-sancov with GNU General Public License v3.0 6 votes vote down vote up
def parent_identical_or_crashes(self, crash, parent):

        # Base names
        cbasename = os.path.basename(crash)
        pbasename = os.path.basename(parent)

        ## Filter queue filenames with sig info
        if self.find_crash_parent_regex.match(pbasename):
            self.logr("Parent ({}) looks like crashing input!".format(pbasename))
            return True

        try:
            diff_out = subprocess.check_output("diff -q {} {}".format(crash, parent),
                                               stderr=subprocess.STDOUT, shell=True)
        except Exception, e:
            diff_out = e.output 
Example #12
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 #13
Source File: aflsancov.py    From afl-sancov with GNU General Public License v3.0 6 votes vote down vote up
def parent_identical_or_crashes(self, crash, parent):

        # Base names
        cbasename = os.path.basename(crash)
        pbasename = os.path.basename(parent)

        ## Filter queue filenames with sig info
        if self.find_crash_parent_regex.match(pbasename):
            self.logr("Parent ({}) looks like crashing input!".format(pbasename))
            return True

        try:
            diff_out = subprocess.check_output("diff -q {} {}".format(crash, parent),
                                               stderr=subprocess.STDOUT, shell=True)
        except Exception, e:
            diff_out = e.output 
Example #14
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 #15
Source File: netns.py    From libcalico with Apache License 2.0 5 votes vote down vote up
def add_ns_default_route(namespace, veth_name_host, veth_name_ns):
    """
    Add a default route to the namespace.

    For IPv4:
    Use the first link local address as the dummy address.
    This only ever appears in an ARP packet.
    The host has proxy arp configured on the other end of the veth so
    will respond with the MAC of the veth

    For IPv6:
    Use the link local address of the host end of the veth.
    This doesn't require proxy NDP (though currently felix still enables
    it).

    :param namespace: The namespace to operate in.
    :type namespace Namespace
    :param veth_name_host: The name of the interface in the host.
    :param veth_name_ns: The name of the interface in the namespace.
    :return: None. Raises CalledProcessError on error.
    """
    # Attempt to fetch the IPv6 address for the host veth. Failure indicates
    # that this host doesn't support IPv6.
    next_hop_6 = get_ipv6_link_local(veth_name_host)

    with NamedNamespace(namespace) as ns:
        # IPv4
        dummy_next_hop_4 = "169.254.1.1"
        # Connected route to next hop & default route.
        ns.check_output(["ip", "-4", "route", "replace",
                         dummy_next_hop_4, "dev", veth_name_ns])
        ns.check_output(["ip", "-4", "route", "replace", "default",
                         "via", dummy_next_hop_4, "dev", veth_name_ns])

        if next_hop_6:
            # Only set the IPv6 route if the host end of the veth got an IPv6
            # address. There's no need to set a device route as the container
            # Will already have a /64 to the link local range.
            ns.check_output(["ip", "-6", "route", "replace", "default",
                             "via", next_hop_6, "dev", veth_name_ns]) 
Example #16
Source File: netns.py    From libcalico with Apache License 2.0 5 votes vote down vote up
def get_ns_veth_mac(namespace, veth_name_ns):
    """
    Return the MAC address of the interface in the namespace.

    :param namespace: The namespace to operate in.
    :type namespace Namespace
    :param veth_name_ns: The name of the interface in the namespace.
    :return: The MAC address as a string. Raises CalledProcessError on error.
    """
    with NamedNamespace(namespace) as ns:
        # Get the MAC address.
        mac = ns.check_output(["cat", "/sys/class/net/%s/address" % veth_name_ns]).strip()
    #TODO MAC should be an EUI object.
    return mac 
Example #17
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 #18
Source File: netns.py    From libcalico with Apache License 2.0 5 votes vote down vote up
def add_ip_to_ns_veth(namespace, ip, veth_name_ns):
    """
    Add an IP to an interface in a namespace.

    :param namespace: The namespace to operate in.
    :type namespace Namespace
    :param ip: The IPAddress to add.
    :param veth_name_ns: The interface to add the address to.
    :return: None. Raises CalledProcessError on error.
    """
    with NamedNamespace(namespace) as ns:
        ns.check_output(["ip", "-%s" % ip.version, "addr", "add",
                       "%s/%s" % (ip, PREFIX_LEN[ip.version]),
                       "dev", veth_name_ns]) 
Example #19
Source File: netns.py    From libcalico with Apache License 2.0 5 votes vote down vote up
def remove_ip_from_ns_veth(namespace, ip, veth_name_ns):
    """
    Remove an IP from an interface in a namespace.

    :param namespace: The namespace to operate in.
    :type namespace Namespace
    :param ip: The IPAddress to remove.
    :param veth_name_ns: The interface to remove the address from.
    :return: None. raises CalledProcessError on error.
    """
    assert isinstance(ip, IPAddress)
    with NamedNamespace(namespace) as ns:
        ns.check_output(["ip", "-%s" % ip.version, "addr", "del",
                       "%s/%s" % (ip, PREFIX_LEN[ip.version]),
                       "dev", veth_name_ns]) 
Example #20
Source File: instaclone.py    From instaclone with Apache License 2.0 5 votes vote down vote up
def version_for(config):
  """
  The version for an item is either the explicit version specified by
  the user, or the SHA1 hash of hashable file.
  """
  bits = []
  if config.version_string:
    bits.append(str(config.version_string))
  if config.version_hashable:
    log.debug("computing sha1 of: %s", config.version_hashable)
    bits.append(file_sha1(config.version_hashable))
  if config.version_command:
    log.debug("version command: %s", config.version_command)
    popenargs = shell_expand_to_popen(config.version_command, os.environ)
    output = subprocess.check_output(
      popenargs, stderr=SHELL_OUTPUT, stdin=DEV_NULL).strip()
    if not configs._CONFIG_VERSION_RE.match(output):
      raise configs.ConfigError(
        "Invalid version output from version command: %r" % output)
    bits.append(output)

  return "-".join(bits)


#
# ---- Command line ---- 
Example #21
Source File: afl-sancov.py    From afl-sancov with GNU General Public License v3.0 5 votes vote down vote up
def does_dry_run_throw_error(self, cmd):

        try:
            out = subprocess.check_output(cmd, stderr=subprocess.STDOUT, shell=True)
        except Exception, e:
            return (e.returncode > 128) 
Example #22
Source File: __init__.py    From ATX with Apache License 2.0 5 votes vote down vote up
def _run_nowait(self, code):
        ''' TODO: change to no wait '''
        print self._proc.poll()
        encoded_code = json.dumps({'command': code, 'nowait': True})
        output = subprocess.check_output([self._bootstrap, 'run', '--nowait', encoded_code], env=self._env)
        return output 
Example #23
Source File: test_subscription_transport.py    From graphql-python-subscriptions with MIT License 5 votes vote down vote up
def test_passes_through_websocket_request_to_on_subscribe(server):
    node_script = '''
        module.paths.push('{0}')
        WebSocket = require('ws')
        const SubscriptionClient =
        require('subscriptions-transport-ws').SubscriptionClient
        const client = new SubscriptionClient('ws://localhost:{1}/socket')
        client.subscribe({{
            query: `subscription context {{
                context
            }}`,
            variables: {{}},
            }}, (error, result) => {{
                if (error) {{
                  console.log(JSON.stringify(error));
                }}
            }}
        );
    '''.format(
        os.path.join(os.path.dirname(__file__), 'node_modules'), TEST_PORT)

    try:
        subprocess.check_output(
            ['node', '-e', node_script], stderr=subprocess.STDOUT, timeout=.2)
    except:
        while True:
            mock = server.get_nowait()
            if mock.name == 'on_subscribe':
                mock.assert_called_once()
                mock.assert_called_with_contains('websocket')
                break 
Example #24
Source File: test_subscription_transport.py    From graphql-python-subscriptions with MIT License 5 votes vote down vote up
def test_should_trigger_on_subscribe_when_client_subscribes(server_with_mocks):
    node_script = '''
        module.paths.push('{0}')
        WebSocket = require('ws')
        const SubscriptionClient =
        require('subscriptions-transport-ws').SubscriptionClient
        const client = new SubscriptionClient('ws://localhost:{1}/socket')
        client.subscribe({{
            query: `subscription useInfo($id: String) {{
            user(id: $id) {{
              id
              name
            }}
          }}`,
            operationName: 'useInfo',
            variables: {{
              id: 3,
            }},
          }}, function (error, result) {{
            // nothing
          }})
    '''.format(
        os.path.join(os.path.dirname(__file__), 'node_modules'), TEST_PORT)
    try:
        subprocess.check_output(
            ['node', '-e', node_script], stderr=subprocess.STDOUT, timeout=.2)
    except:
        while True:
            mock = server_with_mocks.get_nowait()
            if mock.name == 'on_subscribe':
                mock.assert_called_once()
                break 
Example #25
Source File: test_subscription_transport.py    From graphql-python-subscriptions with MIT License 5 votes vote down vote up
def test_should_call_unsubscribe_when_client_closes_cxn(server_with_mocks):
    node_script = '''
        module.paths.push('{0}')
        WebSocket = require('ws')
        const SubscriptionClient =
        require('subscriptions-transport-ws').SubscriptionClient
        const client = new SubscriptionClient('ws://localhost:{1}/socket')
        client.subscribe({{
            query: `subscription useInfo($id: String) {{
            user(id: $id) {{
              id
              name
            }}
          }}`,
            operationName: 'useInfo',
            variables: {{
              id: 3,
            }},
          }}, function (error, result) {{
            // nothing
          }}
        )
        setTimeout(() => {{
            client.client.close()
        }}, 500)
    '''.format(
        os.path.join(os.path.dirname(__file__), 'node_modules'), TEST_PORT)
    try:
        subprocess.check_output(
            ['node', '-e', node_script], stderr=subprocess.STDOUT, timeout=1)
    except:
        while True:
            mock = server_with_mocks.get_nowait()
            if mock.name == 'on_unsubscribe':
                mock.assert_called_once()
                break 
Example #26
Source File: test_subscription_transport.py    From graphql-python-subscriptions with MIT License 5 votes vote down vote up
def test_trigger_on_disconnect_when_client_disconnects(server_with_mocks):
    node_script = '''
        module.paths.push('{0}')
        WebSocket = require('ws')
        const SubscriptionClient =
        require('subscriptions-transport-ws').SubscriptionClient
        const client = new SubscriptionClient('ws://localhost:{1}/socket')
        client.client.close()
    '''.format(
        os.path.join(os.path.dirname(__file__), 'node_modules'), TEST_PORT)
    subprocess.check_output(['node', '-e', node_script])
    mock = server_with_mocks.get_nowait()
    assert mock.name == 'on_disconnect'
    mock.assert_called_once() 
Example #27
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 #28
Source File: client.py    From ATX with Apache License 2.0 5 votes vote down vote up
def devices(self):
        '''get a dict of attached devices. key is the device serial, value is device name.'''
        out = self.run_cmd('devices') #subprocess.check_output([self.adb_path(), 'devices']).decode("utf-8")
        if 'adb server is out of date' in out:
            out = self.run_cmd('devices')
        match = "List of devices attached"
        index = out.find(match)
        if index < 0:
            raise EnvironmentError("adb is not working.")
        return dict(re.findall(r'([^\s]+)\t(\w+)', out)) 
Example #29
Source File: __init__.py    From ATX with Apache License 2.0 5 votes vote down vote up
def _close(self):
        print 'Terminate instruments'
        if self._proc:
            self._proc.terminate()
        # 1. remove pipe
        subprocess.check_output([self._bootstrap, 'reset'], env=self._env) 
Example #30
Source File: subprocess.py    From twitter-stock-recommendation with MIT License 5 votes vote down vote up
def check_output(*args, **kwargs):
        raise OSError("subprocess.check_output is not supported")