Commit e3c770b3 authored by Jérome Perrin's avatar Jérome Perrin

make it work on current business template format

parent 7f5477b0
import compiler
import _ast
import sys
import os
from pyflakes.checker import Checker
......@@ -29,43 +29,34 @@ except ImportError:
def begin_transaction():
get_transaction().begin()
# by default, we ignore warnings about unused imports
fatal_messages = ( messages.ImportStarUsed, messages.UndefinedName,
messages.DuplicateArgument, messages.RedefinedFunction )
ignored_messages = (messages.ReturnOutsideFunction, )
def check(codeString, filename, bound_names=()):
bound_names = [n for n in bound_names if not hasattr(__builtin__, n)]
try:
bound_names = [n for n in bound_names if not hasattr(__builtin__, n)]
for name in bound_names:
setattr(__builtin__, name, 1)
tree = compile(codeString, filename, "exec", _ast.PyCF_ONLY_AST)
except (SyntaxError, IndentationError):
value = sys.exc_info()[1]
try:
tree = compiler.parse(codeString)
except (SyntaxError, IndentationError):
value = sys.exc_info()[1]
try:
(lineno, offset, line) = value[1][1:]
except IndexError:
print >> sys.stderr, 'could not compile %r' % (filename,)
return 1
if line.endswith("\n"):
line = line[:-1]
print >> sys.stderr, '%s:%d: could not compile' % (filename, lineno)
print >> sys.stderr, line
print >> sys.stderr, " " * (offset-2), "^"
(lineno, offset, line) = value[1][1:]
except IndexError:
print >> sys.stderr, 'could not compile %r' % (filename,)
return 1
else:
warnings = 0
w = Checker(tree, filename)
w.messages.sort(lambda a, b: cmp(a.lineno, b.lineno))
for warning in w.messages:
if isinstance(warning, fatal_messages):
warnings += 1
print warning
return warnings
finally:
for name in bound_names:
if hasattr(__builtin__, name):
delattr(__builtin__, name)
if line.endswith("\n"):
line = line[:-1]
print >> sys.stderr, '%s:%d: could not compile' % (filename, lineno)
print >> sys.stderr, line
print >> sys.stderr, " " * (offset-2), "^"
return 1
else:
warnings = 0
w = Checker(tree, filename, builtins=bound_names)
w.messages.sort(lambda a, b: cmp(a.lineno, b.lineno))
for warning in w.messages:
if not isinstance(warning, ignored_messages):
warnings += 1
print warning
return warnings
def checkZopeProduct(path):
......@@ -118,7 +109,7 @@ def checkBusinessTemplate(path):
global _connection
if _connection is not None:
return _connection
db = ZODB.DB(DemoStorage(quota=(1<<20)))
db = ZODB.DB(DemoStorage())
_connection = db.open()
begin_transaction()
return _connection
......@@ -131,8 +122,7 @@ def checkBusinessTemplate(path):
if not filename.endswith('.xml'):
continue
filename = os.path.join(dirpath, filename)
file_obj = file(filename)
try:
with open(filename) as file_obj:
if 'Products.PythonScripts.PythonScript' in file_obj.read():
file_obj.seek(0)
obj = _getConnection().importFile(file_obj,
......@@ -140,10 +130,15 @@ def checkBusinessTemplate(path):
blacklist_names = []
if 'WorkflowTemplateItem' in dirpath:
blacklist_names = ['context']
warnings += check(obj._body, filename,
python_code = obj._body
python_filename = filename[:-4] + '.py'
if os.path.exists(python_filename):
# "new" business template format, where python code is
# in a separate .py file
python_code = file(python_filename, "U").read()
filename = python_filename
warnings += check(python_code, filename,
getValidNames(obj, blacklist_names=blacklist_names))
finally:
file_obj.close()
warnings += checkZopeProduct(os.path.join(path, 'TestTemplateItem'))
warnings += checkZopeProduct(os.path.join(path, 'ExtensionTemplateItem'))
......@@ -158,7 +153,7 @@ def isProduct(path):
return os.path.exists(os.path.join(path, '__init__.py'))
def isBusinessTemplate(path):
return os.path.exists(os.path.join(path, 'bt', 'revision'))
return os.path.exists(os.path.join(path, 'bt', 'title'))
def main():
warnings = 0
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment