|
|
3d8d28 |
|
|
|
cbf076 |
import datetime
|
|
|
05525d |
|
|
|
05525d |
import db.types
|
|
|
05525d |
import db.pool
|
|
|
b838e2 |
import db.cache
|
|
|
b838e2 |
import page.root
|
|
|
b838e2 |
import page.error
|
|
|
b838e2 |
import action.actions
|
|
|
b838e2 |
import session
|
|
|
e5b0ac |
from repoproxy import RepoProxy
|
|
|
e5b0ac |
from repotypes.git import RepotypeGit
|
|
|
cbf076 |
|
|
|
cbf076 |
|
|
|
3d8d28 |
class Server:
|
|
|
3d8d28 |
def __init__(self, config):
|
|
|
e5b0ac |
self.repotypes = {
|
|
|
e5b0ac |
'git': RepotypeGit() }
|
|
|
cbf076 |
|
|
|
e5b0ac |
self.config = self.apply_defaults(config)
|
|
|
e5b0ac |
self.check_config(self.config)
|
|
|
e5b0ac |
|
|
|
e5b0ac |
self.dbtypebytype = db.types.bytype
|
|
|
e5b0ac |
self.dbtypebychar = db.types.bychar
|
|
|
e5b0ac |
self.dbpool = db.pool.Pool(self)
|
|
|
e5b0ac |
self.dbcache = db.cache.Cache(self)
|
|
|
e5b0ac |
|
|
|
e5b0ac |
self.sessions = session.SessionManager(self)
|
|
|
e5b0ac |
|
|
|
e5b0ac |
self.repoproxy = RepoProxy(self)
|
|
|
e5b0ac |
|
|
|
e5b0ac |
self.pageroot = page.root.RootPage()
|
|
|
e5b0ac |
self.pageforbidden = page.error.ErrorPage('403', 'Forbidden')
|
|
|
e5b0ac |
self.pagenotfound = page.error.ErrorPage('404', 'Not Found')
|
|
|
e5b0ac |
|
|
|
e5b0ac |
self.actions = action.actions.actions
|
|
|
e5b0ac |
|
|
|
e5b0ac |
for key, repotype in self.repotypes.items():
|
|
|
e5b0ac |
repotype.configure(self, self.config['repositories'][key])
|
|
|
e5b0ac |
|
|
|
e5b0ac |
|
|
|
e5b0ac |
def get_defaults(self):
|
|
|
e5b0ac |
return self.apply_defaults(dict())
|
|
|
e5b0ac |
|
|
|
e5b0ac |
|
|
|
e5b0ac |
def apply_defaults(self, config):
|
|
|
cbf076 |
config_db = config.get('db', dict())
|
|
|
cbf076 |
config_db_pool = config_db.get('pool', dict())
|
|
|
b838e2 |
config_db_cache = config_db.get('cache', dict())
|
|
|
e5b0ac |
|
|
|
b838e2 |
config_users = config.get('users', dict())
|
|
|
e5b0ac |
|
|
|
b838e2 |
config_session = config.get('session', dict())
|
|
|
cbf076 |
|
|
|
e5b0ac |
config_repositories = config.get('repositories', dict())
|
|
|
e5b0ac |
config_repositories_git = config_repositories.get('git', dict())
|
|
|
e5b0ac |
|
|
|
e5b0ac |
urlprefix = self.fixpath( config.get('urlprefix', '') )
|
|
|
e5b0ac |
|
|
|
e5b0ac |
return {
|
|
|
e5b0ac |
'protocol' : str(config.get('protocol', 'https://')),
|
|
|
e5b0ac |
'domain' : str(config.get('domain', '')),
|
|
|
572081 |
'name' : str(config.get('name', 'Earthworm')),
|
|
|
572081 |
'debug' : bool(config.get('debug')),
|
|
|
e5b0ac |
|
|
|
cbf076 |
'urlprefix' : urlprefix,
|
|
|
cbf076 |
'urldataprefix' : str(config.get('urldataprefix', urlprefix + '/data')),
|
|
|
cbf076 |
|
|
|
b838e2 |
'db': {
|
|
|
cbf076 |
'connection' : dict(config_db.get('connection', dict())),
|
|
|
4656ad |
'prefix' : str(config_db.get('prefix', '')),
|
|
|
cbf076 |
'retrytime' : float(config_db.get('retrytime', 0)),
|
|
|
cbf076 |
'pool': {
|
|
|
cbf076 |
'read' : int(config_db_pool.get('read' , 10)),
|
|
|
cbf076 |
'write' : int(config_db_pool.get('write', 10)),
|
|
|
cbf076 |
},
|
|
|
b838e2 |
'cache': {
|
|
|
b838e2 |
'maxcount' : int(config_db_cache.get('maxcount', 10000)),
|
|
|
b838e2 |
},
|
|
|
b838e2 |
},
|
|
|
b838e2 |
|
|
|
b838e2 |
'users': {
|
|
|
b838e2 |
'salt' : str(config_users.get('salt', 'ndina82899nda90pn0al')),
|
|
|
b838e2 |
'selfcreate' : bool(config_users.get('selfcreate', False)),
|
|
|
b838e2 |
'selfdelete' : bool(config_users.get('selfdelete', False)),
|
|
|
b838e2 |
'showlist' : bool(config_users.get('showlist', False)),
|
|
|
b838e2 |
'showprofile' : bool(config_users.get('showprofile', True)),
|
|
|
b838e2 |
},
|
|
|
b838e2 |
|
|
|
b838e2 |
'session': {
|
|
|
b838e2 |
'time' : float(config_session.get('time', 30*60))
|
|
|
cbf076 |
},
|
|
|
e5b0ac |
|
|
|
e5b0ac |
'repositories': {
|
|
|
e5b0ac |
'git': {
|
|
|
e5b0ac |
'internalurl' : str(config_repositories_git.get('internalurl', '')),
|
|
|
e5b0ac |
'path' : str(config_repositories_git.get('path', '')),
|
|
|
e5b0ac |
'command' : str(config_repositories_git.get('command', '/usr/bin/git')),
|
|
|
e5b0ac |
'environment' : config_repositories_git.get('environment', dict()),
|
|
|
e5b0ac |
},
|
|
|
e5b0ac |
},
|
|
|
cbf076 |
}
|
|
|
e5b0ac |
|
|
|
e5b0ac |
|
|
|
e5b0ac |
def check_config(self, config):
|
|
|
e5b0ac |
assert config['protocol'] == 'http://' \
|
|
|
e5b0ac |
or config['protocol'] == 'https://'
|
|
|
e5b0ac |
assert config['db']['retrytime'] >= 0
|
|
|
e5b0ac |
assert config['db']['prefix'] == '' \
|
|
|
e5b0ac |
or config['db']['prefix'].isidentifier()
|
|
|
e5b0ac |
assert config['db']['pool']['read'] > 0
|
|
|
e5b0ac |
assert config['db']['pool']['write'] > 0
|
|
|
e5b0ac |
assert config['db']['cache']['maxcount'] > 0
|
|
|
e5b0ac |
assert config['session']['time'] > 0
|
|
|
cbf076 |
|
|
|
e5b0ac |
for i in config['domain'].split('.'):
|
|
|
e5b0ac |
assert i.isidentifier()
|
|
|
e5b0ac |
|
|
|
e5b0ac |
config['urlprefix'] = self.fixpath(config['urlprefix'])
|
|
|
e5b0ac |
config['urldataprefix'] = self.fixpath(config['urldataprefix'])
|
|
|
e5b0ac |
|
|
|
e5b0ac |
for key, repotype in self.repotypes.items():
|
|
|
e5b0ac |
repotype.check_config(self, config['repositories'][key])
|
|
|
e5b0ac |
|
|
|
e5b0ac |
|
|
|
e5b0ac |
def fixpath(self, path, root = False):
|
|
|
e5b0ac |
assert type(path) is str
|
|
|
e5b0ac |
while path and path[-1] == '/':
|
|
|
e5b0ac |
del path[-1]
|
|
|
e5b0ac |
if path != '':
|
|
|
e5b0ac |
assert path[0] == '/'
|
|
|
e5b0ac |
for i in path[1:].split('/'):
|
|
|
b838e2 |
assert i.isalnum()
|
|
|
e5b0ac |
if root:
|
|
|
e5b0ac |
assert path and path[0] == '/'
|
|
|
e5b0ac |
return path
|
|
|
b838e2 |
|
|
|
e5b0ac |
|
|
|
e5b0ac |
def fixenv(self, environment):
|
|
|
e5b0ac |
assert type(environment) is dict
|
|
|
e5b0ac |
for k, v in environment:
|
|
|
e5b0ac |
assert type(k) is str and k.isidentifier()
|
|
|
e5b0ac |
assert type(v) is str
|
|
|
e5b0ac |
return environment
|