Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Darren Paul Griffith http://madphilosopher.ca/

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

Scan the FreeBSD ports collection and print one-line summary for each port

The FreeBSD ports collection is a skelton of installable software on a local FreeBSD machine. This small utility lets you scan the collection by category, to find out what software is available in that category. Call the script portinfo.py.

A deeper explanation of the script can be found here.

"""Scans a directory under /usr/ports/ and gives the portname and comment for each port found."""

import os
import os.path
import sys

def findmakefiles(curdir):
    '''goes one level deep under curdir to find and act on the Makefiles.'''

    for name in os.listdir(curdir):
        pathname =  os.path.join(curdir,  name, 'Makefile')  # COMMENTs are found in the Makefiles
        if os.path.isfile(pathname):
            processmakefile(name, pathname)

def processmakefile(name, pathname):
    '''grabs the port's COMMENT from the Makefile.'''
    f = open(pathname)
    data = f.readlines()
    for line in data:
        if line[0:7] == 'COMMENT':
            try:
                comment = line.strip().split('\t')[1]   # most COMMENTs are tab delimited
            except:
                comment = line.strip()                  # some are not, so just print the whole line
            print "%-15s  %s" % (name, comment)

    f.close()

def usage():
    print 'Usage: portinfo.py /usr/ports/[category]'
    sys.exit(1)


if __name__ == '__main__':

    # get directory from command line
    args = sys.argv[1:]
    if len(args) == 1:
        curdir = args[0]
    else:
        usage()
    
##    curdir = '/usr/ports/security/'
 
    # scan this directory   
    if os.path.isdir(curdir):
        findmakefiles(curdir)
    else:
        sys.stdout = sys.stderr
        print "'%s' is not a valid directory." % curdir
        usage()

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS