Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: cae2bde8e4
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 422 lines (329 sloc) 8.926 kb
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 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
# This code is a part of Slash, and is released under the GPL.
# Copyright 1997-2005 by Open Source Technology Group. See README
# and COPYING for more information, or see http://slashcode.com/.

package Slash::Tools;

use strict;
use warnings;

use Carp;
use DB_File;
use Fcntl qw(O_RDWR O_CREAT);
use File::Basename qw(basename dirname);
use File::Find;

use base 'Exporter';

our %config;
our $VERSION = '2.005001';
our @EXPORT = qw(
pmpath pathpm pmpathsrc counterpart srcfile installfile basefile
basefile basename dirname
syntax_check %CONFIG
@BIN_EXT $BIN_EXT $BIN_RE
myprint myexit mysystem myask
);

our @BIN_EXT = qw(gz tgz bz2 gif jpg png ico);
our $BIN_EXT = join '|', @BIN_EXT;
our $BIN_RE = qr/\.(?:$BIN_EXT)$/;

my(%cache);
# if cache gets stale, you can use force => 0, or heck, just
# rm ~/.slash_tools_cache
our %CONFIG = (
source => '/usr/local/src/slash',
install => '/usr/local/slash',
cache => "$ENV{HOME}/.slash_tools_cache",
config => "$ENV{HOME}/.slash_tools_config",
force => 0
);

sub import {
my $proto = shift;
        my $class = ref $proto || $proto;

my %newconfig = @_;
if (open my $fh, '<', $newconfig{config} || $CONFIG{config}) {
while (<$fh>) {
next if /^\W/;
chomp;
my @a = split ' ', $_, 2;
$CONFIG{$a[0]} = $a[1] if $a[0] && $a[1];
}
}

# handle config inputs, which means we are ignoring normal
# import behavior ... you get everything, which is not really
# playing nice, but oh well! -- pudge
%CONFIG = ( %CONFIG, %newconfig );

tie %cache, 'DB_File', $CONFIG{cache}, O_RDWR|O_CREAT, 0644, $DB_HASH
or croak "Can't tie to $CONFIG{cache}: $!";

local $Exporter::ExportLevel = 1;
return $class->SUPER::import;
}


sub syntax_check {
my($file) = @_;
my $check = `$^X -wc \Q$file\E 2>&1`;
if ($check =~ / syntax OK$/) {
undef $@;
return 1;
} else {
$@ = $check;
return 0;
}
}

# return the base file of the requested file (relative to src)
sub basefile {
my($file, $force) = @_;
if ($file !~ /^\Q$CONFIG{source}\E/) {
$file = counterpart($file, $force);
}
$file =~ s/^\Q$CONFIG{source}\/\E//;
return $file;
}


# return the src file of the requested file
sub srcfile {
my($file, $force) = @_;
if ($file !~ /^\Q$CONFIG{source}\E/) {
return counterpart($file, $force);
}
return $file;
}

# return the installed file of the requested file
sub installfile {
my($file, $force) = @_;
if ($file =~ /^\Q$CONFIG{source}\E/) {
return counterpart($file, $force);
}
return $file;
}

sub counterpart {
my($this, $force) = @_;
my $counterpart;

my $key = join $;, 'pathpm', $CONFIG{source}, $CONFIG{install}, $this;
$force = $CONFIG{force} unless defined $force;
return $cache{$key} if !$force && $cache{$key};

if ($this =~ /\.pm$/) {
if ($this =~ /^\Q$CONFIG{source}\E/) {
$counterpart = pmpath(pathpm($this, $force), $force);
} else {
$counterpart = pmpathsrc(pathpm($this, $force), $force);
}
} else {
if ($this =~ /^\Q$CONFIG{source}\E/) {
($counterpart = $this) =~ s/^\Q$CONFIG{source}\E/$CONFIG{install}/;
} else {
($counterpart = $this) =~ s/^\Q$CONFIG{install}\E/$CONFIG{source}/;
}
}

$cache{$key} = $counterpart if $counterpart;

return $counterpart;
}

sub _getpackage {
my($path) = @_;

if (open my $fh, '<', $path) {
my $code = do { local $/; <$fh> };
while ($code =~ /^\s*package\s+([A-Za-z0-9_:]+)\s*;/mg) {
next if $1 eq 'main';
return $1;
}
} else {
carp "Can't open $path: $!";
return;
}

}

# convert a path to a perl module
sub pathpm {
my($path, $force) = @_;
return unless $path;

my $key = join $;, 'pathpm', $path;
$force = $CONFIG{force} unless defined $force;
return $cache{$key} if !$force && $cache{$key};

my $package = _getpackage($path);
$cache{$key} = $package if $package;

return $package;
}

# convert a perl module to a src path
sub pmpathsrc {
my($module, $force) = @_;
return unless defined $module;

my $key = join $;, 'pmpathsrc', $CONFIG{source}, $module;
$force = $CONFIG{force} unless defined $force;
return $cache{$key} if !$force && $cache{$key};

(my $modname = $module) =~ s/^.+::(\w+)$/$1/;
$modname .= '.pm';

my $found;
find(sub {
return if $found;
return unless $_ eq $modname;
my $name = $File::Find::name;

my $package = _getpackage($name);
$cache{$key} = $found = $name if $package && $package eq $module;
}, $CONFIG{source});

return $found;
}

# convert a perl module to an installed path
sub pmpath {
undef $@;
my($module, $force) = @_;
return unless defined $module;

my $key = join $;, 'pmpath', $module;
$force = $CONFIG{force} unless defined $force;
return $cache{$key} if !$force && $cache{$key};

(my $path = $module) =~ s{::}{/}g;
(my $name = $module) =~ s/^.+::(\w+)$/$1/;
my $pathmod = $path . '.pm';

if ($INC{$pathmod}) {
$cache{$key} = $INC{$pathmod};
return $INC{$pathmod};
}

my $return;
eval "require $module";
if ($@) {
# find from perl error
if ($@ =~ m{\s['"]?(\S+/$pathmod)}) {
$return = $1;
# find from shared library error
} elsif ($@ =~ m{\s['"]?(\S+/)auto/$path(/$name)?\.\w+}) {
$return = "$1$pathmod";
}

if (!$return || ! -e $return) {
carp "install path for '$module' not found";
} else {
$cache{$key} = $return;
}
} else {
if ($INC{$pathmod}) {
$cache{$key} = $return = $INC{$pathmod};
} else {
carp "path for '$module' unavailable in %INC";
}
}

return $return;
}

sub myprint {
print join "\n", @_, '' if @_;
}

sub myexit {
print STDERR join "\n", @_, '' if @_;
exit;
}

sub myask {
local $| = 1;
print join "\n", @_, '' if @_;
print "Continue? [Yn] ";
my $ans = <>;
return $ans =~ /^n/i ? 0 : 1;
}

sub mysystem {
#print "@_\n";
system(@_);
}

package Slash::Tools::BBEdit;
use Carp;
use Cwd;
use File::Basename;

sub new {
require Mac::Glue;
bless { glue => Mac::Glue->new('BBEdit') }, __PACKAGE__;
}

sub front {
my($self) = @_;
return $self->{glue}->obj(window => 1)->get;
}

sub frontpath {
my($self) = @_;
return $self->front->prop('file')->get;
}

sub output {
my($self, $output, $opt) = @_;

my $title = $opt->{title} || 'Slash Tools Output';
open my $bbedit, qq'|bbedit --view-top --clean -t "$title"' or carp $!;
print $bbedit $output;
}

sub file {
my($self) = @_;

my $file = $ARGV[0];
unless ($file) {
$file = $self->frontpath;
}

return $file;
}

sub do_prep {
my($self) = @_;

my $file = $self->file;
return unless $file;

my $cwd = cwd();
chdir dirname(Slash::Tools::srcfile($file));

return(basename($file), $cwd);
}

sub do {
my($self, $cmd, $opt) = @_;

my($file, $orig_cwd) = $self->do_prep;
my $basename = basename($file);

my $output = `$cmd \Q$file\E`;
if ($output) {
$self->output($output, { title => "$cmd $basename" });
$self->front->prop('source_language')->set(to => $opt->{type})
if $opt->{type};
$self->front->prop('source_language')->set(to => '(none)')
if $opt->{notype};
}

chdir $orig_cwd;
}

sub gitdiff {
my($self, $args) = @_;

my($file, $orig_cwd) = $self->do_prep;

local $ENV{GIT_EXTERNAL_DIFF} = 'bbdiff-git';
system('git', 'diff', split(' ', $args), '--', $file);

chdir $orig_cwd;
}

sub diff {
my($self, $args, $file) = @_;

$file ||= $self->file;
my $src_file = Slash::Tools::srcfile($file);
my $install_file = Slash::Tools::installfile($file);

if (-f $src_file && -f $install_file) {
system('bbdiff', split(' ', ($args||'')), $install_file, $src_file);
} else {
carp "$src_file or $install_file does not exist";
}
}


package Slash::Tools::Mac;

sub new {
require MacPerl;
shift;
my $self = bless { @_ }, __PACKAGE__;
$self->{creator} ||= 'R*ch';
$self;
}

sub set_type {
my $self = shift;
return unless $self->{creator};

my($file) = @_;
return if $file =~ $BIN_RE;

my($creator, $type) = MacPerl::GetFileInfo($file);
return if $creator && $type && $creator eq $self->{creator} && $type eq 'TEXT';

MacPerl::SetFileInfo($self->{creator}, 'TEXT', $file);
}


1;


__END__

Example config file:

source /usr/local/src/gittest/slash
install /usr/local/slash


Example code:

use Slash::Tools;

# find all files in src that are not installed
use File::Find;
find(sub {
return if /^\./;
my $name = $File::Find::name;
return if $name =~ m{/CVS/} || $name =~ m{/\.};
return unless -f $name;

my $counterpart = counterpart($name) || '';
printf "%s => %s\n", $name, $counterpart
if !$counterpart || !-e $counterpart;
}, $CONFIG{source});


# do syntax checks of modules
for ('Slash::Constants', 'Slash::Utility', 'Slash::Utility::Access',
'Slash::Apache', 'Slash::Apache', 'Slash::Apache::User',
'Slash::SearchToo::KinoSearch', 'Slash',
'Slash::XML', 'Slash::Custom::Bulkmail', 'Slash::XML::OAI') {
syntax_check(pmpath($_)) or warn $@;
}

# counterpart sanity checks:
my $file = pmpath('Slash::XML::Atom');
print "Yay, doesn't match!\n" if $file ne counterpart($file);
print "Yay, matches!\n" if $file eq counterpart(counterpart($file));
Something went wrong with that request. Please try again.