<commit_before>from lacli import __version__
from lacli.log import getLogger
from lacore.adf.persist import make_adf
from lacore.adf.elements import Archive, Certificate, Meta, Cipher
from lacli.archive import restore_archive
from lacli.basecmd import LaBaseCommand
from lacli.command import command
from lacli.loginutil import login_async
from lacore.decorators import expand_args
from lacore.exceptions import UploadError
from twisted.python.log import msg, err
from twisted.internet import reactor, defer, threads, task
from thrift.transport import TTwisted
from thrift.protocol import TBinaryProtocol
from lacli.server.interface.ClientInterface import CLI, ttypes
from lacli.server.error import tthrow, log_hide
from itertools import starmap
from lacli.progress import ServerProgressHandler
from lacli.upload import UploadState
from binascii import b2a_hex
from StringIO import StringIO
import os
import errno
import treq
import json


class LaServerCommand(LaBaseCommand, CLI.Processor):
    """Run a RPC server

    Usage: lacli server [--no-detach] [--port <port>] [--mhole]

    Options:
        --no-detach              don't detach from terminal
        --port <port>            port to listen on [default: 9090]
        --mhole                  start a manhole on the next port number
    """
    prompt = 'lacli:server> '

    def __init__(self, *args, **kwargs):
        super(LaServerCommand, self).__init__(*args, **kwargs)
        self.logincmd = self.registry.cmd.login
        self.manhole = False
        self.srm = self.prefs['command']['srm']
        if self.prefs['gui']['rememberme'] is True:
            self.logincmd.username = self.prefs['gui']['username']
            self.logincmd.password = self.prefs['gui']['password']
        UploadState.init(self.cache)
        CLI.Processor.__init__(self, self)

    def makecmd(self, options):
        cmd = ["run"]
        if options['--port']:
            cmd.append(options['--port'])
        if options['--mhole']:
            self.manhole = True
        return " ".join(cmd)

    def get_server(self):
        factory = TBinaryProtocol.TBinaryProtocolFactory()
        return TTwisted.ThriftServerFactory(
            processor=self, iprot_factory=factory)

    @command(port=int)
    def do_run(self, port=9090):
        """
        Usage: run [<port>]
        """
        reactor.listenTCP(port, self.get_server(), interface='127.0.0.1')
        if self.manhole is True:
            from twisted.manhole.telnet import ShellFactory
            f = ShellFactory()
            f.username = f.password = '<PASSWORD>'
            reactor.listenTCP(port+1, f)
        msg('Running reactor')
        self.batch = True
        if self.prefs['gui']['rememberme'] is True:
            self.logincmd.username = self.prefs['gui']['username']
            self.logincmd.password = <PASSWORD>.prefs['gui']['password']
            self.logincmd.email = self.prefs['gui']['email']
        reactor.run()
        self.batch = False

    def process(self, iprot, oprot):
        d = CLI.Processor.process(self, iprot, oprot)
        d.addErrback(self.logUnhandledError)
        return d

    def logUnhandledError(self, error):
        err(error)
        getLogger().debug("unhandled error: ", exc_info=True)
        return error

    def toArchiveInfo(self, docs):
        title = docs['archive'].title
        size = docs['archive'].meta.size
        created = docs['archive'].meta.created
        description = docs['archive'].description
        md5 = docs['auth'].md5.encode('hex')
        if title is not None:<commit_msg>append info from environment variable to server user agent<commit_after>from lacli import __version__
from lacli.log import getLogger
from lacore.adf.persist import make_adf
from lacore.adf.elements import Archive, Certificate, Meta, Cipher
from lacli.archive import restore_archive
from lacli.basecmd import LaBaseCommand
from lacli.command import command
from lacli.loginutil import login_async
from lacore.decorators import expand_args
from lacore.exceptions import UploadError
from twisted.python.log import msg, err
from twisted.internet import reactor, defer, threads, task
from thrift.transport import TTwisted
from thrift.protocol import TBinaryProtocol
from lacli.server.interface.ClientInterface import CLI, ttypes
from lacli.server.error import tthrow, log_hide
from itertools import starmap
from lacli.progress import ServerProgressHandler
from lacli.upload import UploadState
from binascii import b2a_hex
from StringIO import StringIO
import os
import errno
import treq
import json


class LaServerCommand(LaBaseCommand, CLI.Processor):
    """Run a RPC server

    Usage: lacli server [--no-detach] [--port <port>] [--mhole]

    Options:
        --no-detach              don't detach from terminal
        --port <port>            port to listen on [default: 9090]
        --mhole                  start a manhole on the next port number
    """
    prompt = 'lacli:server> '

    def __init__(self, *args, **kwargs):
        super(LaServerCommand, self).__init__(*args, **kwargs)
        self.logincmd = self.registry.cmd.login
        self.manhole = False
        self.srm = self.prefs['command']['srm']
        if self.prefs['gui']['rememberme'] is True:
            self.logincmd.username = self.prefs['gui']['username']
            self.logincmd.password = self.prefs['gui']['password']
        UploadState.init(self.cache)
        CLI.Processor.__init__(self, self)

    def makecmd(self, options):
        cmd = ["run"]
        if options['--port']:
            cmd.append(options['--port'])
        if options['--mhole']:
            self.manhole = True
        return " ".join(cmd)

    def get_server(self):
        factory = TBinaryProtocol.TBinaryProtocolFactory()
        return TTwisted.ThriftServerFactory(
            processor=self, iprot_factory=factory)

    @command(port=int)
    def do_run(self, port=9090):
        """
        Usage: run [<port>]
        """
        reactor.listenTCP(port, self.get_server(), interface='127.0.0.1')
        if self.manhole is True:
            from twisted.manhole.telnet import ShellFactory
            f = ShellFactory()
            f.username = f.password = '<PASSWORD>'
            reactor.listenTCP(port+1, f)
        msg('Running reactor')
        self.batch = True
        if self.prefs['gui']['rememberme'] is True:
            self.logincmd.username = self.prefs['gui']['username']
            self.logincmd.password = self.prefs['gui']['password']
            self.logincmd.email = self.prefs['gui']['email']
        ua = os.getenv("LA_API_AGENT_INFO")
        if ua is not None:
            self.registry.session.prefs['user_agent'] += " (" + ua + ")"
        reactor.run()
        self.batch = False

    def process(self, iprot, oprot):
        d = CLI.Processor.process(self, iprot, oprot)
        d.addErrback(self.logUnhandledError)
        return d

    def logUnhandledError(self, error):
        err(error)
        getLogger().debug("unhandled error: ", exc_info=True)
        return error

    def toArchiveInfo(self, docs):
        title = docs['archive'].title
        size = docs['archive'].meta.size
        created = docs['archive'].meta.created
        description = docs['archive'].description
        md5 = docs['auth'].md5.encode('hex')
        if title is not None:
{'fraction_non_alphanumeric': 0.1115494490545504, 'fraction_numerical': 0.004897292885321725, 'mean_word_length': 2.752935171005615, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 14, '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': '2849436', 'n_tokens_mistral': 2191, 'n_tokens_neox': 2097, 'n_words': 548}