From 2bfa7eb4a846900bdc9b368d17001e953eedd5bb Mon Sep 17 00:00:00 2001 From: Emmanuel Viennet Date: Sat, 16 Jan 2021 19:33:35 +0100 Subject: [PATCH] List and check usage of old zope methods --- misc/check_zope_usage.py | 39 ++++++++++++++++++++++++++++++++++++ misc/extract_code_strings.py | 21 ++++++++++++++----- sco_zope.py | 9 +++++---- 3 files changed, 60 insertions(+), 9 deletions(-) create mode 100755 misc/check_zope_usage.py diff --git a/misc/check_zope_usage.py b/misc/check_zope_usage.py new file mode 100755 index 00000000..85ffd7ab --- /dev/null +++ b/misc/check_zope_usage.py @@ -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" "
  • " + +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)) diff --git a/misc/extract_code_strings.py b/misc/extract_code_strings.py index 4812756d..01f894e2 100755 --- a/misc/extract_code_strings.py +++ b/misc/extract_code_strings.py @@ -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)) diff --git a/sco_zope.py b/sco_zope.py index a7bac893..bf9d8d27 100644 --- a/sco_zope.py +++ b/sco_zope.py @@ -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)