/[gentoo]/src/packages/ebuilddb.py
Gentoo

Contents of /src/packages/ebuilddb.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.4 - (hide annotations) (download) (as text)
Mon May 9 00:47:08 2005 UTC (9 years, 4 months ago) by marduk
Branch: MAIN
Branch point for: pre_2-0
Changes since 1.3: +58 -22 lines
File MIME type: text/x-python
Copy various functions from portage to get around importing portage (speedup).

Masked property now a database column.

Manually set profile to override host system's profile.

1 marduk 1.4 #!/usr/bin/python -O
2    
3     __revision__ = '$Revision: $'
4     # $Source: $
5 marduk 1.1
6     import config
7     import sys
8     import os
9     import time
10     import re
11     import changelogs
12     import MySQLdb
13    
14     def db_connect():
15     return MySQLdb.connect(host = config.HOST,
16     user = config.USER,
17     passwd = config.PASSWD,
18     db = config.DATABASE)
19    
20     def find_ebuilds():
21     #print "walking..."
22     ebuilds=[]
23     # yeah, i know we can os.path.walk, but this runs faster ;-)
24     pipe = os.popen("find %s -name '*.ebuild'" % config.PORTAGE_DIR)
25     s = pipe.read()
26     pipe.close()
27     return s.split()
28    
29 marduk 1.4 def parse_ebuild(s, pkgsplit):
30 marduk 1.1 """Parse ebuild info based on path name"""
31     parsed = {}
32     s=s.split('/')
33     parsed['category'] = s[-3]
34     package = s[-1].split('.ebuild')[0]
35 marduk 1.4 pieces = pkgsplit(package)
36 marduk 1.3 if not pieces:
37     return None
38     parsed['name'] = pieces[0]
39     if pieces[2] == 'r0':
40     parsed['version'] = pieces[1]
41     else:
42     parsed['version'] = '-'.join(pieces[1:])
43 marduk 1.1
44     return parsed
45    
46     def get_ebuild_record(db,ebinfo):
47     c = db.cursor()
48     query = ('SELECT * FROM ebuild WHERE category="%s" AND name="%s" '
49     'AND version="%s" LIMIT 1' % (ebinfo['category'], ebinfo['name'],
50     ebinfo['version']))
51     c.execute(query)
52     result = c.fetchone()
53     return result
54    
55     def create_ebuild_record(db,ebinfo):
56     c = db.cursor()
57     d = db.cursor()
58     # update the package table
59    
60     # this is a really ugly dict comprehension
61 marduk 1.4 escaped = {}
62     for item in ebinfo.items():
63     x, y = item
64     if type(y) is str:
65     y = MySQLdb.escape_string(y)
66     escaped[x] = y
67    
68     query=('INSERT INTO package SET category="%(category)s",name="%(name)s",'
69     'homepage="%(homepage)s",description="%(description)s",'
70     'license="%(license)s"' % escaped)
71     try:
72     c.execute(query)
73     except MySQLdb.cursors.IntegrityError:
74     # duplicate key (we hope)
75     query = ('REPLACE INTO package VALUES ("%(category)s","%(name)s",'
76     '"%(homepage)s","%(description)s","%(license)s",0)' % escaped)
77     c.execute(query)
78 marduk 1.1
79     # then add particular ebuild
80     query = ('INSERT INTO ebuild VALUES ("%(category)s","%(name)s",'
81 marduk 1.4 '"%(version)s",%(time)s,"%(archs)s","%(changelog)s","",%(masked)d)'
82 marduk 1.1 % escaped)
83     d.execute(query)
84    
85     def update_ebuild_record(db,ebinfo):
86     c = db.cursor()
87 marduk 1.4 escaped = {}
88     for item in ebinfo.items():
89     x, y = item
90     if type(y) is str:
91     y = MySQLdb.escape_string(y)
92     escaped[x] = y
93    
94 marduk 1.3 query="""REPLACE INTO package VALUES ("%(category)s","%(name)s",\
95 marduk 1.4 "%(homepage)s","%(description)s","%(license)s",0);""" % escaped
96 marduk 1.3 c.execute(query)
97    
98 marduk 1.1 query = ('UPDATE ebuild '
99     'SET when_found="%(time)s",'
100     'arch="%(archs)s",'
101     'changelog="%(changelog)s",'
102 marduk 1.4 'prevarch="%(prevarch)s", '
103     'is_masked=%(masked)d '
104 marduk 1.1 'WHERE category="%(category)s" '
105     'AND name="%(name)s" '
106     'AND version="%(version)s" ' % escaped)
107 marduk 1.3 try:
108     c.execute(query)
109     except MySQLdb.MySQLError, data:
110     print 'error occurred: '
111     print 'query: %s' % query
112     print 'error: %s' % data
113    
114 marduk 1.1
115     def get_extended_info(ebuild):
116     filename = os.path.join(config.PORTAGE_DIR,'metadata/cache',
117     ebuild['category'],'%s-%s' % (ebuild['name'],ebuild['version']))
118     try:
119     lines = open(filename,'r').readlines()
120     except IOError:
121 marduk 1.4 print 'Error opening %s' % filename
122 marduk 1.1 lines = []
123     lines = [ s.strip() for s in lines ]
124     try:
125     ebuild['archs'] = lines[8]
126     except IndexError:
127     ebuild['archs'] = ''
128     try:
129     ebuild['homepage'] = lines[5]
130     except IndexError:
131     ebuild['homepage'] = 'http://www.gentoo.org/'
132     try:
133     ebuild['license'] = lines[6]
134     except IndexError:
135     ebuild['license'] = ''
136     try:
137     ebuild['description'] = lines[7]
138     except IndexError:
139     ebuild['description'] = ''
140     return ebuild
141    
142     def get_mtime(s):
143     """Get mtime of file, return in format that MySQL would like"""
144     try:
145     t = os.path.getmtime(s)
146     except:
147     return 'NULL'
148     str = time.strftime("%Y%m%d%H%M%S",time.localtime(t))
149     return str
150    
151 marduk 1.4 def is_masked(tree, ebuild):
152     """Return true if packages is masked in tree"""
153     return (not tree.visible(['%(category)s/%(name)s-%(version)s' % ebuild]))
154    
155 marduk 1.1 def main():
156     ebuilds = find_ebuilds()
157     db = db_connect()
158    
159 marduk 1.4 sys.path = ["/usr/lib/portage/pym"]+sys.path
160     import portage
161     # ... and then wait like a f**king hour
162     sys.path = sys.path[1:]
163    
164     # We want to use the base profile, not any system-specific one
165     config = portage.config(config_profile_path = '/usr/portage/profiles/base')
166    
167     tree = portage.portdbapi('/usr/portage', config)
168    
169 marduk 1.1 for s in ebuilds:
170 marduk 1.4 fields = parse_ebuild(s, portage.pkgsplit)
171 marduk 1.3 if not fields:
172 marduk 1.1 continue
173     result = get_ebuild_record(db,fields)
174     fields = get_extended_info(fields)
175     fields['changelog'] = changelogs.changelog('%s/ChangeLog'
176     % os.path.dirname(s))
177     fields['time'] = get_mtime(s)
178 marduk 1.4 fields['masked'] = int(is_masked(tree, fields))
179    
180 marduk 1.1 if not result:
181     create_ebuild_record(db,fields)
182     elif result[4] != fields['archs']:
183     #print 'ebuild archs=',fields['archs']
184     #print 'db archs=',result[4]
185     #print
186     # keywords change, update db
187     fields['prevarch'] = result[4]
188     update_ebuild_record(db,fields)
189 marduk 1.4 elif result[7] != fields['masked']:
190     #print 'mask changed for %s-%s' % (fields['name'], fields['version'])
191     #print fields['masked']
192     fields['prevarch'] = result[4]
193     update_ebuild_record(db,fields)
194 marduk 1.1
195 marduk 1.4 db.commit()
196 marduk 1.1 if __name__ == '__main__':
197     main()

  ViewVC Help
Powered by ViewVC 1.1.20