Created
June 16, 2011 21:38
-
-
Save jkeyes/1030351 to your computer and use it in GitHub Desktop.
Get the maximum vserver id and maximum source id from a cherokee configuration file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import re | |
def increpl(matchobj): | |
""" | |
Replace the include statement with the contnet of the file. | |
""" | |
inc = matchobj.group(1) | |
return parse(inc) | |
def parse(filename): | |
""" | |
Returns the configuration with all includes resolved. | |
""" | |
cfile = open(filename, 'r') | |
content = cfile.read() | |
cfile.close() | |
return re.sub('include\s*=\s*(.*)\s*', increpl, content) | |
def get_ids(filename): | |
""" | |
Returns the maximum vserver id, and the maximum source id | |
present in this configuration. | |
""" | |
config = parse(filename) | |
vs_ids = [int(x) for x in (re.findall('vserver\!(\d+)\!', config))] | |
src_ids = [int(x) for x in (re.findall('source\!(\d+)\!', config))] | |
return max(list(set(vs_ids))), max(list(set(src_ids))) | |
if __name__ == "__main__": | |
vserver_id, source_id = get_ids("cherokee.conf") | |
print vserver_id, source_id |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment