diff options
Diffstat (limited to 'utils')
-rwxr-xr-x | utils/db_init.py | 99 |
1 files changed, 50 insertions, 49 deletions
diff --git a/utils/db_init.py b/utils/db_init.py index 98bbe0d..725ea74 100755 --- a/utils/db_init.py +++ b/utils/db_init.py @@ -1,17 +1,28 @@ #!/usr/bin/env python -import sys - -from datetime import datetime +import os, sys from pkgcore.config import load_config from pkgcore.cache import metadata from pkgcore.ebuild import repository -from pymongo import Connection +from sqlalchemy import create_engine +from sqlalchemy.orm import scoped_session, sessionmaker +from sqlalchemy.orm.exc import NoResultFound + +path = os.path.join(os.path.dirname(__file__), os.path.pardir) +sys.path.insert(0, path) + +from grumpy.models import Base, Developer, Ebuild, Herd, Package def main(path): - # Set up a link to our MongoDB collection - db = Connection('localhost', 27017)['grumpy'] + engine = create_engine('postgresql://grumpy:grumpy@localhost/grumpy') + session = scoped_session(sessionmaker(autocommit=False, + autoflush=False, + bind=engine)) + Base.query = session.query_property() + + Base.metadata.drop_all(bind=engine) + Base.metadata.create_all(bind=engine) # pkgcore part to fetch all the ebuild information conf = load_config() @@ -21,30 +32,38 @@ def main(path): eclass_cache=eclass_cache) for pkg in overlay_repo: - ver_id = "%s/%s-%s" % (pkg.category, pkg.package, pkg.version) - pkg_id = "%s/%s" % (pkg.category, pkg.package) + pkg_id = "%s/%s" % (pkg.package, pkg.category) - # Load package info from packages collection - package = db.ebuilds.find_one(dict(_id=pkg_id)) + package = Package.query.filter_by(cp=pkg_id).first() if not package: - # ...or create data if pkg not in db - package = dict( - _id = pkg_id, - _ver = 0, # Internal schema version - cat = pkg.category, - pkg = pkg.package, - desc = pkg.description, - ldesc = pkg.longdescription, - homepage = pkg.homepage, - herds = list(pkg.herds) if pkg.herds else [], - maintainers = [m.email for m in pkg.maintainers] \ - if pkg.maintainers else [], - ) - else: - # TODO: Should we update it here? - pass - # Housekeeping - package['synced_on'] = datetime.now() + + package = Package(pkg.category, pkg.package, pkg.description, \ + pkg.longdescription, pkg.homepage) + # Fetch devs and herds from db + devs = [] + for dev in pkg.maintainers: + try: + devs.append(Developer.query.filter_by(email=dev.email).one()) + except NoResultFound: + devs.append(Developer(dev.email)) + session.add_all(devs) + herds = [] + for herd in pkg.herds: + # Workaround for empty <herd></herd> tags + if herd is None: + herd = 'fix-me' + herd = herd.strip() + try: + herds.append(Herd.query.filter_by(name=herd).one()) + except NoResultFound: + herds.append(Herd(herd)) + session.add_all(herds) + + package.devs = devs + package.herds = herds + + session.add(package) + session.commit() # Parse IUSE (iuse contains all flags, fiuse contains only '+use') iuse = list() @@ -56,27 +75,9 @@ def main(path): else: iuse.append(u) - # We don't care about existing ebuilds and just overwrite them - ebuild = dict( - _id = ver_id, - _ver = 0, - eapi = pkg.eapi, - cat = pkg.category, - pkg = pkg.package, - iuse = iuse, - fiuse = fiuse, - keywords = list(pkg.keywords) if pkg.keywords else [], - slot = pkg.slot, - version = pkg.version, - # TODO, need to figure out a proper queryable structure for these -# license = pkg.license, -# depends = pkg.depends, -# rdepends = pkg.rdepends - - # Housekeeping - synced_on = datetime.now() - ) - print "Adding: ", db.packages.save(package), db.ebuilds.save(ebuild) + session.add(Ebuild(package, pkg.version, pkg.revision, pkg.eapi, \ + pkg.slot, pkg.keywords, iuse, fiuse)) + session.commit() if __name__ == '__main__': if len(sys.argv) != 2: |