rev 9267 - in people/modax/copyright-helper/trunk: . licenses

Modestas Vainius modax-guest at alioth.debian.org
Mon Feb 4 10:07:07 UTC 2008


Author: modax-guest
Date: 2008-02-04 10:07:07 +0000 (Mon, 04 Feb 2008)
New Revision: 9267

Modified:
   people/modax/copyright-helper/trunk/CHCore.pm
   people/modax/copyright-helper/trunk/CHLicenses.pm
   people/modax/copyright-helper/trunk/copyright-helper.pl
   people/modax/copyright-helper/trunk/licenses/gnugpl.pm
Log:
* 0.3 - added license summary.

Modified: people/modax/copyright-helper/trunk/CHCore.pm
===================================================================
--- people/modax/copyright-helper/trunk/CHCore.pm	2008-02-04 09:56:59 UTC (rev 9266)
+++ people/modax/copyright-helper/trunk/CHCore.pm	2008-02-04 10:07:07 UTC (rev 9267)
@@ -246,7 +246,20 @@
     return $count;
 }
 
+sub CHCore::File::getLicenseSummary($) {
+    my $self = shift;
+    my $summary = new CHCore::LicenseSummary;
+    my $count = $self->__createLicenseSummary($summary);
 
+    return ($summary, $count);
+}
+
+sub CHCore::File::__createLicenseSummary($$) {
+    my ($self, $summary) = @_;
+
+    return $summary->addFile($self);
+}
+
 ##################### Directory #############################3
 
 sub CHCore::Directory::new($$$) {
@@ -435,6 +448,26 @@
     return $count;
 }
 
+sub CHCore::Directory::__createLicenseSummary($$) {
+    my ($self, $summary) = @_;
+    my $count = 0;
+
+    # Process self first
+    $count = CHCore::File::__createLicenseSummary($self, $summary);
+
+    # Files now
+    $count += $summary->addFiles($self->getFiles());
+
+    # And directories if recursive...
+    if ($self->isRecursive()) {
+        for my $d (@{$self->getDirectories()}){
+            $count += $d->__createLicenseSummary($summary);
+        }
+    }
+
+    return $count;
+}
+
 ############ CopyrightSummary ###################
 
 sub CHCore::CopyrightSummary::new($) {
@@ -694,6 +727,16 @@
     return $str;
 }
 
+sub __handle_indent_args {
+    my ($lvl1, $lvl2, $lvl3) = @_;
+
+    $lvl1 = 0 unless defined $lvl1;
+    $lvl2 = $lvl1 + 2 unless defined $lvl2;
+    $lvl3 = $lvl2 + 2 unless defined $lvl3;
+
+    return (" " x $lvl1, " " x $lvl2, " " x $lvl3);
+}
+
 sub CHCore::CopyrightSummary::toStringFiles {
     my ($self, $desc_indent, $file_indent) = @_;
 
@@ -745,10 +788,15 @@
 }
 
 sub CHCore::LicenseSummary::getLicenses($) {
-    my @values = values(%{shift()->{licenses}});
-    return \@values;
+    my @licenses = map($_->[0], values(%{shift()->{licenses}}));
+    return \@licenses;
 }
 
+sub CHCore::LicenseSummary::getLicenseFiles($$) {
+    my ($self, $licid) = @_;
+    return $self->{licenses}->{$licid}->[1]
+}
+
 sub CHCore::LicenseSummary::getImplicit($) {
     return shift()->{implicit};
 }
@@ -761,27 +809,96 @@
     return scalar(@{shift()->getLicenses()});
 }
 
+sub CHCore::LicenseSummary::addFile($$) {
+    my ($self, $f) = @_;
+    my $licenses = $self->{licenses};
+
+    if (my $lic = $f->getLicense()) {
+        if ($f->isInherited("license")) {
+            push @{$self->{implicit}}, $f;
+        } else {
+            push @{$self->{explicit}}, $f;
+        }
+        my $id = $lic->getID();
+        my $val;
+        my $count = 0;
+        if (exists($licenses->{$id})) {
+            $val = $licenses->{$id};
+        } else {
+            $val = [ $lic, [] ];
+            $licenses->{$id} = $val;
+            $count = 1;
+        }
+        push @{$val->[1]}, $f;
+        return $count;
+    }
+
+    return 0;
+}
 sub CHCore::LicenseSummary::addFiles($\@) {
     
     my ($self, $files) = @_;
-    my $licenses = $self->{licenses};
     my $count = 0;
 
     for my $f (@$files) {
-        if (my $lic = $f->getLicense()) {
-            if ($f->isInherited("license")) {
-                push @{$self->{implicit}}, $f;
-            } else {
-                push @{$self->{explicit}}, $f;
+        $count += $self->addFile($f);
+    }
+
+    return $count;
+}
+
+sub CHCore::LicenseSummary::__toStringLicense($$) {
+    my ($self, $lic, $files, $i_lvl1, $i_lvl2) = @_;
+    my $str = $i_lvl1 . "* " . $lic->getFullLicenseString() . "\n";
+    if (@$files) {
+        $str .= $i_lvl2 . sprintf("The files (%d) below are licensed under this license:\n", scalar(@$files));
+        for my $f (@$files) {
+            $str .= $i_lvl2 . "- " . $f->getPathWithoutRoot() . "\n";
+        }
+    }
+    return $str;
+}
+
+sub CHCore::LicenseSummary::toString {
+    my ($self, $indent_lvl1, $indent_lvl2, $indent_lvl3) = @_;
+    my ($i_lvl1, $i_lvl2, $i_lvl3) =
+        __handle_indent_args($indent_lvl1, $indent_lvl2, $indent_lvl3);
+    my $maxlic = 0;
+    my $maxfiles = 0;
+    my $str = "";
+
+    if ($self->getLicenseCount() == 1) {
+        my @values = values(%{$self->{licenses}});
+        my $lic_array = $values[0];
+        my $maxlic = $lic_array->[0];
+
+        $str .= $i_lvl1 . "License for all components is:\n";
+        $str .= $self->__toStringLicense($maxlic, [], $i_lvl2, $i_lvl3);
+        return $str;
+    } else {
+        for my $lic_array (values(%{$self->{licenses}})) {
+            my $lic = $lic_array->[0];
+            my $files = $lic_array->[1];
+            if (scalar(@$files) > $maxfiles) {
+                $maxfiles = scalar(@$files);
+                $maxlic = $lic;
             }
-            if (!exists($licenses->{$lic->getID()})) {
-                $licenses->{$lic->getID()} = $lic;
-                $count++;
+        }
+
+        $str .= $i_lvl1 . "License for all components unless stated otherwise:\n";
+        $str .= $self->__toStringLicense($maxlic, [], $i_lvl2, $i_lvl3);
+        $str .= "\n";
+
+        $str .= $i_lvl1 . "Other used licenses:\n";
+        for my $lic_array (values(%{$self->{licenses}})) {
+            my $lic = $lic_array->[0];
+            my $files = $lic_array->[1];
+            if (!$lic->equals($maxlic)) {
+                $str .= $self->__toStringLicense($lic, $files, $i_lvl2, $i_lvl3) . "\n";
             }
         }
+        return $str;
     }
-
-    return $count;
 }
 
 1;

Modified: people/modax/copyright-helper/trunk/CHLicenses.pm
===================================================================
--- people/modax/copyright-helper/trunk/CHLicenses.pm	2008-02-04 09:56:59 UTC (rev 9266)
+++ people/modax/copyright-helper/trunk/CHLicenses.pm	2008-02-04 10:07:07 UTC (rev 9267)
@@ -155,17 +155,17 @@
     return $self->getFoundInText() && $self->getVersion() > -1;
 }
 
-sub CHParsers::LicenseBase::matchCopyrightedFile($\@) {
+sub CHLicenses::LicenseBase::matchCopyrightedFile($\@) {
     return 0;
 }
 
-sub CHParsers::LicenseBase::matchLicenseText($\@) {
+sub CHLicenses::LicenseBase::matchLicenseText($\@) {
     return 0;
 }
 
-sub CHParsers::LicenseBase::equals($$) {
+sub CHLicenses::LicenseBase::equals($$) {
     my ($self, $other) = @_;
-    if ($other-isa(ref($self))) {
+    if ($other->isa(ref($self))) {
         return $self->getID() eq $other->getID();
     } else {
         return 0;

Modified: people/modax/copyright-helper/trunk/copyright-helper.pl
===================================================================
--- people/modax/copyright-helper/trunk/copyright-helper.pl	2008-02-04 09:56:59 UTC (rev 9266)
+++ people/modax/copyright-helper/trunk/copyright-helper.pl	2008-02-04 10:07:07 UTC (rev 9267)
@@ -66,7 +66,7 @@
         $fileobj = new CHCore::Directory(0, $filename);
         $fileobj->setRecursive(1);
     } else {
-        print_msg "Sorry, but file '$filename' does not exist";
+        print_msg "Sorry, but file '$filename' does not exist\n";
         return 0;
     }
 
@@ -128,6 +128,11 @@
 
 sub license_report($) {
     my $fileobj = shift;
+    my ($summary, $count) = $fileobj->getLicenseSummary();
+    
+    print_msg "Calculating license summary ... ";
+    print $summary->toString();
+    print_msg "done.\n";
 }
 
 sub main() {
@@ -140,6 +145,8 @@
             push @reports, "copyright";
         } elsif ($arg eq "-f" || $arg eq "--file" || $arg eq "--file-report") {
             push @reports, "file";
+        } elsif ($arg eq "-l" || $arg eq "--license" || $arg eq "--license-report") {
+            push(@reports, "license");
         } elsif (!$filename) {
             $filename = "$arg";
         } else {
@@ -160,6 +167,8 @@
                 copyright_report($fileobj);
             } elsif ($report eq "file") {            
                 file_report($fileobj);
+            } elsif ($report eq "license") {
+                license_report($fileobj);
             }
             print "\n";
         }
@@ -169,7 +178,7 @@
 }
 
 # Entry point
-${main::VERSION}='0.2';
+${main::VERSION}='0.3';
 print_msg "\n";
 print_msg "Copyright Helper v${main::VERSION}\n";
 print_msg "Extracts copyright and license information from source code\n\n";

Modified: people/modax/copyright-helper/trunk/licenses/gnugpl.pm
===================================================================
--- people/modax/copyright-helper/trunk/licenses/gnugpl.pm	2008-02-04 09:56:59 UTC (rev 9266)
+++ people/modax/copyright-helper/trunk/licenses/gnugpl.pm	2008-02-04 10:07:07 UTC (rev 9267)
@@ -15,6 +15,7 @@
 
 package CHLicenses::gnugpl;
 use strict;
+use CHLicenses;
 our @ISA = qw( CHLicenses::LicenseBase );
 
 sub matchCopyrightedFile($\@) {




More information about the pkg-kde-commits mailing list