summaryrefslogtreecommitdiff
path: root/gnu/usr.bin/perl/cpan/IO-Compress/private/MakeUtil.pm
blob: aa540c68fda3aec5edf0d9230c3537c48dff5eb1 (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
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
package MakeUtil ;
package main ;

use strict ;

use Config qw(%Config);
use File::Copy;

my $VERSION = '1.0';


BEGIN
{
    eval { require File::Spec::Functions ; File::Spec::Functions->import() } ;
    if ($@)
    {
        *catfile = sub { return "$_[0]/$_[1]" }
    }
}

require VMS::Filespec if $^O eq 'VMS';


unless($ENV{PERL_CORE}) {
    $ENV{PERL_CORE} = 1 if grep { $_ eq 'PERL_CORE=1' } @ARGV;
}

$ENV{SKIP_FOR_CORE} = 1 if $ENV{PERL_CORE} || $ENV{MY_PERL_CORE} ;



sub MY::libscan
{
    my $self = shift;
    my $path = shift;

    return undef
        if $path =~ /(~|\.bak|_bak)$/ ||
           $path =~ /\..*\.sw(o|p)$/  ||
           $path =~ /\B\.svn\b/;

    return $path;
}

sub MY::postamble
{
    return ''
        if $ENV{PERL_CORE} ;

    my @files = getPerlFiles('MANIFEST');

    # Note: Once you remove all the layers of shell/makefile escaping
    # the regular expression below reads
    #
    #    /^\s*local\s*\(\s*\$^W\s*\)/
    #
    my $postamble = '

MyTrebleCheck:
	@echo Checking for $$^W in files: '. "@files" . '
	perl -ne \'						\
	    exit 1 if /^\s*local\s*\(\s*\$$\^W\s*\)/; \'		\
         ' . " @files || " . '				\
	(echo found unexpected $$^W ; exit 1)
	@echo All is ok.

';

    return $postamble;
}

sub getPerlFiles
{
    my @manifests = @_ ;

    my @files = ();

    for my $manifest (@manifests)
    {
        my $prefix = './';

        $prefix = $1
            if $manifest =~ m#^(.*/)#;

        open M, "<$manifest"
            or die "Cannot open '$manifest': $!\n";
        while (<M>)
        {
            chomp ;
            next if /^\s*#/ || /^\s*$/ ;

            s/^\s+//;
            s/\s+$//;

            #next if m#t/Test/More\.pm$# or m#t/Test/Builder\.pm$#;

            /^(\S+)\s*(.*)$/;

            my ($file, $rest) = ($1, $2);

            if ($file =~ /\.(pm|pl|t)$/ and $file !~ /MakeUtil.pm/)
            {
                push @files, "$prefix$file";
            }
            elsif ($rest =~ /perl/i)
            {
                push @files, "$prefix$file";
            }

        }
        close M;
    }

    return @files;
}

sub UpDowngrade
{
    return if defined $ENV{TipTop};

    my @files = @_ ;

    # our and use bytes/utf8 is stable from 5.6.0 onward
    # warnings is stable from 5.6.1 onward

    # Note: this code assumes that each statement it modifies is not
    #       split across multiple lines.


    my $warn_sub = '';
    my $our_sub = '' ;

    my $upgrade ;
    my $downgrade ;
    my $do_downgrade ;

    my $caller = (caller(1))[3] || '';

    if ($caller =~ /downgrade/)
    {
        $downgrade = 1;
    }
    elsif ($caller =~ /upgrade/)
    {
        $upgrade = 1;
    }
    else
    {
        $do_downgrade = 1
            if $] < 5.006001 ;
    }

#    else
#    {
#        my $opt = shift @ARGV || '' ;
#        $upgrade = ($opt =~ /^-upgrade/i);
#        $downgrade = ($opt =~ /^-downgrade/i);
#        push @ARGV, $opt unless $downgrade || $upgrade;
#    }


    if ($downgrade || $do_downgrade) {
        # From: use|no warnings "blah"
        # To:   local ($^W) = 1; # use|no warnings "blah"
        $warn_sub = sub {
                            s/^(\s*)(no\s+warnings)/${1}local (\$^W) = 0; #$2/ ;
                            s/^(\s*)(use\s+warnings)/${1}local (\$^W) = 1; #$2/ ;
                        };
    }
    #elsif ($] >= 5.006001 || $upgrade) {
    elsif ($upgrade) {
        # From: local ($^W) = 1; # use|no warnings "blah"
        # To:   use|no warnings "blah"
        $warn_sub = sub {
            s/^(\s*)local\s*\(\$\^W\)\s*=\s*\d+\s*;\s*#\s*((no|use)\s+warnings.*)/$1$2/ ;
          };
    }

    if ($downgrade || $do_downgrade) {
        $our_sub = sub {
	    if ( /^(\s*)our\s+\(\s*([^)]+\s*)\)/ ) {
                my $indent = $1;
                my $vars = join ' ', split /\s*,\s*/, $2;
                $_ = "${indent}use vars qw($vars);\n";
            }
	    elsif ( /^(\s*)((use|no)\s+(bytes|utf8)\s*;.*)$/)
            {
                $_ = "$1# $2\n";
            }
          };
    }
    #elsif ($] >= 5.006000 || $upgrade) {
    elsif ($upgrade) {
        $our_sub = sub {
	    if ( /^(\s*)use\s+vars\s+qw\((.*?)\)/ ) {
                my $indent = $1;
                my $vars = join ', ', split ' ', $2;
                $_ = "${indent}our ($vars);\n";
            }
	    elsif ( /^(\s*)#\s*((use|no)\s+(bytes|utf8)\s*;.*)$/)
            {
                $_ = "$1$2\n";
            }
          };
    }

    if (! $our_sub && ! $warn_sub) {
        warn "Up/Downgrade not needed.\n";
	if ($upgrade || $downgrade)
          { exit 0 }
        else
          { return }
    }

    foreach (@files) {
        #if (-l $_ )
          { doUpDown($our_sub, $warn_sub, $_) }
          #else
          #{ doUpDownViaCopy($our_sub, $warn_sub, $_) }
    }

    warn "Up/Downgrade complete.\n" ;
    exit 0 if $upgrade || $downgrade;

}


sub doUpDown
{
    my $our_sub = shift;
    my $warn_sub = shift;

    return if -d $_[0];

    local ($^I) = ($^O eq 'VMS') ? "_bak" : ".bak";
    local (@ARGV) = shift;

    while (<>)
    {
        print, last if /^__(END|DATA)__/ ;

        &{ $our_sub }() if $our_sub ;
        &{ $warn_sub }() if $warn_sub ;
        print ;
    }

    return if eof ;

    while (<>)
      { print }
}

sub doUpDownViaCopy
{
    my $our_sub = shift;
    my $warn_sub = shift;
    my $file     = shift ;

    use File::Copy ;

    return if -d $file ;

    my $backup = $file . ($^O eq 'VMS') ? "_bak" : ".bak";

    copy($file, $backup)
        or die "Cannot copy $file to $backup: $!";

    my @keep = ();

    {
        open F, "<$file"
            or die "Cannot open $file: $!\n" ;
        while (<F>)
        {
            if (/^__(END|DATA)__/)
            {
                push @keep, $_;
                last ;
            }

            &{ $our_sub }() if $our_sub ;
            &{ $warn_sub }() if $warn_sub ;
            push @keep, $_;
        }

        if (! eof F)
        {
            while (<F>)
              { push @keep, $_ }
        }
        close F;
    }

    {
        open F, ">$file"
            or die "Cannot open $file: $!\n";
        print F @keep ;
        close F;
    }
}


sub FindBrokenDependencies
{
    my $version = shift ;
    my %thisModule = map { $_ => 1} @_;

    my @modules = qw(
                    IO::Compress::Base
                    IO::Compress::Base::Common
                    IO::Uncompress::Base

                    Compress::Raw::Zlib
                    Compress::Raw::Bzip2

                    IO::Compress::RawDeflate
                    IO::Uncompress::RawInflate
                    IO::Compress::Deflate
                    IO::Uncompress::Inflate
                    IO::Compress::Gzip
                    IO::Compress::Gzip::Constants
                    IO::Uncompress::Gunzip
                    IO::Compress::Zip
                    IO::Uncompress::Unzip

                    IO::Compress::Bzip2
                    IO::Uncompress::Bunzip2

                    IO::Compress::Lzf
                    IO::Uncompress::UnLzf

                    IO::Compress::Lzop
                    IO::Uncompress::UnLzop

                    Compress::Zlib
                    );

    my @broken = ();

    foreach my $module ( grep { ! $thisModule{$_} } @modules)
    {
        my $hasVersion = getInstalledVersion($module);

        # No need to upgrade if the module isn't installed at all
        next
            if ! defined $hasVersion;

        # If already have C::Z version 1, then an upgrade to any of the
        # IO::Compress modules will not break it.
        next
            if $module eq 'Compress::Zlib' && $hasVersion < 2;

        if ($hasVersion < $version)
        {
            push @broken, $module
        }
    }

    return @broken;
}

sub getInstalledVersion
{
    my $module = shift;
    my $version;

    eval " require $module; ";

    if ($@ eq '')
    {
        no strict 'refs';
        $version = ${ $module . "::VERSION" };
        $version = 0
    }

    return $version;
}

package MakeUtil ;

1;