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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
|
# -*- coding: utf-8 -*-
"""
grumpy.models
~~~~~~~~~~~~~
This module contains the models and low-level database glue for the
application.
:copyright: (c) by 2010 Priit Laes.
:license: BSD, see LICENSE for details.
"""
from datetime import datetime
from flaskext.sqlalchemy import SQLAlchemy
from sqlalchemy.sql import func
from sqlalchemy.orm.collections import column_mapped_collection
import json, random, string, time
from . import app
from .utils import compare_version
db = SQLAlchemy(app)
# Association tables
package_developers = db.Table('package_developers', db.Model.metadata,
db.Column('package_id', db.Integer, db.ForeignKey('packages.id')),
db.Column('developer_id', db.Integer, db.ForeignKey('developers.id'))
)
package_herds = db.Table('package_herds', db.Model.metadata,
db.Column('package_id', db.Integer, db.ForeignKey('packages.id')),
db.Column('herd_id', db.Integer, db.ForeignKey('herds.id'))
)
package_users = db.Table('package_users', db.Model.metadata,
db.Column('package_id', db.Integer, db.ForeignKey('packages.id')),
db.Column('user_id', db.Integer, db.ForeignKey('users.id'))
)
class Developer(db.Model):
"""Represents developers in the system"""
__tablename__ = 'developers'
id = db.Column('id', db.Integer, primary_key=True)
email = db.Column('email', db.String, nullable=False, unique=True)
def __init__(self, email):
self.email = email
def __repr__(self):
return '<%s> - %s' % (self.__class__.__name__, self.email)
class Ebuild(db.Model):
"""Represents single ebuilds (cpv) in the system"""
__tablename__ = 'ebuilds'
id = db.Column('id', db.Integer, primary_key=True)
cpv = db.Column('cpv', db.String, nullable=False, unique=True)
eapi = db.Column('eapi', db.Integer, nullable=False)
# Regular USE flags
iuse = db.Column('iuse', db.String)
# Forced USE flags ('-use' and '+use')
iuse_neg = db.Column('iuse_neg', db.String)
iuse_pos = db.Column('iuse_pos', db.String)
keywords = db.Column('keywords', db.String)
slot = db.Column('slot', db.String)
revision = db.Column('revision', db.Integer)
version = db.Column('version', db.String, nullable=False)
package_id = db.Column('package_id', db.Integer, db.ForeignKey('packages.id'))
# TODO: depend, rdepend, licenses
# TODO: extra info?
def __init__(self, ebuild):
"""Initialize ebuild using pkgcore's ebuild class as an argument."""
self.cpv = ebuild.cpvstr
self.version = ebuild.version
self.revision = ebuild.revision
self.slot = ebuild.slot
self.eapi = ebuild.eapi
self.keywords = self._parse_keywords(ebuild)
self.iuse = self._parse_iuse(ebuild)
self.iuse_pos = self._parse_iuse_pos(ebuild)
self.iuse_neg = self._parse_iuse_neg(ebuild)
def sync(self, ebuild_src):
"""Update ebuild values from ebuild_src."""
self.__init__(ebuild_src)
def _parse_iuse(self, ebuild_src):
"""Returns list of USE flags from ebuild_src."""
return ','.join([f for f in ebuild_src.iuse if f[0] not in ('+','-')])
def _parse_iuse_neg(self, ebuild_src):
"""Returns list of forced USE flags (+use) from ebuild_src."""
return ','.join([kw[1:] for kw in ebuild_src.iuse if kw[0] == '-'])
def _parse_iuse_pos(self, ebuild_src):
"""Returns list of forced USE flags (+use) from ebuild_src."""
return ','.join([kw[1:] for kw in ebuild_src.iuse if kw[0] == '+'])
def _parse_keywords(self, ebuild_src):
"""Returns list of keywords from pkgcore's Ebuild."""
return ','.join(ebuild_src.keywords)
@property
def fullver(self):
if self.revision > 0:
return "%s-r%d" % (self.version, self.revision)
return self.version
def __repr__(self):
return '<%s> - %s' % (self.__class__.__name__, self.cpv)
class Herd(db.Model):
"""Represents herds in the system"""
__tablename__ = 'herds'
id = db.Column('id', db.Integer, primary_key=True)
name = db.Column('name', db.String, nullable=False, unique=True)
# TODO: Do we really need to store other information about herds here?
def __init__(self, name):
self.name = name
def __repr__(self):
return '<%s> - %s' % (self.__class__.__name__, self.name)
class Package(db.Model):
"""Represents packages in the system"""
__tablename__ = 'packages'
id = db.Column('id', db.Integer, primary_key=True)
key = db.Column('key', db.String, nullable=False, unique=True)
pkg = db.Column('pkg', db.String, nullable=False)
desc = db.Column('desc', db.String)
ldesc = db.Column('ldesc', db.String)
homepage = db.Column('homepage', db.String)
mtime = db.Column('mtime', db.DateTime)
cat_id = db.Column('cat_id', db.Integer, db.ForeignKey('categories.id'))
qaissues = db.relationship("PkgIssue", backref='qa_issues', \
cascade='all, delete-orphan')
devs = db.relationship(Developer, secondary=package_developers, \
backref=db.backref('packages', \
order_by=["%s.key" % __tablename__]))
herds = db.relationship(Herd, secondary=package_herds, backref='packages')
ebuilds = db.relationship(Ebuild, backref='package', \
cascade='all, delete-orphan', \
collection_class=column_mapped_collection(Ebuild.cpv))
favorites = db.relationship("User", secondary=package_users,
backref='favorites')
def __init__(self, ebuild_src, mtime=time.time()):
self.key = ebuild_src.key
self.pkg = ebuild_src.package
self.desc = ebuild_src.description
self.homepage = ebuild_src.homepage
self.mtime = datetime.fromtimestamp(mtime)
# shared pkg data (from metadata.xml)
self.ldesc = ebuild_src.longdescription
self.devs = self._parse_maintainers(ebuild_src)
self.herds = self._parse_herds(ebuild_src)
def __repr__(self):
return '<%s> - %s' % (self.__class__.__name__, self.key)
@property
def versions(self):
"""Returns sorted list of versions package has in Portage"""
return sorted(list(set(\
[e.version for e in self.ebuilds.values()])), \
compare_version, reverse=True)
def _parse_maintainers(self, ebuild_src):
"""Update package maintainers."""
devs = []
for dev in ebuild_src.maintainers:
if not dev.email:
continue
devs.append(dev.email.strip())
devs = set(devs)
if not devs:
return []
# Sync developers with database
out = []
for dev in Developer.query.filter(Developer.email.in_(devs)).all():
if dev.email in devs:
out.append(dev)
devs.remove(dev.email)
for dev in devs:
out.append(Developer(dev))
return out
def _parse_herds(self, ebuild_src):
"""Update package herds."""
herds = []
for herd in ebuild_src.herds:
if not herd or herd.strip() is None:
herd = 'fix-me'
herds.append(herd.strip())
herds = set(herds)
if not herds:
return []
# Sync herds with database
out = []
for herd in Herd.query.filter(Herd.name.in_(herds)).all():
if herd.name in herds:
out.append(herd)
herds.remove(herd.name)
for herd in herds:
out.append(Herd(herd))
return out
def sync(self, ebuild_src, mtime=time.time()):
"""Update package values from ebuild_src."""
self.__init__(ebuild_src, mtime)
# def rename(self, newcat, newpkg):
# """Rename package and its ebuilds"""
# cp = "%s/%s" % (newpkg, newcat)
# self.cat = newcat
# self.pkg = newpkg
# self.cp = cp
# for ebuild in self.ebuilds:
# ebuild.cpv = "%s-%s" % (cp, ebuild.version)
class Category(db.Model):
"""Represents portage categories"""
__tablename__ = 'categories'
id = db.Column('id', db.Integer, primary_key=True)
name = db.Column('name', db.String, nullable=False, unique=True)
packages = db.relationship(Package, backref='category', \
cascade='all, delete-orphan', \
collection_class=column_mapped_collection(Package.key))
def __init__(self, name):
self.name = name
def __repr__(self):
return '<%s> "%s"' % (self.__class__.__name__, self.name)
class PkgIssue(db.Model):
"""Package-related issues"""
__tablename__ = 'pkg_issues'
id = db.Column('id', db.Integer, primary_key=True)
plugin = db.Column('plugin', db.String)
type = db.Column('type', db.String)
data = db.Column('data', db.String)
created_on = db.Column('created_on', db.DateTime, default=func.now())
_package = db.Column('package_id', db.Integer, db.ForeignKey('packages.id'))
package = db.relationship(Package)
def __init__(self, package, plugin, type, data=None):
self.package = package
self.plugin = plugin
self.data = data
self.type = type
def __repr__(self):
return '<%s> - %s : %s' % \
(self.__class__.__name__, self.plugin, self.type)
class Setting(db.Model):
"""Housekeeping table for storing various system settings and info."""
__tablename__ = 'settings'
name = db.Column('name', db.String, primary_key=True, unique=True)
rawdata = db.Column('data', db.Text)
def __init__(self, name, data):
self.name = name
self.rawdata = json.dumps(data)
@property
def data(self):
return json.loads(self.rawdata)
def __repr__(self):
return '<%s> - %s' % (self.__class__.__name__, self.name)
class User(db.Model):
"""User model"""
__tablename__ = 'users'
id = db.Column('id', db.Integer, primary_key=True)
openid = db.Column('openid', db.String)
email = db.Column('user', db.String, unique=True)
apitoken = db.Column('apitoken', db.String)
regtoken = db.Column('regtoken', db.String)
created_on = db.Column('created_on', db.DateTime, default=func.now())
def __init__(self, email, openid):
self.email = email
self.openid = openid
# API token
self.apitoken = None
# Registration token
self.regtoken = self.generate_token(8)
def generate_token(self, length):
return ''.join([random.choice(string.ascii_letters + \
string.digits) for x in xrange(length)])
|