Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
  • Loading branch information...
commit 63c5e6621aac1cdf4d02d0b99266251cd1643c26 1 parent 1af45dc
Luke Plant authored January 20, 2010

Showing 1 changed file with 29 additions and 46 deletions. Show diff stats Hide diff stats

  1. 75  extras/csrf_migration_helper.py
75  extras/csrf_migration_helper.py
@@ -119,11 +119,7 @@
119 119
 import os
120 120
 import sys
121 121
 import re
122  
-try:
123  
-    set
124  
-except NameError:
125  
-    from sets import Set as set
126  
-
  122
+from optparse import OptionParser
127 123
 
128 124
 USAGE = """
129 125
 This tool helps to locate forms that need CSRF tokens added and the
@@ -150,10 +146,12 @@ def get_template_dirs():
150 146
     """
151 147
     from django.conf import settings
152 148
     dirs = set()
153  
-    if 'django.template.loaders.filesystem.load_template_source' in settings.TEMPLATE_LOADERS:
  149
+    if ('django.template.loaders.filesystem.load_template_source' in settings.TEMPLATE_LOADERS 
  150
+        or  'django.template.loaders.filesystem.Loader' in settings.TEMPLATE_LOADERS):
154 151
         dirs.update(map(unicode, settings.TEMPLATE_DIRS))
155 152
 
156  
-    if 'django.template.loaders.app_directories.load_template_source' in settings.TEMPLATE_LOADERS:
  153
+    if ('django.template.loaders.app_directories.load_template_source' in settings.TEMPLATE_LOADERS
  154
+        or 'django.template.loaders.app_directories.Loader' in settings.TEMPLATE_LOADERS):
157 155
         from django.template.loaders.app_directories import app_template_dirs
158 156
         dirs.update(app_template_dirs)
159 157
     return dirs
@@ -204,7 +202,7 @@ def includes_template(self, t):
204 202
         Returns true if this template includes template 't' (via {% include %})
205 203
         """
206 204
         for r in t.relative_filenames:
207  
-            if re.search(r'\{%\s*include\s+"' + re.escape(r) + r'"\s*%\}', self.content):
  205
+            if re.search(r'\{%\s*include\s+(\'|")' + re.escape(r) + r'(\1)\s*%\}', self.content):
208 206
                 return True
209 207
         return False
210 208
 
@@ -219,12 +217,11 @@ def related_templates(self):
219 217
             pass
220 218
 
221 219
         retval = set([self])
222  
-        for r in self.relative_filenames:
223  
-            for t in self.all_templates:
224  
-                if t.includes_template(self):
225  
-                    # If two templates mutually include each other, directly or
226  
-                    # indirectly, we have a problem here...
227  
-                    retval = retval.union(t.related_templates())
  220
+        for t in self.all_templates:
  221
+            if t.includes_template(self):
  222
+                # If two templates mutually include each other, directly or
  223
+                # indirectly, we have a problem here...
  224
+                retval = retval.union(t.related_templates())
228 225
 
229 226
         self._related_templates = retval
230 227
         return retval
@@ -261,6 +258,8 @@ def get_python_code(paths):
261 258
     """
262 259
     retval = []
263 260
     for p in paths:
  261
+        if not os.path.isdir(p):
  262
+            raise Exception("'%s' is not a directory." % p)
264 263
         for (dirpath, dirnames, filenames) in os.walk(p):
265 264
             for f in filenames:
266 265
                 if len([True for e in PYTHON_SOURCE_EXTENSIONS if f.endswith(e)]) > 0:
@@ -338,37 +337,21 @@ def main(pythonpaths):
338 337
         print "----"
339 338
 
340 339
 
341  
-if __name__ == '__main__':
342  
-    # Hacky argument parsing, one day I'll learn OptParse...
343  
-    args = list(sys.argv[1:])
344  
-    if len(args) > 0:
345  
-        if args[0] in ['--help', '-h', '-?', '--usage']:
346  
-            print USAGE
347  
-            sys.exit(0)
348  
-        else:
349  
-            if args[0].startswith('--settings='):
350  
-                module = args[0][len('--settings='):]
351  
-                os.environ["DJANGO_SETTINGS_MODULE"] = module
352  
-                args = args[1:]
353  
-
354  
-            if args[0].startswith('-'):
355  
-                print "Unknown option: %s" % args[0]
356  
-                print USAGE
357  
-                sys.exit(1)
358  
-
359  
-            pythonpaths = args
360  
-
361  
-            if os.environ.get("DJANGO_SETTINGS_MODULE", None) is None:
362  
-                print "You need to set DJANGO_SETTINGS_MODULE or use the '--settings' parameter"
363  
-                sys.exit(1)
364  
-            if len(pythonpaths) == 0:
365  
-                print "Unrecognised command: %s" % command
366  
-                print USAGE
367  
-                sys.exit(1)
368  
-
369  
-            main(pythonpaths)
  340
+parser = OptionParser(usage=USAGE)
  341
+parser.add_option("", "--settings", action="store", dest="settings", help="Dotted path to settings file")
370 342
 
  343
+if __name__ == '__main__':
  344
+    options, args = parser.parse_args()
  345
+    if len(args) == 0:
  346
+        parser.print_help()
  347
+        sys.exit(1)
  348
+
  349
+    settings = getattr(options, 'settings', None)
  350
+    if settings is None:
  351
+        if os.environ.get("DJANGO_SETTINGS_MODULE", None) is None:
  352
+            print "You need to set DJANGO_SETTINGS_MODULE or use the '--settings' parameter"
  353
+            sys.exit(1)
371 354
     else:
372  
-        # no args
373  
-        print USAGE
374  
-        sys.exit(0)
  355
+        os.environ["DJANGO_SETTINGS_MODULE"] = settings
  356
+
  357
+    main(args)

0 notes on commit 63c5e66

Please sign in to comment.
Something went wrong with that request. Please try again.