2021-06-13 18:29:53 +02:00
|
|
|
#!/usr/bin/env python
|
2021-06-02 14:50:41 +02:00
|
|
|
# -*- coding: UTF-8 -*
|
|
|
|
|
|
|
|
|
|
|
|
"""Outil pour migration ScoDoc 7 => ScoDoc 8
|
|
|
|
|
2021-06-13 18:29:53 +02:00
|
|
|
- Liste des appels de la forme context.xxx
|
|
|
|
./refactor.py showcontextcalls app/scodoc/*.p
|
|
|
|
|
|
|
|
- remplace context.xxx par module.xxx
|
|
|
|
|
2021-06-21 10:17:16 +02:00
|
|
|
./refactor.py refactor module.method app/scodoc/*.py
|
2021-06-13 18:29:53 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
2021-06-02 14:50:41 +02:00
|
|
|
Pour chaque module dans views:
|
|
|
|
- construire la liste des fonctions définies dans ce module:
|
|
|
|
get_module_functions
|
|
|
|
|
|
|
|
Pour chaque module dans views et dans scodoc:
|
|
|
|
- remplacer context.xxx par app.views.M.xxx
|
|
|
|
où M est le module de views définissant xxx
|
|
|
|
Si xxx n'est pas trouvé, erreur !
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
from __future__ import print_function
|
|
|
|
import re
|
|
|
|
from pprint import pprint as pp
|
2021-06-12 22:43:22 +02:00
|
|
|
import os
|
2021-06-02 14:50:41 +02:00
|
|
|
import sys
|
|
|
|
import types
|
2021-06-12 22:43:22 +02:00
|
|
|
import tempfile
|
|
|
|
import shutil
|
2021-06-02 14:50:41 +02:00
|
|
|
import click
|
|
|
|
|
|
|
|
TYPES_TO_SCAN = {
|
|
|
|
types.FunctionType,
|
|
|
|
# types.ClassType,
|
|
|
|
# types.DictionaryType,
|
|
|
|
# types.FloatType,
|
|
|
|
# types.IntType,
|
|
|
|
# types.ListType,
|
|
|
|
# types.StringType,
|
|
|
|
# types.TupleType,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def get_module_symbols(module):
|
|
|
|
"""returns list of symbols (functions and constants) defined in the given module"""
|
|
|
|
return [
|
|
|
|
f.__name__
|
|
|
|
for f in [getattr(module, name) for name in dir(module)]
|
|
|
|
if (type(f) in TYPES_TO_SCAN)
|
|
|
|
and ((type(f) != types.FunctionType) or (f.__module__ == module.__name__))
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
# print("\n".join(f.__name__ for f in get_module_functions(notes)))
|
|
|
|
|
|
|
|
|
|
|
|
def scan_views_symbols():
|
|
|
|
"""Scan modules in app.views and returns
|
|
|
|
{ }
|
|
|
|
"""
|
2021-06-12 22:43:22 +02:00
|
|
|
import app
|
|
|
|
|
2021-06-02 14:50:41 +02:00
|
|
|
views_modules = [
|
|
|
|
getattr(app.views, mod_name)
|
|
|
|
for mod_name in dir(app.views)
|
|
|
|
if type(getattr(app.views, mod_name)) == types.ModuleType
|
|
|
|
]
|
|
|
|
sym2mod = {} # symbole_name : module
|
|
|
|
for module in views_modules:
|
|
|
|
start = "app.views."
|
|
|
|
assert module.__name__.startswith(start)
|
|
|
|
module_name = module.__name__[len(start) :]
|
|
|
|
symbols = set(get_module_symbols(module))
|
|
|
|
print("%d symbols defined in %s" % (len(symbols), module))
|
|
|
|
dups = symbols.intersection(sym2mod)
|
|
|
|
if len(dups):
|
|
|
|
print("duplicated symbols !")
|
|
|
|
for dup in dups:
|
|
|
|
print("%s:\t%s\t%s" % (dup, sym2mod[dup], module_name))
|
|
|
|
|
|
|
|
sym2mod.update({s: module_name for s in symbols})
|
|
|
|
return sym2mod
|
|
|
|
|
|
|
|
|
|
|
|
def replace_context_calls(sourcefilename, sym2mod):
|
|
|
|
undefined_list = [] # noms de fonctions non présents dans les modules "views"
|
|
|
|
|
|
|
|
def repl(m):
|
|
|
|
funcname = m.group(1)
|
|
|
|
module = sym2mod.get(funcname, False)
|
|
|
|
if module:
|
|
|
|
return module + "." + funcname
|
|
|
|
else:
|
|
|
|
undefined_list.append((sourcefilename, funcname))
|
|
|
|
return m.group(0) # leave unchanged
|
|
|
|
|
|
|
|
print("reading %s" % sourcefilename)
|
|
|
|
source = open(sourcefilename).read()
|
|
|
|
exp = re.compile(r"context\.([a-zA-Z0-9_]+)")
|
|
|
|
source2 = exp.sub(repl, source)
|
|
|
|
return source2, undefined_list
|
|
|
|
|
|
|
|
|
2021-06-12 22:43:22 +02:00
|
|
|
# sym2mod = scan_views_symbols()
|
|
|
|
|
|
|
|
# source2, undefined_list = replace_context_calls("app/scodoc/sco_core.py", sym2mod)
|
2021-06-02 14:50:41 +02:00
|
|
|
|
2021-06-12 22:43:22 +02:00
|
|
|
|
|
|
|
def list_context_calls(sourcefilename):
|
|
|
|
"""List of methods called on context in this file"""
|
|
|
|
source = open(sourcefilename).read()
|
|
|
|
exp = re.compile(r"context\.([a-zA-Z0-9_]+)")
|
|
|
|
return sorted(set(exp.findall(source)))
|
|
|
|
|
|
|
|
|
2021-06-13 18:29:53 +02:00
|
|
|
def get_context_calls(src_filenames):
|
|
|
|
"""returns { method_name : [ list of module names in which it is called ] }"""
|
2021-06-12 22:43:22 +02:00
|
|
|
S = {}
|
|
|
|
for sourcefilename in src_filenames:
|
|
|
|
l = list_context_calls(sourcefilename)
|
|
|
|
module_name = os.path.splitext(os.path.split(sourcefilename)[1])[0]
|
|
|
|
for m in l:
|
|
|
|
if m in S:
|
|
|
|
S[m].append(module_name)
|
|
|
|
else:
|
|
|
|
S[m] = [module_name]
|
2021-06-13 18:29:53 +02:00
|
|
|
return S
|
|
|
|
|
|
|
|
|
|
|
|
@click.group()
|
|
|
|
def cli():
|
|
|
|
pass
|
2021-06-12 22:43:22 +02:00
|
|
|
|
|
|
|
|
|
|
|
@cli.command()
|
|
|
|
@click.argument("src_filenames", nargs=-1)
|
2021-06-13 18:29:53 +02:00
|
|
|
def showcontextcalls(src_filenames):
|
|
|
|
click.echo("Appels de méthodes sur l'object context")
|
|
|
|
S = get_context_calls(src_filenames)
|
|
|
|
#
|
|
|
|
for method in sorted(S.keys()):
|
|
|
|
print(method + ":\t" + ", ".join(S[method]))
|
|
|
|
|
|
|
|
|
|
|
|
@cli.command()
|
|
|
|
@click.argument("modulemethod", nargs=1)
|
|
|
|
@click.argument("src_filenames", nargs=-1)
|
|
|
|
def refactor(modulemethod, src_filenames):
|
|
|
|
"""Replace call context.method(...)
|
|
|
|
by module.method(context, ...)
|
2021-06-12 22:43:22 +02:00
|
|
|
in all given source filenames
|
|
|
|
"""
|
2021-06-13 18:29:53 +02:00
|
|
|
modulemethod = str(modulemethod) # avoid unicode in Python2
|
|
|
|
frags = modulemethod.split(".")
|
|
|
|
if len(frags) < 2:
|
|
|
|
raise click.BadParameter("must be module.method", param_hint="modulemethod")
|
|
|
|
module = ".".join(frags[:-1])
|
|
|
|
method = frags[-1]
|
2021-06-12 22:43:22 +02:00
|
|
|
backup = tempfile.mkdtemp(dir="/tmp")
|
|
|
|
for sourcefilename in src_filenames:
|
2021-06-13 18:29:53 +02:00
|
|
|
source_module_name = os.path.splitext(os.path.split(sourcefilename)[1])[0]
|
|
|
|
is_local = source_module_name == module
|
2021-06-12 22:43:22 +02:00
|
|
|
source = open(sourcefilename).read()
|
2021-06-13 18:29:53 +02:00
|
|
|
if not is_local:
|
|
|
|
source2 = source.replace(
|
|
|
|
"context." + method + "(", module + "." + method + "(context, "
|
|
|
|
)
|
2021-06-16 16:59:31 +02:00
|
|
|
source2 = source2.replace(
|
|
|
|
"context.Notes." + method + "(", module + "." + method + "(context, "
|
|
|
|
)
|
2021-06-13 18:29:53 +02:00
|
|
|
else:
|
|
|
|
# call in the same module:
|
|
|
|
source2 = source.replace("context." + method + "(", method + "(context, ")
|
|
|
|
if source2 != source:
|
|
|
|
print("changed %s" % sourcefilename)
|
|
|
|
shutil.move(sourcefilename, backup)
|
|
|
|
open(sourcefilename, "w").write(source2)
|
2021-06-12 22:43:22 +02:00
|
|
|
print("Done.\noriginal files saved in %s\n" % backup)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
try:
|
|
|
|
cli(obj={})
|
|
|
|
except SystemExit as e:
|
|
|
|
if e.code != 0:
|
|
|
|
raise
|