euscanwww: scan_upstream imports euscan.scan

scan_upstream command imports euscan.scan and simply calls a function,
it doesn't call a subprocess

Signed-off-by: volpino <fox91@anche.no>
This commit is contained in:
volpino 2012-05-31 18:42:37 +02:00
parent e8718748b4
commit 47ec539e1d
3 changed files with 26 additions and 11 deletions

View File

@ -1,14 +1,16 @@
import subprocess
import portage import portage
import sys import sys
import re import re
from StringIO import StringIO
from optparse import make_option from optparse import make_option
from django.utils import timezone from django.utils import timezone
from django.db.transaction import commit_on_success from django.db.transaction import commit_on_success
from django.core.management.base import BaseCommand from django.core.management.base import BaseCommand
from euscan import CONFIG, output
from euscan.scan import scan_upstream
from djeuscan.models import Package, Version, EuscanResult, VersionLog from djeuscan.models import Package, Version, EuscanResult, VersionLog
@ -17,13 +19,23 @@ class ScanUpstream(object):
self.quiet = quiet self.quiet = quiet
def scan(self, package): def scan(self, package):
cmd = ['euscan', package] CONFIG["format"] = "dict"
output.set_query(package)
scan_upstream(package)
out = output.get_formatted_output()
out_json = output.get_formatted_output("json")
fp = subprocess.Popen(cmd, stdout=subprocess.PIPE, try:
stderr=subprocess.PIPE) cp = out["metadata"]["cp"]
output = StringIO(fp.communicate()[0]) except KeyError:
return {}
self.parse_output(output) for out in out["result"]:
self.store_version(cp, out["version"], " ".join(out["urls"]))
self.store_result(cp, out_json)
return out
def store_result(self, package, log): def store_result(self, package, log):
# Remove previous logs # Remove previous logs

View File

@ -67,4 +67,4 @@ ROBOTS_TXT_BLACKLIST_DOMAINS = [
] ]
from out import EuscanOutput from out import EuscanOutput
output = out.EuscanOutput(CONFIG) output = EuscanOutput(CONFIG)

View File

@ -95,7 +95,7 @@ class EuscanOutput(object):
"metadata": {}, "metadata": {},
} }
def get_formatted_output(self): def get_formatted_output(self, format_=None):
data = {} data = {}
for query in self.queries: for query in self.queries:
@ -105,8 +105,11 @@ class EuscanOutput(object):
"messages": self.queries[query]["output"].getvalue(), "messages": self.queries[query]["output"].getvalue(),
} }
if self.config["format"].lower() == "json": format_ = format_ or self.config["format"]
if format_.lower() == "json":
return json.dumps(data, indent=self.config["indent"]) return json.dumps(data, indent=self.config["indent"])
elif format_.lower() == "dict":
return data
else: else:
raise TypeError("Invalid output format") raise TypeError("Invalid output format")
@ -133,7 +136,7 @@ class EuscanOutput(object):
print "%s: %s" % (key.capitalize(), value) print "%s: %s" % (key.capitalize(), value)
def __getattr__(self, key): def __getattr__(self, key):
if not self.config["quiet"]: if not self.config["quiet"] and self.current_query is not None:
output = self.queries[self.current_query]["output"] output = self.queries[self.current_query]["output"]
return getattr(output, key) return getattr(output, key)
else: else: