/[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.3 Revision 1.4
1#!/usr/bin/python -OO 1#!/usr/bin/python -O
2
3__revision__ = '$Revision: 1.4 $'
4# $Source: /var/cvsroot/gentoo/src/packages/ebuilddb.py,v $
2 5
3import config 6import config
4import sys 7import sys
5import os 8import os
6import time 9import time
7import re 10import re
8import changelogs 11import changelogs
9import MySQLdb 12import MySQLdb
10
11# import portage, but temporarily redirect stderr
12if 'portage' not in dir():
13 null = open('/dev/null', 'w')
14 tmp = sys.stderr
15 sys.stderr = null
16 sys.path = ["/usr/lib/portage/pym"]+sys.path
17 import portage
18 sys.stderr = tmp
19 sys.path = sys.path[1:]
20 13
21def db_connect(): 14def db_connect():
22 return MySQLdb.connect(host = config.HOST, 15 return MySQLdb.connect(host = config.HOST,
23 user = config.USER, 16 user = config.USER,
24 passwd = config.PASSWD, 17 passwd = config.PASSWD,
31 pipe = os.popen("find %s -name '*.ebuild'" % config.PORTAGE_DIR) 24 pipe = os.popen("find %s -name '*.ebuild'" % config.PORTAGE_DIR)
32 s = pipe.read() 25 s = pipe.read()
33 pipe.close() 26 pipe.close()
34 return s.split() 27 return s.split()
35 28
36def parse_ebuild(s): 29def parse_ebuild(s, pkgsplit):
37 """Parse ebuild info based on path name""" 30 """Parse ebuild info based on path name"""
38 parsed = {} 31 parsed = {}
39 s=s.split('/') 32 s=s.split('/')
40 parsed['category'] = s[-3] 33 parsed['category'] = s[-3]
41 package = s[-1].split('.ebuild')[0] 34 package = s[-1].split('.ebuild')[0]
42 pieces = portage.pkgsplit(package) 35 pieces = pkgsplit(package)
43 if not pieces: 36 if not pieces:
44 return None 37 return None
45 parsed['name'] = pieces[0] 38 parsed['name'] = pieces[0]
46 if pieces[2] == 'r0': 39 if pieces[2] == 'r0':
47 parsed['version'] = pieces[1] 40 parsed['version'] = pieces[1]
63 c = db.cursor() 56 c = db.cursor()
64 d = db.cursor() 57 d = db.cursor()
65 # update the package table 58 # update the package table
66 59
67 # this is a really ugly dict comprehension 60 # this is a really ugly dict comprehension
68 escaped = dict([(x,MySQLdb.escape_string(y)) for (x,y) in ebinfo.items()]) 61 escaped = {}
69 query="""REPLACE INTO package VALUES ("%(category)s","%(name)s",\ 62 for item in ebinfo.items():
70 "%(homepage)s","%(description)s","%(license)s");""" % escaped 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:
71 c.execute(query) 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)
72 78
73 # then add particular ebuild 79 # then add particular ebuild
74 query = ('INSERT INTO ebuild VALUES ("%(category)s","%(name)s",' 80 query = ('INSERT INTO ebuild VALUES ("%(category)s","%(name)s",'
75 '"%(version)s",%(time)s,"%(archs)s","%(changelog)s","")' 81 '"%(version)s",%(time)s,"%(archs)s","%(changelog)s","",%(masked)d)'
76 % escaped) 82 % escaped)
77 d.execute(query) 83 d.execute(query)
78 84
79def update_ebuild_record(db,ebinfo): 85def update_ebuild_record(db,ebinfo):
80 c = db.cursor() 86 c = db.cursor()
81 escaped = dict([(x,MySQLdb.escape_string(y)) for (x,y) in ebinfo.items()]) 87 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
82 query="""REPLACE INTO package VALUES ("%(category)s","%(name)s",\ 94 query="""REPLACE INTO package VALUES ("%(category)s","%(name)s",\
83 "%(homepage)s","%(description)s","%(license)s");""" % escaped 95 "%(homepage)s","%(description)s","%(license)s",0);""" % escaped
84 c.execute(query) 96 c.execute(query)
85 97
86 query = ('UPDATE ebuild ' 98 query = ('UPDATE ebuild '
87 'SET when_found="%(time)s",' 99 'SET when_found="%(time)s",'
88 'arch="%(archs)s",' 100 'arch="%(archs)s",'
89 'changelog="%(changelog)s",' 101 'changelog="%(changelog)s",'
90 'prevarch="%(prevarch)s" ' 102 'prevarch="%(prevarch)s", '
103 'is_masked=%(masked)d '
91 'WHERE category="%(category)s" ' 104 'WHERE category="%(category)s" '
92 'AND name="%(name)s" ' 105 'AND name="%(name)s" '
93 'AND version="%(version)s" ' % escaped) 106 'AND version="%(version)s" ' % escaped)
94 try: 107 try:
95 c.execute(query) 108 c.execute(query)
103 filename = os.path.join(config.PORTAGE_DIR,'metadata/cache', 116 filename = os.path.join(config.PORTAGE_DIR,'metadata/cache',
104 ebuild['category'],'%s-%s' % (ebuild['name'],ebuild['version'])) 117 ebuild['category'],'%s-%s' % (ebuild['name'],ebuild['version']))
105 try: 118 try:
106 lines = open(filename,'r').readlines() 119 lines = open(filename,'r').readlines()
107 except IOError: 120 except IOError:
121 print 'Error opening %s' % filename
108 lines = [] 122 lines = []
109 lines = [ s.strip() for s in lines ] 123 lines = [ s.strip() for s in lines ]
110 try: 124 try:
111 ebuild['archs'] = lines[8] 125 ebuild['archs'] = lines[8]
112 except IndexError: 126 except IndexError:
132 except: 146 except:
133 return 'NULL' 147 return 'NULL'
134 str = time.strftime("%Y%m%d%H%M%S",time.localtime(t)) 148 str = time.strftime("%Y%m%d%H%M%S",time.localtime(t))
135 return str 149 return str
136 150
151def 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
137def main(): 155def main():
138 ebuilds = find_ebuilds() 156 ebuilds = find_ebuilds()
139 db = db_connect() 157 db = db_connect()
140 158
159 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
141 for s in ebuilds: 169 for s in ebuilds:
142 fields = parse_ebuild(s) 170 fields = parse_ebuild(s, portage.pkgsplit)
143 if not fields: 171 if not fields:
144 continue 172 continue
145 result = get_ebuild_record(db,fields) 173 result = get_ebuild_record(db,fields)
146 fields = get_extended_info(fields) 174 fields = get_extended_info(fields)
147 fields['changelog'] = changelogs.changelog('%s/ChangeLog' 175 fields['changelog'] = changelogs.changelog('%s/ChangeLog'
148 % os.path.dirname(s)) 176 % os.path.dirname(s))
149 fields['time'] = get_mtime(s) 177 fields['time'] = get_mtime(s)
178 fields['masked'] = int(is_masked(tree, fields))
179
150 if not result: 180 if not result:
151 create_ebuild_record(db,fields) 181 create_ebuild_record(db,fields)
152 elif result[4] != fields['archs']: 182 elif result[4] != fields['archs']:
153 #print 'ebuild archs=',fields['archs'] 183 #print 'ebuild archs=',fields['archs']
154 #print 'db archs=',result[4] 184 #print 'db archs=',result[4]
155 #print 185 #print
156 # keywords change, update db 186 # keywords change, update db
157 fields['prevarch'] = result[4] 187 fields['prevarch'] = result[4]
158 update_ebuild_record(db,fields) 188 update_ebuild_record(db,fields)
189 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)
159 194
195 db.commit()
160if __name__ == '__main__': 196if __name__ == '__main__':
161 main() 197 main()

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

  ViewVC Help
Powered by ViewVC 1.1.20