1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
|
// Reconcile DEP-5 debian/copyright to licensecheck
//
// Copyright : 2023-2025 P Blackman
// License : BSD-2-clause
//
// Test for false positive on GPL or-later licences, or with trailing .0
// Test for gpl-3.0 URL
unit gpl;
{$mode delphi}
interface
function CheckGPL (Fname : AnsiString; Dep5, Actual: String) : Boolean;
Function AdjustGPL (LicenseStr : String) : String;
implementation uses StrUtils, support;
// Extract license short string, expecting a GPL license
function GetFileLicense (Fname : AnsiString) : String;
const MaxLines : Integer = 100;
var Lines,
LposGPL,
LposGPL3 : Integer;
L1,L2, Line : AnsiString;
lfile : Text;
begin
result := '';
Lines := 0;
if OpenFile (FName, lfile) then
begin
while not EOF (lfile) and (Lines < MaxLines) do
begin
readln (lfile, Line);
LposGPL := NPos ('GPL', Line, 1);
LposGPL3 := NPos ('www.gnu.org/licenses/gpl-3.0', Line, 1);
if LposGPL > 0 then
begin
L1 := ExtractSubstr(Line, LposGPL, WhiteSpace);
Result := L1;
if L1 = 'GPL' then // LposGPL updated
begin
L2 := ExtractSubstr(Line, LposGPL, WhiteSpace + ['"']);
if (length(L2) = 3) then
begin
if (L2[3] = '+') then
result := L1 + L2
else
if (L2 = '(>=') then
result := L1 + '+';
end
end;
Lines := MaxLines // terminate loop
end
else
if LposGPL3 > 0 then
begin
result := 'GPL-3';
Lines := MaxLines // terminate loop
end
else
inc (Lines);
end;
Close (lfile);
end;
end;
// Return true if Actual is 2+ or 3+ as shown in d/copyright
// Return true for GPL3 if the URL is found in the file header
function CheckGPL (Fname : AnsiString; Dep5, Actual: String) : Boolean;
var License : String;
begin
result := false;
if (Dep5 = 'GPL-3+') and (Actual = 'GPL-3')
or (Dep5 = 'GPL-2+') and (Actual = 'GPL-2')
or (Dep5 = 'GPL-3') and (Actual = 'GPL')
or (Dep5 = 'GPL-3+') and (Actual = 'GPL') then
begin
License := GetFileLicense (FName);
if (License <> '') and (license [length(License)] = '+') then
result := true
else
if Dep5 = License then
result := true;
end;
end;
// In d/copyright, change GPL3.0 to GPL3 etc,
// so strings match licencecheck
Function AdjustGPL (LicenseStr : String) : String;
begin
iF StartsStr ('GPL', LicenseStr)
and (FindPart ('.0', LicenseStr) <> 0)
and (length (LicenseStr) < 15) then
begin
LicenseStr := DelChars (LicenseStr, '.');
LicenseStr := DelChars (LicenseStr, '0');
end;
result := LicenseStr;
end;
end.
|