#!/usr/bin/env python # -*- coding: utf-8 -*- # --------------------------------------------------------------------- # CGI Frontend for whohas # --------------------------------------------------------------------- # Copyright (c) 2008-2009 SCS TeleMedia AG # # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions # are met: # 1. Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # 2. Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # 3. The name of the author may not be used to endorse or promote products # derived from this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, # INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY # AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL # THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; # OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, # WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR # OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF # ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # -------------------------------------------------------------------------- # $Id: whohas.cgi,v 1.4 2009-02-24 17:32:18 obiwan Exp $ # -------------------------------------------------------------------------- # ########################################################### # TODO: # NOTES: # CHANGES: fixed columns widths for 0.23 # ########################################################### """ __author__ = "$Author: obiwan $ (cschnee@telemedia.ch)" __appname__ = "whohas.cgi" __version__ = "$Revision: 1.4 $" __date__ = "$Date: 2009-02-24 17:32:18 $" __license__ = "BSD" """ ################ # IMPORTS ################ import os, sys, cgi, re, datetime ################ # CONSTANTS ################ DISTS = {'arch': 'Arch Linux', 'debian': 'Debian', 'fedora': 'Fedora', 'fink': 'Fink', 'freebsd': 'FreeBSD', \ 'macports': 'MacPorts', 'gentoo': 'Gentoo', 'netbsd': 'NetBSD', 'openbsd': 'OpenBSD', 'opensuse': 'OpenSuSE', \ 'slackware': 'Slackware', 'sourcemage': 'Source Mage', 'ubuntu': 'Ubuntu', } selected_dist = 'debian' # measure start time starttime = datetime.datetime.now() # The CGI 'must-have' print "Content-type: text/html\n" # Head of Page head1 = """ Whohas

Package Search for multiple distributions:

A basic web frontend to whohas from Philipp Wesche.

""" # footer foot1 = """


%s

Powered by whohas, python and apache, kindly hosted by Acme Labs. """ # Search form goes here searchform_start = """

      
""" # Result Table Start rtable_start = """

""" # Result Table Row rtable_row = """""" # Result Table End rtable_end = """
Dist Pkg-Name Version Pkg-Size Release /
Branch
%s %s %s %s %s
""" print head1 # check if a query parameter was sent whform = cgi.FieldStorage() f_q = whform.getfirst("q") f_dist = whform.getlist("dist") # create distform tmpl distopt = '' _dists = DISTS.keys() _dists.sort() for _distname in _dists: if _distname == selected_dist: distopt += """ """ %(_distname, DISTS[_distname]) else: distopt += """ """ %(_distname, DISTS[_distname]) if f_q == None: print searchform_start %(len(DISTS)+5, distopt, "") else: # spend some cycles on security badassregex = re.compile("[^-a-zA-Z0-9]") q = badassregex.sub("", f_q) if f_dist != None: dist = map (lambda ldist: badassregex.sub("", ldist), f_dist) search_dists = ",".join(dist) if 'alldists' in dist: distopt = '' else: distopt = '' for _distname in _dists: if _distname in dist: distopt += """ """ %(_distname, DISTS[_distname]) else: distopt += """ """ %(_distname, DISTS[_distname]) print searchform_start %(len(DISTS)+5, distopt, q) if f_q != None: if search_dists == '' or search_dists == None or search_dists == 'alldists': cmd = 'whohas ' + q else: cmd = "whohas -d %s %s" %(search_dists, q) searchout = os.popen(cmd) results = [] # Head of Result Table print rtable_start while 1: resultline = searchout.readline() if resultline == "": break #resultrow = re.split("\t+", resultline, ) #results.append(resultrow) dist = resultline[:12].strip() pkgname = resultline[12:51].strip() pkgver = resultline[51:70].strip() pkgsize = resultline[70:74].strip() pkgdate = resultline[76:87].strip() pkgbranch = resultline[86:97].strip() pkgurl = resultline[98:].strip() print rtable_row %(dist, pkgurl, pkgname, pkgver, pkgsize, pkgbranch) #print " dist: %s\n pkgname: %s\n pkgver: %s\n pkgsize: %s\n branch: %s\n div2: %s\n pkgurl: %s" %(dist, pkgname, pkgver, pkgsize, branch, div2, pkgurl) print rtable_end searchout.close() tdelta = datetime.datetime.now() - starttime tdelta_str = """Search took %ss %sms.""" %(tdelta.seconds, tdelta.microseconds/1000) print foot1 %(tdelta_str,) print "\n" # EOF