Various fixes/improvements to the the extras/csrf_migration_helper.py script

- Fixed various bugs/oddities with command line parsing
- Find 'include' tags that use single quotes
- Fixed for new style TEMPLATE_LOADERS setting
- Don't silently ignore non-existent paths
- Removed Python-2.3-ism
- Removed an unnecessary loop



git-svn-id: http://code.djangoproject.com/svn/django/trunk@12267 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Luke Plant 2010-01-20 14:18:13 +00:00
parent 1af45dc7bc
commit 63c5e6621a
1 changed files with 28 additions and 45 deletions

View File

@ -119,11 +119,7 @@ PYTHON_ENCODING = "UTF-8"
import os import os
import sys import sys
import re import re
try: from optparse import OptionParser
set
except NameError:
from sets import Set as set
USAGE = """ USAGE = """
This tool helps to locate forms that need CSRF tokens added and the This tool helps to locate forms that need CSRF tokens added and the
@ -150,10 +146,12 @@ def get_template_dirs():
""" """
from django.conf import settings from django.conf import settings
dirs = set() dirs = set()
if 'django.template.loaders.filesystem.load_template_source' in settings.TEMPLATE_LOADERS: if ('django.template.loaders.filesystem.load_template_source' in settings.TEMPLATE_LOADERS
or 'django.template.loaders.filesystem.Loader' in settings.TEMPLATE_LOADERS):
dirs.update(map(unicode, settings.TEMPLATE_DIRS)) dirs.update(map(unicode, settings.TEMPLATE_DIRS))
if 'django.template.loaders.app_directories.load_template_source' in settings.TEMPLATE_LOADERS: if ('django.template.loaders.app_directories.load_template_source' in settings.TEMPLATE_LOADERS
or 'django.template.loaders.app_directories.Loader' in settings.TEMPLATE_LOADERS):
from django.template.loaders.app_directories import app_template_dirs from django.template.loaders.app_directories import app_template_dirs
dirs.update(app_template_dirs) dirs.update(app_template_dirs)
return dirs return dirs
@ -204,7 +202,7 @@ class Template(object):
Returns true if this template includes template 't' (via {% include %}) Returns true if this template includes template 't' (via {% include %})
""" """
for r in t.relative_filenames: for r in t.relative_filenames:
if re.search(r'\{%\s*include\s+"' + re.escape(r) + r'"\s*%\}', self.content): if re.search(r'\{%\s*include\s+(\'|")' + re.escape(r) + r'(\1)\s*%\}', self.content):
return True return True
return False return False
@ -219,12 +217,11 @@ class Template(object):
pass pass
retval = set([self]) retval = set([self])
for r in self.relative_filenames: for t in self.all_templates:
for t in self.all_templates: if t.includes_template(self):
if t.includes_template(self): # If two templates mutually include each other, directly or
# If two templates mutually include each other, directly or # indirectly, we have a problem here...
# indirectly, we have a problem here... retval = retval.union(t.related_templates())
retval = retval.union(t.related_templates())
self._related_templates = retval self._related_templates = retval
return retval return retval
@ -261,6 +258,8 @@ def get_python_code(paths):
""" """
retval = [] retval = []
for p in paths: for p in paths:
if not os.path.isdir(p):
raise Exception("'%s' is not a directory." % p)
for (dirpath, dirnames, filenames) in os.walk(p): for (dirpath, dirnames, filenames) in os.walk(p):
for f in filenames: for f in filenames:
if len([True for e in PYTHON_SOURCE_EXTENSIONS if f.endswith(e)]) > 0: if len([True for e in PYTHON_SOURCE_EXTENSIONS if f.endswith(e)]) > 0:
@ -338,37 +337,21 @@ def main(pythonpaths):
print "----" print "----"
parser = OptionParser(usage=USAGE)
parser.add_option("", "--settings", action="store", dest="settings", help="Dotted path to settings file")
if __name__ == '__main__': if __name__ == '__main__':
# Hacky argument parsing, one day I'll learn OptParse... options, args = parser.parse_args()
args = list(sys.argv[1:]) if len(args) == 0:
if len(args) > 0: parser.print_help()
if args[0] in ['--help', '-h', '-?', '--usage']: sys.exit(1)
print USAGE
sys.exit(0)
else:
if args[0].startswith('--settings='):
module = args[0][len('--settings='):]
os.environ["DJANGO_SETTINGS_MODULE"] = module
args = args[1:]
if args[0].startswith('-'):
print "Unknown option: %s" % args[0]
print USAGE
sys.exit(1)
pythonpaths = args
if os.environ.get("DJANGO_SETTINGS_MODULE", None) is None:
print "You need to set DJANGO_SETTINGS_MODULE or use the '--settings' parameter"
sys.exit(1)
if len(pythonpaths) == 0:
print "Unrecognised command: %s" % command
print USAGE
sys.exit(1)
main(pythonpaths)
settings = getattr(options, 'settings', None)
if settings is None:
if os.environ.get("DJANGO_SETTINGS_MODULE", None) is None:
print "You need to set DJANGO_SETTINGS_MODULE or use the '--settings' parameter"
sys.exit(1)
else: else:
# no args os.environ["DJANGO_SETTINGS_MODULE"] = settings
print USAGE
sys.exit(0) main(args)