summaryrefslogtreecommitdiff
path: root/ninka-excel.pl
blob: da0ca10305e7306a273d28a1f94e8c1f94b17fd3 (plain)
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/usr/bin/perl
#
#    Copyright (C) 2014  Anthony Kohan and Daniel M. German
#
#    This program 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 2 of
#    the License, or (at your option) any later version.
#
#    This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
#

use strict;
use Switch;
use File::Temp;
use File::Find;
use File::Basename;
use Scalar::Util qw(looks_like_number);
use Spreadsheet::WriteExcel;



if(scalar(@ARGV) != 2){
    print STDERR "Incorrect number of arguments\n";
    print STDERR "Correct usage is: perl ninka-wrapper <path to package file> <database name>\n";
    exit 1;
}

my $path = $0;

$path =~ s/\/+[^\/]+$//;
if ($path eq "") {
    $path = "./";
}

my ($pack, $excelFile) = @ARGV;

my $workbook = Spreadsheet::WriteExcel->new($excelFile);
my $worksheet = $workbook->add_worksheet();
my $format = $workbook->add_format(); # Add a format
$format->set_bold();
$format->set_color('blue');
$format->set_align('center');

$worksheet->set_column(0, 9,  30);
$worksheet->write(0, 0, 'Container File', $format);
$worksheet->write(0, 1, 'Path', $format);
$worksheet->write(0, 2, 'Filename', $format);
$worksheet->write(0, 3, 'Licenses', $format);
$worksheet->write(0, 4, 'Num found', $format);
$worksheet->write(0, 5, 'Lines', $format);
$worksheet->write(0, 6, 'TokensIgnored', $format);
$worksheet->write(0, 7, 'TokensUnmatched', $format);
$worksheet->write(0, 8, 'TokensUnknown', $format);
$worksheet->write(0, 9, 'Tokens', $format);

my $tempdir = File::Temp->newdir();
my $dirname = $tempdir->dirname;

print "***** Extracting file [$pack] to temporary directory [$dirname] *****\n";
my $packext = getExtension($pack);
if ($packext eq ".bz2" || $packext eq ".gz") {
    execute("tar -xvf '$pack' --directory '$dirname'");
} elsif ($packext eq ".jar" || $packext eq ".zip") {
    execute("unzip -d $dirname $pack");
} else {
    print "ninka-wrapper does not support packages with extension [$packext]\n";
}

my @files;
find(
    sub { push @files, $File::Find::name unless -d; }, 
    $dirname
);

print "***** Beginning Execution of Ninka *****\n";
foreach my $file (@files) {
    if (-T $file) {
	print "Running ninka on file [$file]\n";
	execute("perl ${path}/ninka.pl '$file'");
    }
}


print "***** Entering Ninka Data into excell file [$excelFile] *****\n";
my $row = 1;

foreach my $file (@files) {
    
    my $filepath = dirname($file);
    $filepath =~ s/$dirname//;
    my $basefile = fileparse($file, ());
    my $packname = basename($pack);

    #Read entire file into a string
    my $filename = "${file}.license";

    $worksheet->write($row, 0, $packname);
    $worksheet->write($row, 1, $filepath);
    $worksheet->write($row, 2, $basefile);
    
    print "Inserting [$basefile] into table spreedsheet\n";

    if (-T $filename)  {

	open (my $fh, '<', $filename) or die "Can't open file $!";
	my $filedata = do { local $/; <$fh> };

	my @columns = parseLicenseData($filedata);
	
	
	my $originalFile = $file;
	$originalFile =~ s/\.license$//;
	
	foreach my $i (0..7) {
	    $worksheet->write($row, $i+3, $columns[$i]);
	} 
	close($fh);

    } else {
	$worksheet->write($row, 3, "Binary File");		
    }
    $row++;
}

$workbook->close();

sub parseLicenseData {
    my ($data) = @_;
    chomp($data);
    my @columns;
    my @fields = split(';', $data);
    if($fields[0] eq "NONE\n"){
	@columns = '' x 7;
	@columns[0] = 'NONE';
    } else {
	@columns = @fields;
    }
    return @columns;
}

sub getExtension {
    my ($file) = @_;
    my $filename = basename($file);
    my ($ext) = $filename =~ /(\.[^.]+)$/;
    return $ext;
}

sub removeExtension {
    my ($file) = @_;
    (my $filename = $file) =~ s/\.[^.]+$//;
    return $filename;
}

sub execute {
    my ($command) = @_;
    my $output = `$command`;
    my $status = ($? >> 8);
    die "execution of [$command] failed: status [$status]\n" if ($status != 0);
    return $output;
}