Skip to content

Instantly share code, notes, and snippets.

Created April 24, 2012 11:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/25a335d7eb8ed9b01945 to your computer and use it in GitHub Desktop.
Save anonymous/25a335d7eb8ed9b01945 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# AUTHORS: bones7456 (bones7456<at>gmail<dot>com) and rking (rking-afraidofspam@panoptic.com)
# License: GPL
# e-file is like apt-file in debian: it is used to search package name via filename for gentoo
# Thanks to: portagefilelist.de and Daniel
VERSION = 20120424
import portage
import httplib
import urllib
import csv
from StringIO import StringIO
import sys
PFL = "www.portagefilelist.de"
PATH = "/index.php/Special:PFLQuery2?file=%s&searchfile=lookup&lookup=file&txt"
PORTDIR = portage.portdb.porttree_root
# TODO: Test PFL "%" wildcards - old e-file simply hangs.
if 2 != len(sys.argv):
print("This is e-file (%s)" % VERSION)
print("Usage: e-file <filename>")
sys.exit(1)
needle = urllib.quote(' '.join(sys.argv[1:]))
conn = httplib.HTTPConnection(PFL)
conn.request("GET", PATH % needle)
res = conn.getresponse()
if httplib.OK != res.status:
print("\n".join([str(e) for e in res.getheaders()]))
print('_' * 80)
print(res.read())
print("Uh-oh. Problem with %s%s%s" % (PFL, PATH, needle))
sys.exit(2)
# Sample row -
# dev-python turbogears /usr/lib64/python2.5/site-packages/turbogears/toolbox/designer/static/diagram/styles foo 1.0.3.2-r0
# As a dict:
# {'category': 'dev-python',
# 'package': 'turbogears',
# 'misc': '',
# 'version': '1.0.3.2-r0',
# 'file': 'foo',
# 'path': '/usr/lib64/python2.5/site-packages/turbogears/toolbox/designer/static/diagram/styles'}
# [Note: Sometimes they come back with multiple rows for the same package, but
# different versions.]
for row in csv.DictReader(StringIO(res.read()), delimiter="\t"):
full_pkg = "%s/%s" % (row['category'], row['package'])
print(full_pkg, row['version'], row['path'])
# TODO: The rest of what e-file does, including:
# - Checking if it is installed, if so, what date
# - Print either a green "[I]" or " * " depending on installed or not.
# - Print all the available versions, on one line.
# - Get the HOMEPAGE from the ebuild
# - Get the DESCRIPTION from the ebuild
# - Print the matched files (I think it's row['path']/row['file'])
# Here it is:
"""
END{
if(FOUND){
for(pkg in vers){
split(pkg,ii,/\//)
if(isgentoo){
NF=0
cmd="ls -tgGd --time-style=+%c /var/db/pkg/" pkg "* 2>/dev/null"
cmd | getline
if(NF==0){
installed=0
}else{
installed=1
install_time=""
for(i=4;i<NF;i++)install_time=install_time " " $i
install_time=substr(install_time,2)
split($NF,install_arr,pkg "-")
}
NF=0
cmd="(grep -h HOMEPAGE\\= " PORTDIR[2] "/" pkg "/*.ebuild | tail -n 1)2>/dev/null"
cmd | getline
if(NF==0){
HOMEPAGE=""
}else{
split($0,tempArr,/\"/)
HOMEPAGE=tempArr[2]
}
NF=0
cmd="(grep -h DESCRIPTION " PORTDIR[2] "/" pkg "/*.ebuild | tail -n 1)2>/dev/null"
cmd | getline
if(NF==0){
DESCRIPTION=""
}else{
split($0,tempArr,/\"/)
DESCRIPTION=tempArr[2]
}
}else{
installed=0
HOMEPAGE=""
DESCRIPTION=""
}
setcolor(1,32)
if(installed){
printf("[I] ")
}else{
printf(" * ")
}
clearcolor()
printf("%s/",ii[1])
setcolor(1,29)
printf("%s\n",ii[2])
setcolor(0,32)
printf("\tAvailable Versions:\t%s\n",vers[pkg])
if(installed){
printf("\tLast Installed Ver:\t")
setcolor(7,34)
printf("%s",install_arr[2])
setcolor(0,35)
printf("(%s)\n",install_time)
setcolor(0,32)
}
if(HOMEPAGE){
printf("\tHomepage:\t\t")
clearcolor()
printf("%s\n",HOMEPAGE)
setcolor(0,32)
}
if(DESCRIPTION){
printf("\tDescription:\t\t")
clearcolor()
printf("%s\n",DESCRIPTION)
setcolor(0,32)
}
printf("\tMatched Files:\t\t")
clearcolor()
printf("%s\n\n",files[pkg])
}
}else{
print "No matches found."
}
}
function setcolor(a,b){
printf("%c[%d;%dm",27,a,b)
}
function clearcolor(){
printf("%c[0m",27)
}
'
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment