Created
September 28, 2012 19:45
-
-
Save terryjbates/3801757 to your computer and use it in GitHub Desktop.
Python program to generate .dbm file for use with Apache RewriteMap
This file contains hidden or 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
#!/usr/bin/env python | |
# | |
# dbm_generate.py | |
'''Script to generate a .dbm file able to be used by Apache RewriteMap directive. Point it to the text file that contains the key/value pairs. This file must contain no empty lines, nor comments (though the code below should avoid that). | |
It may also be possible to use this file as a template to read any current .dbm files and make mods, versus generating anew each time. | |
''' | |
import dbm | |
import re | |
def main(): | |
# set up pattern to ignore comments and whitespaces | |
pattern = re.compile('^#|^\s|^\n|^\r') | |
# read in our set of keys/values from the file | |
# this file is of form : | |
# <key> <value> | |
# ex: | |
# apply_now http://rinky-dink.com/apply-now | |
filename = 'rewrite.txt' | |
f = open(filename,'r') | |
# create our dbm object | |
my_dbm = dbm.open('rewrite.dbm', 'n') | |
for line in f: | |
# strip trailing whitespace | |
line = line.strip() | |
# if we don't match our pattern, go to work on the | |
if not re.match(pattern, line): | |
#print line | |
(source, target) = line.split() | |
print '######' | |
print source | |
print target | |
# append the key and value to the dbm file | |
my_dbm[source] = target | |
# print out the keys of our dbm file | |
# to confirm functionality. | |
print my_dbm.keys() | |
# REMEMBER, the 'dbm' file is actually *two* files. | |
# Copy both (.pag and .dir) to target directory, but | |
# refer to the 'rewrite.dbm' filepath inside of httpd.conf | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment