sync_templates.py now sync correctly and has some command line options

git-svn-id: https://django-dbtemplates.googlecode.com/svn/trunk@17 cfb8ba98-e953-0410-9cff-959ffddf5974

committer: leidel <leidel@cfb8ba98-e953-0410-9cff-959ffddf5974>

--HG--
extra : convert_revision : e67e7d92fbb75c5b02c7378dade7fc94587bc038
This commit is contained in:
leidel 2007-07-30 20:00:52 +00:00
parent df651dd7e7
commit 371ada2ae2
2 changed files with 61 additions and 47 deletions

View file

@ -1,6 +1,7 @@
#!/usr/bin/env python
import os
import sys
from optparse import OptionParser
def setup_environ():
"""
@ -14,9 +15,9 @@ def setup_environ():
# Set DJANGO_SETTINGS_MODULE appropriately.
os.environ['DJANGO_SETTINGS_MODULE'] = '%s.settings' % project_name
return project_name
return project_name, project_module
def synctemplates(extension=".html", overwrite=False):
def synctemplates(project_module, extension, overwrite):
"""
Helper function for syncing templates in TEMPLATES_DIRS with the
dbtemplates contrib app.
@ -25,6 +26,9 @@ def synctemplates(extension=".html", overwrite=False):
from django.template import TemplateDoesNotExist
from dbtemplates.models import Template
if not extension.startswith("."):
extension = ".%s" % extension
tried = []
synced = []
existing = []
@ -36,60 +40,63 @@ def synctemplates(extension=".html", overwrite=False):
site = None
if site is not None:
for template_dir in settings.TEMPLATE_DIRS:
if os.path.isdir(template_dir):
for dirpath, subdirs, filenames in os.walk(template_dir):
for file in filenames:
if file.endswith(extension) and not file.startswith("."):
filepath = os.path.join(dirpath, file)
filename = filepath.split(template_dir)[1][1:]
try:
if type(project_module.settings.TEMPLATE_DIRS) in (tuple, list):
for template_dir in project_module.settings.TEMPLATE_DIRS:
if os.path.isdir(template_dir):
for dirpath, subdirs, filenames in os.walk(template_dir):
for file in filenames:
if file.endswith(extension) and not file.startswith("."):
filepath = os.path.join(dirpath, file)
filename = filepath.split(template_dir)[1][1:]
try:
t = Template.objects.get(name__exact=filename)
except Template.DoesNotExist:
filecontent = open(filepath, "r").read()
t = Template(name=filename, content=filecontent)
t.save()
t.sites.add(site)
synced.append(filename)
else:
if overwrite:
t.content = open(filepath, "r").read()
try:
t = Template.objects.get(name__exact=filename)
except Template.DoesNotExist:
filecontent = open(filepath, "r").read()
t = Template(name=filename, content=filecontent)
t.save()
t.sites.add(site)
overwritten.append(t.name)
synced.append(filename)
else:
existing.append(t.name)
except IOError:
tried.append(filepath)
except:
raise TemplateDoesNotExist
if overwrite:
t.content = open(filepath, "r").read()
t.save()
t.sites.add(site)
overwritten.append(t.name)
else:
existing.append(t.name)
except IOError:
tried.append(filepath)
except:
raise TemplateDoesNotExist
if len(existing) > 0:
print "\nAlready existing templates:"
for _existing in existing:
print _existing
if len(existing) > 0:
print "Already existing templates (use --force to overwrite):"
for e in existing:
print e
if len(overwritten) > 0:
print "\nOverwritten existing templates:"
for _replaced in overwritten:
print _replaced
if len(overwritten) > 0:
print "Overwritten existing templates:"
for o in overwritten:
print o
if len(synced) > 0:
print "\nSuccessfully synced templates:"
for _synced in synced:
print _synced
if len(synced) > 0:
print "Successfully synced templates:"
for s in synced:
print s
if len(tried) > 0:
print "\nTried to sync but failed:"
for _tried in tried:
print _tried
if len(tried) > 0:
print "Tried to sync but failed:"
for t in tried:
print t
else:
print "Please make sure settings.TEMPLATE_DIRS is a list or tuple."
def main():
def main((options, args)):
try:
project_name = setup_environ()
project_name, project_module = setup_environ()
print "Loading settings from project '%s'.. done." % project_name
synctemplates()
synctemplates(project_module, options.ext, options.overwrite)
except ImportError, e:
print "Please make sure a settings.py file exists in the current directory."
print e
@ -101,4 +108,11 @@ def main():
sys.exit(0)
if __name__ == "__main__":
main()
parser = OptionParser()
parser.add_option("-e", "--ext", dest="ext", action="store",
help="file extension of the files you want to sync with the database [default: %default]",
type="string", default="html")
parser.add_option("-f", "--force",
action="store_true", dest="overwrite", default=False,
help="overwrite existing database templates")
main(parser.parse_args())

View file

@ -1,7 +1,7 @@
from distutils.core import setup
setup(name='dbtemplates',
version='0.2.2',
version='0.2.3',
description='Template loader for database stored templates',
author='Jannis Leidel',
author_email='jannis@leidel.info',