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

import BaseHTTPServer
import SocketServer
import cgi
import getopt
import os
import re
import socket
import sys
import threading
import time

DEFAULT_ADDRESS = "0.0.0.0"
DEFAULT_PORT = 9002
DEFAULT_LOG_FILENAME = "facilitator.log"

LOG_DATE_FORMAT = "%Y-%m-%d %H:%M:%S"

class options(object):
    log_filename = DEFAULT_LOG_FILENAME
    log_file = sys.stdout
    daemonize = True

def usage(f = sys.stdout):
    print >> f, """\
Usage: %(progname)s <OPTIONS> [HOST] [PORT]
Flash bridge facilitator: Register client addresses with HTTP POST requests
and serve them out again with HTTP GET. Listen on HOST and PORT, by default
%(addr)s %(port)d.
  -d, --debug         don't daemonize, log to stdout.
  -h, --help          show this help.
  -l, --log FILENAME  write log to FILENAME (default \"%(log)s\").\
""" % {
    "progname": sys.argv[0],
    "addr": DEFAULT_ADDRESS,
    "port": DEFAULT_PORT,
    "log": DEFAULT_LOG_FILENAME,
}

log_lock = threading.Lock()
def log(msg):
    log_lock.acquire()
    try:
        print >> options.log_file, (u"%s %s" % (time.strftime(LOG_DATE_FORMAT), msg)).encode("UTF-8")
        options.log_file.flush()
    finally:
        log_lock.release()

def format_addr(addr):
    host, port = addr
    if not host:
        return u":%d" % port
    # Numeric IPv6 address?
    try:
        addrs = socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM, socket.IPPROTO_TCP, socket.AI_NUMERICHOST)
        af = addrs[0][0]
    except socket.gaierror, e:
        af = 0
    if af == socket.AF_INET6:
        return u"[%s]:%d" % (host, port)
    else:
        return u"%s:%d" % (host, port)

class Reg(object):
    def __init__(self, id):
        self.id = id

    def __unicode__(self):
        return u"%s" % (self.id)

    def __str__(self):
        return unicode(self).encode("UTF-8")

    def __cmp__(self, other):
        return cmp((self.id), (other.id))

    @staticmethod
    def parse(spec, defhost = None, defport = None):
        host = None
        port = None
        m = re.match(r'^\[(.+)\]:(\d*)$', spec)
        if m:
            host, port = m.groups()
            af = socket.AF_INET6
        else:
            m = re.match(r'^(.*):(\d*)$', spec)
            if m:
                host, port = m.groups()
                if host:
                    af = socket.AF_INET
                else:
                    # Has to be guessed from format of defhost.
                    af = 0
        host = host or defhost
        port = port or defport
        if not (host and port):
            raise ValueError("Bad address specification \"%s\"" % spec)
<commit_msg>Use parse_addr_spec to parse regs.<commit_after>#!/usr/bin/env python

import BaseHTTPServer
import SocketServer
import cgi
import getopt
import os
import re
import socket
import sys
import threading
import time

DEFAULT_ADDRESS = "0.0.0.0"
DEFAULT_PORT = 9002
DEFAULT_LOG_FILENAME = "facilitator.log"

LOG_DATE_FORMAT = "%Y-%m-%d %H:%M:%S"

class options(object):
    log_filename = DEFAULT_LOG_FILENAME
    log_file = sys.stdout
    daemonize = True

def usage(f = sys.stdout):
    print >> f, """\
Usage: %(progname)s <OPTIONS> [HOST] [PORT]
Flash bridge facilitator: Register client addresses with HTTP POST requests
and serve them out again with HTTP GET. Listen on HOST and PORT, by default
%(addr)s %(port)d.
  -d, --debug         don't daemonize, log to stdout.
  -h, --help          show this help.
  -l, --log FILENAME  write log to FILENAME (default \"%(log)s\").\
""" % {
    "progname": sys.argv[0],
    "addr": DEFAULT_ADDRESS,
    "port": DEFAULT_PORT,
    "log": DEFAULT_LOG_FILENAME,
}

log_lock = threading.Lock()
def log(msg):
    log_lock.acquire()
    try:
        print >> options.log_file, (u"%s %s" % (time.strftime(LOG_DATE_FORMAT), msg)).encode("UTF-8")
        options.log_file.flush()
    finally:
        log_lock.release()

def parse_addr_spec(spec, defhost = None, defport = None):
    host = None
    port = None
    m = None
    # IPv6 syntax.
    if not m:
        m = re.match(ur'^\[(.+)\]:(\d+)$', spec)
        if m:
            host, port = m.groups()
            af = socket.AF_INET6
    if not m:
        m = re.match(ur'^\[(.+)\]:?$', spec)
        if m:
            host, = m.groups()
            af = socket.AF_INET6
    # IPv4 syntax.
    if not m:
        m = re.match(ur'^(.+):(\d+)$', spec)
        if m:
            host, port = m.groups()
            af = socket.AF_INET
    if not m:
        m = re.match(ur'^:?(\d+)$', spec)
        if m:
            port, = m.groups()
            af = 0
    if not m:
        host = spec
        af = 0
    host = host or defhost
    port = port or defport
    if not (host and port):
        raise ValueError("Bad address specification \"%s\"" % spec)
    return af, host, int(port)

def format_addr(addr):
    host, port = addr
    if not host:
        return u":%d" % port
    # Numeric IPv6 address?
    try:
        addrs = socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM, socket.IPPROTO_TCP, socket.AI_NUMERICHOST)
        af = addrs[0][0]
    except socket.gaierror, e:
        af = 0
    if af == socket.AF_INET6:
        return u"[%s]:%d" % (host, port)
    else:
        return u"%s:%d" % (host, port)

class Reg(object):
    def __init__(self, id):
        self.id = id

    def __unicode__(self):
        return u"%s" % (self.id)

    def __str__(self):
        return unicode(self).encode("UTF-8")

    def __cmp__(self, other):
        return cmp((self.id), (other.id))

    @staticmethod
    def parse(spec, defhost = None, defport = None):
        af, host, port = parse_addr_spec(spec, defhost, defport)
{'fraction_non_alphanumeric': 0.13863837312113175, 'fraction_numerical': 0.007427055702917772, 'mean_word_length': 2.256188831318365, 'pattern_counts': {'":': 10, '<': 5, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '2703718', 'n_tokens_mistral': 2000, 'n_tokens_neox': 1925, 'n_words': 543}