From 0d10575cbcd4d9cee3c18c5a4325192021d83b89 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Thu, 13 Mar 2025 15:47:02 +0100 Subject: [PATCH] Bug 39325: [DO NOT PUSH] script Script used to generate the "Automatic fix" patch. I don't think we need this script pushed. Signed-off-by: Owen Leonard --- misc/devel/codespell.pl | 70 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100755 misc/devel/codespell.pl diff --git a/misc/devel/codespell.pl b/misc/devel/codespell.pl new file mode 100755 index 00000000000..1296c64e195 --- /dev/null +++ b/misc/devel/codespell.pl @@ -0,0 +1,70 @@ +#!/usr/bin/perl +use Modern::Perl; +use Test::PerlTidy; +use Test::More; +use File::Slurp qw(read_file write_file); +use Parallel::ForkManager; + +my @files; +push @files, + qx{git ls-files '*.pl' '*.PL' '*.pm' '*.t' ':(exclude)installer/data/mysql/updatedatabase.pl' ':(exclude)installer/data/mysql/update22to30.pl' ':(exclude)misc/cronjobs/build_browser_and_cloud.pl'}; +push @files, qx{git ls-files svc opac/svc}; # Files without extension +push @files, qx{git ls-files '*.tt' '*.inc'}; +push @files, + qx{git ls-files '*.js' '*.ts' '*.vue' ':(exclude)koha-tmpl/intranet-tmpl/lib' ':(exclude)koha-tmpl/intranet-tmpl/js/Gettext.js' ':(exclude)koha-tmpl/opac-tmpl/lib' ':(exclude)koha-tmpl/opac-tmpl/bootstrap/js/Gettext.js'}; + +my $nproc ||= qx{nproc}; +my $pm = Parallel::ForkManager->new($nproc); + +my $files_edit; +$pm->run_on_finish( + sub { + my ( $pid, $exit_code, $ident, $exit_signal, $core_dump, $data_ref ) = @_; + $files_edit->{ $data_ref->{file} } = $data_ref->{edits}; + } +); + +while ( my ( $i, $file ) = each @files ) { + chomp $file; + $pm->start and next; + say sprintf "%s (%s/%s)", $file, $i + 1, scalar @files; + my $output = qx{codespell -d --ignore-words .codespell-ignore $file}; + chomp $output; + my @edits; + if ($output) { + my @lines = split "\n", $output; + for my $line (@lines) { + + # C4/Auth.pm:144: authentification ==> authentication + if ( $line =~ m{(?[^:]*):(?\d+): (?\w+) ==> (?.*)} ) { + my $line_number = $+{line} - 1; + my $pattern = $+{pattern}; + my $replace = $+{replace}; + if ( $replace =~ m{,} ) { + $replace = "FIXME CODESPELL ($pattern ==> $replace)"; + } + push @edits, { + line_number => $line_number, + pattern => $pattern, + replace => $replace, + }; + } else { + warn "Unsupported output line: $line"; + } + } + } + $pm->finish( 0, { file => $file, edits => \@edits } ); +} +$pm->wait_all_children; + +while ( my ( $file, $edits ) = each %$files_edit ) { + my @content = read_file($file); + for my $edit (@$edits) { + my $file = $edit->{file}; + my $line_number = $edit->{line_number}; + my $pattern = $edit->{pattern}; + my $replace = $edit->{replace}; + $content[$line_number] =~ s#$pattern#$replace#g; + } + write_file( $file, @content ); +} -- 2.39.5