/[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.5
1#!/usr/bin/python -O 1#!/usr/bin/python -O
2 2
3__revision__ = '$Revision: 1.4 $' 3__revision__ = '$Revision: 1.5 $'
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'] = lines[8]
126 except IndexError: 124 except IndexError:
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 t = 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 str = time.strftime("%Y%m%d%H%M%S",time.localtime(t))
149 return str 147 return str
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():
156 ebuilds = find_ebuilds() 154 ebuilds = find_ebuilds()
157 db = db_connect() 155 db = db_connect()
158 156
159 sys.path = ["/usr/lib/portage/pym"]+sys.path 157 sys.path = ["/usr/lib/portage/pym"]+sys.path
160 import portage 158 import portage
161 # ... and then wait like a f**king hour 159 # ... and then wait like a f**king hour
162 sys.path = sys.path[1:] 160 sys.path = sys.path[1:]
163 161
164 # We want to use the base profile, not any system-specific one 162 # We want to use the base profile, not any system-specific one
165 config = portage.config(config_profile_path = '/usr/portage/profiles/base') 163 p_config = portage.config(config_profile_path = '/usr/portage/profiles/base')
166 164
167 tree = portage.portdbapi('/usr/portage', config) 165 tree = portage.portdbapi('/usr/portage', p_config)
168 166
169 for s in ebuilds: 167 for s in ebuilds:
170 fields = parse_ebuild(s, portage.pkgsplit) 168 fields = parse_ebuild(s, portage.pkgsplit)
171 if not fields: 169 if not fields:
172 continue 170 continue
173 result = get_ebuild_record(db,fields) 171 result = get_ebuild_record(db,fields)
174 fields = get_extended_info(fields) 172 fields = get_extended_info(fields)
175 fields['changelog'] = changelogs.changelog('%s/ChangeLog' 173 fields['changelog'] = changelogs.changelog('%s/ChangeLog'
176 % os.path.dirname(s)) 174 % os.path.dirname(s))
177 fields['time'] = get_mtime(s) 175 fields['time'] = get_mtime(s)
178 fields['masked'] = int(is_masked(tree, fields)) 176 fields['masked'] = int(is_masked(tree, fields))
179 177
180 if not result: 178 if not result:
181 create_ebuild_record(db,fields) 179 create_ebuild_record(db,fields)
182 elif result[4] != fields['archs']: 180 elif result[4] != fields['archs']:
183 #print 'ebuild archs=',fields['archs'] 181 #print 'ebuild archs=',fields['archs']
184 #print 'db archs=',result[4] 182 #print 'db archs=',result[4]
189 elif result[7] != fields['masked']: 187 elif result[7] != fields['masked']:
190 #print 'mask changed for %s-%s' % (fields['name'], fields['version']) 188 #print 'mask changed for %s-%s' % (fields['name'], fields['version'])
191 #print fields['masked'] 189 #print fields['masked']
192 fields['prevarch'] = result[4] 190 fields['prevarch'] = result[4]
193 update_ebuild_record(db,fields) 191 update_ebuild_record(db,fields)
194 192
195 db.commit() 193 db.commit()
196if __name__ == '__main__': 194if __name__ == '__main__':
197 main() 195 main()

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

  ViewVC Help
Powered by ViewVC 1.1.20