Skip to content
Snippets Groups Projects
Commit 09edf41d authored by Seblu's avatar Seblu
Browse files

repository can be unaivailable

This also change underlying way of initiliaze a repository.
A repository can now be defined and be innaccessible
parent fdd0aada
No related branches found
No related tags found
No related merge requests found
......@@ -75,19 +75,11 @@ def c_build(parser, args):
def c_init(parser, args):
'''
Create a empty repository
Create an empty repository
'''
# we cannot use load_repositories because repo doesn't exists
if args.repo_path is not None:
# from command line
Repository.create(RepositoryConfig(None, path=args.repo_path))
else:
# from config
for repoconf in RepoConfigFile(args.repo_config).repos:
# filtering on repository name if present
if args.repo_filter is not None and repoconf.name != args.repo_filter:
continue
Repository.create(repoconf)
repoman = load_repositories(args)
for reponame in args.repository:
repoman[reponame].init()
def c_add(parser, args):
'''
......@@ -314,8 +306,8 @@ p_build.set_defaults(func=c_build)
# init command parser
p_init = subparsers.add_parser("init", help=c_init.__doc__.lower())
p_init.add_argument("repo_filter", nargs="?", default=argparse.SUPPRESS,
help="Name or path of a repository to init")
p_init.add_argument("repository", nargs="+",
help="repository's name to initialize")
p_init.set_defaults(func=c_init)
# add command parser
......
......@@ -26,7 +26,7 @@ class Database(object):
arrow("Creating repository database")
# check locality
if istools.pathtype(path) != "file":
raise NotImplementedError("Database creation must be local")
raise Exception("Database creation must be local")
path = os.path.abspath(path)
if os.path.exists(path):
raise Exception("Database already exists. Remove it before")
......@@ -43,7 +43,7 @@ class Database(object):
def __init__(self, path):
# check locality
if istools.pathtype(path) != "file":
raise NotImplementedError("Database creation must be local")
raise Exception("Database must be local")
self.path = os.path.abspath(path)
self.conn = sqlite3.connect(self.path, isolation_level=None)
self.conn.execute("PRAGMA foreign_keys = ON")
......
......@@ -26,15 +26,34 @@ class Repository(object):
def __init__(self, config):
self.config = config
self.db = Database(config.dbpath)
try:
self.db = Database(config.dbpath)
except:
self.db = None
def __getattribute__(self, name):
'''
Raise an error if repository is unavailable
Unavailable can be caused because db is not accessible or
because repository is not initialized
'''
db = object.__getattribute__(self, "db")
config = object.__getattribute__(self, "config")
# config and init are always accessible
if name in ("init", "config"):
return object.__getattribute__(self, name)
# if no db (not init or not accessible) raise error
if db is None:
raise Exception("Repository %s is not availabe" % config.name)
return object.__getattribute__(self, name)
@classmethod
def create(cls, config):
def init(self):
'''
Create an empty base repository
Initialize an empty base repository
'''
config = self.config
# check local repository
if istools.pathtype(config.path) != "file":
if not istools.isfile(self.config.path):
raise Exception("Repository creation must be local")
# create base directories
arrow("Creating base directories")
......@@ -50,13 +69,13 @@ class Repository(object):
raise Exception("Unable to create directory %s: %s" % (config.path, e))
arrowlevel(-1)
# create database
dbpath = os.path.join(config.path, config.dbname)
d = Database.create(dbpath)
istools.chrights(dbpath, uid=config.uid, gid=config.gid, mode=config.fmod)
# create last file
self = cls(config)
d = Database.create(config.dbpath)
istools.chrights(config.dbpath, uid=config.uid,
gid=config.gid, mode=config.fmod)
# load database
self.db = Database(config.dbpath)
# create/update last file
self.update_last()
return self
def update_last(self):
'''
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment