| 1 |
marduk |
1.1 |
#!/usr/bin/python -OO |
| 2 |
|
|
|
| 3 |
|
|
import config |
| 4 |
|
|
import sys |
| 5 |
|
|
import os |
| 6 |
|
|
import time |
| 7 |
|
|
import re |
| 8 |
|
|
import changelogs |
| 9 |
|
|
import MySQLdb |
| 10 |
|
|
import md5 |
| 11 |
|
|
|
| 12 |
|
|
FINDVER = re.compile('-[0-9]') |
| 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 |
|
|
def parse_ebuild(s): |
| 30 |
|
|
"""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 |
|
|
pos=FINDVER.search(package).start() |
| 36 |
|
|
parsed['name'] = package[:pos] |
| 37 |
|
|
parsed['version'] = package[pos+1:] |
| 38 |
|
|
|
| 39 |
|
|
return parsed |
| 40 |
|
|
|
| 41 |
|
|
def get_ebuild_record(db,ebinfo): |
| 42 |
|
|
c = db.cursor() |
| 43 |
|
|
query = ('SELECT * FROM ebuild WHERE category="%s" AND name="%s" ' |
| 44 |
|
|
'AND version="%s" LIMIT 1' % (ebinfo['category'], ebinfo['name'], |
| 45 |
|
|
ebinfo['version'])) |
| 46 |
|
|
c.execute(query) |
| 47 |
|
|
result = c.fetchone() |
| 48 |
|
|
return result |
| 49 |
|
|
|
| 50 |
|
|
def create_ebuild_record(db,ebinfo): |
| 51 |
|
|
c = db.cursor() |
| 52 |
|
|
d = db.cursor() |
| 53 |
|
|
# update the package table |
| 54 |
|
|
|
| 55 |
|
|
# this is a really ugly dict comprehension |
| 56 |
|
|
escaped = dict([(x,MySQLdb.escape_string(y)) for (x,y) in ebinfo.items()]) |
| 57 |
|
|
query="""REPLACE INTO package VALUES ("%(category)s","%(name)s",\ |
| 58 |
|
|
"%(homepage)s","%(description)s","%(license)s");""" % escaped |
| 59 |
|
|
c.execute(query) |
| 60 |
|
|
|
| 61 |
|
|
# then add particular ebuild |
| 62 |
|
|
query = ('INSERT INTO ebuild VALUES ("%(category)s","%(name)s",' |
| 63 |
|
|
'"%(version)s",%(time)s,"%(archs)s","%(changelog)s","")' |
| 64 |
|
|
% escaped) |
| 65 |
|
|
d.execute(query) |
| 66 |
|
|
|
| 67 |
|
|
def update_ebuild_record(db,ebinfo): |
| 68 |
|
|
c = db.cursor() |
| 69 |
|
|
escaped = dict([(x,MySQLdb.escape_string(y)) for (x,y) in ebinfo.items()]) |
| 70 |
|
|
query = ('UPDATE ebuild ' |
| 71 |
|
|
'SET when_found="%(time)s",' |
| 72 |
|
|
'arch="%(archs)s",' |
| 73 |
|
|
'changelog="%(changelog)s",' |
| 74 |
|
|
'prevarch="%(prevarch)s" ' |
| 75 |
|
|
'WHERE category="%(category)s" ' |
| 76 |
|
|
'AND name="%(name)s" ' |
| 77 |
|
|
'AND version="%(version)s" ' % escaped) |
| 78 |
|
|
c.execute(query) |
| 79 |
|
|
|
| 80 |
|
|
def get_extended_info(ebuild): |
| 81 |
|
|
filename = os.path.join(config.PORTAGE_DIR,'metadata/cache', |
| 82 |
|
|
ebuild['category'],'%s-%s' % (ebuild['name'],ebuild['version'])) |
| 83 |
|
|
try: |
| 84 |
|
|
lines = open(filename,'r').readlines() |
| 85 |
|
|
except IOError: |
| 86 |
|
|
lines = [] |
| 87 |
|
|
lines = [ s.strip() for s in lines ] |
| 88 |
|
|
try: |
| 89 |
|
|
ebuild['archs'] = lines[8] |
| 90 |
|
|
except IndexError: |
| 91 |
|
|
ebuild['archs'] = '' |
| 92 |
|
|
try: |
| 93 |
|
|
ebuild['homepage'] = lines[5] |
| 94 |
|
|
except IndexError: |
| 95 |
|
|
ebuild['homepage'] = 'http://www.gentoo.org/' |
| 96 |
|
|
try: |
| 97 |
|
|
ebuild['license'] = lines[6] |
| 98 |
|
|
except IndexError: |
| 99 |
|
|
ebuild['license'] = '' |
| 100 |
|
|
try: |
| 101 |
|
|
ebuild['description'] = lines[7] |
| 102 |
|
|
except IndexError: |
| 103 |
|
|
ebuild['description'] = '' |
| 104 |
|
|
return ebuild |
| 105 |
|
|
|
| 106 |
|
|
def get_mtime(s): |
| 107 |
|
|
"""Get mtime of file, return in format that MySQL would like""" |
| 108 |
|
|
try: |
| 109 |
|
|
t = os.path.getmtime(s) |
| 110 |
|
|
except: |
| 111 |
|
|
return 'NULL' |
| 112 |
|
|
str = time.strftime("%Y%m%d%H%M%S",time.localtime(t)) |
| 113 |
|
|
return str |
| 114 |
|
|
|
| 115 |
|
|
def main(): |
| 116 |
|
|
ebuilds = find_ebuilds() |
| 117 |
|
|
db = db_connect() |
| 118 |
|
|
|
| 119 |
|
|
for s in ebuilds: |
| 120 |
|
|
try: |
| 121 |
|
|
fields = parse_ebuild(s) |
| 122 |
|
|
except: |
| 123 |
|
|
continue |
| 124 |
|
|
result = get_ebuild_record(db,fields) |
| 125 |
|
|
fields = get_extended_info(fields) |
| 126 |
|
|
fields['changelog'] = changelogs.changelog('%s/ChangeLog' |
| 127 |
|
|
% os.path.dirname(s)) |
| 128 |
|
|
fields['time'] = get_mtime(s) |
| 129 |
|
|
if not result: |
| 130 |
|
|
create_ebuild_record(db,fields) |
| 131 |
|
|
elif result[4] != fields['archs']: |
| 132 |
|
|
#print 'ebuild archs=',fields['archs'] |
| 133 |
|
|
#print 'db archs=',result[4] |
| 134 |
|
|
#print |
| 135 |
|
|
# keywords change, update db |
| 136 |
|
|
fields['prevarch'] = result[4] |
| 137 |
|
|
update_ebuild_record(db,fields) |
| 138 |
|
|
|
| 139 |
|
|
|
| 140 |
|
|
if __name__ == '__main__': |
| 141 |
|
|
main() |