<commit_before>#!/usr/bin/env python

# functions common to the various kinds of daemon

# TODO: define a log function that sends a pubsub message to the
# logger daemon

import time, binascii
from Crypto import Random

def signal_handler(a, b):
    global clean_shutdown
    clean_shutdown = True
    r.publish("exit", "clean-exit")
    r.lpush("exit", "clean-exit")

def short(session):
    """Return the first 12 bytes of a session ID, or, for a
    challenge ID, the challenge ID with the session ID truncated."""
    tmp = session.partition(":")
    return tmp[0][:12] + "..." + tmp[1] + tmp[2]

def ancient(session, state):
    """Given that this session is in the specified named state,
    decide whether the daemon should forcibly expire it for being too
    old, even if no client request has caused the serve to mark the
    session as expired.  This is most relevant to truly abandoned
    sessions that no client ever asks about."""
    age = int(time.time()) - int(r.hget(session, "created"))
    if state == "makechallenge" and age > 120:
        if debug: print "considered", short(session), "ancient"
        return True
    if state == "testchallenge" and age > 600:
        if debug: print "considered", short(session), "ancient"
        return True
    return False

def random():
    """Return 64 hex digits representing a new 32-byte random number."""
    return binascii.hexlify(Random.get_random_bytes(32))

def random_raw():
    """Return 32 random bytes."""
    return Random.get_random_bytes(32)<commit_msg>and we've moved ancient out of daemon_common into policy<commit_after>#!/usr/bin/env python

# functions common to the various kinds of daemon

# TODO: define a log function that sends a pubsub message to the
# logger daemon

import time, binascii
from Crypto import Random

def signal_handler(a, b):
    global clean_shutdown
    clean_shutdown = True
    r.publish("exit", "clean-exit")
    r.lpush("exit", "clean-exit")

def short(session):
    """Return the first 12 bytes of a session ID, or, for a
    challenge ID, the challenge ID with the session ID truncated."""
    tmp = session.partition(":")
    return tmp[0][:12] + "..." + tmp[1] + tmp[2]

def random():
    """Return 64 hex digits representing a new 32-byte random number."""
    return binascii.hexlify(Random.get_random_bytes(32))

def random_raw():
    """Return 32 random bytes."""
    return Random.get_random_bytes(32)
{'fraction_non_alphanumeric': 0.10969072164948454, 'fraction_numerical': 0.016494845360824743, 'mean_word_length': 3.5946969696969697, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 5, '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': '6688539', 'n_tokens_mistral': 751, 'n_tokens_neox': 699, 'n_words': 283}