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

Contents of /src/packages/search.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.4 - (show annotations) (download) (as text)
Mon May 9 01:05:41 2005 UTC (9 years, 6 months ago) by marduk
Branch: MAIN
CVS Tags: release_1-5, v1_3_1, HEAD
Branch point for: pre_2-0, v1_3
Changes since 1.3: +7 -9 lines
File MIME type: text/x-python
Small speedup.

1 #!/usr/bin/python -OO
2
3 import sys
4 import config
5 import cgi
6 import gentoo
7 import os
8 import re
9 from MySQLdb import escape_string
10
11 form = cgi.FieldStorage()
12 type = form.getvalue("type","")
13 sstring = form.getvalue("sstring","")
14 offset = form.getvalue("offset","0")
15
16 if not sstring:
17 sys.exit(0)
18
19 if type == 'desc':
20 col = "description"
21 else:
22 col = "name"
23
24 def query_to_dict(q):
25 pkginfo = {}
26 keys = ('category','name','homepage','description','license')
27 for i in range(len(keys)):
28 try:
29 pkginfo[keys[i]] = q[i]
30 except IndexError:
31 continue
32 return pkginfo
33
34 def write_to_cache(s):
35 try:
36 open(cachefile,'w').write(s)
37 except IOError:
38 pass
39
40 def sort_by_weight(a, b):
41 """Right now we just sort based on whether the sstring is in the name"""
42 a_name = a['name']
43 b_name = b['name']
44
45 a_match = re.search(sstring, a_name, re.IGNORECASE)
46 b_match = re.search(sstring, b_name, re.IGNORECASE)
47
48 if a_match and b_match:
49 return 0
50 if a_match:
51 return -1
52 return 1
53
54 # if it's in the cache, just write that out
55 qs = sys.argv[1]
56 cachefile = os.path.join(config.LOCALHOME,'search/cache',qs)
57 if os.path.exists(cachefile):
58 #sys.stdout.write('<div class="centerpage">using cached results</div>\n')
59 sys.stdout.write(open(cachefile,'r').read())
60 sys.exit(0)
61
62 escaped = escape_string(sstring)
63 query = ('SELECT category,name,homepage,description,license '
64 'FROM package WHERE name REGEXP "%s" or description REGEXP "%s" '
65 'ORDER BY category LIMIT %s,%s'
66 % (escaped,escaped,offset,config.MAXPERPAGE))
67
68 import ebuilddb
69 db = ebuilddb.db_connect()
70 c = db.cursor()
71 try:
72 c.execute(query)
73 results = c.fetchall()
74 except:
75 results = None
76
77 if not results:
78 s = ( '<div class="centerpage">\n'
79 '<table width="100%%" border="0" align="center"'
80 ' cellspacing="0"><tr><td colspan="3" class="fields">'
81 'Nothing found.<br></td></tr>\n'
82 '<tr><td class="item" colspan="3">'
83 '<p>I could not find any ebuild that match'
84 ' your query. Try a different query or check the'
85 ' <a HREF="%s">'
86 'packages.gentoo.org main page</a>.</p></td></tr></table>\n'
87 '</div>\n' % config.FEHOME)
88 sys.stdout.write(s)
89 write_to_cache(s)
90 sys.exit(0)
91
92 pkgs = [ query_to_dict(i) for i in results ]
93 pkgs.sort(sort_by_weight)
94
95 s = '\n'.join([gentoo.package_to_html(pkg,db) for pkg in pkgs])
96 #for pkg in pkgs:
97 # #print pkg
98 # html = gentoo.package_to_html(pkg,db)
99 # s = '%s\n%s' % (s,gentoo.package_to_html(pkg,db))
100
101 if offset != "0":
102 s = '%s<a href="?sstring=%s;offset=%s">[Previous]</a> ' % (s,sstring,int(offset) - config.MAXPERPAGE)
103 if len(results) == config.MAXPERPAGE:
104 s = '%s<a href="?sstring=%s;offset=%s">[Next]</a> ' % (s,sstring,int(offset) + config.MAXPERPAGE)
105
106 sys.stdout.write(s)
107 write_to_cache(s)

  ViewVC Help
Powered by ViewVC 1.1.20