Skip to content

Instantly share code, notes, and snippets.

@rkulla
Last active September 26, 2015 15:57
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 rkulla/1122022 to your computer and use it in GitHub Desktop.
Save rkulla/1122022 to your computer and use it in GitHub Desktop.
Look up a BSD system's Port package's description instantly
#!/usr/bin/env python
# portell.py -by Ryan Kulla
# I wrote this in ~2001 just for fun
# Description: Lookup a BSD (FreeBSD, et al) system's Port package's description instantly.
# Creates an index of existing Ports for easy and quick look up.
# Usage: portell.py <portname>
import sys, os, shelve
try:
PORTELL_DB = os.environ['PORTELL_PATH']
except KeyError:
PORTELL_DB = "/var/db/portell.db"
def display_usage():
print "Usage: %s <portname>" % sys.argv[0]
print "To update the database: %s -u" % sys.argv[0]
print "The db will be \"/var/db/portell.db\" or whatever PORTELL_PATH points to"
sys.exit(0)
def update_db():
ports_dir = "/usr/ports/"
rm_db()
try:
db = get_db()
os.chmod(PORTELL_DB, 0666)
except:
print "Can't read or write %s. Check the permissions." % PORTELL_DB
sys.exit(0)
os.path.walk(ports_dir, write_pathname, db)
db.close()
def rm_db():
if os.path.exists(PORTELL_DB):
os.unlink(PORTELL_DB)
if os.path.exists(PORTELL_DB + ".db"):
os.unlink(PORTELL_DB + ".db")
def get_db():
db = shelve.open(PORTELL_DB)
fix_dbdb()
return db
def fix_dbdb():
# Sometimes the os seems to append its own .db extention to files
if os.path.exists(PORTELL_DB + ".db"):
os.rename(PORTELL_DB + ".db", PORTELL_DB)
def write_pathname(db, dirname, names):
'/'.join(dirname.split('/')[:5]) # chop port path subdirs
db[os.path.basename(dirname)] = dirname
def read_descr(descr_path):
try:
descr_file = open(descr_path, 'r').readlines()
print "%s reads:\n" % descr_path
for line in descr_file:
print line,
except IOError, errmsg:
print errmsg
def main():
if len(sys.argv) != 2:
display_usage()
elif sys.argv[1] == '-u':
msg = ("Recreating database %s..." % PORTELL_DB)
if not os.path.exists(PORTELL_DB):
msg = ("Creating database %s..." % PORTELL_DB)
print msg
update_db()
print "done."
sys.exit(0)
else:
portname = sys.argv[1]
db = get_db()
if db.has_key(portname):
if os.uname()[0].lower() == "freebsd":
filename = "/pkg-descr"
else:
filename = "/pkg/DESCR"
read_descr(db[portname] + filename)
else:
print >> sys.stderr, "Can't find %s's description file" % portname
db.close()
if __name__=='__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment