Skip to content

Commit

Permalink
Item9765: Fix shebang line(s) of perl script(s) in bin or tools direc…
Browse files Browse the repository at this point in the history
…tories; script got kind of refactored and was also tested under Windows

git-svn-id: http://svn.foswiki.org/trunk@9522 0b4bb1d4-4e5a-0410-9cc4-b2b747904278
  • Loading branch information
IngoKappler authored and IngoKappler committed Oct 6, 2010
1 parent 91211ee commit b167778
Showing 1 changed file with 105 additions and 41 deletions.
146 changes: 105 additions & 41 deletions core/tools/rewriteshbang.pl
@@ -1,68 +1,132 @@
#! perl -w
#!/usr/bin/perl -w
# See bottom of file for license and copyright information
use strict;
use warnings;
use Cwd;

print "\nRunning Perl $]\n";

print <<'END';
Change the "shebang" lines of all perl scripts found in the current
directory.
"shebang" lines tell the shell what interpreter to use for running
scripts. By default the Foswiki bin scripts are set to user the
"/usr/bin/perl" interpreter, which is where perl lives on most
UNIX-like platforms. On some platforms you will need to change this line
to run a different interpreter e.g. "D:\indigoperl\bin\perl"
or "/usr/bin/speedy"
Change the "shebang" lines of all perl scripts found either in the current
directory or as configured in @directories. Default is to change:
../tools and ../bin directories. You will be asked to confirm accordingly.
"shebang" lines tell the shell what interpreter to use for running scripts.
By default the Foswiki bin scripts are set to use the "/usr/bin/perl"
interpreter, which is where perl lives on most UNIX-like platforms. On some
platforms you will need to change this line to run a different interpreter
e.g. "D:\indigoperl\bin\perl" or "/usr/bin/speedy"
This script will change the "shebang" lines of all scripts found in
the directory where the script is run from.
This script will change the "shebang" lines of all scripts found in the
confirmed directories except for itself.
Note: the path to the interpreter *must not* contain any spaces.
END

my $new = $^X;
my $new_path = $^X;
my @directories = ( '../tools', '../bin', );
my $not_just_cwd = '';
my $os = $^O;
$/ = "\n";

while (1) {
print "Enter path to interpreter [hit enter to choose '$new']: ";
print "Enter path to interpreter [hit enter to choose '$new_path']: ";
my $n = <>;
chomp $n;
last if( !$n );
$new = $n;
};
last if ( !$n ); # exit if $n undefined or 0
$new_path = $n;
}

unless( -x $new ) {
print "Warning: I could not find an executable at $new
Are you sure you want to use this path (y/n)? ";
unless ( -x "$new_path" ) {
print "Warning: I could not find an executable at \"$new_path\"
Are you sure you want to use this path (y/n)?: ";
my $n = <>;
die "Aborted" unless $n =~ /^y/i;
}

my $changed = 0;
my $scanned = 0;
opendir(D, ".") || die $!;
foreach my $file (grep { -f && /^\w+$/ } readdir D) {
$scanned++;
$/ = undef;
open(F, '<', $file) || die $!;
my $contents = <F>;
close F;

if( $contents =~ s/^#!\s*\S+/#!$new/s ) {
my $mode = (stat($file))[2];
chmod( oct(600), "$file");
open(F, '>', $file) || die $!;
print F $contents;
while (1) {
print
"\n\"No\" will only change \"shebang\" lines of scripts found in the directory
where the script is run from. Are you sure you want to change scripts
in the ";
print scalar(@directories);
print " directories:\n";
foreach (@directories) {
print "$_\n";
}
print "(y/n)?: ";
$not_just_cwd = <>;
chomp $not_just_cwd;
last if ( $not_just_cwd =~ /^[yn]/i );
}

if ( $not_just_cwd =~ /^y/i ) {
foreach (@directories) {
chdir "$_" || die "Can't change to: $!";
opendir( D, "." ) || die "Can't open: $!";
&change_files;
}
}
else {
opendir( D, "." ) || die "Can't open: $!";
&change_files;
}

print "\n";

#############
# Subroutines

sub change_files {

my $cwd = getcwd;
my $scanned = 0;
my $changed = 0;

# Grep relevant files to process while only including .pl, .cgi or .?cgi
# Keep .bak and this script excluded.
# ^\w+$ matches only alphanumeric characters between beginning and end
# ^\w+\.pl$ alphanumeric, a dot and pl between beginning and end
# ^\w+\..?cgi$ alphanumeric, a dot, 0 or 1 single char. and cgi between...

my @files =
grep ( -f && /(^\w+$|^(?!rewriteshbang)\w+\.pl$|^\w+\..?cgi$)/,
readdir(D) );
closedir(D);

print "\nModified files:\n";

foreach my $file (@files) {
$scanned++;
$/ = undef;
open( F, "<$file" ) || die $!;
my $contents = <F>;
close F;
chmod( $mode, "$file");
print "$file modified\n";
$changed++;
} else {
print "$file modified\n";

if ( $contents =~ s/^#!\s*\S+/#!$new_path/s ) {
my $mode = ( stat("$file") )[2];
chmod( oct(600), "$file" );
open( F, ">$file" ) || die $!;
print F $contents;
close F;
chmod( $mode, "$file" );
print "$cwd/$file\n";
$changed++;
}
else {
print "$cwd/$file\n";
}
}
print "$changed of $scanned files changed\n";
}

if ( $os =~ /^MSWin/ ) {
print "\nFinished, closing in 10 seconds, so Windows users may not have
the window closed immediately.\n";
sleep(10);
}
closedir(D);
print "$changed of $scanned files changed\n";
exit 0
__END__
Foswiki - The Free and Open Source Wiki, http://foswiki.org/
Expand Down

0 comments on commit b167778

Please sign in to comment.