1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
|
#!/usr/bin/env python
#
# Natural Language Toolkit: Deprecated Function & Class Finder
#
# Copyright (C) 2001-2025 NLTK Project
# Author: Edward Loper <edloper@gmail.com>
# URL: <https://www.nltk.org/>
# For license information, see LICENSE.TXT
"""
This command-line tool takes a list of python files or directories,
and searches them for calls to deprecated NLTK functions, or uses of
deprecated NLTK classes. For each use of a deprecated object it
finds, it will print out a warning containing the offending line, as
well as its line number and containing file name. If the terminal has
color support (and if epydoc is installed), then the offending
identifier will be highlighted in red.
"""
######################################################################
# Imports
######################################################################
import os
import re
import sys
import textwrap
import tokenize
from doctest import DocTestParser, register_optionflag
from cStringIO import StringIO
import nltk.corpus
from nltk import defaultdict
######################################################################
# Regexps
######################################################################
#: A little over-simplified, but it'll do.
STRING_PAT = (
r"\s*[ur]{0,2}(?:"
r'"""[\s\S]*?"""|'
'"[^"\n]+?"|'
r"'''[\s\S]*?'''|"
"'[^'\n]+?'"
r")\s*"
)
STRING_RE = re.compile(STRING_PAT)
STRINGS_PAT = f"{STRING_PAT}(?:[+]?{STRING_PAT})*"
STRINGS_RE = re.compile(STRINGS_PAT)
# Define a regexp to search for deprecated definitions.
DEPRECATED_DEF_PAT = (
rf"^\s*@deprecated\s*\(\s*({STRINGS_PAT})\s*\)\s*\n+"
+ r"\s*def\s*(\w+).*"
+ r"|"
+ r"^\s*class\s+(\w+)\s*\(.*Deprecated.*\):\s*"
)
DEPRECATED_DEF_RE = re.compile(DEPRECATED_DEF_PAT, re.MULTILINE)
CORPUS_READ_METHOD_RE = re.compile(
r"({})\.read\(".format("|".join(re.escape(n) for n in dir(nltk.corpus)))
)
CLASS_DEF_RE = re.compile(r"^\s*class\s+(\w+)\s*[:\(]")
######################################################################
# Globals
######################################################################
# Yes, it's bad programming practice, but this is a little hack
# script. :) These get initialized by find_deprecated_defs.
deprecated_funcs = defaultdict(set)
deprecated_classes = defaultdict(set)
deprecated_methods = defaultdict(set)
try:
from epydoc.cli import TerminalController
except ImportError:
class TerminalController:
def __getattr__(self, attr):
return ""
term = TerminalController()
######################################################################
# Code
######################################################################
def strip_quotes(s):
s = s.strip()
while s and (s[0] in "ur") and (s[-1] in "'\""):
s = s[1:]
while s and (s[0] in "'\"" and (s[0] == s[-1])):
s = s[1:-1]
s = s.strip()
return s
def find_class(s, index):
lines = s[:index].split("\n")
while lines:
m = CLASS_DEF_RE.match(lines[-1])
if m:
return m.group(1) + "."
lines.pop()
return "?."
def find_deprecated_defs(pkg_dir):
"""
Return a list of all functions marked with the @deprecated
decorator, and classes with an immediate Deprecated base class, in
all Python files in the given directory.
"""
# Walk through the directory, finding python files.
for root, dirs, files in os.walk(pkg_dir):
for filename in files:
if filename.endswith(".py"):
# Search the file for any deprecated definitions.
s = open(os.path.join(root, filename)).read()
for m in DEPRECATED_DEF_RE.finditer(s):
if m.group(2):
name = m.group(2)
msg = " ".join(
strip_quotes(s) for s in STRING_RE.findall(m.group(1))
)
msg = " ".join(msg.split())
if m.group()[0] in " \t":
cls = find_class(s, m.start())
deprecated_methods[name].add((msg, cls, "()"))
else:
deprecated_funcs[name].add((msg, "", "()"))
else:
name = m.group(3)
m2 = STRING_RE.match(s, m.end())
if m2:
msg = strip_quotes(m2.group())
else:
msg = ""
msg = " ".join(msg.split())
deprecated_classes[name].add((msg, "", ""))
def print_deprecated_uses(paths):
dep_names = set()
dep_files = set()
for path in sorted(paths):
if os.path.isdir(path):
dep_names.update(
print_deprecated_uses([os.path.join(path, f) for f in os.listdir(path)])
)
elif path.endswith(".py"):
print_deprecated_uses_in(open(path).readline, path, dep_files, dep_names, 0)
elif path.endswith(".doctest") or path.endswith(".txt"):
for example in DocTestParser().get_examples(open(path).read()):
ex = StringIO(example.source)
try:
print_deprecated_uses_in(
ex.readline, path, dep_files, dep_names, example.lineno
)
except tokenize.TokenError:
print(
term.RED + "Caught TokenError -- "
"malformatted doctest?" + term.NORMAL
)
return dep_names
def print_deprecated_uses_in(readline, path, dep_files, dep_names, lineno_offset):
tokiter = tokenize.generate_tokens(readline)
context = [""]
for typ, tok, start, end, line in tokiter:
# Remember the previous line -- it might contain
# the @deprecated decorator.
if line is not context[-1]:
context.append(line)
if len(context) > 10:
del context[0]
esctok = re.escape(tok)
# Ignore all tokens except deprecated names.
if not (
tok in deprecated_classes
or (tok in deprecated_funcs and re.search(rf"\b{esctok}\s*\(", line))
or (
tok in deprecated_methods
and re.search(rf"(?!<\bself)[.]\s*{esctok}\s*\(", line)
)
):
continue
# Hack: only complain about read if it's used after a corpus.
if tok == "read" and not CORPUS_READ_METHOD_RE.search(line):
continue
# Ignore deprecated definitions:
if DEPRECATED_DEF_RE.search("".join(context)):
continue
# Print a header for the first use in a file:
if path not in dep_files:
print("\n" + term.BOLD + path + term.NORMAL)
print(f" {term.YELLOW}linenum{term.NORMAL}")
dep_files.add(path)
# Mark the offending token.
dep_names.add(tok)
if term.RED:
sub = term.RED + tok + term.NORMAL
elif term.BOLD:
sub = term.BOLD + tok + term.NORMAL
else:
sub = "<<" + tok + ">>"
line = re.sub(rf"\b{esctok}\b", sub, line)
# Print the offending line.
print(
" {}[{:5d}]{} {}".format(
term.YELLOW, start[0] + lineno_offset, term.NORMAL, line.rstrip()
)
)
def main():
paths = sys.argv[1:] or ["."]
print("Importing nltk...")
try:
import nltk
except ImportError:
print("Unable to import nltk -- check your PYTHONPATH.")
sys.exit(-1)
print("Finding definitions of deprecated functions & classes in nltk...")
find_deprecated_defs(nltk.__path__[0])
print("Looking for possible uses of deprecated funcs & classes...")
dep_names = print_deprecated_uses(paths)
if not dep_names:
print("No deprecated funcs or classes found!")
else:
print("\n" + term.BOLD + "What you should use instead:" + term.NORMAL)
for name in sorted(dep_names):
msgs = (
deprecated_funcs[name]
.union(deprecated_classes[name])
.union(deprecated_methods[name])
)
for msg, prefix, suffix in msgs:
print(
textwrap.fill(
term.RED + prefix + name + suffix + term.NORMAL + ": " + msg,
width=75,
initial_indent=" " * 2,
subsequent_indent=" " * 6,
)
)
if __name__ == "__main__":
main()
|