Blame SOURCES/fatunpack

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