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

Contents of /src/packages/ebuilddb.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.3 - (hide annotations) (download) (as text)
Sun Oct 17 16:53:21 2004 UTC (9 years, 11 months ago) by marduk
Branch: MAIN
Changes since 1.2: +30 -9 lines
File MIME type: text/x-python
Various fixes & enhancements

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

  ViewVC Help
Powered by ViewVC 1.1.20