History

checkpatch: get default codespell dictionary path from package location

The standard location of dictionary.txt is under codespell's package, on my machine atm (codespell 2.1, Artix Linux): /usr/lib/python3.9/site-packages/codespell_lib/data/dictionary.txt Since we enable the codespell by default for SOF I have constant: No codespell typos will be found - file '/usr/share/codespell/dictionary.txt': No such file or directory The patch proposes to try to fix up the path following the recommendation found here: https://github.com/codespell-project/codespell/issues/1540

Peter Ujfalusi committed 5 years ago

Commit 0ee3e7b1 parent 70a11651 file · +28 −4Browse files

Showing the changes to scripts/checkpatch.pl (1 of 1 file). Show all

Unified · Split

scripts/checkpatch.pl+28−4
@@ -63,6 +63,7 @@ my $min_conf_desc_length = 4; · 62 unmodified lines
6363 my $spelling_file = "$D/spelling.txt";
6464 my $codespell = 0;
6565 my $codespellfile = "/usr/share/codespell/dictionary.txt";
66+my $user_codespellfile = "";
6667 my $conststructsfile = "$D/const_structs.checkpatch";
6768 my $docsfile = "$D/../Documentation/dev-tools/checkpatch.rst";
6869 my $typedefsfile;
@@ -130,7 +131,7 @@ Options: · 61 unmodified lines
130131 --ignore-perl-version override checking of perl version. expect
131132 runtime errors.
132133 --codespell Use the codespell dictionary for spelling/typos
133− (default:/usr/share/codespell/dictionary.txt)
134+ (default:$codespellfile)
134135 --codespellfile Use this codespell dictionary
135136 --typedefsfile Read additional types from this file
136137 --color[=WHEN] Use colors 'always', 'never', or only when output
@@ -317,7 +318,7 @@ GetOptions( · 180 unmodified lines
317318 'debug=s' => \%debug,
318319 'test-only=s' => \$tst_only,
319320 'codespell!' => \$codespell,
320− 'codespellfile=s' => \$codespellfile,
321+ 'codespellfile=s' => \$user_codespellfile,
321322 'typedefsfile=s' => \$typedefsfile,
322323 'color=s' => \$color,
323324 'no-color' => \$color, #keep old behaviors of -nocolor
@@ -325,9 +326,32 @@ GetOptions( · 1 unmodified line
325326 'kconfig-prefix=s' => \${CONFIG_},
326327 'h|help' => \$help,
327328 'version' => \$help
328−) or help(1);
329+) or $help = 2;
330+
331+if ($user_codespellfile) {
332+ # Use the user provided codespell file unconditionally
333+ $codespellfile = $user_codespellfile;
334+} elsif (!(-f $codespellfile)) {
335+ # If /usr/share/codespell/dictionary.txt is not present, try to find it
336+ # under codespell's install directory: <codespell_root>/data/dictionary.txt
337+ if (($codespell || $help) && which("codespell") ne "" && which("python") ne "") {
338+ my $python_codespell_dict = << "EOF";
339+
340+import os.path as op
341+import codespell_lib
342+codespell_dir = op.dirname(codespell_lib.__file__)
343+codespell_file = op.join(codespell_dir, 'data', 'dictionary.txt')
344+print(codespell_file, end='')
345+EOF
346+
347+ my $codespell_dict = `python -c "$python_codespell_dict" 2> /dev/null`;
348+ $codespellfile = $codespell_dict if (-f $codespell_dict);
349+ }
350+}
329351
330−help(0) if ($help);
352+# $help is 1 if either -h, --help or --version is passed as option - exitcode: 0
353+# $help is 2 if invalid option is passed - exitcode: 1
354+help($help - 1) if ($help);
331355
332356 die "$P: --git cannot be used with --file or --fix\n" if ($git && ($file || $fix));
333357 die "$P: --verbose cannot be used with --terse\n" if ($verbose && $terse);