From 09edf41d43371b14637f4757979c769eec9e3f46 Mon Sep 17 00:00:00 2001 From: Seblu <sebastien.luttringer@smartjog.com> Date: Wed, 24 Aug 2011 14:51:23 +0200 Subject: [PATCH] repository can be unaivailable This also change underlying way of initiliaze a repository. A repository can now be defined and be innaccessible --- bin/is | 20 ++++++------------ installsystems/database.py | 4 ++-- installsystems/repository.py | 41 ++++++++++++++++++++++++++---------- 3 files changed, 38 insertions(+), 27 deletions(-) diff --git a/bin/is b/bin/is index 1abf180..b8b834b 100755 --- a/bin/is +++ b/bin/is @@ -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 diff --git a/installsystems/database.py b/installsystems/database.py index 3722afe..8bb13b2 100644 --- a/installsystems/database.py +++ b/installsystems/database.py @@ -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") diff --git a/installsystems/repository.py b/installsystems/repository.py index 9a998ff..d268dc2 100644 --- a/installsystems/repository.py +++ b/installsystems/repository.py @@ -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): ''' -- GitLab