/[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 Revision 1.2
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","")
32 return pkginfo 32 return pkginfo
33 33
34def write_to_cache(s): 34def write_to_cache(s):
35 open(cachefile,'w').write(s) 35 open(cachefile,'w').write(s)
36 36
37def 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
37# if it's in the cache, just write that out 51# if it's in the cache, just write that out
38qs = sys.argv[1] 52qs = sys.argv[1]
39cachefile = os.path.join(config.LOCALHOME,'search/cache',qs) 53cachefile = os.path.join(config.LOCALHOME,'search/cache',qs)
40if os.path.exists(cachefile): 54if os.path.exists(cachefile):
41 #sys.stdout.write('<div class="centerpage">using cached results</div>\n') 55 #sys.stdout.write('<div class="centerpage">using cached results</div>\n')
46query = ('SELECT category,name,homepage,description,license ' 60query = ('SELECT category,name,homepage,description,license '
47 'FROM package WHERE name REGEXP "%s" or description REGEXP "%s" ' 61 'FROM package WHERE name REGEXP "%s" or description REGEXP "%s" '
48 'LIMIT %s,%s' 62 'LIMIT %s,%s'
49 % (escaped,escaped,offset,config.MAXPERPAGE)) 63 % (escaped,escaped,offset,config.MAXPERPAGE))
50 64
65import ebuilddb
51db = ebuilddb.db_connect() 66db = ebuilddb.db_connect()
52c = db.cursor() 67c = db.cursor()
53try: 68try:
54 c.execute(query) 69 c.execute(query)
55 results = c.fetchall() 70 results = c.fetchall()
72 sys.stdout.write(s) 87 sys.stdout.write(s)
73 write_to_cache(s) 88 write_to_cache(s)
74 sys.exit(0) 89 sys.exit(0)
75 90
76pkgs = [ query_to_dict(i) for i in results ] 91pkgs = [ query_to_dict(i) for i in results ]
92pkgs.sort(sort_by_weight)
77 93
78s = '' 94s = ''
79for pkg in pkgs: 95for pkg in pkgs:
80 #print pkg 96 #print pkg
81 html = gentoo.package_to_html(pkg,db) 97 html = gentoo.package_to_html(pkg,db)
82 s = '%s\n%s<br>\n<br>\n' % (s,gentoo.package_to_html(pkg,db)) 98 s = '%s\n%s' % (s,gentoo.package_to_html(pkg,db))
83 99
84if offset != "0": 100if offset != "0":
85 s = '%s<a href="?sstring=%s;offset=%s">[Previous]</a> ' % (s,sstring,int(offset) - config.MAXPERPAGE) 101 s = '%s<a href="?sstring=%s;offset=%s">[Previous]</a> ' % (s,sstring,int(offset) - config.MAXPERPAGE)
86if len(results) == config.MAXPERPAGE: 102if len(results) == config.MAXPERPAGE:
87 s = '%s<a href="?sstring=%s;offset=%s">[Next]</a> ' % (s,sstring,int(offset) + config.MAXPERPAGE) 103 s = '%s<a href="?sstring=%s;offset=%s">[Next]</a> ' % (s,sstring,int(offset) + config.MAXPERPAGE)

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.2

  ViewVC Help
Powered by ViewVC 1.1.20