diff options
author | Vikraman Choudhury <vikraman.choudhury@gmail.com> | 2011-06-08 03:34:06 +0530 |
---|---|---|
committer | Vikraman Choudhury <vikraman.choudhury@gmail.com> | 2011-06-08 03:34:06 +0530 |
commit | f175ca30a0c692f5c28f8baa7cc397fac853f766 (patch) | |
tree | 8568c1fc978871663187b2e76f69304e0565c33b | |
parent | fix foreign key constraints (diff) | |
download | gentoostats-f175ca30a0c692f5c28f8baa7cc397fac853f766.tar.gz gentoostats-f175ca30a0c692f5c28f8baa7cc397fac853f766.tar.bz2 gentoostats-f175ca30a0c692f5c28f8baa7cc397fac853f766.zip |
update post handler
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | server/helpers.py | 81 | ||||
-rw-r--r-- | server/post.py | 110 |
3 files changed, 145 insertions, 47 deletions
@@ -1,2 +1,3 @@ *.*~ *.pyc +*.swp diff --git a/server/helpers.py b/server/helpers.py new file mode 100644 index 0000000..571ffa6 --- /dev/null +++ b/server/helpers.py @@ -0,0 +1,81 @@ + +import re + +# split package name into cpv +# based on pkgsplit code +# in portage/versions.py + +def pkgsplit(pkgname): + cpv={} + pkgsplit = pkgname.split('/',1) + cpv['cat'] = pkgsplit[0] + pv_re =re.compile(r'(?x)^(?P<pn>[\w\+][\w\+-]*?(?P<pn_inval>-(cvs\.)?(\d+)((\.\d+)*)([a-z]?)((_(pre|p|beta|alpha|rc)\d*)*)(-r(\d+))?)?)-(?P<ver>(cvs\.)?(\d+)((\.\d+)*)([a-z]?)((_(pre|p|beta|alpha|rc)\d*)*))(-r(?P<rev>\d+))?$') + m = pv_re.match(pkgsplit[1]) + cpv['pkg'] = m.group('pkg') + rev = m.group('rev') + if rev is None: + cpv['ver'] = m.group('ver') + else: + cpv['ver'] = m.group('ver') + '-r' + rev + return cpv + +# get functions for index keys +# lookup key and insert if not found + +def get_kwkey(db, keyword): + db_keyword = db.select('keywords', vars={'keyword':keyword}, where='keyword=$keyword') + if len(db_keyword): + kwkey = db_keyword[0].kwkey + else: + kwkey = db.insert('keywords', keyword=keyword) + return kwkey + +def get_lkey(db, lang): + db_lang = db.select('lang', vars={'lang':lang}, where='lang=$lang') + if len(db_lang): + lkey = db_lang[0].lkey + else: + lkey = db.insert('lang', lang=lang) + return lkey + +def get_fkey(db, feature): + db_feature = db.select('features', vars={'feature':feature}, where='feature=$feature') + if len(db_feature): + fkey = db_feature[0].fkey + else: + fkey = db.insert('features', feature=feature) + return fkey + +def get_mkey(db, mirror): + db_mirror = db.select('gentoo_mirrors', vars={'mirror':mirror}, where='mirror=$mirror') + if len(db_mirror): + mkey = db_mirror[0].mkey + else: + mkey = db.insert('gentoo_mirrors', mirror=mirror) + return mkey + +def get_ukey(db, useflag): + db_useflag = db.select('useflags', vars={'useflag':useflag}, where='useflag=$useflag') + if len(db_useflag): + ukey = db_useflag[0].ukey + else: + ukey = db.insert('useflags', useflag=useflag) + return ukey + +def get_pkey(db, package): + cpv = pkgsplit(package) + db_package = db.select('packages', vars=cpv, where='cat=$cat and pkg=$pkg and ver=$ver') + if len(db_package): + pkey = db_package[0].pkey + else: + pkey = db.insert('packages', cat=cpv['cat'], pkg=cpv['pkg'], ver=cpv['ver']) + return pkey + +def get_rkey(db, repo): + db_repo = db.select('repositories', vars={'repo':repo}, where='repo=$repo') + if len(db_repo): + rkey = db_repo[0].rkey + else: + rkey = db.insert('repositories', repo=repo) + return rkey + diff --git a/server/post.py b/server/post.py index 0aa1d98..1ca491d 100644 --- a/server/post.py +++ b/server/post.py @@ -1,23 +1,6 @@ #!/usr/bin/env python -import re - -def pkgsplit(pkgname): - #TODO: Improve this - cpv={} - pkgsplit = pkgname.split('/',1) - cpv['cat'] = pkgsplit[0] - - pv_re =re.compile(r'(?x)^(?P<pn>[\w\+][\w\+-]*?(?P<pn_inval>-(cvs\.)?(\d+)((\.\d+)*)([a-z]?)((_(pre|p|beta|alpha|rc)\d*)*)(-r(\d+))?)?)-(?P<ver>(cvs\.)?(\d+)((\.\d+)*)([a-z]?)((_(pre|p|beta|alpha|rc)\d*)*))(-r(?P<rev>\d+))?$') - m = pv_re.match(pkgsplit[1]) - cpv['pn'] = m.group('pn') - rev = m.group('rev') - if rev is None: - cpv['ver'] = m.group('ver') - else: - cpv['ver'] = m.group('ver') + '-r' + rev - - return cpv +from helpers import * def handler(uuid, data, db): #TODO: Handle exceptions @@ -27,35 +10,68 @@ def handler(uuid, data, db): if data['AUTH']['UUID'] != uuid: return 'Invalid uuid!' - host = db.select('hosts', vars={'uuid':uuid}, where='uuid=$uuid') - if len(host): - if data['AUTH']['PASSWD'] == host[0].passwd: - exists = True - else: - return 'Wrong password!' - else: - db.insert('hosts', uuid=uuid, passwd=data['AUTH']['PASSWD']) - exists = False - - if exists: - db.delete('env', vars={'uuid':uuid}, where='uuid=$uuid') - - for var in ['CFLAGS', 'CXXFLAGS', 'LDFLAGS', 'CHOST', 'FEATURES']: - db.insert('env', uuid=uuid, var=var, value=data[var]) + # Insert in hosts + db_host = db.select('hosts', vars={'uuid':uuid}, where='uuid=$uuid') + if len(db_host): + if data['AUTH']['PASSWD'] != db_host[0].passwd: + return 'Wrong password!' + # This should delete all host entries from all tables + db.delete('hosts', vars={'uuid':uuid}, where='uuid=$uuid') + db.insert('hosts', uuid=uuid, passwd=data['AUTH']['PASSWD']) - if exists: - db.delete('useflags', vars={'uuid':uuid}, where='uuid=$uuid') - - pkg = data['PACKAGES'] - for cpv in pkg.keys(): - t = pkgsplit(cpv) - s = db.select('packages', vars={'cat':t['cat'], 'pkg':t['pn'], 'ver':t['ver']}, - where='cat=$cat and pkg=$pkg and ver=$ver') - if len(s) == 0: - pkey = db.insert('packages', cat=t['cat'], pkg=t['pn'], ver=t['ver']) - else: - pkey = s[0].pkey - for use in pkg[cpv]: - db.insert('useflags', uuid=uuid, useflag=use, pkey=str(pkey)) + # Insert in env + for var in ['ARCH','CHOST','CFLAGS','CXXFLAGS','FFLAGS','LDFLAGS','MAKEOPTS','SYNC','PLATFORM','PROFILE','LASTSYNC']: + db.insert('env', uuid=uuid, var=var, value=data[var]) + # Insert in global_keywords + for keyword in data['ACCEPT_KEYWORDS']: + kwkey = get_kwkey(db, keyword) + db.insert('global_keywords', uuid=uuid, kwkey=kwkey) + + # Insert in host_lang + for lang in data['LANG']: + lkey = get_lkey(db, lang) + db.insert('host_lang', uuid=uuid, lkey=lkey) + + # Insert in host_features + for feature in data['FEATURES']: + fkey = get_fkey(db, feature) + db.insert('host_features', uuid=uuid, fkey=fkey) + + # Insert in host_mirrors + for mirror in data['GENTOO_MIRRORS']: + mkey = get_mkey(db, mirror) + db.insert('host_mirrors', uuid=uuid, mkey=mkey) + + # Insert in global_useflags + for useflag in data['USE']: + ukey = get_ukey(db, useflag) + db.insert('global_useflags', uuid=uuid, ukey=ukey) + + # Handle packages + for package in data['PACKAGES'].keys(): + pkey = get_pkey(db, package) + data_pkg = data['PACKAGES'][package] + kwkey = get_kwkey(db, data_pkg['KEYWORD']) + rkey = get_rkey(db, data_pkg['REPO']) + + # Insert in installed_packages + ipkey = db.insert('installed_packages', uuid=uuid, pkey=pkey, build_time=data_pkg['BUILD_TIME'], + counter=data_pkg['COUNTER'], kwkey=kwkey, rkey=rkey, size=data_pkg['SIZE']) + + # Insert in plus_useflags + for useflag in data_pkg['USE']['PLUS']: + ukey = get_ukey(db, useflag) + db.insert('plus_useflags', ipkey=ipkey, ukey=ukey) + + # Insert in minus_useflags + for useflag in data_pkg['USE']['MINUS']: + ukey = get_ukey(db, useflag) + db.insert('minus_useflags', ipkey=ipkey, ukey=ukey) + + # Insert in unset_useflags + for useflag in data_pkg['USE']['UNSET']: + ukey = get_ukey(db, useflag) + db.insert('unset_useflags', ipkey=ipkey, ukey=ukey) + return 'POST for ' + uuid + ' successful' |