From 8c4d69b9efe5a0190bc3dcfa4fb45fb8ef12215f Mon Sep 17 00:00:00 2001 From: Sebastien Luttringer Date: Mon, 20 Feb 2012 13:15:38 +0100 Subject: [PATCH] add json output to repo command --- bin/is | 49 ++++----------------- completion/bash/is | 2 +- installsystems/repository.py | 82 +++++++++++++++++++++++++++++++++--- 3 files changed, 85 insertions(+), 48 deletions(-) diff --git a/bin/is b/bin/is index 1fa03f8..7d0bc06 100755 --- a/bin/is +++ b/bin/is @@ -52,39 +52,6 @@ def load_repositories(args): repoman.register(repoconf, nosync=args.no_sync) return repoman -def show_repositories(repoman, pattern, local=None, online=None, - url=False, state=True): - ''' - Show repository inside manager - if :param online: is true, list only online repositories - if :param online: is false, list only offline repostiories - if :param online: is None, list both online and offline repostiories. - if :param local: is true, list only local repositories - if :param local: is false, list only remote repostiories - if :param local: is None, list both local and remote repostiories. - ''' - for reponame in fnmatch.filter(repoman.names, pattern): - repo = repoman[reponame] - if repo.config.offline and online is True: - continue - if not repo.config.offline and online is False: - continue - if repo.local and local is False: - continue - if not repo.local and local is True: - continue - so = "#l##r#Off#R# " if repo.config.offline else "#l##g#On#R# " - sl = "#l##y#Local#R# " if repo.local else "#l##c#Remote#R# " - rc = "#l##r#" if repo.config.offline else "#l##g#" - s = "" - if state: - s += "%s%s " % (so, sl) - rc = "#l##b#" - s += "%s%s#R#"% (rc, repo.config.name) - if url: - s += " (%s)" % repo.config.path - out(s) - def get_images(patterns, repoman, local=True, min=None, max=None): ''' Select and load a package image from a standard naming type @@ -379,13 +346,13 @@ def c_repo(args): if args.purge: args.no_sync = True repoman = load_repositories(args) - for pattern in args.repository: - if args.purge: - repoman.purge_cache(pattern) - else: - show_repositories(repoman, pattern, - online=args.online, local=args.local, - url=args.url, state=args.state) + if args.purge: + repoman.purge_repositories(args.repository) + else: + repoman.show_repositories(args.repository, + online=args.online, local=args.local, + o_url=args.url, o_state=args.state, + o_json=args.json) def c_search(args): ''' @@ -629,6 +596,8 @@ def arg_parser_init(): # repo command parser p = subparser.add_parser("repo", help=c_repo.__doc__.lower()) g = p.add_mutually_exclusive_group() + p.add_argument("-j", "--json", action="store_true", + help="output is formated in json") g.add_argument("-l", "--local", action="store_true", default=None, help="list local repository (filter)") g.add_argument("-r", "--remote", action="store_false", dest="local", diff --git a/completion/bash/is b/completion/bash/is index deba6b5..f05eafa 100644 --- a/completion/bash/is +++ b/completion/bash/is @@ -160,7 +160,7 @@ _is() { _filedir -d ;; repo) - [[ "$cur" == -* ]] && _opt '-h --help -l --local -r --remote -o --online -O --offline -s --state --force-offline --purge -u --url' && return 0 + [[ "$cur" == -* ]] && _opt '-h --help -l --local -r --remote -o --online -O --offline -s --state --force-offline --purge -u --url -j --json' && return 0 _repo ;; search) diff --git a/installsystems/repository.py b/installsystems/repository.py index 33a876f..a5f27b6 100644 --- a/installsystems/repository.py +++ b/installsystems/repository.py @@ -526,7 +526,6 @@ class RepositoryManager(object): ''' return len(self.repos) - def __getitem__(self, key): ''' Return a repostiory by its position in list @@ -537,7 +536,7 @@ class RepositoryManager(object): for repo in self.repos: if repo.config.name == key: return repo - raise Exception("No repository named: %s" % key) + raise IndexError("No repository named: %s" % key) else: raise TypeError @@ -811,14 +810,23 @@ class RepositoryManager(object): if len(s) > 0: out(s) - def purge_cache(self, pattern): + def select_repositories(self, patterns): + ''' + Return a list of repository + ''' + ans = set() + for pattern in patterns: + ans |= set(fnmatch.filter(self.names, pattern)) + return sorted(ans) + + def purge_repositories(self, patterns): ''' Remove local cached repository files ''' - for reponame in fnmatch.filter(self.names, pattern): + for reponame in self.select_repositories(patterns): arrow("Purging cache of repository %s" % reponame) db = os.path.join(self.cache_path, reponame) - if os.path.exists(db): + if os.path.lexists(db): try: os.unlink(db) arrow("done", 1) @@ -827,6 +835,51 @@ class RepositoryManager(object): else: arrow("nothing to do", 1) + def show_repositories(self, patterns, local=None, online=None, + o_url=False, o_state=False, o_json=False): + ''' + Show repository inside manager + if :param online: is true, list only online repositories + if :param online: is false, list only offline repostiories + if :param online: is None, list both online and offline repostiories. + if :param local: is true, list only local repositories + if :param local: is false, list only remote repostiories + if :param local: is None, list both local and remote repostiories. + ''' + # build repositories dict + repos = {} + for reponame in self.select_repositories(patterns): + repo = self[reponame] + if repo.config.offline and online is True: + continue + if not repo.config.offline and online is False: + continue + if repo.local and local is False: + continue + if not repo.local and local is True: + continue + repos[reponame] = dict(repo.config.items()) + repos[reponame]["local"] = repo.local + # display result + if o_json: + s = json.dumps(repos) + else: + l = [] + for name, repo in repos.items(): + ln = "" + so = "#l##r#Off#R# " if repo["offline"] else "#l##g#On#R# " + sl = "#l##y#Local#R# " if repo["local"] else "#l##c#Remote#R# " + rc = "#l##r#" if repo["offline"] else "#l##g#" + if o_state: + ln += "%s%s " % (so, sl) + rc = "#l##b#" + ln += "%s%s#R#"% (rc, name) + if o_url: + ln += " (%s)" % repo["path"] + l.append(ln) + s = os.linesep.join(l) + out(s) + class RepositoryConfig(object): ''' @@ -835,6 +888,8 @@ class RepositoryConfig(object): def __init__(self, name, **kwargs): # set default value for arguments + self._valid_param = ("name", "path", "dbpath", "lastpath", + "uid", "gid", "fmod", "dmod", "offline") self.name = Repository.check_repository_name(name) self.path = "" self._offline = False @@ -852,8 +907,8 @@ class RepositoryConfig(object): def __str__(self): l = [] - for a in ("name", "path", "dbpath", "lastpath", "uid", "gid", "fmod", "dmod", "offline"): - l.append("%s: %s" % (a, getattr(self, a))) + for k, v in self.items(): + l.append("%s: %s" % (k, v)) return os.linesep.join(l) def __eq__(self, other): @@ -865,6 +920,19 @@ class RepositoryConfig(object): def __contains__(self, key): return key in self.__dict__ + def __getitem__(self, key): + if key not in self._valid_param: + raise IndexError(key) + return getattr(self, key) + + def __iter__(self): + for p in self._valid_param: + yield p + + def items(self): + for p in self: + yield p, self[p] + @property def lastpath(self): ''' -- GitLab