List and check usage of old zope methods

This commit is contained in:
Emmanuel Viennet 2021-01-16 19:33:35 +01:00
parent 0e7857e5ca
commit 2bfa7eb4a8
3 changed files with 60 additions and 9 deletions

39
misc/check_zope_usage.py Executable file
View File

@ -0,0 +1,39 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Check usage of (published) ScoDoc methods
Quick method: just grep method name in all constant strings from the source code base.
Usage:
check_zope_usage.py methods-list.txt string-constants.txt
methods-list.txt : fichier texte, module et un nom de méthode / ligne
ZScoUsers get_userlist
string-constants.txt : les constantes chaines, extraites par extract_code_strings.py
"scolars.py" "</li><li>"
E. Viennet 2021-01-09
"""
from __future__ import print_function
import sys
methods_filename = sys.argv[1]
constants_filename = sys.argv[2]
with open(methods_filename) as f:
methods = [l.strip().split("\t")[1] for l in f]
print("%d methods" % len(methods))
with open(constants_filename) as f:
constants = [l[:-1].split("\t")[1] for l in f]
print("%d constants" % len(constants))
for method in methods:
n = 0
for c in constants:
if method in c:
n += 1
print("%s\t%s" % (method, n))

View File

@ -5,7 +5,7 @@
Useful to check if an API function is used in a generated web page !
Usage:
extract_code_strings.py source.py ...
extract_code_strings.py source.py ... > string-constants.txt
(replace RT by an existing departement id)
@ -15,13 +15,24 @@ from __future__ import print_function
import sys
import ast
import types
L = []
for srcfilename in sys.argv[1:]:
print("processing %s" % srcfilename, file=sys.stderr)
# print("processing %s" % srcfilename, file=sys.stderr)
with open(srcfilename) as f:
p = ast.parse(f.read())
L.extend(x.s.strip() for x in ast.walk(p) if x.__class__ == ast.Str)
# L.extend(x.s.strip() for x in ast.walk(p) if x.__class__ == ast.Str)
for x in ast.walk(p):
if x.__class__ == ast.Str:
if type(x.s) == types.StringType:
s = x.s
else:
s = x.s.encode("UTF-8")
# remove tabs and cr
s = s.translate(None, "\t\n")
if len(s):
print("%s\t%s" % (srcfilename, s))
L = sorted(set(L)) # uniq | sort
print("\n".join(L))
# L = sorted(set(L)) # uniq | sort
# print("\n".join(L))

View File

@ -52,15 +52,16 @@ file_path = Globals.package_home(globals())
import inspect
LOG_SECURITY=False # use for dev
LOG_SECURITY = False # use for dev
class ClassSecurityInfo(ZopeClassSecurityInfo):
def declareProtected(self, perm, funcname):
if LOG_SECURITY:
frame = inspect.currentframe()
module = frame.f_back.f_locals["__module__"]
if str(module).strip() == "ZAbsences":
raise Exception()
module = str(frame.f_back.f_locals["__module__"])
assert module.startswith("Products.ScoDoc.")
module = module[len("Products.ScoDoc.") :]
with open("/tmp/protected_methods.txt", "a") as f:
f.write("%s\t%s\n" % (module, funcname))
super(ClassSecurityInfo, self).declareProtected(perm, funcname)