Blame SOURCES/fatunpack

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