summaryrefslogtreecommitdiffstats
path: root/leafymiracle/populate.py
blob: 9f78fc55b6941753568840996f98bfede29a3369 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# Copyright (C) 2011 Luke Macken <lmacken@redhat.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from sqlalchemy import create_engine
from sqlalchemy.exc import IntegrityError
from kitchen.text.converters import to_unicode

from models import Root, Category, Group, Package, DBSession, initialize_sql

def populate(comps='comps-f16'):
    from yum.comps import Comps

    session = DBSession()

    c = Comps()
    c.add('comps/%s.xml' % comps)

    for group in c.groups:
        g = Group(id=group.groupid, name=group.name, description=group.description)
        session.add(g)

        for package in group.packages:
            p = session.query(Package).filter_by(name=to_unicode(package)).first()
            if not p:
                p = Package(name=package)
                session.add(p)
            p.group = g

        session.flush()

    root = Root(name=u'Fedora')
    session.add(root)
    session.flush()

    for category in c.categories:
        c = Category(id=category.categoryid, name=category.name,
                     description=category.description)
        session.add(c)
        root.categories.append(c)
        for group in category.groups:
            g = session.query(Group).filter_by(group_id=to_unicode(group)).first()
            if not g:
                print "Cannot find group: %s" % group
            else:
                g.category = c

        session.flush()

    session.commit()

def build_comps():
    import subprocess
    subprocess.call('git clone git://git.fedorahosted.org/comps.git', shell=True)
    subprocess.call('make comps-f16.xml', cwd='comps', shell=True)


if __name__ == '__main__':
    print "Initializing LeafyMiracle..."
    engine = create_engine('sqlite:///leafymiracle.db')
    initialize_sql(engine)
    build_comps()
    try:
        populate()
        print "Complete!"
    except IntegrityError, e:
        DBSession.rollback()