| 1 |
package PerlReq::Utils; |
| 2 |
|
| 3 |
=head1 NAME |
| 4 |
|
| 5 |
PerlReq::Utils - auxiliary routines for L<B::PerlReq>, L<perl.req> and L<perl.prov> |
| 6 |
|
| 7 |
=head1 DESCRIPTION |
| 8 |
|
| 9 |
This module provides the following convenience functions: |
| 10 |
|
| 11 |
=over |
| 12 |
|
| 13 |
=cut |
| 14 |
|
| 15 |
require Exporter; |
| 16 |
@ISA = qw(Exporter); |
| 17 |
@EXPORT = qw(argv explode inc path2mod mod2path path2dep mod2dep sv_version verf verf_perl); |
| 18 |
|
| 19 |
use strict; |
| 20 |
|
| 21 |
=item B<path2mod> |
| 22 |
|
| 23 |
Convert file path to module name, e.g. I<File/Find.pm> -> I<File::Find>. |
| 24 |
|
| 25 |
=cut |
| 26 |
|
| 27 |
sub path2mod ($) { |
| 28 |
local $_ = shift; |
| 29 |
s/\//::/g; |
| 30 |
s/\.pm$//; |
| 31 |
return $_; |
| 32 |
} |
| 33 |
|
| 34 |
=item B<mod2path> |
| 35 |
|
| 36 |
Convert module name to file path, e.g. I<File::Find> -> I<File/Find.pm>. |
| 37 |
|
| 38 |
=cut |
| 39 |
|
| 40 |
sub mod2path ($) { |
| 41 |
local $_ = shift; |
| 42 |
s/::/\//g; |
| 43 |
return $_ . ".pm"; |
| 44 |
} |
| 45 |
|
| 46 |
=item B<path2dep> |
| 47 |
|
| 48 |
Convert file path to conventional dependency name, |
| 49 |
e.g. I<File/Find.pm> -> I<perl(File/Find.pm)>. |
| 50 |
Note that this differs from RedHat conventional form I<perl(File::Find)>. |
| 51 |
|
| 52 |
=cut |
| 53 |
|
| 54 |
sub path2dep ($) { |
| 55 |
my $path = shift; |
| 56 |
return "perl($path)"; |
| 57 |
} |
| 58 |
|
| 59 |
=item B<mod2dep> |
| 60 |
|
| 61 |
Convert module name to conventional dependency name, |
| 62 |
e.g. I<File::Find> -> I<perl(File/Find.pm)>. |
| 63 |
Note that this differs from RedHat conventional form I<perl(File::Find)>. |
| 64 |
|
| 65 |
=cut |
| 66 |
|
| 67 |
sub mod2dep ($) { |
| 68 |
my $mod = shift; |
| 69 |
return path2dep(mod2path($mod)); |
| 70 |
} |
| 71 |
|
| 72 |
=item B<verf> |
| 73 |
|
| 74 |
Format module version number, e.g. I<2.12> -> I<2.120>. Currently |
| 75 |
truncated to 3 digits after decimal point, except for all zeroes, e.g. |
| 76 |
I<2.000> -> I<2.0>. |
| 77 |
|
| 78 |
Update. The algorithm has been amended in almost compatible way |
| 79 |
so that versions do not lose precision when truncated. Now we allow |
| 80 |
one more I<.ddd> series at the end, but I<.000> is still truncated |
| 81 |
by default, e.g. I<2.123> -> I<2.123>, I<2.123456> -> I<2.123.456>. |
| 82 |
|
| 83 |
=cut |
| 84 |
|
| 85 |
sub verf ($) { |
| 86 |
my $v = shift; |
| 87 |
$v = sprintf "%.6f", $v; |
| 88 |
$v =~ s/[.]000000$/.0/ || |
| 89 |
$v =~ s/000$// || |
| 90 |
$v =~ s/(\d\d\d)$/.$1/ && $v =~ s/[.]000[.]/.0./; |
| 91 |
return $v; |
| 92 |
} |
| 93 |
|
| 94 |
=item B<verf_perl> |
| 95 |
|
| 96 |
Format Perl version number, e.g. I<5.005_03> -> I<1:5.5.30>. |
| 97 |
|
| 98 |
=cut |
| 99 |
|
| 100 |
sub verf_perl ($) { |
| 101 |
my $v = shift; |
| 102 |
my $major = int($v); |
| 103 |
my $minor = ($v * 1000) % ($major * 1000); |
| 104 |
my $micro = ($v * 1000 * 1000) % ($minor * 1000 + $major * 1000 * 1000); |
| 105 |
return "1:$major.$minor.$micro"; |
| 106 |
} |
| 107 |
|
| 108 |
=item B<sv_version> |
| 109 |
|
| 110 |
Extract version number from B::SV object. v-strings converted to floats |
| 111 |
according to Perl rules, e.g. I<1.2.3> -> I<1.002003>. |
| 112 |
|
| 113 |
=cut |
| 114 |
|
| 115 |
sub sv_version ($) { |
| 116 |
my $sv = shift; |
| 117 |
if ($$sv == ${B::sv_yes()}) { |
| 118 |
# very special case: (0==0) -> 1 |
| 119 |
return 1; |
| 120 |
} |
| 121 |
if ($sv->can("FLAGS")) { |
| 122 |
use B qw(SVf_IOK SVf_NOK); |
| 123 |
if ($sv->FLAGS & SVf_IOK) { |
| 124 |
return $sv->int_value; |
| 125 |
} |
| 126 |
if ($sv->FLAGS & SVf_NOK) { |
| 127 |
return $sv->NV; |
| 128 |
} |
| 129 |
} |
| 130 |
if ($sv->can("MAGIC")) { |
| 131 |
for (my $mg = $sv->MAGIC; $mg; $mg = $mg->MOREMAGIC) { |
| 132 |
next if $mg->TYPE ne "V"; |
| 133 |
my @v = $mg->PTR =~ /(\d+)/g; |
| 134 |
return $v[0] + $v[1] / 1000 + $v[2] / 1000 / 1000; |
| 135 |
} |
| 136 |
} |
| 137 |
# handle version objects |
| 138 |
my $vobj = ${$sv->object_2svref}; |
| 139 |
my $vnum; |
| 140 |
if (ref($vobj) eq "version") { |
| 141 |
$vnum = $vobj->numify; |
| 142 |
$vnum =~ s/_//g; |
| 143 |
return 0 + $vnum; |
| 144 |
} |
| 145 |
elsif ($sv->can("PV") and $sv->PV =~ /^[v.]?\d/) { |
| 146 |
# upgrade quoted-string version to version object |
| 147 |
require version; |
| 148 |
$vobj = eval { version->parse($sv->PV) }; |
| 149 |
if ($@) { |
| 150 |
warn $@; |
| 151 |
return undef; |
| 152 |
} |
| 153 |
$vnum = $vobj->numify; |
| 154 |
$vnum =~ s/_//g; |
| 155 |
return 0 + $vnum; |
| 156 |
} |
| 157 |
return undef; |
| 158 |
} |
| 159 |
|
| 160 |
=item B<argv> |
| 161 |
|
| 162 |
Obtain a list of files passed on the command line. When command line |
| 163 |
is empty, obtain a list of files from standard input, one file per line. |
| 164 |
Die when file list is empty. Check that each file exists, or die |
| 165 |
otherwise. Canonicalize each filename with C<File::Spec::rel2abs()> |
| 166 |
function (which makes no checks against the filesystem). |
| 167 |
|
| 168 |
=cut |
| 169 |
|
| 170 |
use File::Spec::Functions qw(rel2abs); |
| 171 |
sub argv { |
| 172 |
my @f = @ARGV ? @ARGV : grep length, map { chomp; $_ } <>; |
| 173 |
die "$0: no files\n" unless @f; |
| 174 |
return map { -f $_ ? rel2abs($_) : die "$0: $_: $!\n" } @f; |
| 175 |
} |
| 176 |
|
| 177 |
=item B<inc> |
| 178 |
|
| 179 |
Obtain a list of Perl library paths from C<@INC> variable, except for |
| 180 |
current directory. The RPM_PERL_LIB_PATH environment variable, if set, |
| 181 |
is treated as a list of paths, seprarated by colons; put these paths |
| 182 |
in front of the list. Canonicalize each path in the list. |
| 183 |
|
| 184 |
Finally, the RPM_BUILD_ROOT environment variable, if set, is treated as |
| 185 |
installation root directory; each element of the list is then prefixed |
| 186 |
with canonicalized RPM_BUILD_ROOT path and new values are put in front |
| 187 |
of the list. |
| 188 |
|
| 189 |
After all, only existent directories are returned. |
| 190 |
|
| 191 |
=cut |
| 192 |
|
| 193 |
my @inc; |
| 194 |
sub inc { |
| 195 |
return @inc if @inc; |
| 196 |
my $root = $ENV{RPM_BUILD_ROOT}; $root &&= rel2abs($root); |
| 197 |
unshift @inc, map rel2abs($_), grep $_ ne ".", @INC; |
| 198 |
unshift @inc, map rel2abs($_), $ENV{RPM_PERL_LIB_PATH} =~ /([^:\s]+)/g; |
| 199 |
unshift @inc, map "$root$_", @inc if $root; |
| 200 |
return @inc = grep -d, @inc; |
| 201 |
} |
| 202 |
|
| 203 |
=item B<explode> |
| 204 |
|
| 205 |
Split given filename into its prefix (which is a valid Perl library |
| 206 |
path, according to the inc() function above) and basename. Return empty |
| 207 |
list if filename does not match any prefix. |
| 208 |
|
| 209 |
=cut |
| 210 |
|
| 211 |
sub explode ($) { |
| 212 |
my $fname = shift; |
| 213 |
my ($prefix) = sort { length($b) <=> length($a) } |
| 214 |
grep { index($fname, $_) == 0 } inc(); |
| 215 |
return unless $prefix; |
| 216 |
my $delim = substr $fname, length($prefix), 1; |
| 217 |
return unless $delim eq "/"; |
| 218 |
my $basename = substr $fname, length($prefix) + 1; |
| 219 |
return unless $basename; |
| 220 |
return ($prefix, $basename); |
| 221 |
} |
| 222 |
|
| 223 |
1; |
| 224 |
|
| 225 |
__END__ |
| 226 |
|
| 227 |
=back |
| 228 |
|
| 229 |
=head1 AUTHOR |
| 230 |
|
| 231 |
Written by Alexey Tourbin <[email protected]>. |
| 232 |
|
| 233 |
=head1 COPYING |
| 234 |
|
| 235 |
Copyright (c) 2004 Alexey Tourbin, ALT Linux Team. |
| 236 |
|
| 237 |
This is free software; you can redistribute it and/or modify it under |
| 238 |
the terms of the GNU Library General Public License as published by the |
| 239 |
Free Software Foundation; either version 2 of the License, or (at your |
| 240 |
option) any later version. |
| 241 |
|
| 242 |
=head1 SEE ALSO |
| 243 |
|
| 244 |
L<B::PerlReq>, L<perl.req>, L<perl.prov> |