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): ...@@ -75,19 +75,11 @@ def c_build(parser, args):
def c_init(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 repoman = load_repositories(args)
if args.repo_path is not None: for reponame in args.repository:
# from command line repoman[reponame].init()
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)
def c_add(parser, args): def c_add(parser, args):
''' '''
...@@ -314,8 +306,8 @@ p_build.set_defaults(func=c_build) ...@@ -314,8 +306,8 @@ p_build.set_defaults(func=c_build)
# init command parser # init command parser
p_init = subparsers.add_parser("init", help=c_init.__doc__.lower()) p_init = subparsers.add_parser("init", help=c_init.__doc__.lower())
p_init.add_argument("repo_filter", nargs="?", default=argparse.SUPPRESS, p_init.add_argument("repository", nargs="+",
help="Name or path of a repository to init") help="repository's name to initialize")
p_init.set_defaults(func=c_init) p_init.set_defaults(func=c_init)
# add command parser # add command parser
......
...@@ -26,7 +26,7 @@ class Database(object): ...@@ -26,7 +26,7 @@ class Database(object):
arrow("Creating repository database") arrow("Creating repository database")
# check locality # check locality
if istools.pathtype(path) != "file": if istools.pathtype(path) != "file":
raise NotImplementedError("Database creation must be local") raise Exception("Database creation must be local")
path = os.path.abspath(path) path = os.path.abspath(path)
if os.path.exists(path): if os.path.exists(path):
raise Exception("Database already exists. Remove it before") raise Exception("Database already exists. Remove it before")
...@@ -43,7 +43,7 @@ class Database(object): ...@@ -43,7 +43,7 @@ class Database(object):
def __init__(self, path): def __init__(self, path):
# check locality # check locality
if istools.pathtype(path) != "file": 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.path = os.path.abspath(path)
self.conn = sqlite3.connect(self.path, isolation_level=None) self.conn = sqlite3.connect(self.path, isolation_level=None)
self.conn.execute("PRAGMA foreign_keys = ON") self.conn.execute("PRAGMA foreign_keys = ON")
......
...@@ -26,15 +26,34 @@ class Repository(object): ...@@ -26,15 +26,34 @@ class Repository(object):
def __init__(self, config): def __init__(self, config):
self.config = 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 init(self):
def create(cls, config):
''' '''
Create an empty base repository Initialize an empty base repository
''' '''
config = self.config
# check local repository # check local repository
if istools.pathtype(config.path) != "file": if not istools.isfile(self.config.path):
raise Exception("Repository creation must be local") raise Exception("Repository creation must be local")
# create base directories # create base directories
arrow("Creating base directories") arrow("Creating base directories")
...@@ -50,13 +69,13 @@ class Repository(object): ...@@ -50,13 +69,13 @@ class Repository(object):
raise Exception("Unable to create directory %s: %s" % (config.path, e)) raise Exception("Unable to create directory %s: %s" % (config.path, e))
arrowlevel(-1) arrowlevel(-1)
# create database # create database
dbpath = os.path.join(config.path, config.dbname) d = Database.create(config.dbpath)
d = Database.create(dbpath) istools.chrights(config.dbpath, uid=config.uid,
istools.chrights(dbpath, uid=config.uid, gid=config.gid, mode=config.fmod) gid=config.gid, mode=config.fmod)
# create last file # load database
self = cls(config) self.db = Database(config.dbpath)
# create/update last file
self.update_last() self.update_last()
return self
def update_last(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