Here's a sample. It's a script I wrote to parse
lilo.conf
import re
import os
import popen2
import sys
import string
# Function to parse out the...
#
# append="mem=768M"
#
# ...line from lilo.conf to give us
# (in this case) the '768'
####
def getACTMemoryStartFromLilo():
# open 'lilo.conf'
liloFile = open("/etc/lilo.conf", 'r')
# Pattern for matching comments so we can ignore them
commentMatcher = re.compile('#.*')
# Pattern for figuring out the start of a stanza
imageMatcher = re.compile('.*image[\s]*=.*')
# Pattern for finding the 'label=linux' line in the stanza
# This makes sure we found the correct stanza.
labelMatcher = re.compile('.*label[\s]*=[\s]*linux.*')
# Pattern for matching and extracting the...
# append="mem=768M"
# ...line
memMatcher = re.compile('.*append[\s]*=[\s]*\"[\s]*mem[\s]*\=[\s]*([0-9]+)[\s]*M')
done = 0
labelFound = 0 # flag
memoryFound = 0 # flag
detectedMemoryOffset = 0
while done == 0:
thisLine = liloFile.readline()
if not thisLine:
done = 1
thisLine = commentMatcher.sub('', thisLine)
thisLine = string.strip(thisLine)
if thisLine != "":
matchResult = imageMatcher.match(thisLine)
# at the start of a new stanza reset our flags
if matchResult:
labelFound = 0
memoryFound = 0
detectedMemoryOffset = 0
matchResult = labelMatcher.match(thisLine)
if matchResult:
labelFound = 1
if memoryFound:
done = 1
matchResult = memMatcher.match(thisLine)
if matchResult:
detectedMemoryOffset = int(matchResult.group(1))
memoryFound = 1
if labelFound:
done = 1
return detectedMemoryOffset
--
MattWalsh - 11 Dec 2001