Created
April 21, 2011 15:21
-
-
Save mitechie/934757 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env python | |
""" | |
Generate a paster template set of files from the project | |
""" | |
import sys | |
import os | |
import re | |
import shutil | |
import fileinput | |
BASE = '/tmp' | |
IGNORE = ['\.pyc', '\.git', 'egg\-info', '\.mako\.py', '\.db'] | |
def copy_files(demo, visit, visit_arg): | |
"""Iterate through the demo app copying files with .tmpl extentions""" | |
os.path.walk(demo, visit, visit_arg) | |
def copy_visit(arg, dirname, names): | |
"""Called for each dir the os.walk traverses""" | |
print dirname, arg | |
# first create the new directory in the structure | |
# clear the abspath of the demo to just the | |
rel_path = os.path.relpath(dirname, arg['demo_root']) | |
new_dir = os.path.join(arg['root'], rel_path) | |
new_dir = new_dir.replace('quippdemo_app', '+package+') | |
print 'new dir ' + new_dir | |
os.mkdir(new_dir) | |
# now create each file | |
for name in names: | |
print 'Name-' + name | |
subname = os.path.join(dirname, name) | |
print subname | |
if os.path.isdir(subname): | |
# ignore, we'll walk it and create it eventually | |
print '...hit dir' | |
else: | |
new_file = os.path.join(new_dir, name) | |
ignore = arg['ignore_regex'].search(subname) | |
if not ignore: | |
if os.path.splitext(new_file)[1] in arg['tmpl_ext']: | |
new_file = new_file + '_tmpl' | |
print 'creating file %s' % new_file | |
shutil.copyfile(subname, new_file) | |
else: | |
print 'Regex Skip ' + new_file | |
print "" | |
def replace_appname(proj, visit, visit_arg): | |
"""Replace all instances of quippdemo_app in files""" | |
os.path.walk(proj, visit, visit_arg) | |
def appname_visit(arg, dirname, names): | |
"""Open each file and replace the quippdemo_app with {{package}}""" | |
print dirname, arg | |
for name in names: | |
print "Name - " + name | |
subname = os.path.join(dirname, name) | |
print subname | |
if os.path.isdir(subname): | |
# ignore, we'll walk it and create it eventually | |
print '...hit dir' | |
else: | |
print 'running replace in file: ' + name | |
repl_file = os.path.join(dirname, name) | |
for repl in arg: | |
replace_string(repl_file, repl[0], repl[1]) | |
print "" | |
def replace_string(filename, find, replace): | |
"""Replace the strings in the file given""" | |
for line in fileinput.input(filename, inplace = 1): | |
sys.stdout.write(line.replace(find, replace)) | |
if __name__ == "__main__": | |
# require a path to start the walk at | |
if len(sys.argv) < 2: | |
print "Please supply a path to start walking at" | |
sys.exit() | |
demo_path = os.path.abspath(sys.argv[1]) | |
demo_root = os.path.dirname(demo_path) | |
root = BASE | |
if os.path.isdir('/tmp/+package+'): | |
print "removing package dir" | |
shutil.rmtree('/tmp/+package+') | |
regex_str = '|'.join(IGNORE) | |
invalid_regex = re.compile(regex_str) | |
arg = { | |
'ignore_regex': invalid_regex, | |
'root': root, | |
'demo_root': demo_root, | |
'tmpl_ext': ['.py', '.js', '.ini', '.in', | |
'.rst', '.txt', '.cfg', '.mako' ], | |
} | |
copy_files(os.path.abspath(demo_path), copy_visit, arg) | |
replace_arg = [ | |
('quippdemo_app', '{{package}}'), | |
] | |
replace_appname('/tmp/+package+', appname_visit, replace_arg) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment