/[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.1.1.1 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
10import md5
11
12FINDVER = re.compile('-[0-9]')
13 13
14def db_connect(): 14def db_connect():
15 return MySQLdb.connect(host = config.HOST, 15 return MySQLdb.connect(host = config.HOST,
16 user = config.USER, 16 user = config.USER,
17 passwd = config.PASSWD, 17 passwd = config.PASSWD,
24 pipe = os.popen("find %s -name '*.ebuild'" % config.PORTAGE_DIR) 24 pipe = os.popen("find %s -name '*.ebuild'" % config.PORTAGE_DIR)
25 s = pipe.read() 25 s = pipe.read()
26 pipe.close() 26 pipe.close()
27 return s.split() 27 return s.split()
28 28
29def parse_ebuild(s): 29def parse_ebuild(s, pkgsplit):
30 """Parse ebuild info based on path name""" 30 """Parse ebuild info based on path name"""
31 parsed = {} 31 parsed = {}
32 s=s.split('/') 32 s=s.split('/')
33 parsed['category'] = s[-3] 33 parsed['category'] = s[-3]
34 package = s[-1].split('.ebuild')[0] 34 package = s[-1].split('.ebuild')[0]
35 pos=FINDVER.search(package).start() 35 pieces = pkgsplit(package)
36 if not pieces:
37 return None
36 parsed['name'] = package[:pos] 38 parsed['name'] = pieces[0]
39 if pieces[2] == 'r0':
37 parsed['version'] = package[pos+1:] 40 parsed['version'] = pieces[1]
41 else:
42 parsed['version'] = '-'.join(pieces[1:])
38 43
39 return parsed 44 return parsed
40 45
41def get_ebuild_record(db,ebinfo): 46def get_ebuild_record(db,ebinfo):
42 c = db.cursor() 47 c = db.cursor()
51 c = db.cursor() 56 c = db.cursor()
52 d = db.cursor() 57 d = db.cursor()
53 # update the package table 58 # update the package table
54 59
55 # this is a really ugly dict comprehension 60 # this is a really ugly dict comprehension
56 escaped = dict([(x,MySQLdb.escape_string(y)) for (x,y) in ebinfo.items()]) 61 escaped = {}
57 query="""REPLACE INTO package VALUES ("%(category)s","%(name)s",\ 62 for item in ebinfo.items():
58 "%(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:
59 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)
60 78
61 # then add particular ebuild 79 # then add particular ebuild
62 query = ('INSERT INTO ebuild VALUES ("%(category)s","%(name)s",' 80 query = ('INSERT INTO ebuild VALUES ("%(category)s","%(name)s",'
63 '"%(version)s",%(time)s,"%(archs)s","%(changelog)s","")' 81 '"%(version)s",%(time)s,"%(archs)s","%(changelog)s","",%(masked)d)'
64 % escaped) 82 % escaped)
65 d.execute(query) 83 d.execute(query)
66 84
67def update_ebuild_record(db,ebinfo): 85def update_ebuild_record(db,ebinfo):
68 c = db.cursor() 86 c = db.cursor()
69 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
94 query="""REPLACE INTO package VALUES ("%(category)s","%(name)s",\
95 "%(homepage)s","%(description)s","%(license)s",0);""" % escaped
96 c.execute(query)
97
70 query = ('UPDATE ebuild ' 98 query = ('UPDATE ebuild '
71 'SET when_found="%(time)s",' 99 'SET when_found="%(time)s",'
72 'arch="%(archs)s",' 100 'arch="%(archs)s",'
73 'changelog="%(changelog)s",' 101 'changelog="%(changelog)s",'
74 'prevarch="%(prevarch)s" ' 102 'prevarch="%(prevarch)s", '
103 'is_masked=%(masked)d '
75 'WHERE category="%(category)s" ' 104 'WHERE category="%(category)s" '
76 'AND name="%(name)s" ' 105 'AND name="%(name)s" '
77 'AND version="%(version)s" ' % escaped) 106 'AND version="%(version)s" ' % escaped)
107 try:
78 c.execute(query) 108 c.execute(query)
109 except MySQLdb.MySQLError, data:
110 print 'error occurred: '
111 print 'query: %s' % query
112 print 'error: %s' % data
113
79 114
80def get_extended_info(ebuild): 115def get_extended_info(ebuild):
81 filename = os.path.join(config.PORTAGE_DIR,'metadata/cache', 116 filename = os.path.join(config.PORTAGE_DIR,'metadata/cache',
82 ebuild['category'],'%s-%s' % (ebuild['name'],ebuild['version'])) 117 ebuild['category'],'%s-%s' % (ebuild['name'],ebuild['version']))
83 try: 118 try:
84 lines = open(filename,'r').readlines() 119 lines = open(filename,'r').readlines()
85 except IOError: 120 except IOError:
121 print 'Error opening %s' % filename
86 lines = [] 122 lines = []
87 lines = [ s.strip() for s in lines ] 123 lines = [ s.strip() for s in lines ]
88 try: 124 try:
89 ebuild['archs'] = lines[8] 125 ebuild['archs'] = lines[8]
90 except IndexError: 126 except IndexError:
110 except: 146 except:
111 return 'NULL' 147 return 'NULL'
112 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))
113 return str 149 return str
114 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
115def main(): 155def main():
116 ebuilds = find_ebuilds() 156 ebuilds = find_ebuilds()
117 db = db_connect() 157 db = db_connect()
118 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
119 for s in ebuilds: 169 for s in ebuilds:
120 try: 170 fields = parse_ebuild(s, portage.pkgsplit)
121 fields = parse_ebuild(s) 171 if not fields:
122 except:
123 continue 172 continue
124 result = get_ebuild_record(db,fields) 173 result = get_ebuild_record(db,fields)
125 fields = get_extended_info(fields) 174 fields = get_extended_info(fields)
126 fields['changelog'] = changelogs.changelog('%s/ChangeLog' 175 fields['changelog'] = changelogs.changelog('%s/ChangeLog'
127 % os.path.dirname(s)) 176 % os.path.dirname(s))
128 fields['time'] = get_mtime(s) 177 fields['time'] = get_mtime(s)
178 fields['masked'] = int(is_masked(tree, fields))
179
129 if not result: 180 if not result:
130 create_ebuild_record(db,fields) 181 create_ebuild_record(db,fields)
131 elif result[4] != fields['archs']: 182 elif result[4] != fields['archs']:
132 #print 'ebuild archs=',fields['archs'] 183 #print 'ebuild archs=',fields['archs']
133 #print 'db archs=',result[4] 184 #print 'db archs=',result[4]
134 #print 185 #print
135 # keywords change, update db 186 # keywords change, update db
136 fields['prevarch'] = result[4] 187 fields['prevarch'] = result[4]
137 update_ebuild_record(db,fields) 188 update_ebuild_record(db,fields)
138 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)
139 194
195 db.commit()
140if __name__ == '__main__': 196if __name__ == '__main__':
141 main() 197 main()

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

  ViewVC Help
Powered by ViewVC 1.1.20