<commit_before>#from ConfigParser import SafeConfigParser, NoOptionError
import couchdb
import argparse
from commands import getstatusoutput
from couchdb.mapping import Document, TextField

class User(Document):
    username = TextField()
    password = TextField()
    privilege = TextField()

def main():
    parser = argparse.ArgumentParser(
            prog='homitysetup',
            formatter_class=argparse.RawDescriptionHelpFormatter,
            description="OpenHomity Hub Setup Tool"
    )
    parser.add_argument("username", help="Username of new Homity Hub user")
    parser.add_argument("password", help="Password of new Homity Hub user")
    parser.add_argument("-c", "--couch", nargs=2, help="Optional Advanced: Username & Password for couchdb")
    args = parser.parse_args()
    
    new_hub_username = args.username
    new_hub_password = <PASSWORD>.password
    couchdb_admin_user, couchdb_admin_pass = args.couch
    
    '''
    Set up CouchDB
    '''
    couch = couchdb.Server(url="http://localhost:5984")
    if couchdb_admin_user:
        couch.resource.credentials = (couchdb_admin_user, couchdb_admin_pass)
        
    for db_name in ['users','sessions','spokes','garages','alarms','cameras','locks']:
        print 'Creating "%s" database' % db_name
        try:
            couch.create(db_name)
            print '"%s" db created' % db_name
        except couchdb.Unauthorized:
            raise Exception('Unauthorized - edit this script to specify couchdb admin user credentials')
        except couchdb.PreconditionFailed:
            print '    "%s" db already exists, moving on' % db_name
    
    print "Creating new user %s" % new_hub_username
    user_db = couch['users']
    new_user = User()
    new_user.username = new_hub_username
    password = <PASSWORD>
    ohomeuser.privilege = 'user'
    salt = uuid.uuid4().hex
    new_user.password = hashlib.sha256(salt.encode() + password.encode()).hexdigest() + ':' + salt

    new_user.store(user_db)
    print "Successfully created %s:%s" % (new_user.username, new_user.password)

    '''
    Set up config & log files
    '''
    try:
        getstatusoutput("cp default.conf /etc/homity/homityhub.conf")
    except:
        raise Exception('Configuration file creation failed - make sure /etc/homity directory is created and owned by current user')
    
    try:
        getstatusoutput("touch /var/log/homity/Hub.log")
    except:
        raise Exception('Log file creation failed - make sure /var/log/homity directory is created and owned by current user')

    
if __name__ == '__main__':
    main()
    <commit_msg>bugfixes<commit_after>#from ConfigParser import SafeConfigParser, NoOptionError
import couchdb, uuid, argparse,hashlib
from commands import getstatusoutput
from couchdb.mapping import Document, TextField

class User(Document):
    username = TextField()
    password = TextField()
    privilege = TextField()

def main():
    parser = argparse.ArgumentParser(
            prog='homitysetup',
            formatter_class=argparse.RawDescriptionHelpFormatter,
            description="OpenHomity Hub Setup Tool"
    )
    parser.add_argument("username", help="Username of new Homity Hub user")
    parser.add_argument("password", help="Password of new Homity Hub user")
    parser.add_argument("-c", "--couch", nargs=2, help="Optional Advanced: Username & Password for couchdb")
    args = parser.parse_args()
    
    new_hub_username = args.username
    new_hub_password = args.password
    couchdb_admin_user, couchdb_admin_pass = args.couch
    
    '''
    Set up CouchDB
    '''
    couch = couchdb.Server(url="http://localhost:5984")
    if couchdb_admin_user:
        couch.resource.credentials = (couchdb_admin_user, couchdb_admin_pass)
        
    for db_name in ['users','sessions','spokes','garages','alarms','cameras','locks']:
        print 'Creating "%s" database' % db_name
        try:
            couch.create(db_name)
            print '"%s" db created' % db_name
        except couchdb.Unauthorized:
            raise Exception('Unauthorized - edit this script to specify couchdb admin user credentials')
        except couchdb.PreconditionFailed:
            print '    "%s" db already exists, moving on' % db_name
    
    print "Creating new user %s" % new_hub_username
    user_db = couch['users']
    new_user = User()
    new_user.username = new_hub_username
    password = <PASSWORD>
    new_user.privilege = 'user'
    salt = uuid.uuid4().hex
    new_user.password = hashlib.sha256(salt.encode() + password.encode()).hexdigest() + ':' + salt

    new_user.store(user_db)
    print "Successfully created %s - %s:%s" % (new_user.id, new_user.username, new_user.password)

    '''
    Set up config & log files
    '''
    print "Copying default.conf to /etc/homity/homityhub.conf"
    try:
        getstatusoutput("cp default.conf /etc/homity/homityhub.conf")
    except:
        raise Exception('Configuration file creation failed - make sure /etc/homity directory is created and owned by current user')
    
    print "Creating log file at /var/log/homity/Hub.log"
    try:
        getstatusoutput("touch /var/log/homity/Hub.log")
    except:
        raise Exception('Log file creation failed - make sure /var/log/homity directory is created and owned by current user')

    
if __name__ == '__main__':
    main()
    
{'fraction_non_alphanumeric': 0.10660941000746826, 'fraction_numerical': 0.0033607169529499626, 'mean_word_length': 3.130300693909021, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, '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': '5346615', 'n_tokens_mistral': 1579, 'n_tokens_neox': 1564, 'n_words': 405}