Skip to content

Instantly share code, notes, and snippets.

@jondb
Last active December 17, 2015 16:09
Show Gist options
  • Save jondb/5637099 to your computer and use it in GitHub Desktop.
Save jondb/5637099 to your computer and use it in GitHub Desktop.
Follow all includes in an apache file and cat the entire config to STDOUT.
#!/usr/bin/env python
"""Usage: acat.py <root-config-file>
This program takes the root apache config file and outputs it
to STDOUT following all Include directives.
source: https://gist.github.com/jondb/5637099
Get it:
curl https://gist.github.com/jondb/5637099/download | tar xvz --strip-components=1 && chmod +x acat.py
"""
import glob
import os
import sys
def ExtractDirectiveArg(line):
"""Returns the value part of a direcive line.
>>> test='"hello \' \" world"'
>>> test
'"hello \' " world"'
>>> print test.decode('string-escape')
"hello ' " world"
"""
try:
return line.split(None, 2)[1].strip().decode('string-escape').strip('"')
except IndexError:
print 'ERROR Extracting Directive from input "%s"' % str(line)
sys.exit(1)
def GetFilesFromDirective(line):
"""Returns the filenames from an Include directive.
http://httpd.apache.org/docs/2.2/mod/core.html#serverroot
http://httpd.apache.org/docs/2.2/mod/core.html#include
args:
line: <string> the entire line starting with include.
serverrot: <string> the server root value from directive.
Returns:
files: list of files.
"""
filepath = ExtractDirectiveArg(line)
if '*' in filepath:
return glob.glob(filepath)
if '/' == filepath[-1]:
return glob.glob(filepath + '*')
else:
return [filepath]
def IsDirective(line, directive):
return line.lower().strip().startswith(directive)
def CatConfig(filename):
print "##### ENTERED FILE %s ####" % os.path.join(os.getcwd(), filename)
infile = open(filename, 'r')
for line in infile:
if IsDirective(line, 'include'):
print "#", line,
for subfile in GetFilesFromDirective(line):
CatConfig(subfile)
elif IsDirective(line, 'serverroot'):
print line
serverroot = ExtractDirectiveArg(line)
os.chdir(serverroot)
else:
print line,
infile.close()
print "##### --- EXITING FILE %s ####" % os.path.join(os.getcwd(), filename)
if __name__ == '__main__':
try:
root_config_file = sys.argv[1]
except IndexError:
print __doc__
sys.exit(1)
os.chdir(os.path.dirname(root_config_file))
CatConfig(root_config_file)
print '# acat.py is DONE! #'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment