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 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
|
#! /usr/bin/perl
use autodie;
use strict;
use utf8;
use open ':locale';
use warnings qw(all);
use feature 'say';
use Getopt::Long;
use Pod::Usage;
use FileHandle;
use Regexp::Assemble;
use Image::ExifTool;
use Font::TTF::Font;
use Font::TTF::Ttc;
=head1 NAME
license-miner - extract copyright/licensing info from complex files
=head1 SYNOPSIS
license-miner [B<options>] [F<path>|inspector:F<path>...]
=head1 OPTIONS
=over 12
=item B<--help>
Print a brief help message and exits.
=item B<--man>
Prints the manual page and exits.
=item B<--verbose>
Prints names of paths and the inspector used.
=item B<--debug>
Prints extracted info.
=item B<--suffix>
Suffix appended to each input filename to form output filename.
Default value: F<.metadata>
=back
=head1 DESCRIPTION
B<This program> will inspect files,
extract their copyright and licensing info,
and save the result next to the files.
File paths are provided either as arguments
or (if no arguments provided) from STDIN.
Each path may optionally be prefixed with an inspector to use.
Default is to pick inspector based on file suffix.
=head1 INSPECTORS
Available inspectors are B<ttf> and B<exif>.
=over 12
=item B<ttf>
TrueType fonts (including Truetype-flavored OpenType and WOFF).
Used by default for extensions F<.ttf>, F<.otf>, F<woff>, F<woff2>.
Beware that some OpenType fonts are not TrueType but Type1,
which may fail to parse correctly based on suffix detection.
If that happens, try force using the exif inspector
by prefixing the path with "exif:".
=item B<ttc>
TrueType collections (including Truetype-flavored OpenType).
Used by default for extension F<.ttc>.
If parsing fails, try force using the exif inspector
by prefixing the path with "exif:".
=item B<exif>
Misc. images, fonts, and other data files.
Used by default for extensions
F<.afm>, F<aif>, F<aifc>, F<aiff>, F<avi>, F<dcm>, F<dfont>, F<doc>,
F<docx>, F<eps>, F<epub>, F<exe>, F<flac>, F<flv>, F<gif>, F<icc>, F<icm>,
F<jpeg>, F<jpg>, F<mov>, F<mp3>, F<mp4>, F<mpeg>, F<mpg>, F<odp>,
F<ods>, F<odt>, F<oga>, F<ogg>, F<ogv>, F<pdf>, F<pfa>, F<pfb>, F<pfm>,
F<png>, F<ppt>, F<pptx>, F<ps>, F<psd>, F<ra>, F<svg>, F<swf>, F<tif>,
F<tiff>, F<wav>, F<webm>, F<webp>, F<xcf>, F<xls>, F<xlsx>.
Beware that some OpenType fonts are not TrueType but Type1,
which may fail to parse correctly based on suffix detection.
If that happens, try force using the exif inspector
by prefixing the path with "exif:".
=back
=head1 ENVIRONMENT VARIABLES
=over 12
=item B<LICENSE_MINER_SUFFIX>
Sets the option B<suffix>, if not provided as commandline argument.
=back
=cut
# avoid custom configuration of ExifTool
BEGIN { $Image::ExifTool::configFile = '' }
my $suffix = $ENV{'LICENSE_MINER_SUFFIX'} || ".metadata";
GetOptions( help => \my $help,
man => \my $man,
verbose => \my $verbose,
debug => \my $debug,
suffix => \$suffix,
) or pod2usage(2);
pod2usage( -verbose => 1 ) if $help;
pod2usage( -verbose => 2, -exitstatus => 0 ) if $man;
# Fail if no paths provided as arguments and STDIN is interactive
pod2usage("$0: No paths provided.") if ((@ARGV == 0) && (-t STDIN));
my $dispatch = {
# TrueType (including Truetype-flavored OpenType and WOFF) fonts
'^(ttf:.*|.*\.(?:ttf|otf|woff2?))$' => sub {
my $file = shift;
$file =~ s/^ttf://;
$file = check_infile($file);
say "ttf: $file" if ($verbose);
my $handle = ($debug)
? *STDOUT{IO}
: FileHandle->new( check_outfile($file), '> :encoding(UTF-8)' );
# source: http://scripts.sil.org/IWS-Chapter08#3054f18b
my %table = (
Copyright => 0,
Trademark => 7,
License => 13,
'License URL' => 14,
);
my $font = Font::TTF::Font->open($file) or do {
say STDERR "ERROR: Failed to parse file as TrueType font: $_";
exit 1;
};
my $fn = $font->{'name'}->read;
foreach (sort keys %table) {
my $value = $fn->find_name($table{$_});
print $handle $_ . ": " . $value . "\n"
if ($value);
}
},
# TrueType (including Truetype-flavored OpenType) collections
'^(ttc:.*|.*\.(?:ttc))$' => sub {
my $file = shift;
$file =~ s/^ttf://;
$file = check_infile($file);
say "ttf: $file" if ($verbose);
my $handle = ($debug)
? *STDOUT{IO}
: FileHandle->new( check_outfile($file), '> :encoding(UTF-8)' );
# source: http://scripts.sil.org/IWS-Chapter08#3054f18b
my %table = (
Copyright => 0,
Trademark => 7,
License => 13,
'License URL' => 14,
);
my $collection = Font::TTF::Ttc->open($file) or do {
say STDERR "ERROR: Failed to parse file as TrueType collection: $_";
exit 1;
};
foreach ( @{$collection->{'directs'}} ) {
my $fn = $_->{'name'}->read;
foreach (sort keys %table) {
my $value = $fn->find_name($table{$_});
print $handle $_ . ": " . $value . "\n"
if ($value);
}
}
},
# exif: misc. images and fonts
'^(exif:.*|.*\.(?:afm|aif|aifc|aiff|avi|dcm|dfont|doc|docx|eps|epub|exe|flac|flv|gif|icc|icm|jpeg|jpg|mov|mp3|mp4|mpeg|mpg|odp|ods|odt|oga|ogg|ogv|pdf|pfa|pfb|pfm|png|ppt|pptx|ps|psd|ra|svg|swf|tif|tiff|wav|webm|webp|xcf|xls|xlsx))$' => sub {
my $file = shift;
$file =~ s/^exif://;
$file = check_infile($file);
say "exif: $file" if ($verbose);
my $exifTool = new Image::ExifTool;
my $handle = ($debug)
? *STDOUT{IO}
: FileHandle->new( check_outfile($file), '> :encoding(UTF-8)' );
my $info = $exifTool->ImageInfo($file, qw[
*Agreement* *Artist* *Author* *Certificate*
*Comment* *Company* *Contact* *Copyright*
*Creator* *Credit* *Disclaimer* *Institution*
*IPTCDigest* *Legal* *Licens* *Modified*
*Notice* *Organization* *Permissions* *Perms*
*Restrictions* *Rights* *Statement* *Terms*
*Trademark*
]);
my $seen;
print $handle "File: $file\n";
foreach (sort keys %$info) {
my $tagdesc = $exifTool->GetDescription($_);
print $handle "$tagdesc: $$info{$_}\n";
}
}
};
my $re = Regexp::Assemble->new( track => 1 )->add( keys %$dispatch );
while( <> ) {
chomp;
if( $re->match($_) ) {
$dispatch->{ $re->matched }( $re->mvar(1) );
}
else {
say STDERR "ERROR: Unsupported or unparseable string: $_";
say STDERR " maybe you need a prefix (e.g. \"exif:fonts/SomeType1Font\"";
exit 1;
}
}
sub check_infile {
my $infile = shift;
unless ( -e $infile ) {
say STDERR "ERROR: file does not exist: $infile";
exit 1;
}
return $infile;
}
sub check_outfile {
my $infile = shift;
my $outfile = $infile . $suffix;
if ( -e $outfile ) {
say STDERR "ERROR: dumpfile exist: $outfile";
say STDERR " remove or put aside and try again";
exit 1;
}
return $outfile;
}
=head1 AUTHOR
Jonas Smedegaard, C<< <dr@jones.dk> >>
=head1 LICENSE AND COPYRIGHT
Copyright 2014-2017 Jonas Smedegaard
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 3, 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/>.
=cut
|