Skip to content
Snippets Groups Projects
Commit 260cebc2 authored by Campbell Barton's avatar Campbell Barton
Browse files

add source checker scripts for spelling and style.

parent 12583490
Branches
Tags
No related merge requests found
#!/usr/bin/env python3
# ***** BEGIN GPL LICENSE BLOCK *****
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# Contributor(s): Campbell Barton
#
# #**** END GPL LICENSE BLOCK #****
# <pep8 compliant>
"""
this script updates XML themes once new settings are added
./blender.bin --background -noaudio --python source/tools/check_source/check_descriptions.py
"""
import bpy
DUPLICATE_WHITELIST = (
# operators
('ACTION_OT_clean', 'GRAPH_OT_clean'),
('ACTION_OT_clickselect', 'GRAPH_OT_clickselect'),
('ACTION_OT_copy', 'GRAPH_OT_copy'),
('ACTION_OT_delete', 'GRAPH_OT_delete'),
('ACTION_OT_duplicate', 'GRAPH_OT_duplicate'),
('ACTION_OT_duplicate_move', 'GRAPH_OT_duplicate_move'),
('ACTION_OT_extrapolation_type', 'GRAPH_OT_extrapolation_type'),
('ACTION_OT_handle_type', 'GRAPH_OT_handle_type'),
('ACTION_OT_interpolation_type', 'GRAPH_OT_interpolation_type'),
('ACTION_OT_keyframe_insert', 'GRAPH_OT_keyframe_insert'),
('ACTION_OT_mirror', 'GRAPH_OT_mirror'),
('ACTION_OT_paste', 'GRAPH_OT_paste'),
('ACTION_OT_sample', 'GRAPH_OT_sample'),
('ACTION_OT_select_all_toggle', 'GRAPH_OT_select_all_toggle'),
('ACTION_OT_select_border', 'GRAPH_OT_select_border'),
('ACTION_OT_select_column', 'GRAPH_OT_select_column'),
('ACTION_OT_select_leftright', 'GRAPH_OT_select_leftright'),
('ACTION_OT_select_less', 'GRAPH_OT_select_less'),
('ACTION_OT_select_linked', 'GRAPH_OT_select_linked'),
('ACTION_OT_select_more', 'GRAPH_OT_select_more'),
('ACTION_OT_view_all', 'CLIP_OT_dopesheet_view_all', 'GRAPH_OT_view_all'),
('ANIM_OT_change_frame', 'CLIP_OT_change_frame'),
('ARMATURE_OT_armature_layers', 'POSE_OT_armature_layers'),
('ARMATURE_OT_autoside_names', 'POSE_OT_autoside_names'),
('ARMATURE_OT_bone_layers', 'POSE_OT_bone_layers'),
('ARMATURE_OT_extrude_forked', 'ARMATURE_OT_extrude_move'),
('ARMATURE_OT_select_all', 'POSE_OT_select_all'),
('ARMATURE_OT_select_hierarchy', 'POSE_OT_select_hierarchy'),
('ARMATURE_OT_select_linked', 'POSE_OT_select_linked'),
('CLIP_OT_cursor_set', 'UV_OT_cursor_set'),
('CLIP_OT_disable_markers', 'CLIP_OT_graph_disable_markers'),
('CLIP_OT_graph_select_border', 'MASK_OT_select_border'),
('CLIP_OT_view_ndof', 'IMAGE_OT_view_ndof'),
('CLIP_OT_view_pan', 'IMAGE_OT_view_pan', 'VIEW2D_OT_pan', 'VIEW3D_OT_view_pan'),
('CLIP_OT_view_zoom', 'VIEW2D_OT_zoom'),
('CLIP_OT_view_zoom_in', 'VIEW2D_OT_zoom_in'),
('CLIP_OT_view_zoom_out', 'VIEW2D_OT_zoom_out'),
('CONSOLE_OT_copy', 'FONT_OT_text_copy', 'TEXT_OT_copy'),
('CONSOLE_OT_delete', 'FONT_OT_delete', 'TEXT_OT_delete'),
('CONSOLE_OT_insert', 'FONT_OT_text_insert', 'TEXT_OT_insert'),
('CONSOLE_OT_paste', 'FONT_OT_text_paste', 'TEXT_OT_paste'),
('CURVE_OT_duplicate', 'MASK_OT_duplicate'),
('CURVE_OT_handle_type_set', 'MASK_OT_handle_type_set'),
('CURVE_OT_switch_direction', 'MASK_OT_switch_direction'),
('FONT_OT_line_break', 'TEXT_OT_line_break'),
('FONT_OT_move', 'TEXT_OT_move'),
('FONT_OT_move_select', 'TEXT_OT_move_select'),
('FONT_OT_text_cut', 'TEXT_OT_cut'),
('GRAPH_OT_properties', 'IMAGE_OT_properties', 'LOGIC_OT_properties', 'NLA_OT_properties'),
('LATTICE_OT_select_ungrouped', 'MESH_OT_select_ungrouped', 'PAINT_OT_vert_select_ungrouped'),
('NODE_OT_add_node', 'NODE_OT_add_search'),
('NODE_OT_move_detach_links', 'NODE_OT_move_detach_links_release'),
('NODE_OT_properties', 'VIEW3D_OT_properties'),
('NODE_OT_toolbar', 'VIEW3D_OT_toolshelf'),
('OBJECT_OT_duplicate_move', 'OBJECT_OT_duplicate_move_linked'),
('WM_OT_context_cycle_enum', 'WM_OT_context_toggle', 'WM_OT_context_toggle_enum'),
('WM_OT_context_set_boolean', 'WM_OT_context_set_enum', 'WM_OT_context_set_float', 'WM_OT_context_set_int', 'WM_OT_context_set_string', 'WM_OT_context_set_value'),
)
DUPLICATE_IGNORE = {
"",
}
def check_duplicates():
import rna_info
DUPLICATE_IGNORE_FOUND = set()
DUPLICATE_WHITELIST_FOUND = set()
structs, funcs, ops, props = rna_info.BuildRNAInfo()
# This is mainly useful for operators,
# other types have too many false positives
#for t in (structs, funcs, ops, props):
for t in (ops, ):
description_dict = {}
print("")
for k, v in t.items():
if v.description not in DUPLICATE_IGNORE:
id_str = ".".join([s if isinstance(s, str) else s.identifier for s in k if s])
description_dict.setdefault(v.description, []).append(id_str)
else:
DUPLICATE_IGNORE_FOUND.add(v.description)
# sort for easier viewing
sort_ls = [(tuple(sorted(v)), k) for k, v in description_dict.items()]
sort_ls.sort()
for v, k in sort_ls:
if len(v) > 1:
if v not in DUPLICATE_WHITELIST:
print("found %d: %r, \"%s\"" % (len(v), v, k))
#print("%r," % (v,))
else:
DUPLICATE_WHITELIST_FOUND.add(v)
test = (DUPLICATE_IGNORE - DUPLICATE_IGNORE_FOUND)
if test:
print("Invalid 'DUPLICATE_IGNORE': %r" % test)
test = (set(DUPLICATE_WHITELIST) - DUPLICATE_WHITELIST_FOUND)
if test:
print("Invalid 'DUPLICATE_WHITELIST': %r" % test)
def main():
check_duplicates()
if __name__ == "__main__":
main()
# ##### BEGIN GPL LICENSE BLOCK #####
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####
# <pep8 compliant>
"""
Script for checking source code spelling.
python3 source/tools/check_source/check_spelling_c.py some_soure_file.py
Currently only python source is checked.
"""
import os
PRINT_QTC_TASKFORMAT = False
if "USE_QTC_TASK" in os.environ:
PRINT_QTC_TASKFORMAT = True
ONLY_ONCE = True
USE_COLOR = True
_only_once_ids = set()
if USE_COLOR:
COLOR_WORD = "\033[92m"
COLOR_ENDC = "\033[0m"
else:
COLOR_FAIL = ""
COLOR_ENDC = ""
import enchant
dict_spelling = enchant.Dict("en_US")
from check_spelling_c_config import (dict_custom,
dict_ignore,
)
def words_from_text(text):
""" Extract words to treat as English for spell checking.
"""
text = text.strip("#'\"")
text = text.replace("/", " ")
text = text.replace("-", " ")
text = text.replace("+", " ")
text = text.replace("%", " ")
text = text.replace(",", " ")
text = text.replace("=", " ")
text = text.replace("|", " ")
words = text.split()
# filter words
words[:] = [w.strip("*?!:;.,'\"`") for w in words]
def word_ok(w):
# check for empty string
if not w:
return False
# ignore all uppercase words
if w.isupper():
return False
# check for string with no characters in it
is_alpha = False
for c in w:
if c.isalpha():
is_alpha = True
break
if not is_alpha:
return False
# check for prefix/suffix which render this not a real word
# example '--debug', '\n'
# TODO, add more
if w[0] in "%-+\\@":
return False
# check for code in comments
for c in "<>{}[]():._0123456789\&*":
if c in w:
return False
# check for words which contain lower case but have upper case
# ending chars eg - 'StructRNA', we can ignore these.
if len(w) > 1:
has_lower = False
for c in w:
if c.islower():
has_lower = True
break
if has_lower and (not w[1:].islower()):
return False
return True
words[:] = [w for w in words if word_ok(w)]
# text = " ".join(words)
# print(text)
return words
class Comment:
__slots__ = ("file",
"text",
"line",
"type",
)
def __init__(self, file, text, line, type):
self.file = file
self.text = text
self.line = line
self.type = type
def parse(self):
return words_from_text(self.text)
def extract_py_comments(filepath):
import token
import tokenize
source = open(filepath, encoding='utf-8')
comments = []
prev_toktype = token.INDENT
tokgen = tokenize.generate_tokens(source.readline)
for toktype, ttext, (slineno, scol), (elineno, ecol), ltext in tokgen:
if toktype == token.STRING and prev_toktype == token.INDENT:
comments.append(Comment(filepath, ttext, slineno, 'DOCSTRING'))
elif toktype == tokenize.COMMENT:
# non standard hint for commented CODE that we can ignore
if not ttext.startswith("#~"):
comments.append(Comment(filepath, ttext, slineno, 'COMMENT'))
prev_toktype = toktype
return comments
def extract_c_comments(filepath):
"""
Extracts comments like this:
/*
* This is a multi-line comment, notice the '*'s are aligned.
*/
"""
i = 0
text = open(filepath, encoding='utf-8').read()
BEGIN = "/*"
END = "*/"
TABSIZE = 4
SINGLE_LINE = False
STRIP_DOXY = True
STRIP_DOXY_DIRECTIVES = (
r"\section",
r"\subsection",
r"\subsubsection",
r"\ingroup",
r"\param",
r"\page",
)
SKIP_COMMENTS = (
"BEGIN GPL LICENSE BLOCK",
)
# http://doc.qt.nokia.com/qtcreator-2.4/creator-task-lists.html#task-list-file-format
# file\tline\ttype\tdescription
# ... > foobar.tasks
# reverse these to find blocks we won't parse
PRINT_NON_ALIGNED = False
PRINT_SPELLING = True
def strip_doxy_comments(block_split):
for i, l in enumerate(block_split):
for directive in STRIP_DOXY_DIRECTIVES:
if directive in l:
l_split = l.split()
l_split[l_split.index(directive) + 1] = " "
l = " ".join(l_split)
del l_split
break
block_split[i] = l
comments = []
while i >= 0:
i = text.find(BEGIN, i)
if i != -1:
i_next = text.find(END, i)
if i_next != -1:
# not essential but seek ack to find beginning of line
while i > 0 and text[i - 1] in {"\t", " "}:
i -= 1
block = text[i:i_next + len(END)]
# add whitespace in front of the block (for alignment test)
ws = []
j = i
while j > 0 and text[j - 1] != "\n":
ws .append("\t" if text[j - 1] == "\t" else " ")
j -= 1
ws.reverse()
block = "".join(ws) + block
ok = True
if not (SINGLE_LINE or ("\n" in block)):
ok = False
if ok:
for c in SKIP_COMMENTS:
if c in block:
ok = False
break
if ok:
# expand tabs
block_split = [l.expandtabs(TABSIZE) for l in block.split("\n")]
# now validate that the block is aligned
align_vals = tuple(sorted(set([l.find("*") for l in block_split])))
is_aligned = len(align_vals) == 1
if is_aligned:
if PRINT_SPELLING:
if STRIP_DOXY:
strip_doxy_comments(block_split)
align = align_vals[0] + 1
block = "\n".join([l[align:] for l in block_split])[:-len(END)]
# now strip block and get text
# print(block)
# ugh - not nice or fast
slineno = 1 + text.count("\n", 0, i)
comments.append(Comment(filepath, block, slineno, 'COMMENT'))
else:
if PRINT_NON_ALIGNED:
lineno = 1 + text.count("\n", 0, i)
if PRINT_QTC_TASKFORMAT:
print("%s\t%d\t%s\t%s" % (filepath, lineno, "comment", align_vals))
else:
print(filepath + ":" + str(lineno) + ":")
i = i_next
else:
pass
return comments
def spell_check_comments(filepath):
if filepath.endswith(".py"):
comment_list = extract_py_comments(filepath)
else:
comment_list = extract_c_comments(filepath)
for comment in comment_list:
for w in comment.parse():
#if len(w) < 15:
# continue
w_lower = w.lower()
if w_lower in dict_custom or w_lower in dict_ignore:
continue
if not dict_spelling.check(w):
if ONLY_ONCE:
if w_lower in _only_once_ids:
continue
else:
_only_once_ids.add(w_lower)
if PRINT_QTC_TASKFORMAT:
print("%s\t%d\t%s\t%s, suggest (%s)" %
(comment.file,
comment.line,
"comment",
w,
" ".join(dict_spelling.suggest(w)),
))
else:
print("%s:%d: %s%s%s, suggest (%s)" %
(comment.file,
comment.line,
COLOR_WORD,
w,
COLOR_ENDC,
" ".join(dict_spelling.suggest(w)),
))
def spell_check_comments_recursive(dirpath):
from os.path import join, splitext
def source_list(path, filename_check=None):
for dirpath, dirnames, filenames in os.walk(path):
# skip '.svn'
if dirpath.startswith("."):
continue
for filename in filenames:
filepath = join(dirpath, filename)
if filename_check is None or filename_check(filepath):
yield filepath
def is_source(filename):
ext = splitext(filename)[1]
return (ext in {".c", ".inl", ".cpp", ".cxx", ".hpp", ".hxx", ".h", ".osl", ".py"})
for filepath in source_list(dirpath, is_source):
spell_check_comments(filepath)
import sys
import os
if __name__ == "__main__":
for filepath in sys.argv[1:]:
if os.path.isdir(filepath):
# recursive search
spell_check_comments_recursive(filepath)
else:
# single file
spell_check_comments(filepath)
# ##### BEGIN GPL LICENSE BLOCK #####
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####
# <pep8 compliant>
# these must be all lower case for comparisons
# correct spelling but ignore
dict_custom = {
"instantiation",
"iterable",
"prepend",
"subclass", "subclasses", "subclassing",
"merchantability",
"precalculate",
"unregister",
"unselected",
"subdirectory",
"decrement",
"boolean",
"decrementing",
# python types
"str",
"enum", "enums",
"int", "ints",
"tuple", "tuples",
# python functions
"repr",
"func",
# accepted abbreviations
"config",
"recalc",
"addon", "addons",
"subdir",
"struct", "structs",
"lookup", "lookups",
"autocomplete",
"namespace",
"multi",
"keyframe", "keyframing",
"coord", "coords",
"dir",
"tooltip",
# general computer terms
"endian",
"contructor",
"unicode",
"jitter",
"quantized",
"searchable",
"metadata",
"hashable",
"stdin",
"stdout",
"stdin",
"opengl",
"boids",
"keymap",
"voxel", "voxels",
"vert", "verts",
"euler", "eulers",
"booleans",
"intrinsics",
"XXX",
"segfault",
"wiki",
"foo",
"diff",
"diffs",
"sudo",
"http",
"url",
"usr",
"env",
"app",
"preprocessor",
# specific computer terms/brands
"posix",
"unix",
"amiga",
"netscape",
"mozilla",
"irix",
"kde",
"qtcreator",
"ack",
# general computer graphics terms
"colinear",
"coplanar",
"barycentric",
"bezier",
"fresnel",
"radiosity",
"reflectance",
"specular",
"nurbs",
"ngon", "ngons",
"bicubic",
"compositing",
"deinterlace",
"shader",
"shaders",
"centroid",
"emissive",
"quaternions",
"lacunarity",
"musgrave",
"normals",
"kerning",
# blender terms
"bmain",
"bmesh",
"bpy",
"bge",
"mathutils",
"fcurve",
"animviz",
"animsys",
"eekadoodle",
"editmode",
"obdata",
"doctree",
# should have apostrophe but ignore for now
# unless we want to get really picky!
"indices",
"vertices",
}
# incorrect spelling but ignore anyway
dict_ignore = {
"tri",
"quad",
"eg",
"ok",
"ui",
"uv",
"arg", "args",
"vec",
"loc",
"dof",
"bool",
"dupli",
"readonly",
"filepath",
"filepaths",
"filename", "filenames",
"submodule", "submodules",
"dirpath",
"x-axis",
"y-axis",
"z-axis",
"a-z",
"id-block",
"node-trees",
"pyflakes",
"pylint",
# acronyms
"cpu",
"gpu",
"nan",
"utf",
"rgb",
"gzip",
"ppc",
"gpl",
"rna",
"nla",
"api",
"rhs",
"lhs",
"ik",
"smpte",
"svn",
"hg",
"gl",
# extensions
"xpm",
"xml",
"py",
"rst",
# tags
"fixme",
"todo",
# sphinx/rst
"rtype",
# slang
"hrmf",
"automagically",
# names
"jahka",
"campbell",
"mikkelsen", "morten",
}
This diff is collapsed.
import os
IGNORE = (
# particles
"source/blender/blenkernel/intern/boids.c",
"source/blender/blenkernel/intern/cloth.c",
"source/blender/blenkernel/intern/collision.c",
"source/blender/blenkernel/intern/effect.c",
"source/blender/blenkernel/intern/implicit.c",
"source/blender/blenkernel/intern/particle.c",
"source/blender/blenkernel/intern/particle_system.c",
"source/blender/blenkernel/intern/pointcache.c",
"source/blender/blenkernel/intern/sca.c",
"source/blender/blenkernel/intern/softbody.c",
"source/blender/blenkernel/intern/smoke.c",
"source/blender/blenlib/intern/fnmatch.c",
"source/blender/blenlib/intern/md5.c",
"source/blender/blenlib/intern/voxel.c",
"source/blender/blenloader/intern/readfile.c",
"source/blender/blenloader/intern/versioning_250.c",
"source/blender/blenloader/intern/versioning_legacy.c",
"source/blender/blenloader/intern/writefile.c",
"source/blender/editors/space_logic/logic_buttons.c",
"source/blender/editors/space_logic/logic_window.c",
"source/blender/imbuf/intern/dds/DirectDrawSurface.cpp",
"source/blender/opencl/intern/clew.c",
"source/blender/opencl/intern/clew.h",
)
IGNORE_DIR = (
"source/blender/collada",
"source/blender/render",
"source/blender/editors/physics",
"source/blender/editors/space_logic",
"source/blender/freestyle",
"source/blender/gpu",
)
SOURCE_DIR = os.path.normpath(os.path.abspath(os.path.normpath(os.path.join(os.path.dirname(__file__), "..", ".."))))
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment