# Path: papaye/scripts/common.py
# def get_settings(inifile):
#     parser = configparser.ConfigParser()
#     current_dir = os.path.abspath(os.path.curdir)
#     parser.read(inifile)
#     return dict(parser.items('app:main', vars={"here": current_dir}))
# 
# Path: papaye/factories/root.py
# def user_root_factory(request_or_connection):
#     if isinstance(request_or_connection, (Request, DummyRequest)):
#         conn = get_connection(request_or_connection)
#     else:
#         conn = request_or_connection
#     zodb_root = conn.root()
#     if not '{}_root'.format(APP_NAME) in zodb_root:
#         return None
#     return zodb_root[APP_ROOT_NAME]['user']
# 
# APP_NAME = __import__(__name__).__name__
# 
# Path: papaye/models.py
# class User(Model):
# 
#     def __init__(self, username, password, **kwargs):
#         self.username = username
#         self.password = self.hash_password(password)
#         self.groups = kwargs.get('groups', [])
# 
#     def hash_password(self, password):
#         return hashlib.sha512(password.encode('utf-8')).hexdigest()
# 
#     def password_verify(self, clear_password):
#         return self.hash_password(clear_password) == self.password
# 
#     def __repr__(self):
#         return '<{}.{} "{}" at {}>'.format(self.__module__, self.__class__.__name__, self.username, id(self))
# 
#     @classmethod
#     def by_username(cls, username, request):
#         """Return user instance by username"""
#         root = user_root_factory(request)
#         if username in [user.username for user in root]:
#             return root[username]
#         return None
# 
# class Root(ClonableModelMixin, SubscriptableBaseModel, Model):
#     _subobjects_attr = 'packages'
#     __name__ = ''
# 
#     def __init__(self, name):
#         self.name = name
#         self.packages = MyOOBTree()
# 
#     @property
#     def __parent__(self):
#         """Root object has no parent"""
#         return None
# 
#     def __acl__(self):
#         acl = [
#             # (Allow, 'group:installer', 'install'),
#             # (Allow, 'group:admin', ALL_PERMISSIONS)
#             (Allow, Authenticated, 'view'),
#         ]
#         registry = get_current_registry()
#         anonymous_install = registry.settings.get('papaye.anonymous_install')
#         acl.append((Allow, Everyone, 'install')) if anonymous_install and anonymous_install == 'true' else False
#         return acl
# 
#     def __iter__(self):
#         return (package for package in self.subobjects.values())
# 
#     def __getitem__(self, name_or_index):
#         if isinstance(name_or_index, int):
#             return next(itertools.islice(self.__iter__(), name_or_index, name_or_index + 1))
#         keys = [key for key in self.subobjects.keys()
#                 if self.format_key(key) == self.format_key(name_or_index)]
#         if len(keys) == 1:
#             return self.subobjects[keys[0]]
# 
#     def __setitem__(self, key, package):
#         if not hasattr(self, '_p_updated_keys'):
#             self._p_updated_keys = []
#         self._p_updated_keys.append(key)
#         # if isinstance(package, Package):
#         #     package.__parent__ = self
#         self.subobjects[key] = package
# 
# def get_connection(settings):
#     uri = settings.get('zodbconn.uri', None)
#     db = db_from_uri(uri, 'unamed', None)
#     return db.open()
# 
# def get_manager(config):
#     manager = PapayeEvolutionManager(
#         config,
#         evolve_packagename='papaye.evolve',
#         sw_version=SW_VERSION,
#         initial_db_version=0
#     )
#     return manager


import argparse
import getpass
import os
import sys
import transaction
from papaye.scripts.common import get_settings
from papaye.factories.root import user_root_factory, APP_NAME
from papaye.models import User, Root, get_connection, get_manager
        return False
    return True


def admin_already_exists(dbconn, username):
    zodb_root = get_database_root(dbconn)
    if not '{}_root'.format(APP_NAME) in zodb_root:
        return False
    return any(
        user.username for user in zodb_root['{}_root'.format(APP_NAME)]['user']
        if user.username == username
    )


def create_app_root(dbconn):
    zodb_root = get_database_root(dbconn)
    manager = get_manager(None)
    if manager.key not in zodb_root:
        zodb_root[manager.key] = {manager.package_name: manager.sw_version}
    if '{}_root'.format(APP_NAME) not in zodb_root:
        zodb_root['{}_root'.format(APP_NAME)] = Root('{}_root'.format(APP_NAME))
    if 'user' not in zodb_root['{}_root'.format(APP_NAME)]:
        zodb_root['{}_root'.format(APP_NAME)]['user'] = Root('user_root')
    if 'repository' not in zodb_root['{}_root'.format(APP_NAME)]:
        zodb_root['{}_root'.format(APP_NAME)]['repository'] = Root(
            'repository_root'
        )


def create_admin_user(dbconn, username, password):

# Path: papaye/scripts/common.py
# def get_settings(inifile):
#     parser = configparser.ConfigParser()
#     current_dir = os.path.abspath(os.path.curdir)
#     parser.read(inifile)
#     return dict(parser.items('app:main', vars={"here": current_dir}))
# 
# Path: papaye/factories/root.py
# def user_root_factory(request_or_connection):
#     if isinstance(request_or_connection, (Request, DummyRequest)):
#         conn = get_connection(request_or_connection)
#     else:
#         conn = request_or_connection
#     zodb_root = conn.root()
#     if not '{}_root'.format(APP_NAME) in zodb_root:
#         return None
#     return zodb_root[APP_ROOT_NAME]['user']
# 
# APP_NAME = __import__(__name__).__name__
# 
# Path: papaye/models.py
# class User(Model):
# 
#     def __init__(self, username, password, **kwargs):
#         self.username = username
#         self.password = self.hash_password(password)
#         self.groups = kwargs.get('groups', [])
# 
#     def hash_password(self, password):
#         return hashlib.sha512(password.encode('utf-8')).hexdigest()
# 
#     def password_verify(self, clear_password):
#         return self.hash_password(clear_password) == self.password
# 
#     def __repr__(self):
#         return '<{}.{} "{}" at {}>'.format(self.__module__, self.__class__.__name__, self.username, id(self))
# 
#     @classmethod
#     def by_username(cls, username, request):
#         """Return user instance by username"""
#         root = user_root_factory(request)
#         if username in [user.username for user in root]:
#             return root[username]
#         return None
# 
# class Root(ClonableModelMixin, SubscriptableBaseModel, Model):
#     _subobjects_attr = 'packages'
#     __name__ = ''
# 
#     def __init__(self, name):
#         self.name = name
#         self.packages = MyOOBTree()
# 
#     @property
#     def __parent__(self):
#         """Root object has no parent"""
#         return None
# 
#     def __acl__(self):
#         acl = [
#             # (Allow, 'group:installer', 'install'),
#             # (Allow, 'group:admin', ALL_PERMISSIONS)
#             (Allow, Authenticated, 'view'),
#         ]
#         registry = get_current_registry()
#         anonymous_install = registry.settings.get('papaye.anonymous_install')
#         acl.append((Allow, Everyone, 'install')) if anonymous_install and anonymous_install == 'true' else False
#         return acl
# 
#     def __iter__(self):
#         return (package for package in self.subobjects.values())
# 
#     def __getitem__(self, name_or_index):
#         if isinstance(name_or_index, int):
#             return next(itertools.islice(self.__iter__(), name_or_index, name_or_index + 1))
#         keys = [key for key in self.subobjects.keys()
#                 if self.format_key(key) == self.format_key(name_or_index)]
#         if len(keys) == 1:
#             return self.subobjects[keys[0]]
# 
#     def __setitem__(self, key, package):
#         if not hasattr(self, '_p_updated_keys'):
#             self._p_updated_keys = []
#         self._p_updated_keys.append(key)
#         # if isinstance(package, Package):
#         #     package.__parent__ = self
#         self.subobjects[key] = package
# 
# def get_connection(settings):
#     uri = settings.get('zodbconn.uri', None)
#     db = db_from_uri(uri, 'unamed', None)
#     return db.open()
# 
# def get_manager(config):
#     manager = PapayeEvolutionManager(
#         config,
#         evolve_packagename='papaye.evolve',
#         sw_version=SW_VERSION,
#         initial_db_version=0
#     )
#     return manager


# Path: papaye/scripts/initialize.py
import argparse
import getpass
import os
import sys
import transaction
from papaye.scripts.common import get_settings
from papaye.factories.root import user_root_factory, APP_NAME
from papaye.models import User, Root, get_connection, get_manager

        return False
    return True


def admin_already_exists(dbconn, username):
    zodb_root = get_database_root(dbconn)
    if not '{}_root'.format(APP_NAME) in zodb_root:
        return False
    return any(
        user.username for user in zodb_root['{}_root'.format(APP_NAME)]['user']
        if user.username == username
    )


def create_app_root(dbconn):
    zodb_root = get_database_root(dbconn)
    manager = get_manager(None)
    if manager.key not in zodb_root:
        zodb_root[manager.key] = {manager.package_name: manager.sw_version}
    if '{}_root'.format(APP_NAME) not in zodb_root:
        zodb_root['{}_root'.format(APP_NAME)] = Root('{}_root'.format(APP_NAME))
    if 'user' not in zodb_root['{}_root'.format(APP_NAME)]:
        zodb_root['{}_root'.format(APP_NAME)]['user'] = Root('user_root')
    if 'repository' not in zodb_root['{}_root'.format(APP_NAME)]:
        zodb_root['{}_root'.format(APP_NAME)]['repository'] = Root(
            'repository_root'
        )


def create_admin_user(dbconn, username, password):

    admin = User(username, password, groups=['group:admin'])