1
0
mirror of https://github.com/django/django.git synced 2024-12-22 17:16:24 +00:00

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

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,7 +217,6 @@ 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
@ -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 "----"
if __name__ == '__main__': parser = OptionParser(usage=USAGE)
# Hacky argument parsing, one day I'll learn OptParse... parser.add_option("", "--settings", action="store", dest="settings", help="Dotted path to settings file")
args = list(sys.argv[1:])
if len(args) > 0:
if args[0] in ['--help', '-h', '-?', '--usage']:
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('-'): if __name__ == '__main__':
print "Unknown option: %s" % args[0] options, args = parser.parse_args()
print USAGE if len(args) == 0:
parser.print_help()
sys.exit(1) sys.exit(1)
pythonpaths = args settings = getattr(options, 'settings', None)
if settings is None:
if os.environ.get("DJANGO_SETTINGS_MODULE", None) 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" print "You need to set DJANGO_SETTINGS_MODULE or use the '--settings' parameter"
sys.exit(1) sys.exit(1)
if len(pythonpaths) == 0:
print "Unrecognised command: %s" % command
print USAGE
sys.exit(1)
main(pythonpaths)
else: else:
# no args os.environ["DJANGO_SETTINGS_MODULE"] = settings
print USAGE
sys.exit(0) main(args)