<commit_before>#!/usr/bin/python

from getpass import getuser, getpass
import os
import sys
import argparse
import ConfigParser
from socket import gethostname
from passlib.hash import argon2
from pwd import getpwnam
from grp import getgrnam


HelpStrings = {
    'add_user': "create a Constantina user account",
    'delete_user': "remove a Constantina user account",
    'password': "<PASSWORD>",
    'revoke_logins': "delete stored session keys, forcing users to re-login",
    'config': "path to the configuration directory",
    'cgi_bin': "path to the directory containing CGI scripts",
    'instance': "config directory isolation: /etc/constantina/<instance>",
    'hostname': "hostname that Constantina will run on",
    'webroot': "where Constantina html resources are served from",
    'username': "the Unix username that Constantina data is owned by",
    'groupname': "the Unix groupname that Constantina data is owned by",
}

class ConstantinaDefaults:
    def __init__(self):
        self.add_user = None
        self.delete_user = None
        self.password = None
        self.instance = "default"
        self.revoke_logins = False
        self.hostname = gethostname()
        self.username = getuser()   # Unix user account the server runs in
        self.groupname = getuser()   # Unix group account the server runs in
        self.webroot = sys.prefix + "/var/www/constantina"
        self.config = sys.prefix + "/etc/constantina"
        self.cgi_bin = sys.prefix + "/var/cgi-bin/constantina"
        self.templates = sys.prefix, "/etc/constantina/templates"
        if sys.prefix == "/usr":
            # Default prefix? Unprefix the target directories
            self.webroot = "/var/www/constantina"
            self.config = "/etc/constantina"
            self.cgi_bin = "/var/cgi-bin/constantina"
            self.templates = "/etc/constantina/templates"


class ConstantinaConfig:
    """
    Support writing the most basic types of Constantina config, such as the
    hostname, webserver user account, and config paths. This is run through
    the setup.py install script in addition to the configuration script itself.
    """
    def __init__(self):
        self.settings = ConfigParser.SafeConfigParser(allow_no_value=True)
        self.uwsgi = ConfigParser.SafeConfigParser(allow_no_value=True)
        self.default = ConstantinaDefaults()
        self.add_user = self.default.add_user
        self.delete_user = self.default.delete_user
        self.password = <PASSWORD>
        self.revoke_logins = self.default.revoke_logins
        self.instance = self.default.instance
        self.hostname = self.default.hostname
        self.webroot = self.default.webroot
        self.username = self.default.username
        self.groupname = self.default.groupname
        self.config = self.default.config
        self.cgi_bin = self.default.cgi_bin
        self.templates = self.default.templates

    def configure(self, section, option, value):
        """
        Don't overwrite config values with None.
        TBD if this is the right thing
        """
        if value is not None:
            self.settings.set(section, option, value)

    def update_configs(self):
        """Make config changes once the config files are staged"""
        self.settings.read(self.config + "/constantina.ini")
        self.configure("server", "hostname", self.hostname)
        self.configure("server", "username", self.username)
        self.configure("server", "groupname", self.username)
        self.configure("paths", "webroot", self.webroot)
        self.configure("paths", "config", self.config)
        self.configure("paths", "cgi_bin", self.cgi_bin)
        with open(self.config + "/constantina.ini", "wb") as cfh:
            self.settings.write(cfh)

        # Set UWSGI config file settings for this instance too
        self.uwsgi.read(self.config + "/uwsgi.ini")
        self.uwsgi.set("uwsgi", "chdir", self.webroot)
        self.uwsgi.set("uwsgi", "env", "INSTANCE=" + self.instance)
        self.uwsgi.set("uwsgi", "procname", "constantina-" + self.instance)
        with open(self.config + "/uwsgi.ini", "wb") as ufh:
            self.uwsgi.write(ufh)

    def update_instance_directory(self, directory):
        """Add instance to the end of the chosen directory"""
        to_update = getattr(self, directory)
        default = getattr(self.default, directory)
        if to_update == default:
            setattr(self, directory, to_update + "/" + self.instance)

        #instance_config = self.config + "/constantina.ini"
        #TODO: Determine if install or configure mode
        #if not os.path.isfile(instance_config):
        #    raise Exception("File not found: \"%s\". Instance \"%s\" not installed?"
        #        % (instance_config, self.instance))<commit_msg>fix setting socket def in uwsgi from constantina_configure<commit_after>#!/usr/bin/python

from getpass import getuser, getpass
import os
import sys
import argparse
import ConfigParser
from socket import gethostname
from passlib.hash import argon2
from pwd import getpwnam
from grp import getgrnam


HelpStrings = {
    'add_user': "create a Constantina user account",
    'delete_user': "remove a Constantina user account",
    'password': "<PASSWORD> a Constant<PASSWORD> user account",
    'revoke_logins': "delete stored session keys, forcing users to re-login",
    'config': "path to the configuration directory",
    'cgi_bin': "path to the directory containing CGI scripts",
    'instance': "config directory isolation: /etc/constantina/<instance>",
    'hostname': "hostname that Constantina will run on",
    'webroot': "where Constantina html resources are served from",
    'username': "the Unix username that Constantina data is owned by",
    'groupname': "the Unix groupname that Constantina data is owned by",
}

class ConstantinaDefaults:
    def __init__(self):
        self.add_user = None
        self.delete_user = None
        self.password = None
        self.instance = "default"
        self.revoke_logins = False
        self.hostname = gethostname()
        self.username = getuser()   # Unix user account the server runs in
        self.groupname = getuser()   # Unix group account the server runs in
        self.webroot = sys.prefix + "/var/www/constantina"
        self.config = sys.prefix + "/etc/constantina"
        self.cgi_bin = sys.prefix + "/var/cgi-bin/constantina"
        self.templates = sys.prefix, "/etc/constantina/templates"
        if sys.prefix == "/usr":
            # Default prefix? Unprefix the target directories
            self.webroot = "/var/www/constantina"
            self.config = "/etc/constantina"
            self.cgi_bin = "/var/cgi-bin/constantina"
            self.templates = "/etc/constantina/templates"


class ConstantinaConfig:
    """
    Support writing the most basic types of Constantina config, such as the
    hostname, webserver user account, and config paths. This is run through
    the setup.py install script in addition to the configuration script itself.
    """
    def __init__(self):
        self.settings = ConfigParser.SafeConfigParser(allow_no_value=True)
        self.uwsgi = ConfigParser.SafeConfigParser(allow_no_value=True)
        self.default = ConstantinaDefaults()
        self.add_user = self.default.add_user
        self.delete_user = self.default.delete_user
        self.password = <PASSWORD>
        self.revoke_logins = self.default.revoke_logins
        self.instance = self.default.instance
        self.hostname = self.default.hostname
        self.webroot = self.default.webroot
        self.username = self.default.username
        self.groupname = self.default.groupname
        self.config = self.default.config
        self.cgi_bin = self.default.cgi_bin
        self.templates = self.default.templates

    def configure(self, section, option, value):
        """
        Don't overwrite config values with None.
        TBD if this is the right thing
        """
        if value is not None:
            self.settings.set(section, option, value)

    def update_configs(self):
        """Make config changes once the config files are staged"""
        self.settings.read(self.config + "/constantina.ini")
        self.configure("server", "hostname", self.hostname)
        self.configure("server", "username", self.username)
        self.configure("server", "groupname", self.username)
        self.configure("paths", "webroot", self.webroot)
        self.configure("paths", "config", self.config)
        self.configure("paths", "cgi_bin", self.cgi_bin)
        with open(self.config + "/constantina.ini", "wb") as cfh:
            self.settings.write(cfh)

        # Set UWSGI config file settings for this instance too
        self.uwsgi.read(self.config + "/uwsgi.ini")
        self.uwsgi.set("uwsgi", "chdir", self.webroot)
        self.uwsgi.set("uwsgi", "env", "INSTANCE=" + self.instance)
        self.uwsgi.set("uwsgi", "procname", "constantina-" + self.instance)
        # TODO: port configured on the bound interface
        self.uwsgi.set("uwsgi", "socket", self.hostname + ":9090")
        with open(self.config + "/uwsgi.ini", "wb") as ufh:
            self.uwsgi.write(ufh)

    def update_instance_directory(self, directory):
        """Add instance to the end of the chosen directory"""
        to_update = getattr(self, directory)
        default = getattr(self.default, directory)
        if to_update == default:
            setattr(self, directory, to_update + "/" + self.instance)

        #instance_config = self.config + "/constantina.ini"
        #TODO: Determine if install or configure mode
        #if not os.path.isfile(instance_config):
        #    raise Exception("File not found: \"%s\". Instance \"%s\" not installed?"
        #        % (instance_config, self.instance))
{'fraction_non_alphanumeric': 0.10921467032408819, 'fraction_numerical': 0.0006095702529716549, 'mean_word_length': 2.8066511987625677, 'pattern_counts': {'":': 3, '<': 10, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '897252', 'n_tokens_mistral': 2798, 'n_tokens_neox': 2764, 'n_words': 783}