euscan: adding json output

Naive json output implmented, probably needs some further tuning

Signed-off-by: volpino <fox91@anche.no>
This commit is contained in:
volpino
2012-05-21 22:24:44 +02:00
parent 373fba6e01
commit 8cb19b5a6b
3 changed files with 170 additions and 85 deletions

View File

@ -3,11 +3,17 @@
# Copyright 2011 Corentin Chary <corentin.chary@gmail.com>
# Distributed under the terms of the GNU General Public License v2
from io import StringIO
from collections import defaultdict
import json
from gentoolkit import pprinter as pp
from portage.output import EOutput
__version__ = "git"
from portage.output import EOutput
CONFIG = {
'nocolor': False,
'quiet': False,
@ -20,11 +26,11 @@ CONFIG = {
'oneshot': True,
'user-agent': 'escan (http://euscan.iksaif.net)',
'skip-robots-txt': False,
'cache': False
'cache': False,
'format': None,
'indent': 2
}
output = EOutput(CONFIG['quiet'])
BLACKLIST_VERSIONS = [
# Compatibility package for running binaries linked against a
# pre gcc 3.4 libstdc++, won't be updated
@ -67,3 +73,67 @@ ROBOTS_TXT_BLACKLIST_DOMAINS = [
'(.*)chromium.org(.*)',
'(.*)nodejs.org(.*)',
]
class EOutputFile(EOutput):
"""
Override of EOutput, allows to specify an output file for writes
"""
def __init__(self, out_file=None, *args, **kwargs):
super(EOutputFile, self).__init__(*args, **kwargs)
self.out_file = out_file
def _write(self, f, msg):
if self.out_file is None:
super(EOutputFile, self)._write(f, msg)
else:
super(EOutputFile, self)._write(self.out_file, msg)
class EuscanOutput(object):
"""
Class that handles output for euscan
"""
def __init__(self, config):
self.config = config
self.data = defaultdict(StringIO)
self.packages = defaultdict(list)
def get_formatted_output(self):
data = {}
for key in self.data:
if key not in ("ebegin", "eend"):
val = [x for x in self.data[key].getvalue().split("\n") if x]
data[key] = val
data["result"] = self.packages
if self.config["format"].lower() == "json":
return json.dumps(data, indent=self.config["indent"])
else:
raise TypeError("Invalid output format")
def result(self, cp, version, url):
if self.config['format']:
self.packages[cp].append({"version": version, "url": url})
else:
if not self.config['quiet']:
print "Upstream Version:", pp.number("%s" % version),
print pp.path(" %s" % url)
else:
print pp.cpv("%s-%s" % (cp, version)) + ":", pp.path(url)
def __getattr__(self, key):
output_file = self.data[key] if self.config["format"] else None
if output_file:
_output = EOutputFile(out_file=self.data[key],
quiet=self.config['quiet'])
ret = getattr(_output, key)
else:
ret = getattr(EOutputFile(quiet=self.config['quiet']), key)
return ret
output = EuscanOutput(CONFIG)

View File

@ -39,7 +39,7 @@ def scan_upstream_urls(cpv, urls):
for filename in urls:
for url in urls[filename]:
if not CONFIG['quiet']:
if not CONFIG['quiet'] and not CONFIG['format']:
pp.uprint()
euscan.output.einfo("SRC_URI is '%s'" % url)
@ -101,7 +101,7 @@ def scan_upstream(query):
)
return []
if not CONFIG['quiet']:
if not CONFIG['quiet'] and not CONFIG['format']:
pp.uprint(
" * %s [%s]" % (pp.cpv(pkg.cpv), pp.section(pkg.repo_name()))
)