diff options
author | Vikraman Choudhury <vikraman.choudhury@gmail.com> | 2011-06-04 23:52:09 +0530 |
---|---|---|
committer | Vikraman Choudhury <vikraman.choudhury@gmail.com> | 2011-06-04 23:52:09 +0530 |
commit | e5ff4157b6470cd665ab8d6f46a108e2714bdf1a (patch) | |
tree | c683f88c2a8d1f07dd7c284008b7374fc688b84f | |
parent | fix trivia in docs (diff) | |
download | gentoostats-e5ff4157b6470cd665ab8d6f46a108e2714bdf1a.tar.gz gentoostats-e5ff4157b6470cd665ab8d6f46a108e2714bdf1a.tar.bz2 gentoostats-e5ff4157b6470cd665ab8d6f46a108e2714bdf1a.zip |
rewrote client, added more fields
-rw-r--r-- | client/environment.py | 47 | ||||
-rw-r--r-- | client/metadata.py | 45 | ||||
-rw-r--r-- | client/packages.py | 1 | ||||
-rw-r--r-- | client/payload.py | 49 | ||||
-rwxr-xr-x | client/send.py (renamed from client/bin/client) | 29 | ||||
-rw-r--r-- | client/useflags.py | 8 |
6 files changed, 146 insertions, 33 deletions
diff --git a/client/environment.py b/client/environment.py index 07b942e..f03a76c 100644 --- a/client/environment.py +++ b/client/environment.py @@ -1,9 +1,52 @@ import logging +import os +import platform import portage +from _emerge.actions import relative_profile_path class Environment(object): + def __init__(self): + self.portdir = portage.settings['PORTDIR'] + def getVar(self, myvar): - ret = portage.settings[myvar] - return ret + return portage.settings[myvar] + + def getPlatform(self): + return platform.platform(aliased=1) + + def getLastSync(self): + lastsync = None + try: + lastsync = portage.grabfile(os.path.join(self.portdir, 'metadata', 'timestamp.chk')) + except portage.exception.PortageException: + pass + if lastsync is None: + return 'Unknown' + return lastsync[0] + + def getProfile(self): + profilever = None + profile = portage.settings.profile_path + if profile: + profilever = relative_profile_path(self.portdir, profile) + if profilever is None: + try: + for parent in portage.grabfile(os.path.join(profile, 'parent')): + profilever = relative_profile_path(self.portdir,os.path.join(profile, parent)) + if profilever is not None: + break + except portage.exception.PortageException: + pass + + if profilever is None: + try: + profilever = '!' + os.readlink(profile) + except (OSError): + pass + + if profilever is None: + profilever = 'Unknown' + + return profilever diff --git a/client/metadata.py b/client/metadata.py new file mode 100644 index 0000000..0b6a751 --- /dev/null +++ b/client/metadata.py @@ -0,0 +1,45 @@ + +import portage +from dbapi import VARDB +from gentoolkit.enalyze.lib import FlagAnalyzer +from gentoolkit.enalyze.lib import KeywordAnalyser + +class Metadata(object): + + def __init__(self, cpv): + self.repo, self.counter, self.build_time, self.size = VARDB.aux_get(cpv,['repository','COUNTER','BUILD_TIME','SIZE']) + + system_use = portage.settings['USE'].split() + fa = FlagAnalyzer(system=system_use) + self.flags = fa.analyse_cpv(cpv) + + arch = portage.settings['ARCH'] + accept_keywords = portage.settings['ACCEPT_KEYWORDS'].split() + ka = KeywordAnalyser(arch=arch,accept_keywords=accept_keywords) + self.keyword = ka.get_inst_keyword_cpv(cpv) + + def getPlusFlags(self): + return list(self.flags[0]) + + def getMinusFlags(self): + return list(self.flags[1]) + + def getUnsetFlags(self): + return list(self.flags[2]) + + def getKeyword(self): + return self.keyword + + def getRepoName(self): + if self.repo: + return self.repo + return 'Unknown' + + def getCounter(self): + return self.counter + + def getBuildTime(self): + return self.build_time + + def getSize(self): + return self.size diff --git a/client/packages.py b/client/packages.py index 73cdfc5..7f9ca35 100644 --- a/client/packages.py +++ b/client/packages.py @@ -1,5 +1,6 @@ import logging +import portage from dbapi import VARDB class Packages(object): diff --git a/client/payload.py b/client/payload.py new file mode 100644 index 0000000..b888af9 --- /dev/null +++ b/client/payload.py @@ -0,0 +1,49 @@ + +import pprint +from environment import Environment +from packages import Packages +from metadata import Metadata + +class Payload(object): + + def __init__(self): + self.payload = dict() + self.payload['PROTOCOL'] = 1 + self.update() + + def update(self): + + env = Environment() + self.payload['PLATFORM'] = env.getPlatform() + self.payload['LASTSYNC'] = env.getLastSync() + self.payload['PROFILE'] = env.getProfile() + + for var in ['ARCH','CFLAGS','CXXFLAGS','FFLAGS','LDFLAGS','MAKEOPTS']: + self.payload[var] = env.getVar(var) + + for var in ['ACCEPT_KEYWORDS','LANG','GENTOO_MIRRORS','SYNC','FEATURES','USE']: + self.payload[var] = env.getVar(var).split() + + self.payload['PACKAGES'] = dict() + for cpv in Packages().getInstalledCPVs(): + m = Metadata(cpv) + p = dict() + p['REPO'] = m.getRepoName() + p['KEYWORD'] = m.getKeyword() + p['USE'] = dict() + p['USE']['PLUS'] = m.getPlusFlags() + p['USE']['MINUS'] = m.getMinusFlags() + p['USE']['UNSET'] = m.getUnsetFlags() + p['COUNTER'] = m.getCounter() + p['SIZE'] = m.getSize() + p['BUILD_TIME'] = m.getBuildTime() + self.payload['PACKAGES'][cpv] = p + + def get(self): + return self.payload + + def dump(self,human=False): + if human: + pprint.pprint(self.payload) + else: + print self.payload diff --git a/client/bin/client b/client/send.py index f920250..8bf2bd2 100755 --- a/client/bin/client +++ b/client/send.py @@ -1,8 +1,6 @@ #!/usr/bin/env python -from packages import Packages -from useflags import UseFlags -from environment import Environment +from payload import Payload import json import urllib, httplib @@ -14,24 +12,6 @@ def getAuthInfo(): } return auth_info -def getPostData(): - p = Packages() - u = UseFlags() - e = Environment() - - post_data = {} - post_data['PROTOCOL'] = 1 - post_data['AUTH'] = getAuthInfo() - for key in ('CFLAGS', 'CXXFLAGS', 'LDFLAGS', 'CHOST', 'FEATURES'): - post_data[key] = e.getVar(key) - - packages = {} - for cpv in p.getInstalledCPVs(sort=True): - packages[cpv] = u.getUseFlags(cpv) - post_data['PACKAGES'] = packages - - return post_data - def serialize(object, human=False): if human: indent = 2 @@ -42,8 +22,11 @@ def serialize(object, human=False): return json.JSONEncoder(indent=indent, sort_keys=sort_keys).encode(object) def main(): - post_body = serialize(getPostData(), human=True) - print post_body + pl = Payload() + pl.dump(human=True) + post_data = pl.get() + post_data['AUTH'] = getAuthInfo() + post_body = serialize(post_data,human=True) post_headers = {"Content-type": "application/json"} myuuid = getAuthInfo()['UUID'] conn = httplib.HTTPConnection("127.0.0.1:8080") diff --git a/client/useflags.py b/client/useflags.py deleted file mode 100644 index 84a8dcf..0000000 --- a/client/useflags.py +++ /dev/null @@ -1,8 +0,0 @@ - -#TODO: Remove gentoolkit dependency -from gentoolkit import flag - -class UseFlags(object): - - def getUseFlags(self, cpv): - return flag.get_flags(cpv, True)[1] |