summaryrefslogtreecommitdiff
path: root/portato/db/sql.py
blob: 581ebc4bbcb0b1bc2cb15d704059de6dd6b17ff7 (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# -*- coding: utf-8 -*-
#
# File: portato/db/sql.py
# This file is part of the Portato-Project, a graphical portage-frontend.
#
# Copyright (C) 2006-2010 René 'Necoro' Neumann
# This is free software.  You may redistribute copies of it under the terms of
# the GNU General Public License version 2.
# There is NO WARRANTY, to the extent permitted by law.
#
# Written by René 'Necoro' Neumann <necoro@necoro.net>

from __future__ import absolute_import, with_statement

try:
    import sqlite3 as sql
except ImportError:
    from pysqlite2 import dbapi2 as sql

try:
    import cPickle as pickle
except ImportError:
    import pickle

import hashlib
import os

from functools import wraps

from ..constants import SESSION_DIR
from ..helper import info, error, debug
from ..backend import system
from .database import Database, PkgData

class SQLDatabase (Database):
    
    FORMAT = "2"
    FORBIDDEN = (".bzr", ".svn", ".git", "CVS", ".hg", "_darcs")
    lock = Database.lock

    def __init__ (self, session):
        """Constructor."""
        Database.__init__(self)

        self._restrict = ""
        self.session = session
        
        updateFormat = False
        if "format" not in session or session["format"] != self.FORMAT:
            debug("Need to update database format from '%s' to '%s'", session.get("format", "undefined"), self.FORMAT)
            session["format"] = self.FORMAT
            updateFormat = True

        pkgdb = os.path.join(SESSION_DIR, "package.db")
        pkgdb_existed = os.path.exists(pkgdb)

        if pkgdb_existed:
            debug("package.db already existant")
        else:
            debug("package.db not existant")

        pkg_conn = sql.connect(pkgdb)
        pkg_conn.row_factory = sql.Row
        if pkgdb_existed and updateFormat:
            debug("Dropping old table")
            pkg_conn.execute("DROP TABLE packages")

        pkg_conn.execute("""
        CREATE TABLE IF NOT EXISTS packages
        (
            name TEXT,
            cat TEXT,
            descr TEXT DEFAULT "",
            inst INTEGER,
            disabled INTEGER
        )""")

        pkg_conn.commit()
        
        self.was_updated = self.updated()
        if self.was_updated or not pkgdb_existed or updateFormat:
            info(_("Cleaning database..."))
            pkg_conn.execute("DELETE FROM packages") # empty db at beginning
            info(_("Populating database..."))
            self.populate(connection = pkg_conn)
            
        pkg_conn.close()

    def search_types(self):
        return self.SEARCH_NAME

    def updated (self):
        changed = False

        def walk (path):
            debug("Walking %s", path)
            
            for root, dirs, files in os.walk(path):
                for f in files:
                    path = os.path.join(root, f)
                    yield "%s %s" % (f, os.stat(path).st_mtime)
                
                for forbidden in self.FORBIDDEN:
                    if forbidden in dirs:
                        dirs.remove(forbidden)

        overlays = system.get_global_settings("PORTDIR_OVERLAY").split()
        hashes = {}
        for overlay in overlays:
            hashes[overlay] = hashlib.md5("".join(walk(overlay))).hexdigest()
        
        timestamp = os.path.join(system.get_global_settings("PORTDIR"), "metadata/timestamp")
        hashes["ROOT"] = hashlib.md5("%s %s" % (timestamp, os.stat(timestamp).st_mtime)).hexdigest()
    
        dbpath = os.path.join(SESSION_DIR, "portdirs.db")
        db_existed = os.path.exists(dbpath)

        if db_existed and "pickle" not in self.session:
            debug("Removing old portdirs.db, as this looks like old DBM format. If it is not - well - no real harm ;)")
            os.remove(dbpath)
            db_existed = False

        self.session["pickle"] = "1" # no need for a certain value

        if db_existed:
            debug("portdirs.db already existant")
            with open(dbpath, "rb") as dbfile:
                db = pickle.load(dbfile)

            # the following could be simplified - losing the debug possibilities
            # so we keep it as is :)
            # there shouldn't be so much overlays, that this would result
            # in performance loss

            for key in set(db.keys()) - set(hashes.keys()):
                debug("Overlay '%s' has been removed", key)
                changed = True
            
            for key in hashes.iterkeys():

                if key not in db:
                    debug("Overlay '%s' has been added.", key)
                    changed = True

                elif db[key] != hashes[key]:
                    debug("Overlay '%s' has been changed.", key)
                    changed = True
            
        else:
            debug("portdirs.db not existant")
            changed = True
        
        if changed:
            with open(dbpath, "wb") as dbfile:
                db = pickle.dump(hashes, dbfile, protocol = -1)

        return changed

    def con (f):
        @wraps(f)
        def wrapper (*args, **kwargs):
            if not "connection" in kwargs:
                con= sql.connect(os.path.join(SESSION_DIR, "package.db"))
                con.row_factory = sql.Row
                kwargs["connection"] = con

            return f(*args, **kwargs)
        
        return Database.lock(wrapper)

    @con
    def populate (self, category = None, connection = None):
        def _get():
            # get the lists
            inst = set(system.find_packages(pkgSet = system.SET_INSTALLED, key=category, with_version = False))
            for p in system.find_packages(key = category, with_version = False):
                cat, pkg = p.split("/")

                yield (cat, pkg, p in inst, False)

        connection.executemany("INSERT INTO packages (cat, name, inst, disabled) VALUES (?, ?, ?, ?)", _get())
        connection.commit()

    @con
    def get_cat (self, category = None, byName = True, showDisabled = False, connection = None):
        sort = "ORDER BY name"
        if not byName:
            sort = "ORDER BY inst DESC, name"

        disabled = "1=1"
        if not showDisabled:
            disabled = "disabled = 0"

        if not category or category == self.ALL:
            c = connection.execute("SELECT cat, name, inst, disabled FROM packages WHERE %s %s %s" % (disabled, self.restrict, sort))
        else:
            c = connection.execute("SELECT cat, name, inst, disabled FROM packages WHERE cat = ? AND %s %s %s" % (disabled, self.restrict ,sort), (category,))
        
        for pkg in c:
            yield PkgData(pkg["cat"], pkg["name"], pkg["inst"], pkg["disabled"])
        c.close()

    @con
    def get_categories (self, installed = False, connection = None):

        if installed:
            where = "inst = 1"
        else:
            where = "1 = 1"

        c = connection.execute("SELECT cat FROM packages WHERE disabled = 0 AND %s %s GROUP BY cat" % (where, self.restrict))

        l = c.fetchall()
        c.close()

        if len(l) > 1:
            yield self.ALL
        
        for cat in l:
            yield cat["cat"]

    def generate_cat_expr (self, cat):
        """
        Generates an expression from a category name to match all packages of the category.

        E.g. as SQLDatabase uses regexps internally, return cat/*

        @param cat: the category
        @type cat: string

        @returns: expression
        """

        return cat+"/*"

    @con
    def reload (self, cat = None, connection = None):
        if cat:
            connection.execute("DELETE FROM packages WHERE cat = ?", (cat,))
            connection.commit()
            self.populate(self.generate_cat_expr(cat), connection = connection)
        else:
            connection.execute("DELETE FROM packages")
            connection.commit()
            self.populate(connection = connection)

    @con
    def disable (self, cpv, connection = None):
        cat, pkg = cpv.split("/")
        connection.execute("UPDATE packages SET disabled = 1 WHERE cat = ? AND name = ?", (cat, pkg))
        connection.commit()

    def get_restrict (self):
        return self._restrict

    @lock
    def set_restrict (self, restrict):
        if not restrict:
            self._restrict = ""
        else:
            restrict = restrict.replace(".*","%").replace(".","_")
            rest = ""

            if self._type & self.SEARCH_NAME:
                if "/" in restrict:
                    rest = "(name LIKE '%s%%' AND cat LIKE '%s')" % (pkg, cat)
                else:
                    rest = "(name LIKE '%%%(restrict)s%%' OR cat LIKE '%(restrict)s%%')" % {"restrict":restrict}
            
            if self._type & self.SEARCH_DESCRIPTION:
                r = "descr LIKE '%%%(restrict)s%%'" % {"restrict":restrict}
                if rest:
                    rest = "(%s OR %s)" % (r, rest)
                else:
                    rest = r

            self._restrict = "AND " + rest

    restrict = property(get_restrict, set_restrict)
    con = staticmethod(con)