Blame SOURCES/fatunpack

bd98d1
#!/usr/bin/perl
bd98d1
use strict;
bd98d1
use warnings;
bd98d1
use File::Path;
bd98d1
use File::Spec;
bd98d1
use Getopt::Long;
bd98d1
bd98d1
my $libdir = 'lib';
bd98d1
my $filter = '';
bd98d1
bd98d1
GetOptions('libdir=s' => \$libdir, 'filter=s' => \$filter) or
bd98d1
    die "Could not parse arguments\n";
bd98d1
if ($filter eq '') {
bd98d1
    # Empty pattern passes previous result by definition. Do not use it.
bd98d1
    # Interpolared compilation is fixed in perl 5.18.0. RT#119095.
bd98d1
    $filter = qr/(?:)/;
bd98d1
}
bd98d1
eval { $filter = qr{$filter}; 1} or
bd98d1
    die "Could not compile filter as a regular expression: $@\n";
bd98d1
bd98d1
my ($file, $filename, $delimiter);
bd98d1
while (<>) {
bd98d1
    if (/^\$fatpacked\{\s*"([^"]*)"\s*\}\s*=.*<<\s*'([^']*)'\s*;/) {
bd98d1
        # Packed module beginning found
bd98d1
        $filename = $1;
bd98d1
        $delimiter = $2;
bd98d1
        if ($filename =~ $filter) {
bd98d1
            print STDERR "Extracting `$filename'\n";
bd98d1
            my $directory = (File::Spec->splitpath($filename))[1];
bd98d1
            File::Path::make_path(File::Spec->catfile($libdir, $directory));
bd98d1
            if ($file) {
bd98d1
                die "Unballanced fat-packed module at line $.\n";
bd98d1
            }
bd98d1
            open($file, '>', File::Spec->catfile($libdir, $filename)) or
bd98d1
                die "Could not create `",
bd98d1
                    File::Spec->catfile($libdir, $filename), "': $!\n";
bd98d1
        } else {
bd98d1
            print STDERR "Removing `$filename'\n";
bd98d1
        }
bd98d1
    } elsif (defined $delimiter and /^\Q$delimiter\E$/) {
bd98d1
        # Packed module end found
bd98d1
        if (defined $file) {
bd98d1
            close($file) or
bd98d1
                die "Could not close `",
bd98d1
                    File::Spec->catfile($libdir, $filename), "': $!\n";
bd98d1
            $file = undef;
bd98d1
        }
bd98d1
        $filename = undef;
bd98d1
        $delimiter = undef;
bd98d1
    } elsif (defined $file) {
bd98d1
        # Packed module to extract
bd98d1
        s/^  //;    # de-escape recursive here-documents
bd98d1
        print $file $_;
bd98d1
    } elsif (! defined $delimiter) {
bd98d1
        # Rest of code to output
bd98d1
        print STDOUT $_;
bd98d1
    }
bd98d1
}
bd98d1
bd98d1
__END__
bd98d1
bd98d1
=encoding utf8
bd98d1
bd98d1
=head1 NAME
bd98d1
bd98d1
fatunpack - Unpacker for App::FatPacker packets
bd98d1
bd98d1
=head1 SYNOPSYS
bd98d1
bd98d1
fatunpack [OPTION…] [PACKED_SCRIPT…]
bd98d1
bd98d1
=head1 DESCRIPTION
bd98d1
bd98d1
This tool unpacks scripts packed with App::FatPacker.
bd98d1
bd98d1
Packed script's file names are specified as positional arguments. If no
bd98d1
argument is given, a script from standard intput will be processed.
bd98d1
bd98d1
The content of packed script stripped of all bundled modules is written to
bd98d1
standard output.
bd98d1
bd98d1
=head1 OPTIONS
bd98d1
bd98d1
=over 8
bd98d1
bd98d1
=item B<--libdir DIRECTORY>
bd98d1
bd98d1
Directory to output unpacked modules to that where bundled into the input
bd98d1
script. Default value is C<lib>.
bd98d1
bd98d1
=item B<--filter REGULAR_EXPRESSION>
bd98d1
bd98d1
Save only modules whose file name matches the B<REGULAR_EXPRESSION>. The file
bd98d1
names are compared without B<--libdir> prefix. The expession is not anchored
bd98d1
by default. Empty expression matches any file name. Default value is empty
bd98d1
regular expression, i.e. to save all modules.
bd98d1
bd98d1
=back
bd98d1
bd98d1
=head1 VERSION
bd98d1
bd98d1
This is version 2.
bd98d1
bd98d1
=head1 COPYRIGHT
bd98d1
bd98d1
Copyright © 2013, 2014  Petr Písař <ppisar@redhat.com>.
bd98d1
bd98d1
=head1 LICENSE
bd98d1
bd98d1
This is free software.  You may redistribute copies of it under the terms of
bd98d1
the GNU General Public License L<http://www.gnu.org/licenses/gpl.html>.
bd98d1
There is NO WARRANTY, to the extent permitted by law.
bd98d1
bd98d1
=cut