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

Contents of /src/packages/search.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 (14 years, 1 month ago) by marduk
Branch: MAIN
Changes since 1.1: +18 -2 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 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 open(cachefile,'w').write(s)
36
37 def sort_by_weight(a, b):
38 """Right now we just sort based on whether the sstring is in the name"""
39 a_name = a['name']
40 b_name = b['name']
41
42 a_match = re.search(sstring, a_name, re.IGNORECASE)
43 b_match = re.search(sstring, b_name, re.IGNORECASE)
44
45 if a_match and b_match:
46 return 0
47 if a_match:
48 return -1
49 return 1
50
51 # if it's in the cache, just write that out
52 qs = sys.argv[1]
53 cachefile = os.path.join(config.LOCALHOME,'search/cache',qs)
54 if os.path.exists(cachefile):
55 #sys.stdout.write('<div class="centerpage">using cached results</div>\n')
56 sys.stdout.write(open(cachefile,'r').read())
57 sys.exit(0)
58
59 escaped = escape_string(sstring)
60 query = ('SELECT category,name,homepage,description,license '
61 'FROM package WHERE name REGEXP "%s" or description REGEXP "%s" '
62 'LIMIT %s,%s'
63 % (escaped,escaped,offset,config.MAXPERPAGE))
64
65 import ebuilddb
66 db = ebuilddb.db_connect()
67 c = db.cursor()
68 try:
69 c.execute(query)
70 results = c.fetchall()
71 except:
72 results = None
73
74 if not results:
75 s = ( '<div class="centerpage">\n'
76 '<table width="100%%" border="0" align="center"'
77 ' cellspacing="0"><tr><td colspan="3" class="fields">'
78 'Nothing found.<br></td></tr>\n'
79 '<tr><td class="item" colspan="3">'
80 '<img '
81 'src="%s/?category=generic"'
82 'align="right" alt=""> <p>I could not find any ebuild that match'
83 ' your query. Try a different query or check the'
84 ' <a HREF="%s">'
85 'fresh ebuilds main page</a>.</p></td></tr></table>\n'
86 '</div>\n' % (config.ICONS,config.FEHOME))
87 sys.stdout.write(s)
88 write_to_cache(s)
89 sys.exit(0)
90
91 pkgs = [ query_to_dict(i) for i in results ]
92 pkgs.sort(sort_by_weight)
93
94 s = ''
95 for pkg in pkgs:
96 #print pkg
97 html = gentoo.package_to_html(pkg,db)
98 s = '%s\n%s' % (s,gentoo.package_to_html(pkg,db))
99
100 if offset != "0":
101 s = '%s<a href="?sstring=%s;offset=%s">[Previous]</a> ' % (s,sstring,int(offset) - config.MAXPERPAGE)
102 if len(results) == config.MAXPERPAGE:
103 s = '%s<a href="?sstring=%s;offset=%s">[Next]</a> ' % (s,sstring,int(offset) + config.MAXPERPAGE)
104
105 sys.stdout.write(s)
106 write_to_cache(s)

  ViewVC Help
Powered by ViewVC 1.1.20