~lubuntu-software-center-team/lubuntu-software-center/vala-port

« back to all changes in this revision

Viewing changes to script/light-software-center-build-db

  • Committer: Stephen Smally
  • Date: 2012-03-04 12:59:13 UTC
  • Revision ID: eco.stefi@fastwebnet.it-20120304125913-bk1iutifwoeoyo0i
Worked a lot!

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
# -*- coding:UTF-8 -*-
 
3
#       Copyright (c) Stephen Smally <stephen.smally@gmail.com>
 
4
#
 
5
#       This program is free software; you can redistribute it and/or modify
 
6
#       it under the terms of the GNU General Public License as published by
 
7
#       the Free Software Foundation; either version 2 of the License, or
 
8
#       (at your option) any later version.
 
9
#       
 
10
#       This program is distributed in the hope that it will be useful,
 
11
#       but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
#       GNU General Public License for more details.
 
14
#       
 
15
#       You should have received a copy of the GNU General Public License
 
16
#       along with this program; if not, write to the Free Software
 
17
#       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 
18
#       MA 02110-1301, USA.
 
19
#  
 
20
 
 
21
usage = '''Build a sqlite3-based database from the given directory of desktop files
 
22
Usage: lubuntu-software-center-build-db [DB_PATH] [DIRECTORY] [CATEGORYFILE]'''
 
23
 
 
24
import sqlite3
 
25
import sys
 
26
import os
 
27
from xdg import DesktopEntry as DE
 
28
import apt_pkg
 
29
from ConfigParser import RawConfigParser
 
30
import platform
 
31
 
 
32
arch_dict = {
 
33
"x86_64": "amd64",
 
34
"i686": "i386"
 
35
}
 
36
 
 
37
arch = arch_dict[platform.machine()]
 
38
 
 
39
if len(sys.argv) < 4:
 
40
    print(usage)
 
41
    sys.exit()
 
42
 
 
43
database = sys.argv[1]
 
44
base_folder = sys.argv[2]
 
45
tags_file = open(sys.argv[3], "r")
 
46
 
 
47
if os.path.exists(database):
 
48
    os.remove(database)
 
49
 
 
50
 
 
51
print("Creating package database in %s" % database)
 
52
 
 
53
db = sqlite3.Connection(database)
 
54
cursor = db.cursor()
 
55
 
 
56
aiddir = os.listdir(base_folder)
 
57
 
 
58
def tag_in_cat(tag):
 
59
    for (index, tags) in app_tags:
 
60
        if tag in tags:
 
61
            return index
 
62
   
 
63
 
 
64
def get_pkg_depends(pkg):
 
65
    depends_list = []
 
66
    rec = []
 
67
    if not (cache[pkg].version_list == []) and (depcache.get_candidate_ver(cache[pkg]) != None):
 
68
        version = depcache.get_candidate_ver(cache[pkg])
 
69
        try:
 
70
            depends_list = version.depends_list_str["Depends"]
 
71
        except:
 
72
            depends_list = []
 
73
        try:
 
74
            rec = version.depends_list_str["Recommends"]
 
75
        except:
 
76
            rec = []
 
77
    
 
78
    elif not cache[pkg].provides_list == []:
 
79
        for pkgs in cache[pkg].provides_list:
 
80
            version = pkgs[2]
 
81
            try:
 
82
                depends_list = version.depends_list_str["Depends"]
 
83
            except:
 
84
                depends_list = []
 
85
            try:
 
86
                rec = version.depends_list_str["Recommends"]
 
87
            except:
 
88
                rec = []
 
89
    return  (depends_list, rec)
 
90
 
 
91
packages = {}
 
92
 
 
93
apt_pkg.init()
 
94
cache = apt_pkg.Cache()
 
95
depcache = apt_pkg.DepCache(cache)
 
96
infos = apt_pkg.PackageRecords(cache)
 
97
 
 
98
for item in aiddir:
 
99
    if item.partition(".desktop")[-2] == ".desktop":
 
100
        single_pkg = DE.DesktopEntry(base_folder+item)
 
101
        name = single_pkg.getName()
 
102
        pkg = single_pkg.get("X-AppInstall-Package")
 
103
        icon = single_pkg.get("Icon")
 
104
        categs = single_pkg.getCategories()
 
105
        if categs == []:
 
106
            categs = [u"Other"]
 
107
        comment = single_pkg.get("Comment").decode("UTF-8")
 
108
        dep_list = []
 
109
        rec_list = []
 
110
        if (pkg in cache) and (cache[pkg].has_versions):
 
111
            try:
 
112
                deps = get_pkg_depends(pkg)           
 
113
                for items in deps[0]:
 
114
                    dep_list.append(items[0][0])
 
115
                for items in deps[1]:
 
116
                    rec_list.append(items[0][0])
 
117
                ver = depcache.get_candidate_ver(cache[pkg])
 
118
                infos.lookup(ver.translated_description.file_list[0])
 
119
                desc = infos.long_desc
 
120
                if comment == u'':
 
121
                    comment = infos.short_desc.decode("UTF-8").capitalize()
 
122
                packages[pkg] = [
 
123
                categs,
 
124
                name.capitalize(),
 
125
                comment,
 
126
                icon,
 
127
                desc,
 
128
                ";".join(dep_list),
 
129
                ";".join(rec_list)
 
130
                ]
 
131
            except AttributeError: pass
 
132
                
 
133
        else:
 
134
            print("%s: package not found" % pkg)
 
135
 
 
136
app_tags = {}
 
137
cat_parser = RawConfigParser()
 
138
cat_parser.readfp(tags_file)
 
139
cursor.execute("CREATE TABLE tables(id, contains, showboth, name, comment, icon)")
 
140
for section in cat_parser.sections():
 
141
    cursor.execute("INSER INTO tables VALUES (?, ?, ?, ?, ?, ?)", (
 
142
        section,
 
143
        cat_parser.get(section, "contains"),
 
144
        cat_parser.get(section, "showboth"),
 
145
        cat_parser.get(section, "name"),
 
146
        cat_parser.get(section, "comment"),
 
147
        cat_parser.get(section, "icon")
 
148
    ))
 
149
    categ_string = "CREATE TABLE %s(name, pkg_name, tags, comment, icon, desc, deps, recs, ID)" % section
 
150
    print("Creating table %s" % section)
 
151
    cursor.execute(categ_string)
 
152
    app_tags[section] = cat_parser.get(section, "contains")
 
153
 
 
154
def in_cat(tags):
 
155
    tag_list = ["packages"]
 
156
    for tag in tags:
 
157
        for item in app_tags.items():
 
158
            if tag in item[1].strip(";").split(";"):
 
159
                if not item[0] in tag_list:
 
160
                    tag_list.append(item[0])
 
161
    return tag_list
 
162
 
 
163
for package in cache.packages:
 
164
    pkg = package.name
 
165
    if package.architecture == arch:
 
166
        if package.has_versions:
 
167
            try:
 
168
                if pkg in packages:
 
169
                    id = 0
 
170
                    cat = ";".join(packages[pkg][0])
 
171
                    name = packages[pkg][1]
 
172
                    comment = packages[pkg][2]
 
173
                    icon = packages[pkg][3]
 
174
                    desc = packages[pkg][4]
 
175
                    deps = packages[pkg][5]
 
176
                    rec = packages[pkg][6]
 
177
                else:
 
178
                    id = 1
 
179
                    ver = depcache.get_candidate_ver(package)
 
180
                    infos.lookup(ver.translated_description.file_list[0])
 
181
                    cat = package.section.replace("multiverse/", "").replace("universe/", "").replace("restricted/", "")
 
182
                    name = pkg.capitalize()
 
183
                    comment = infos.short_desc
 
184
                    icon = u"deb"
 
185
                    desc = infos.long_desc
 
186
                    deps_func = get_pkg_depends(pkg)
 
187
                    dep_list = []
 
188
                    rec_list = []       
 
189
                    for items in deps_func[0]:
 
190
                        dep_list.append(items[0][0])
 
191
                    for items in deps_func[1]:
 
192
                        rec_list.append(items[0][0])
 
193
                    deps = ";".join(dep_list)
 
194
                    recs = ";".join(rec_list)
 
195
                for items in in_cat(cat.split(";")):
 
196
                    cursor.execute("INSERT INTO %s VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)" % items , (name, pkg, cat, comment, icon, desc, deps, recs, id))
 
197
            except:
 
198
                print("%s: not found" % pkg)
 
199
    
 
200
print("Done")
 
201
db.commit()
 
202