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

Contents of /src/packages/ebuilddb.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.2 - (show annotations) (download) (as text)
Thu May 27 02:29:48 2004 UTC (10 years, 3 months ago) by marduk
Branch: MAIN
Changes since 1.1: +0 -1 lines
File MIME type: text/x-python
Oh, this is a big update: code cleanups, HTML cleanups, support for hard
masked packages and smart, er, less-brain-dead search reporting.

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

  ViewVC Help
Powered by ViewVC 1.1.20