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

Diff of /src/packages/ebuilddb.py

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 1.4 Revision 1.8.2.1
1#!/usr/bin/python -O 1#!/usr/bin/python -O
2 2
3__revision__ = '$Revision: 1.4 $' 3__revision__ = '$Revision: 1.8.2.1 $'
4# $Source: /var/cvsroot/gentoo/src/packages/ebuilddb.py,v $ 4# $Source: /var/cvsroot/gentoo/src/packages/ebuilddb.py,v $
5 5
6import config 6import config
7import sys 7import sys
8import os 8import os
9import time 9import time
10import re
11import changelogs 10import changelogs
12import MySQLdb 11import MySQLdb
13 12
14def db_connect(): 13def db_connect():
15 return MySQLdb.connect(host = config.HOST, 14 return MySQLdb.connect(host = config.HOST,
16 user = config.USER, 15 user = config.USER,
17 passwd = config.PASSWD, 16 passwd = config.PASSWD,
18 db = config.DATABASE) 17 db = config.DATABASE)
19 18
20def find_ebuilds(): 19def find_ebuilds():
21 #print "walking..." 20 #print "walking..."
22 ebuilds=[]
23 # yeah, i know we can os.path.walk, but this runs faster ;-) 21 # yeah, i know we can os.path.walk, but this runs faster ;-)
24 pipe = os.popen("find %s -name '*.ebuild'" % config.PORTAGE_DIR) 22 pipe = os.popen("find %s -name '*.ebuild'" % config.PORTAGE_DIR)
25 s = pipe.read() 23 s = pipe.read()
26 pipe.close() 24 pipe.close()
27 return s.split() 25 return s.split()
28 26
29def parse_ebuild(s, pkgsplit): 27def parse_ebuild(s, pkgsplit):
30 """Parse ebuild info based on path name""" 28 """Parse ebuild info based on path name"""
31 parsed = {} 29 parsed = {}
32 s=s.split('/') 30 s=s.split('/')
33 parsed['category'] = s[-3] 31 parsed['category'] = s[-3]
43 41
44 return parsed 42 return parsed
45 43
46def get_ebuild_record(db,ebinfo): 44def get_ebuild_record(db,ebinfo):
47 c = db.cursor() 45 c = db.cursor()
48 query = ('SELECT * FROM ebuild WHERE category="%s" AND name="%s" ' 46 query = ('SELECT * FROM ebuild WHERE category="%s" AND name="%s" '
49 'AND version="%s" LIMIT 1' % (ebinfo['category'], ebinfo['name'], 47 'AND version="%s" LIMIT 1' % (ebinfo['category'], ebinfo['name'],
50 ebinfo['version'])) 48 ebinfo['version']))
51 c.execute(query) 49 c.execute(query)
52 result = c.fetchone() 50 result = c.fetchone()
53 return result 51 return result
62 for item in ebinfo.items(): 60 for item in ebinfo.items():
63 x, y = item 61 x, y = item
64 if type(y) is str: 62 if type(y) is str:
65 y = MySQLdb.escape_string(y) 63 y = MySQLdb.escape_string(y)
66 escaped[x] = y 64 escaped[x] = y
67 65
68 query=('INSERT INTO package SET category="%(category)s",name="%(name)s",' 66 query=('INSERT INTO package SET category="%(category)s",name="%(name)s",'
69 'homepage="%(homepage)s",description="%(description)s",' 67 'homepage="%(homepage)s",description="%(description)s",'
70 'license="%(license)s"' % escaped) 68 'license="%(license)s"' % escaped)
71 try: 69 try:
72 c.execute(query) 70 c.execute(query)
76 '"%(homepage)s","%(description)s","%(license)s",0)' % escaped) 74 '"%(homepage)s","%(description)s","%(license)s",0)' % escaped)
77 c.execute(query) 75 c.execute(query)
78 76
79 # then add particular ebuild 77 # then add particular ebuild
80 query = ('INSERT INTO ebuild VALUES ("%(category)s","%(name)s",' 78 query = ('INSERT INTO ebuild VALUES ("%(category)s","%(name)s",'
81 '"%(version)s",%(time)s,"%(archs)s","%(changelog)s","",%(masked)d)' 79 '"%(version)s",%(time)s,"%(archs)s","%(changelog)s","",%(masked)d)'
82 % escaped) 80 % escaped)
83 d.execute(query) 81 d.execute(query)
84 82
85def update_ebuild_record(db,ebinfo): 83def update_ebuild_record(db,ebinfo):
86 c = db.cursor() 84 c = db.cursor()
87 escaped = {} 85 escaped = {}
88 for item in ebinfo.items(): 86 for item in ebinfo.items():
89 x, y = item 87 x, y = item
90 if type(y) is str: 88 if type(y) is str:
91 y = MySQLdb.escape_string(y) 89 y = MySQLdb.escape_string(y)
92 escaped[x] = y 90 escaped[x] = y
93 91
94 query="""REPLACE INTO package VALUES ("%(category)s","%(name)s",\ 92 query="""REPLACE INTO package VALUES ("%(category)s","%(name)s",\
95 "%(homepage)s","%(description)s","%(license)s",0);""" % escaped 93 "%(homepage)s","%(description)s","%(license)s",0);""" % escaped
96 c.execute(query) 94 c.execute(query)
97 95
98 query = ('UPDATE ebuild ' 96 query = ('UPDATE ebuild '
99 'SET when_found="%(time)s",' 97 'SET when_found="%(time)s",'
100 'arch="%(archs)s",' 98 'arch="%(archs)s",'
101 'changelog="%(changelog)s",' 99 'changelog="%(changelog)s",'
102 'prevarch="%(prevarch)s", ' 100 'prevarch="%(prevarch)s", '
108 c.execute(query) 106 c.execute(query)
109 except MySQLdb.MySQLError, data: 107 except MySQLdb.MySQLError, data:
110 print 'error occurred: ' 108 print 'error occurred: '
111 print 'query: %s' % query 109 print 'query: %s' % query
112 print 'error: %s' % data 110 print 'error: %s' % data
113 111
114 112
115def get_extended_info(ebuild): 113def get_extended_info(ebuild):
116 filename = os.path.join(config.PORTAGE_DIR,'metadata/cache', 114 filename = os.path.join(config.PORTAGE_DIR,'metadata/cache',
117 ebuild['category'],'%s-%s' % (ebuild['name'],ebuild['version'])) 115 ebuild['category'],'%s-%s' % (ebuild['name'],ebuild['version']))
118 try: 116 try:
119 lines = open(filename,'r').readlines() 117 lines = open(filename,'r').readlines()
120 except IOError: 118 except IOError:
121 print 'Error opening %s' % filename 119 print 'Error opening %s' % filename
122 lines = [] 120 lines = []
123 lines = [ s.strip() for s in lines ] 121 lines = [ s.strip() for s in lines ]
124 try: 122 try:
125 ebuild['archs'] = lines[8] 123 ebuild['archs'] = ','.join(lines[8].split())
126 except IndexError: 124 except IndexError:
127 ebuild['archs'] = '' 125 ebuild['archs'] = ''
128 try: 126 try:
129 ebuild['homepage'] = lines[5] 127 ebuild['homepage'] = lines[5]
130 except IndexError: 128 except IndexError:
140 return ebuild 138 return ebuild
141 139
142def get_mtime(s): 140def get_mtime(s):
143 """Get mtime of file, return in format that MySQL would like""" 141 """Get mtime of file, return in format that MySQL would like"""
144 try: 142 try:
145 t = os.path.getmtime(s) 143 mtime = os.path.getmtime(s)
146 except: 144 except OSError:
147 return 'NULL' 145 return 'NULL'
148 str = time.strftime("%Y%m%d%H%M%S",time.localtime(t)) 146 strftime = time.strftime("%Y%m%d%H%M%S",time.localtime(mtime))
149 return str 147 return strftime
150 148
151def is_masked(tree, ebuild): 149def is_masked(tree, ebuild):
152 """Return true if packages is masked in tree""" 150 """Return true if packages is masked in tree"""
153 return (not tree.visible(['%(category)s/%(name)s-%(version)s' % ebuild])) 151 return (not tree.visible(['%(category)s/%(name)s-%(version)s' % ebuild]))
154 152
155def main(): 153def main(argv = []):
154 """Main program entry point"""
155 # We need to "fake" as repoman so portage will ignore local system
156 # settings
157 os.environ['PORTAGE_CALLER'] = 'repoman'
158
156 ebuilds = find_ebuilds() 159 ebuilds = find_ebuilds()
157 db = db_connect() 160 db = db_connect()
158 161
159 sys.path = ["/usr/lib/portage/pym"]+sys.path 162 sys.path = ["/usr/lib/portage/pym"]+sys.path
160 import portage 163 import portage
161 # ... and then wait like a f**king hour 164 # ... and then wait like a f**king hour
162 sys.path = sys.path[1:] 165 sys.path = sys.path[1:]
163 166
164 # We want to use the base profile, not any system-specific one 167 # We want to use the base profile, not any system-specific one
165 config = portage.config(config_profile_path = '/usr/portage/profiles/base') 168 p_config = portage.config(config_profile_path='/usr/portage/profiles/base',
166 169 local_config=False)
170
167 tree = portage.portdbapi('/usr/portage', config) 171 tree = portage.portdbapi('/usr/portage', p_config)
168 172
173 # are we updating the db or just rebuilding it?
174 rebuild = len(argv) == 2 and argv[1] == 'rebuild'
169 for s in ebuilds: 175 for s in ebuilds:
170 fields = parse_ebuild(s, portage.pkgsplit) 176 fields = parse_ebuild(s, portage.pkgsplit)
171 if not fields: 177 if not fields:
172 continue 178 continue
173 result = get_ebuild_record(db,fields) 179 result = get_ebuild_record(db,fields)
174 fields = get_extended_info(fields) 180 fields = get_extended_info(fields)
175 fields['changelog'] = changelogs.changelog('%s/ChangeLog' 181 fields['changelog'] = changelogs.changelog('%s/ChangeLog'
176 % os.path.dirname(s)) 182 % os.path.dirname(s))
177 fields['time'] = get_mtime(s) 183 fields['time'] = get_mtime(s)
178 fields['masked'] = int(is_masked(tree, fields)) 184 fields['masked'] = int(is_masked(tree, fields))
179 185
180 if not result: 186 if not (result or rebuild):
181 create_ebuild_record(db,fields) 187 create_ebuild_record(db,fields)
188 elif result and rebuild:
189 fields['prevarch'] = result[6]
190 if ' ' in fields['prevarch']:
191 # old db layout
192 fields['prevarch'] = ','.join(fields['prevarch'].split())
193 fields['arch'] = [4]
194 update_ebuild_record(db, fields)
195 elif rebuild:
196 pass
182 elif result[4] != fields['archs']: 197 elif result[4].split(',') != fields['archs'].split(','):
183 #print 'ebuild archs=',fields['archs'] 198 #print 'ebuild archs=',fields['archs']
184 #print 'db archs=',result[4] 199 #print 'db archs=',result[4]
185 #print 200 #print
186 # keywords change, update db 201 # keywords change, update db
187 fields['prevarch'] = result[4] 202 fields['prevarch'] = result[4]
189 elif result[7] != fields['masked']: 204 elif result[7] != fields['masked']:
190 #print 'mask changed for %s-%s' % (fields['name'], fields['version']) 205 #print 'mask changed for %s-%s' % (fields['name'], fields['version'])
191 #print fields['masked'] 206 #print fields['masked']
192 fields['prevarch'] = result[4] 207 fields['prevarch'] = result[4]
193 update_ebuild_record(db,fields) 208 update_ebuild_record(db,fields)
194 209
195 db.commit() 210 db.commit()
196if __name__ == '__main__': 211if __name__ == '__main__':
197 main() 212 sys.exit(main(sys.argv))

Legend:
Removed from v.1.4  
changed lines
  Added in v.1.8.2.1

  ViewVC Help
Powered by ViewVC 1.1.20