Scan the FreeBSD ports collection and print one-line summary for each port
A deeper explanation of the script can be found here.
1 2 """Scans a directory under /usr/ports/ and gives the portname and comment for each port found.""" 3 4 import os 5 import os.path 6 import sys 7 8 def findmakefiles(curdir): 9 '''goes one level deep under curdir to find and act on the Makefiles.''' 10 11 for name in os.listdir(curdir): 12 pathname = os.path.join(curdir, name, 'Makefile') # COMMENTs are found in the Makefiles 13 if os.path.isfile(pathname): 14 processmakefile(name, pathname) 15 16 def processmakefile(name, pathname): 17 '''grabs the port's COMMENT from the Makefile.''' 18 f = open(pathname) 19 data = f.readlines() 20 for line in data: 21 if line[0:7] == 'COMMENT': 22 try: 23 comment = line.strip().split('\t')[1] # most COMMENTs are tab delimited 24 except: 25 comment = line.strip() # some are not, so just print the whole line 26 print "%-15s %s" % (name, comment) 27 28 f.close() 29 30 def usage(): 31 print 'Usage: portinfo.py /usr/ports/[category]' 32 sys.exit(1) 33 34 35 if __name__ == '__main__': 36 37 # get directory from command line 38 args = sys.argv[1:] 39 if len(args) == 1: 40 curdir = args[0] 41 else: 42 usage() 43 44 ## curdir = '/usr/ports/security/' 45 46 # scan this directory 47 if os.path.isdir(curdir): 48 findmakefiles(curdir) 49 else: 50 sys.stdout = sys.stderr 51 print "'%s' is not a valid directory." % curdir 52 usage()