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 sys
import os import os
from pyflakes.checker import Checker from pyflakes.checker import Checker
...@@ -29,17 +29,12 @@ except ImportError: ...@@ -29,17 +29,12 @@ except ImportError:
def begin_transaction(): def begin_transaction():
get_transaction().begin() get_transaction().begin()
# by default, we ignore warnings about unused imports ignored_messages = (messages.ReturnOutsideFunction, )
fatal_messages = ( messages.ImportStarUsed, messages.UndefinedName,
messages.DuplicateArgument, messages.RedefinedFunction )
def check(codeString, filename, bound_names=()): def check(codeString, filename, bound_names=()):
try:
bound_names = [n for n in bound_names if not hasattr(__builtin__, n)] bound_names = [n for n in bound_names if not hasattr(__builtin__, n)]
for name in bound_names:
setattr(__builtin__, name, 1)
try: try:
tree = compiler.parse(codeString) tree = compile(codeString, filename, "exec", _ast.PyCF_ONLY_AST)
except (SyntaxError, IndentationError): except (SyntaxError, IndentationError):
value = sys.exc_info()[1] value = sys.exc_info()[1]
try: try:
...@@ -55,17 +50,13 @@ def check(codeString, filename, bound_names=()): ...@@ -55,17 +50,13 @@ def check(codeString, filename, bound_names=()):
return 1 return 1
else: else:
warnings = 0 warnings = 0
w = Checker(tree, filename) w = Checker(tree, filename, builtins=bound_names)
w.messages.sort(lambda a, b: cmp(a.lineno, b.lineno)) w.messages.sort(lambda a, b: cmp(a.lineno, b.lineno))
for warning in w.messages: for warning in w.messages:
if isinstance(warning, fatal_messages): if not isinstance(warning, ignored_messages):
warnings += 1 warnings += 1
print warning print warning
return warnings return warnings
finally:
for name in bound_names:
if hasattr(__builtin__, name):
delattr(__builtin__, name)
def checkZopeProduct(path): def checkZopeProduct(path):
...@@ -118,7 +109,7 @@ def checkBusinessTemplate(path): ...@@ -118,7 +109,7 @@ def checkBusinessTemplate(path):
global _connection global _connection
if _connection is not None: if _connection is not None:
return _connection return _connection
db = ZODB.DB(DemoStorage(quota=(1<<20))) db = ZODB.DB(DemoStorage())
_connection = db.open() _connection = db.open()
begin_transaction() begin_transaction()
return _connection return _connection
...@@ -131,8 +122,7 @@ def checkBusinessTemplate(path): ...@@ -131,8 +122,7 @@ def checkBusinessTemplate(path):
if not filename.endswith('.xml'): if not filename.endswith('.xml'):
continue continue
filename = os.path.join(dirpath, filename) filename = os.path.join(dirpath, filename)
file_obj = file(filename) with open(filename) as file_obj:
try:
if 'Products.PythonScripts.PythonScript' in file_obj.read(): if 'Products.PythonScripts.PythonScript' in file_obj.read():
file_obj.seek(0) file_obj.seek(0)
obj = _getConnection().importFile(file_obj, obj = _getConnection().importFile(file_obj,
...@@ -140,10 +130,15 @@ def checkBusinessTemplate(path): ...@@ -140,10 +130,15 @@ def checkBusinessTemplate(path):
blacklist_names = [] blacklist_names = []
if 'WorkflowTemplateItem' in dirpath: if 'WorkflowTemplateItem' in dirpath:
blacklist_names = ['context'] 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)) getValidNames(obj, blacklist_names=blacklist_names))
finally:
file_obj.close()
warnings += checkZopeProduct(os.path.join(path, 'TestTemplateItem')) warnings += checkZopeProduct(os.path.join(path, 'TestTemplateItem'))
warnings += checkZopeProduct(os.path.join(path, 'ExtensionTemplateItem')) warnings += checkZopeProduct(os.path.join(path, 'ExtensionTemplateItem'))
...@@ -158,7 +153,7 @@ def isProduct(path): ...@@ -158,7 +153,7 @@ def isProduct(path):
return os.path.exists(os.path.join(path, '__init__.py')) return os.path.exists(os.path.join(path, '__init__.py'))
def isBusinessTemplate(path): 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(): def main():
warnings = 0 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