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

Diff of /src/packages/search.py

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 1.1.1.1 Revision 1.4
1#!/usr/bin/python -OO 1#!/usr/bin/python -OO
2 2
3import sys 3import sys
4import config 4import config
5import cgi 5import cgi
6import ebuilddb
7import gentoo 6import gentoo
8import os 7import os
8import re
9from MySQLdb import escape_string 9from MySQLdb import escape_string
10 10
11form = cgi.FieldStorage() 11form = cgi.FieldStorage()
12type = form.getvalue("type","") 12type = form.getvalue("type","")
13sstring = form.getvalue("sstring","") 13sstring = form.getvalue("sstring","")
30 except IndexError: 30 except IndexError:
31 continue 31 continue
32 return pkginfo 32 return pkginfo
33 33
34def write_to_cache(s): 34def write_to_cache(s):
35 try:
35 open(cachefile,'w').write(s) 36 open(cachefile,'w').write(s)
37 except IOError:
38 pass
39
40def 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
36 53
37# if it's in the cache, just write that out 54# if it's in the cache, just write that out
38qs = sys.argv[1] 55qs = sys.argv[1]
39cachefile = os.path.join(config.LOCALHOME,'search/cache',qs) 56cachefile = os.path.join(config.LOCALHOME,'search/cache',qs)
40if os.path.exists(cachefile): 57if os.path.exists(cachefile):
43 sys.exit(0) 60 sys.exit(0)
44 61
45escaped = escape_string(sstring) 62escaped = escape_string(sstring)
46query = ('SELECT category,name,homepage,description,license ' 63query = ('SELECT category,name,homepage,description,license '
47 'FROM package WHERE name REGEXP "%s" or description REGEXP "%s" ' 64 'FROM package WHERE name REGEXP "%s" or description REGEXP "%s" '
48 'LIMIT %s,%s' 65 'ORDER BY category LIMIT %s,%s'
49 % (escaped,escaped,offset,config.MAXPERPAGE)) 66 % (escaped,escaped,offset,config.MAXPERPAGE))
50 67
68import ebuilddb
51db = ebuilddb.db_connect() 69db = ebuilddb.db_connect()
52c = db.cursor() 70c = db.cursor()
53try: 71try:
54 c.execute(query) 72 c.execute(query)
55 results = c.fetchall() 73 results = c.fetchall()
60 s = ( '<div class="centerpage">\n' 78 s = ( '<div class="centerpage">\n'
61 '<table width="100%%" border="0" align="center"' 79 '<table width="100%%" border="0" align="center"'
62 ' cellspacing="0"><tr><td colspan="3" class="fields">' 80 ' cellspacing="0"><tr><td colspan="3" class="fields">'
63 'Nothing found.<br></td></tr>\n' 81 'Nothing found.<br></td></tr>\n'
64 '<tr><td class="item" colspan="3">' 82 '<tr><td class="item" colspan="3">'
65 '<img '
66 'src="%s/?category=generic"'
67 'align="right" alt=""> <p>I could not find any ebuild that match' 83 '<p>I could not find any ebuild that match'
68 ' your query. Try a different query or check the' 84 ' your query. Try a different query or check the'
69 ' <a HREF="%s">' 85 ' <a HREF="%s">'
70 'fresh ebuilds main page</a>.</p></td></tr></table>\n' 86 'packages.gentoo.org main page</a>.</p></td></tr></table>\n'
71 '</div>\n' % (config.ICONS,config.FEHOME)) 87 '</div>\n' % config.FEHOME)
72 sys.stdout.write(s) 88 sys.stdout.write(s)
73 write_to_cache(s) 89 write_to_cache(s)
74 sys.exit(0) 90 sys.exit(0)
75 91
76pkgs = [ query_to_dict(i) for i in results ] 92pkgs = [ query_to_dict(i) for i in results ]
93pkgs.sort(sort_by_weight)
77 94
78s = '' 95s = '\n'.join([gentoo.package_to_html(pkg,db) for pkg in pkgs])
79for pkg in pkgs: 96#for pkg in pkgs:
80 #print pkg 97# #print pkg
81 html = gentoo.package_to_html(pkg,db) 98# html = gentoo.package_to_html(pkg,db)
82 s = '%s\n%s<br>\n<br>\n' % (s,gentoo.package_to_html(pkg,db)) 99# s = '%s\n%s' % (s,gentoo.package_to_html(pkg,db))
83 100
84if offset != "0": 101if offset != "0":
85 s = '%s<a href="?sstring=%s;offset=%s">[Previous]</a> ' % (s,sstring,int(offset) - config.MAXPERPAGE) 102 s = '%s<a href="?sstring=%s;offset=%s">[Previous]</a> ' % (s,sstring,int(offset) - config.MAXPERPAGE)
86if len(results) == config.MAXPERPAGE: 103if len(results) == config.MAXPERPAGE:
87 s = '%s<a href="?sstring=%s;offset=%s">[Next]</a> ' % (s,sstring,int(offset) + config.MAXPERPAGE) 104 s = '%s<a href="?sstring=%s;offset=%s">[Next]</a> ' % (s,sstring,int(offset) + config.MAXPERPAGE)

Legend:
Removed from v.1.1.1.1  
changed lines
  Added in v.1.4

  ViewVC Help
Powered by ViewVC 1.1.20