<commit_before>#
# Copyright (c) 2005-2006 rPath, Inc.
#
# All rights reserved
#

from cStringIO import StringIO
from copy import deepcopy
import kid
import sys
import os
import time

from mod_python import apache

from mint import mint_error
from mint import shimclient
from mint.session import SqlSession
from webhandler import WebHandler, normPath, HttpNotFound, HttpForbidden
from conary.web.fields import strFields, intFields, listFields, boolFields

configGroups = {
    'Server Setup':
        ('hostName', 'siteDomainName'),
    'Branding':
        ('companyName', 'corpSite'),
    'Repository Setup':
        ('defaultBranch',),
}


class SetupHandler(WebHandler):
    def handle(self, context):
        self.__dict__.update(**context)

        path = normPath(context['cmd'])
        cmd = path.split('/')[1]

        # only admins are allowed here
        if not self.auth.admin and self.cfg.configured:
            raise HttpForbidden

        # first-time setup; check for <sid>.txt
        if not self.cfg.configured:
            if not self.session:    
                self._session_start()

            self.session.save()
            # FIXME: parameterize /srv/mint/
            if not os.path.exists(self.cfg.dataPath + "/%s.txt" % self.session.id()):
                return self.secure

        if not cmd:
            return self.setup
        try:
            return self.__getattribute__(cmd)
        except AttributeError:
            raise HttpNotFound

    def setup(self, auth):
        if '.' not in self.req.hostname:
            return self._write("error", error = "You must access the rBuilder server as a fully-qualified domain name:"
                                                " eg., <strong>http://rbuilder.example.com/</strong>, not just <strong>http://rbuilder/</strong>")

        self.cfg.hostName = self.req.hostname.split(".")[0]
        self.cfg.siteDomainName = ".".join(self.req.hostname.split(".")[1:])

        return self._write("setup/setup", configGroups = configGroups,
            newCfg = self.cfg, errors = [])

    def processSetup(self, auth, **kwargs):
        mintClient = shimclient.ShimMintClient(self.cfg, [self.cfg.authUser, self.cfg.authPass])

        errors = []

        if 'new_username' not in kwargs:
            errors.append("You must enter a username to be created")
        if 'new_email' not in kwargs:
            errors.append("You must enter a username to be created")
        if 'new_password' not in kwargs or 'new_password2' not in kwargs:
            errors.append("You must enter initial passwords")
        elif kwargs['new_password'] != kwargs['new_password2']:
            errors.append("Passwords must match")

        # rewrite configuration file
        keys = self.fields.keys()
        newCfg = deepcopy(self.cfg)

        for key in keys:
            if key in newCfg:
                newCfg[key] = self.fields[key]

        if errors:
            return self._write("setup/setup", configGroups = configGroups, newCfg = newCfg,
                errors = errors)

        # hack until we support SSL-rbuilder appliance
        newCfg.postCfg()
        newCfg.secureHost = newCfg.siteHost
        newCfg.projectDomainName = newCfg.externalDomainName = newCfg.siteDomainName
        newCfg.bugsEmail = newCfg.adminMail = kwargs['new_email']

        # create new user
        userId = mintClient.registerNewUser(<commit_msg>warn user if hostname entry contains any periods<commit_after>#
# Copyright (c) 2005-2006 rPath, Inc.
#
# All rights reserved
#

from cStringIO import StringIO
from copy import deepcopy
import kid
import sys
import os
import time

from mod_python import apache

from mint import mint_error
from mint import shimclient
from mint.session import SqlSession
from webhandler import WebHandler, normPath, HttpNotFound, HttpForbidden
from conary.web.fields import strFields, intFields, listFields, boolFields

configGroups = {
    'Server Setup':
        ('hostName', 'siteDomainName'),
    'Branding':
        ('companyName', 'corpSite'),
    'Repository Setup':
        ('defaultBranch',),
}


class SetupHandler(WebHandler):
    def handle(self, context):
        self.__dict__.update(**context)

        path = normPath(context['cmd'])
        cmd = path.split('/')[1]

        # only admins are allowed here
        if not self.auth.admin and self.cfg.configured:
            raise HttpForbidden

        # first-time setup; check for <sid>.txt
        if not self.cfg.configured:
            if not self.session:    
                self._session_start()

            self.session.save()
            # FIXME: parameterize /srv/mint/
            if not os.path.exists(self.cfg.dataPath + "/%s.txt" % self.session.id()):
                return self.secure

        if not cmd:
            return self.setup
        try:
            return self.__getattribute__(cmd)
        except AttributeError:
            raise HttpNotFound

    def setup(self, auth):
        if '.' not in self.req.hostname:
            return self._write("error", error = "You must access the rBuilder server as a fully-qualified domain name:"
                                                " eg., <strong>http://rbuilder.example.com/</strong>, not just <strong>http://rbuilder/</strong>")

        self.cfg.hostName = self.req.hostname.split(".")[0]
        self.cfg.siteDomainName = ".".join(self.req.hostname.split(".")[1:])

        return self._write("setup/setup", configGroups = configGroups,
            newCfg = self.cfg, errors = [])

    def processSetup(self, auth, **kwargs):
        mintClient = shimclient.ShimMintClient(self.cfg, [self.cfg.authUser, self.cfg.authPass])

        errors = []

        if '.' in kwargs['hostName']:
            errors.append("""The hostname of the rBuilder server must not contain periods. The
                hostname is only the first part of the fully-qualified domain name.""")
        if 'new_username' not in kwargs:
            errors.append("You must enter a username to be created")
        if 'new_email' not in kwargs:
            errors.append("You must enter a username to be created")
        if '<PASSWORD>_password' not in kwargs or '<PASSWORD>' not in kwargs:
            errors.append("You must enter initial passwords")
        elif kwargs['new_password'] != kwargs['<PASSWORD>_<PASSWORD>']:
            errors.append("Passwords must match")

        # rewrite configuration file
        keys = self.fields.keys()
        newCfg = deepcopy(self.cfg)

        for key in keys:
            if key in newCfg:
                newCfg[key] = self.fields[key]

        if errors:
            return self._write("setup/setup", configGroups = configGroups, newCfg = newCfg,
                errors = errors)

        # hack until we support SSL-rbuilder appliance
        newCfg.postCfg()
        newCfg.secureHost = newCfg.siteHost
        newCfg.projectDomainName = newCfg.externalDomainName = newCfg.siteDomainName
        newCfg.bugsEmail = newCfg.adminMail = kwargs['new_email']

        # create new user
        userId = mintClient.registerNewUser(
{'fraction_non_alphanumeric': 0.1040022547914318, 'fraction_numerical': 0.0033821871476888386, 'mean_word_length': 2.3179055633473586, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1256550', 'n_tokens_mistral': 1970, 'n_tokens_neox': 1923, 'n_words': 557}