[migration] re-try without print_function when compiling a script fails
authorJulien Cristau <julien.cristau@logilab.fr>
Wed, 28 Oct 2015 17:39:30 +0000
changeset 10802 3d948d35d94f
parent 10801 b4beaf0bccea
child 10803 d261bd7c47cf
[migration] re-try without print_function when compiling a script fails As part of the python3 porting we added the __future__.print_function import to cubicweb.migration. That started affecting all migration scripts it runs, which may not be prepared. So if compiling a script results in SyntaxError, try again without the print_function flag. Closes #7935030
migration.py
--- a/migration.py	Wed Sep 23 15:32:17 2015 +0200
+++ b/migration.py	Wed Oct 28 17:39:30 2015 +0000
@@ -26,6 +26,7 @@
 import tempfile
 from os.path import exists, join, basename, splitext
 from itertools import chain
+from warnings import warn
 
 from six import string_types
 
@@ -350,7 +351,14 @@
                 pyname = splitext(basename(migrscript))[0]
             scriptlocals['__name__'] = pyname
             with open(migrscript, 'rb') as fobj:
-                code = compile(fobj.read(), migrscript, 'exec')
+                fcontent = fobj.read()
+            try:
+                code = compile(fcontent, migrscript, 'exec')
+            except SyntaxError:
+                # try without print_function
+                code = compile(fcontent, migrscript, 'exec', 0, True)
+                warn('[3.22] script %r should be updated to work with print_function'
+                     % migrscript, DeprecationWarning)
             exec(code, scriptlocals)
             if funcname is not None:
                 try: