2021-10-12 11:31:33

by Péter Ujfalusi

[permalink] [raw]
Subject: [PATCH v5] 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

Signed-off-by: Peter Ujfalusi <[email protected]>
---
Hi,

Changes since v4:
- Use the readable version suggested by Joe Perches (store the pyhton snippet in
python_codespell_dict)

Changes since v3:
- Do not try to override the use provided codespell file location

Changes since v2:
- Only try to check for dictionary path it is enabled or when the help is
displayed
- Move the check after the GetOptions()
- Set $help to 2 in case invalid option is passed in order to be able to use
correct exitcode and still display the correct path for dictionary.txt

Changes sicne v1:
- add missing ';' to the line updating the $codespellfile with $codespell_dict

Regards,
Peter

scripts/checkpatch.pl | 31 +++++++++++++++++++++++++++----
1 file changed, 27 insertions(+), 4 deletions(-)

diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index c27d2312cfc3..9fe6f596738a 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -63,6 +63,7 @@ my $min_conf_desc_length = 4;
my $spelling_file = "$D/spelling.txt";
my $codespell = 0;
my $codespellfile = "/usr/share/codespell/dictionary.txt";
+my $user_codespellfile = "";
my $conststructsfile = "$D/const_structs.checkpatch";
my $docsfile = "$D/../Documentation/dev-tools/checkpatch.rst";
my $typedefsfile;
@@ -130,7 +131,7 @@ Options:
--ignore-perl-version override checking of perl version. expect
runtime errors.
--codespell Use the codespell dictionary for spelling/typos
- (default:/usr/share/codespell/dictionary.txt)
+ (default:$codespellfile)
--codespellfile Use this codespell dictionary
--typedefsfile Read additional types from this file
--color[=WHEN] Use colors 'always', 'never', or only when output
@@ -317,7 +318,7 @@ GetOptions(
'debug=s' => \%debug,
'test-only=s' => \$tst_only,
'codespell!' => \$codespell,
- 'codespellfile=s' => \$codespellfile,
+ 'codespellfile=s' => \$user_codespellfile,
'typedefsfile=s' => \$typedefsfile,
'color=s' => \$color,
'no-color' => \$color, #keep old behaviors of -nocolor
@@ -325,9 +326,31 @@ GetOptions(
'kconfig-prefix=s' => \${CONFIG_},
'h|help' => \$help,
'version' => \$help
-) or help(1);
+) or $help = 2;
+
+my $python_codespell_dict = << "EOF";
+import os.path as op
+import codespell_lib
+codespell_dir = op.dirname(codespell_lib.__file__)
+codespell_file = op.join(codespell_dir, 'data', 'dictionary.txt')
+print(codespell_file, end='')
+EOF
+;
+
+if ($user_codespellfile) {
+ # Use the user provided codespell file unconditionally
+ $codespellfile = $user_codespellfile;
+} else {
+ # Try to find the codespell install location to use it as default path
+ if (($codespell || $help) && which("codespell") ne "" && which("python") ne "") {
+ my $codespell_dict = `python3 -c "$python_codespell_dict" 2> /dev/null`;
+ $codespellfile = $codespell_dict if (-e $codespell_dict);
+ }
+}

-help(0) if ($help);
+# $help is 1 if either -h, --help or --version is passed as option - exitcode: 0
+# $help is 2 if invalid option is passed - exitcode: 1
+help($help - 1) if ($help);

die "$P: --git cannot be used with --file or --fix\n" if ($git && ($file || $fix));
die "$P: --verbose cannot be used with --terse\n" if ($verbose && $terse);
--
2.33.0


2021-10-12 14:03:32

by Joe Perches

[permalink] [raw]
Subject: Re: [PATCH v5] checkpatch: get default codespell dictionary path from package location

On Tue, 2021-10-12 at 14:29 +0300, Peter Ujfalusi wrote:
> 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

trivia:

> @@ -325,9 +326,31 @@ GetOptions(
> ? 'kconfig-prefix=s' => \${CONFIG_},
> ? 'h|help' => \$help,
> ? 'version' => \$help
> -) or help(1);
> +) or $help = 2;
> +
> +my $python_codespell_dict = << "EOF";

Because you add a semicolon after "EOF"

> +import os.path as op
> +import codespell_lib
> +codespell_dir = op.dirname(codespell_lib.__file__)
> +codespell_file = op.join(codespell_dir, 'data', 'dictionary.txt')
> +print(codespell_file, end='')
> +EOF
> +;

this is an extra semicolon

> +if ($user_codespellfile) {
> + # Use the user provided codespell file unconditionally
> + $codespellfile = $user_codespellfile;
> +} else {
> + # Try to find the codespell install location to use it as default path
> + if (($codespell || $help) && which("codespell") ne "" && which("python") ne "") {

and I would keep the my $python_codespell_dict definition local to this block.

> + my $codespell_dict = `python3 -c "$python_codespell_dict" 2> /dev/null`;
> + $codespellfile = $codespell_dict if (-e $codespell_dict);
> + }
> +}

It's wrong to check which("python") and then exec `python3 ...`


2021-10-12 17:58:42

by Péter Ujfalusi

[permalink] [raw]
Subject: Re: [PATCH v5] checkpatch: get default codespell dictionary path from package location



On 12/10/2021 16:57, Joe Perches wrote:
> On Tue, 2021-10-12 at 14:29 +0300, Peter Ujfalusi wrote:
>> 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
>
> trivia:
>
>> @@ -325,9 +326,31 @@ GetOptions(
>>   'kconfig-prefix=s' => \${CONFIG_},
>>   'h|help' => \$help,
>>   'version' => \$help
>> -) or help(1);
>> +) or $help = 2;
>> +
>> +my $python_codespell_dict = << "EOF";
>
> Because you add a semicolon after "EOF"
>
>> +import os.path as op
>> +import codespell_lib
>> +codespell_dir = op.dirname(codespell_lib.__file__)
>> +codespell_file = op.join(codespell_dir, 'data', 'dictionary.txt')
>> +print(codespell_file, end='')
>> +EOF
>> +;
>
> this is an extra semicolon

Ah, I see. It shows that perl is not my fo to scripting language.

>
>> +if ($user_codespellfile) {
>> + # Use the user provided codespell file unconditionally
>> + $codespellfile = $user_codespellfile;
>> +} else {
>> + # Try to find the codespell install location to use it as default path
>> + if (($codespell || $help) && which("codespell") ne "" && which("python") ne "") {
>
> and I would keep the my $python_codespell_dict definition local to this block.

OK, I did it like this because to my eyes this looks strange:

if ($user_codespellfile) {
# Use the user provided codespell file unconditionally
$codespellfile = $user_codespellfile;
} else {
# Try to find the codespell install location to use it as default path
if (($codespell || $help) && which("codespell") ne "" && which("python") ne "") {
my $python_codespell_dict = << "EOF";

import os.path as op
import codespell_lib
codespell_dir = op.dirname(codespell_lib.__file__)
codespell_file = op.join(codespell_dir, 'data', 'dictionary.txt')
print(codespell_file, end='')
EOF

my $codespell_dict = `python -c "$python_codespell_dict" 2> /dev/null`;
$codespellfile = $codespell_dict if (-e $codespell_dict);
}
}

>> + my $codespell_dict = `python3 -c "$python_codespell_dict" 2> /dev/null`;
>> + $codespellfile = $codespell_dict if (-e $codespell_dict);
>> + }
>> +}
>
> It's wrong to check which("python") and then exec `python3 ...`

Oops, you are right.

>
>

--
Péter