# Path: bts_tools/core.py
# def run(cmd, capture_io=False, verbose=True, log_on_fail=True,
#         run_dir=None, forward_signals=False, pidfile=None, shell=False):
#     r = _run(cmd, capture_io=capture_io, verbose=verbose,
#              run_dir=run_dir, forward_signals=forward_signals, pidfile=pidfile, shell=shell)
#     if r.status != 0:
#         if log_on_fail:
#             log.warning('Failed running: %s' % cmd)
#             if capture_io:
#                 log.warning('\nSTDOUT:\n{}\nSTDERR:\n:{}'.format(r.stdout, r.stderr))
#         raise RuntimeError('Failed running: %s' % cmd)
#     return r
# 
# def get_bin_name(client=None, build_env=None):
#     return os.path.basename(get_full_bin_name(client, build_env))


from .core import run, get_bin_name
import psutil
import logging

    if proc is not None:
        try:
            if proc.status() in [psutil.STATUS_RUNNING, psutil.STATUS_SLEEPING]:
                log.debug('returning cached proc for binary on port {}: {}'.format(port, proc))
                return proc

            else:
                log.debug('found cached proc on port {}, but status is {}'.format(port, statuses.get(proc.status(), proc.status())))
        except psutil.NoSuchProcess:
            del _process_cache[port]  # remove stale entry


    # find the process corresponding to our node by looking at the rpc port
    #log.debug('find bts binary on {}:{}'.format(host, port))
    try:
        lines = run('lsof -i :{}'.format(port), verbose=False, log_on_fail=False, capture_io=True).stdout.split('\n')
    except RuntimeError:
        log.debug('found no process listening on port {}'.format(port))
        return None

    lines = [l for l in lines if 'LISTEN' in l]
    if not lines:
        return None
    if len(lines) > 1:
        log.warning('More than 1 potential witness process: {}'.format(lines))

    pid = int(lines[0].split()[1])
    proc = psutil.Process(pid)


# Path: bts_tools/core.py
# def run(cmd, capture_io=False, verbose=True, log_on_fail=True,
#         run_dir=None, forward_signals=False, pidfile=None, shell=False):
#     r = _run(cmd, capture_io=capture_io, verbose=verbose,
#              run_dir=run_dir, forward_signals=forward_signals, pidfile=pidfile, shell=shell)
#     if r.status != 0:
#         if log_on_fail:
#             log.warning('Failed running: %s' % cmd)
#             if capture_io:
#                 log.warning('\nSTDOUT:\n{}\nSTDERR:\n:{}'.format(r.stdout, r.stderr))
#         raise RuntimeError('Failed running: %s' % cmd)
#     return r
# 
# def get_bin_name(client=None, build_env=None):
#     return os.path.basename(get_full_bin_name(client, build_env))


# Path: bts_tools/process.py
from .core import run, get_bin_name
import psutil
import logging


    if proc is not None:
        try:
            if proc.status() in [psutil.STATUS_RUNNING, psutil.STATUS_SLEEPING]:
                log.debug('returning cached proc for binary on port {}: {}'.format(port, proc))
                return proc

            else:
                log.debug('found cached proc on port {}, but status is {}'.format(port, statuses.get(proc.status(), proc.status())))
        except psutil.NoSuchProcess:
            del _process_cache[port]  # remove stale entry


    # find the process corresponding to our node by looking at the rpc port
    #log.debug('find bts binary on {}:{}'.format(host, port))
    try:
        lines = run('lsof -i :{}'.format(port), verbose=False, log_on_fail=False, capture_io=True).stdout.split('\n')
    except RuntimeError:
        log.debug('found no process listening on port {}'.format(port))
        return None

    lines = [l for l in lines if 'LISTEN' in l]
    if not lines:
        return None
    if len(lines) > 1:
        log.warning('More than 1 potential witness process: {}'.format(lines))

    pid = int(lines[0].split()[1])
    proc = psutil.Process(pid)


    bin_name = node.build_env().get('witness_filename') or get_bin_name(node.client_name)