<commit_before>import time
import sys
import logging

import psutil
import bottle
from pkg_resources import resource_filename


logging.basicConfig(stream=sys.stdout, level=logging.INFO)
log = logging.getLogger(__name__)

app = bottle.Bottle()

app.config.meta_set('port', 'filter', int)
app.config.meta_set('debug', 'filter', lambda s: str(s).lower() == 'true')
app.config.meta_set('rsm.port-start', 'filter', int)
app.config.load_dict({
    'host': 'localhost',
    'port': 8888,
    'server': 'wsgiref',
    'debug': False,
    'rsm': {
        'port-start': 10000
        }
    })

db = {}
port_counter = app.config['rsm.port-start']


def netstat(port):
    status = {
        'listen': False,
        'established': [],
        }
    for c in psutil.net_connections(kind='tcp'):
        if port in c.laddr and c.status == 'LISTEN':
            status['listen'] = True
        if port in c.raddr and c.status == 'ESTABLISHED':
            status['established'].append(c.pid)
    return status


@app.route('/debug/<filepath:path>')
def static_debug(filepath):
    if not app.config['debug']:
        raise bottle.HTTPError(status=404)
    return bottle.static_file(filepath, root='static')


@app.route('/dist/<filepath:path>')
def static_dist(filepath):
    return bottle.static_file(
        filepath, root=resource_filename(__name__, 'static'))


@app.get('/uuids')
def uuids():
    for uuid in db:
        db[uuid].update(netstat(db[uuid]['port']))
    resp = {
        'success': True,
        'db': db.values(),
        }
    return resp


@app.get('/uuid/<uuid>')
def check(uuid):
    global port_counter
    if uuid not in db:
        db[uuid] = {
            'uuid': uuid,
            'name': bottle.request.query.get('name'),
            'port': port_counter,
            'active': False,
            'listen': False,
            'established': [],
            }
        port_counter += 1
    db[uuid]['name'] = bottle.request.query.get('name')
    db[uuid]['time'] = int(time.time())
    db[uuid].update(netstat(db[uuid]['port']))
    return db[uuid]


@app.post('/uuid/<uuid>')
def update(uuid):
    if uuid not in db:
        raise bottle.HTTPError(status=404)
    db[uuid].update(netstat(db[uuid]['port']))
    db[uuid]['active'] = bottle.request.json['active'] is True
    if not db[uuid]['active']:
        terminate(uuid)
    return db[uuid]


@app.get('/uuid/<uuid>/terminate')
def terminate(uuid):
    if uuid not in db:
        raise bottle.HTTPError(status=404)
    db[uuid].update(netstat(db[uuid]['port']))
    for pid in db[uuid]['established']:
        psutil.Process(pid).terminate()
    db[uuid].update(netstat(db[uuid]['port']))
    return db[uuid]


def run():
    global port_counter
    if len(sys.argv) > 1:
        for cfg in sys.argv[1:]:
            app.config.load_config(cfg)
    port_counter = app.config['rsm.port-start']
    if app.config['debug']:
        log.setLevel(logging.DEBUG)
        bottle.debug(True)
    app.run(
        server=app.config['server'],
        host=app.config['host'],
        port=app.config['port'],
        reloader=app.config['debug'],
        )


if __name__ == '__main__':
    app.config['debug'] = True
    run()<commit_msg>store listening ssh process pid<commit_after>import time
import sys
import logging

import psutil
import bottle
from pkg_resources import resource_filename


logging.basicConfig(stream=sys.stdout, level=logging.INFO)
log = logging.getLogger(__name__)

app = bottle.Bottle()

app.config.meta_set('port', 'filter', int)
app.config.meta_set('debug', 'filter', lambda s: str(s).lower() == 'true')
app.config.meta_set('rsm.port-start', 'filter', int)
app.config.load_dict({
    'host': 'localhost',
    'port': 8888,
    'server': 'wsgiref',
    'debug': False,
    'rsm': {
        'port-start': 10000
        }
    })

db = {}
port_counter = app.config['rsm.port-start']


def netstat(port):
    status = {
        'listen': False,
        'established': [],
        }
    for c in psutil.net_connections(kind='tcp'):
        if port in c.laddr and c.status == 'LISTEN':
            status['listen'] = c.pid
        if port in c.raddr and c.status == 'ESTABLISHED':
            status['established'].append(c.pid)
    return status


@app.route('/debug/<filepath:path>')
def static_debug(filepath):
    if not app.config['debug']:
        raise bottle.HTTPError(status=404)
    return bottle.static_file(filepath, root='static')


@app.route('/dist/<filepath:path>')
def static_dist(filepath):
    return bottle.static_file(
        filepath, root=resource_filename(__name__, 'static'))


@app.get('/uuids')
def uuids():
    for uuid in db:
        db[uuid].update(netstat(db[uuid]['port']))
    resp = {
        'success': True,
        'db': db.values(),
        }
    return resp


@app.get('/uuid/<uuid>')
def check(uuid):
    global port_counter
    if uuid not in db:
        db[uuid] = {
            'uuid': uuid,
            'name': bottle.request.query.get('name'),
            'port': port_counter,
            'active': False,
            'listen': False,
            'established': [],
            }
        port_counter += 1
    db[uuid]['name'] = bottle.request.query.get('name')
    db[uuid]['time'] = int(time.time())
    db[uuid].update(netstat(db[uuid]['port']))
    return db[uuid]


@app.post('/uuid/<uuid>')
def update(uuid):
    if uuid not in db:
        raise bottle.HTTPError(status=404)
    db[uuid].update(netstat(db[uuid]['port']))
    db[uuid]['active'] = bottle.request.json['active'] is True
    if not db[uuid]['active']:
        terminate(uuid)
    return db[uuid]


@app.get('/uuid/<uuid>/terminate')
def terminate(uuid):
    if uuid not in db:
        raise bottle.HTTPError(status=404)
    db[uuid].update(netstat(db[uuid]['port']))
    pid = db[uuid]['listen']
    if pid:
        psutil.Process(pid).terminate()
    db[uuid].update(netstat(db[uuid]['port']))
    return db[uuid]


def run():
    global port_counter
    if len(sys.argv) > 1:
        for cfg in sys.argv[1:]:
            app.config.load_config(cfg)
    port_counter = app.config['rsm.port-start']
    if app.config['debug']:
        log.setLevel(logging.DEBUG)
        bottle.debug(True)
    app.run(
        server=app.config['server'],
        host=app.config['host'],
        port=app.config['port'],
        reloader=app.config['debug'],
        )


if __name__ == '__main__':
    app.config['debug'] = True
    run()
{'fraction_non_alphanumeric': 0.1690535491905355, 'fraction_numerical': 0.006537982565379826, 'mean_word_length': 2.808535862477771, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2429618', 'n_tokens_mistral': 2177, 'n_tokens_neox': 2131, 'n_words': 423}