our
$VERSION
=
'0.10'
;
our
@EXPORT_OK
=
qw(
annotate_copyright load_meta license_types
deannotate_copyright howl_notice
write_LICENSE check_license_files
)
;
sub
howl_notice {
my
$msg
=
shift
||
undef
;
$msg
= -T (
$msg
||
''
) ? (slurp
$msg
||
''
,{
err_mode
=>
'quiet'
}) ||
undef
:
$msg
;
$msg
//= default_copyright_notice();
$msg
=~ s/^(?!
$msg
;
}
sub
annotate_copyright {
my
(
$files
,
$msg
) =
@_
;
$msg
//= default_copyright_notice();
return
unless
$msg
;
return
_annotate_copyright(
$files
,
$msg
)
unless
ref
$files
;
my
$i
=0;
_annotate_copyright(
$_
,
$msg
) &&
$i
++
for
@$files
;
$i
;
}
sub
deannotate_copyright {
my
(
$files
,
$msg
) =
@_
;
$msg
//= default_copyright_notice();
return
unless
$msg
;
return
_deannotate_copyright(
$files
,
$msg
)
unless
ref
$files
;
my
$i
=0;
_deannotate_copyright(
$_
,
$msg
) &&
$i
++
for
@$files
;
$i
;
}
sub
load_meta {
my
$base
=
shift
||
return
;
return
$base
if
UNIVERSAL::isa(
$base
,
'CPAN::Meta'
);
return
CPAN::Meta->load_file(
$base
)
if
-T
$base
and -r _ ;
first {
$_
}
map
{ -T and -r and CPAN::Meta->load_file(
$_
) }
map
{
$base
.
"/$_"
}
qw/ META.json META.yml /
;
}
sub
license_types {
qw/
AGPL_3 BSD GFDL_1_3 LGPL_3_0 OpenSSL Sun
Apache_1_1 CC0_1_0 GPL_1 MIT Perl_5 Zlib
Apache_2_0 Custom GPL_2 Mozilla_1_0 PostgreSQL
Artistic_1_0 FreeBSD GPL_3 Mozilla_1_1 QPL_1_0
Artistic_2_0 GFDL_1_2 LGPL_2_1 None SSLeay
/
;
}
sub
write_LICENSE {
my
(
$dir
,
$author
,
$type
) =
@_
;
my
$meta
= load_meta(
$dir
);
$author
//= find_authors(
$meta
);
$type
//= find_license(
$meta
) ||
return
;
my
$lok
= check_META_file(
$dir
);
$lok
or INFO($::o->usage) and INFO(
qq(The "list" command lists available licenses)
) and
return
;
unless
($::opts->{yes}) {
-T
"$dir/LICENSE"
? (prompt
'-yes'
,
'Overide LICENSE?'
) ||
return
: 1;
}
DEBUG
'Adding LICENSE file'
;
open
my
(
$o
),
'>'
,
"$dir/LICENSE"
or
die
$! ;
say
{
$o
}
$lok
->fulltext;
}
sub
check_license_files {
my
$dir
=
shift
||
return
;
check_LICENSE_file(
$dir
);
check_META_file(
$dir
);
}
sub
is_annotated {
my
(
$file
,
$msg
) =
@_
;
$msg
//=
'copyright (c)'
;
my
$contents
= slurp
$file
or
return
;
() =
$contents
=~ /\Q
$msg
/gis ;
}
sub
default_copyright_notice {
my
$geco
=
ucfirst
([
getpwuid
$<]->[6] ||
getlogin
);
my
$year
= 1900 + [
localtime
]->[5];
sprintf
'%s %s, %s'
,
'# Copyright (C)'
,
$year
,
$geco
;
}
sub
_annotate_copyright {
my
(
$file
,
$msg
) =
@_
;
return
unless
-T
$file
;
return
if
is_annotated(
$file
,
$msg
);
my
$perms
= ((
stat
(
$file
))[2]) & 07777 ;
open
my
(
$in
),
'<'
,
$file
or
return
;
unlink
$file
;
open
my
(
$out
),
'>'
,
"$file"
;
chmod
(
$perms
| 0600,
$out
);
print
{
$out
}
scalar
<
$in
>;
print
{
$out
}
$msg
,
"\n"
;
print
{
$out
} <
$in
> ;
}
sub
_deannotate_copyright {
my
(
$file
,
$msg
) =
@_
;
return
unless
-T
$file
;
my
$perms
= ((
stat
(
$file
))[2]) & 07777 ;
my
$content
= slurp
$file
or
return
;
$content
=~ s/\Q
$msg
\E//g ;
open
my
(
$out
),
'>'
,
"$file"
;
chmod
(
$perms
| 0600,
$out
);
print
{
$out
}
$content
;
}
sub
find_authors {
my
$meta
=
shift
||
return
;
$meta
= load_meta(
$meta
) ||
return
;;
my
@authors
=
map
{ s/^\s*|\s*$//o;
$_
}
map
{ s/([^,]+),(.+)/$2 $1/o;
$_
}
map
{ s/\W*<.*>\s*//so;
$_
}
$meta
->author;
my
$h
;
@{
$h
}{
@authors
} = (1)x
@authors
;
join
', '
,
sort
keys
%$h
;
}
sub
find_license {
my
$meta
=
shift
||
return
;
$meta
= load_meta(
$meta
) ||
return
;
my
(
$license
) =
$meta
->license or
return
''
;
ucfirst
$license
;
}
sub
is_license_type {
my
$type
= (
shift
||
return
);
first {
$type
eq
$_
} license_types() or
return
;
}
sub
license_text {
my
(
$type
,
$holder
) =
@_
;
return
unless
$type
||
''
=~ /^\w{2,16}$/o;
$holder
//=
getlogin
;
$type
=
'Software::License::'
.
ucfirst
(
$type
||
return
);
eval
"use $type"
;
return
if
$@;
$type
->new( {
holder
=>
$holder
} );
}
sub
check_LICENSE_file {
my
$dir
=
shift
||
return
;
INFO
'Searching for LICENSE .... '
.
((-T
"$dir/LICENSE"
and -r _ ) ?
'found'
:
'not found'
);
}
sub
check_META_file {
my
$meta
= load_meta(
shift
||
return
) ;
DEBUG
'Searching for META .... '
. (
$meta
?
'found'
:
'not found'
);
return
unless
$meta
;
my
$license
= find_license(
$meta
) or
return
;
DEBUG
' extracting license type .... '
. (
$license
?
$license
:
'NOT found'
);
DEBUG
' license type is valid .... '
. (is_license_type(
$license
) ?
'yes'
:
'no'
);
my
$authors
= find_authors(
$meta
);
DEBUG
' authors .... '
. (
$authors
eq
'unknown'
?
return
:
$authors
);
my
$text
= license_text(
$license
,
$authors
||
'unknown'
);
DEBUG
' license text is available .... '
. (
$text
?
'yes'
:
'no'
);
return
$text
;
}
1;