From 4334e42288461f63661e9a074c92b69c003d0252 Mon Sep 17 00:00:00 2001 From: Philip Orr Date: Wed, 27 Sep 2023 08:44:40 +0000 Subject: [PATCH] Bug 29324: Prepend copyright or license statement This patch adds a new perl script based on find-license-problems.pl to prepend missing copyright or license statements (or both) to files. To test: 1. Apply the patch 2. Run perl xt/find-license-problems.t 3. You should get some fails 4. Run perl xt/prependcopyrightandlicense.pl 5. Run perl xt/find-license-problems.t again 6. You should now no longer have any fails. --- xt/prependcopyrightandlicense.pl | 94 ++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 xt/prependcopyrightandlicense.pl diff --git a/xt/prependcopyrightandlicense.pl b/xt/prependcopyrightandlicense.pl new file mode 100644 index 0000000000..4b6608087f --- /dev/null +++ b/xt/prependcopyrightandlicense.pl @@ -0,0 +1,94 @@ +#!/usr/bin/perl +# +# Prepend copyright and license statements to Koha source files. +# Only prepends statements if they are missing in the file. +# Statements will be added at the beginning of the file. +# +# Copyright 2023 Koha development team +# +# This file is part of Koha. +# +# Koha 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 3 of the License, or +# (at your option) any later version. +# +# Koha 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 Koha; if not, see . + +use strict; +use warnings; +use Modern::Perl; +use Path::Tiny; +use File::Spec; +use File::Find; + +my @files; +my ( $hascopyright, $hasgpl, $hasv3, $hasorlater, $haslinktolicense, + $hasfranklinst, $is_not_us, $needs_copyright ) = (0)x8; +my $copyrightstatement = "# Copyright 2023 Koha development team\n"; +my $licensestatement = "# Koha is free software; you can redistribute it and/or modify it\n +# under the terms of the GNU General Public License as published by\n +# the Free Software Foundation; either version 3 of the License, or\n +# (at your option) any later version.\n +#\n +# Koha is distributed in the hope that it will be useful, but\n +# WITHOUT ANY WARRANTY; without even the implied warranty of\n +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n +# GNU General Public License for more details.\n +#\n +# You should have received a copy of the GNU General Public License\n +# along with Koha; if not, see .\n"; + +sub wanted { + my $name = $File::Find::name; + push @files, $name + unless $name =~ /\/(\.git|installer\/data\/mysql\/db_revs|koha-tmpl|node_modules|swagger-ui|Koha.Schema.Result)(\/.*)?$/ || + $name =~ /\.(gif|jpg|odt|ogg|pdf|png|po|psd|svg|swf|zip|patch)$/ || + $name =~ m[(xt/find-license-problems|xt/fix-old-fsf-address|misc/translator/po2json)] || + ! -f $name; +} + +find({ wanted => \&wanted, no_chdir => 1 }, File::Spec->curdir()); + +foreach my $name (sort @files) { + open( my $fh, '<', $name ) || die "cannot open file $name $!"; + my ( $hascopyright, $hasgpl, $hasv3, $hasorlater, $haslinktolicense, + $hasfranklinst, $is_not_us, $needs_copyright ) = (0)x8; + while ( my $line = <$fh> ) { + $hascopyright = 1 if ( $line =~ /^(#|--)?\s*Copyright.*\d\d/ ); + $hasgpl = 1 if ( $line =~ /GNU General Public License/ ); + $hasv3 = 1 if ( $line =~ /either version 3/ ); + $hasorlater = 1 + if ( $line =~ /any later version/ + || $line =~ /at your option/ ); + $haslinktolicense = 1 if $line =~ m|http://www\.gnu\.org/licenses|; + $hasfranklinst = 1 if ( $line =~ /51 Franklin Street/ ); + $is_not_us = 1 if $line =~ m|This file is part of the Zebra server|; + $needs_copyright = 1 if $line =~ m|This file is part of Koha| || $name =~ /\.(pl|pm)/; + } + close $fh; + + my $filecontent = path($name)->slurp_utf8; + if ($hascopyright == 0 + && $hasgpl + && $hasv3 + && $hasorlater + && $haslinktolicense) { + path($name)->spew_utf8($copyrightstatement, $filecontent); + } + elsif ($hascopyright == 0 + && $hasgpl == 0 + && $hasv3 == 0 + && $haslinktolicense == 0 + && $hasorlater == 0) { + path($name)->spew_utf8($copyrightstatement, $licensestatement, $filecontent); + } + next unless $hascopyright || $needs_copyright; + next if $is_not_us; +} \ No newline at end of file -- 2.30.2