would anyone like to translate the following perl script to Python or
Scheme (scsh)?
the file takes a inpath, and report all html files in it above certain
size. (counting inline images)
also print a sorted report of html files and their size.
(a copy of the script is here:
)
Xah
xah@xahlee.org
∑ http://xahlee.org/
# perl
# Tue Oct 4 14:36:48 PDT 2005
# given a dir, report all html file's size. (counting inline images)
# XahLee.org
use Data::Dumper;
use File::Find;
use File::Basename;
$inpath = '/Users/t/web/mydirectory/';
$sizeLimit = 800 * 1000;
# $inpath = $ARGV[0]; # should give a full path; else the
$File::Find::di r won't give full path.
while ($inpath =~ m@^(.+)/$@) { $inpath = $1;} # get rid of trailing
slash
die "dir $inpath doesn't exist! $!" unless -e $inpath;
############### ############### ############### #####
# subroutines
# getInlineImg($f ile_full_path) returns a array that is a list of
inline images. For example, it may return ('xx.jpg','../image.png')
sub getInlineImg ($) { $full_file_name = $_[0];
@linx =(); open (FF, "<$full_file_na me") or die "error: can not open
$full_file_name $!";
while (<FF>) { @txt_segs = split(m/img/, $_); shift @txt_segs;
for $lin (@txt_segs) { if ($lin =~ m@ src\s*=\s*\"([^\"]+)\"@i) {
push @linx, $1; }}
} close FF;
return @linx;
}
# linkFullPath($d ir,$locallink) returns a string that is the full path
to the local link. For example,
linkFullPath('/Users/t/public_html/a/b', '../image/t.png') returns
'Users/t/public_html/a/image/t.png'. The returned result will not
contain double slash or '../' string.
sub linkFullPath($$ ){ $result=$_[0] . $_[1]; while ($result =~
s@\/\/@\/@) {}; while ($result =~ s@/[^\/]+\/\.\.@@) {}; return
$result;}
# listLocalLinks( $html_file_full _path) returns a array where each
element is a full path of local links in the html.
sub listLocalLinks( $) {
my $htmlfile= $_[0];
my ($name, $dir, $suffix) = fileparse($html file, ('\.html') );
my @aa = getlinks($htmlf ile);
@aa = grep(!m/\#/, @aa);
@aa = grep (!m/^mailto:/, @aa);
@aa = grep (!m/^http:/, @aa);
my @linkedFiles=() ;
foreach my $lix (@aa) { push @linkedFiles, linkFullPath($d ir,$lix);}
return @linkedFiles;
}
# listInlineImg($ html_file_full_ path) returns a array where each
element is a full path to inline images in the html.
sub listInlineImg($ ) {
my $htmlfile= $_[0];
my ($name, $dir, $suffix) = fileparse($html file, ('\.html') );
my @aa = getInlineImg($h tmlfile);
my @result=();
foreach my $ele (@aa) { push @result, linkFullPath($d ir,$ele);}
return @result;
}
############### ############### ############### #####
sub checkLink {
if (
-T $File::Find::na me
&& $File::Find::na me =~ m@\.html$@
) {
$total= -s $File::Find::na me;
@h2 = listInlineImg($ File::Find::nam e);
for my $ln (@h2) {$total += -s $ln;};
if ( $total > $sizeLimit) {print "problem: file:
$File::Find::na me, size: $total\n";}
push (@result, [$total, $File::Find::na me]);
};
}
find(\&checkLin k, $inpath);
@result = sort { $b->[0] <=> $a->[0]} @result;
print Dumper(\@result );
print "done reporting. (any file above size are printed above.)";
__END__
Scheme (scsh)?
the file takes a inpath, and report all html files in it above certain
size. (counting inline images)
also print a sorted report of html files and their size.
(a copy of the script is here:
)
Xah
xah@xahlee.org
∑ http://xahlee.org/
# perl
# Tue Oct 4 14:36:48 PDT 2005
# given a dir, report all html file's size. (counting inline images)
# XahLee.org
use Data::Dumper;
use File::Find;
use File::Basename;
$inpath = '/Users/t/web/mydirectory/';
$sizeLimit = 800 * 1000;
# $inpath = $ARGV[0]; # should give a full path; else the
$File::Find::di r won't give full path.
while ($inpath =~ m@^(.+)/$@) { $inpath = $1;} # get rid of trailing
slash
die "dir $inpath doesn't exist! $!" unless -e $inpath;
############### ############### ############### #####
# subroutines
# getInlineImg($f ile_full_path) returns a array that is a list of
inline images. For example, it may return ('xx.jpg','../image.png')
sub getInlineImg ($) { $full_file_name = $_[0];
@linx =(); open (FF, "<$full_file_na me") or die "error: can not open
$full_file_name $!";
while (<FF>) { @txt_segs = split(m/img/, $_); shift @txt_segs;
for $lin (@txt_segs) { if ($lin =~ m@ src\s*=\s*\"([^\"]+)\"@i) {
push @linx, $1; }}
} close FF;
return @linx;
}
# linkFullPath($d ir,$locallink) returns a string that is the full path
to the local link. For example,
linkFullPath('/Users/t/public_html/a/b', '../image/t.png') returns
'Users/t/public_html/a/image/t.png'. The returned result will not
contain double slash or '../' string.
sub linkFullPath($$ ){ $result=$_[0] . $_[1]; while ($result =~
s@\/\/@\/@) {}; while ($result =~ s@/[^\/]+\/\.\.@@) {}; return
$result;}
# listLocalLinks( $html_file_full _path) returns a array where each
element is a full path of local links in the html.
sub listLocalLinks( $) {
my $htmlfile= $_[0];
my ($name, $dir, $suffix) = fileparse($html file, ('\.html') );
my @aa = getlinks($htmlf ile);
@aa = grep(!m/\#/, @aa);
@aa = grep (!m/^mailto:/, @aa);
@aa = grep (!m/^http:/, @aa);
my @linkedFiles=() ;
foreach my $lix (@aa) { push @linkedFiles, linkFullPath($d ir,$lix);}
return @linkedFiles;
}
# listInlineImg($ html_file_full_ path) returns a array where each
element is a full path to inline images in the html.
sub listInlineImg($ ) {
my $htmlfile= $_[0];
my ($name, $dir, $suffix) = fileparse($html file, ('\.html') );
my @aa = getInlineImg($h tmlfile);
my @result=();
foreach my $ele (@aa) { push @result, linkFullPath($d ir,$ele);}
return @result;
}
############### ############### ############### #####
sub checkLink {
if (
-T $File::Find::na me
&& $File::Find::na me =~ m@\.html$@
) {
$total= -s $File::Find::na me;
@h2 = listInlineImg($ File::Find::nam e);
for my $ln (@h2) {$total += -s $ln;};
if ( $total > $sizeLimit) {print "problem: file:
$File::Find::na me, size: $total\n";}
push (@result, [$total, $File::Find::na me]);
};
}
find(\&checkLin k, $inpath);
@result = sort { $b->[0] <=> $a->[0]} @result;
print Dumper(\@result );
print "done reporting. (any file above size are printed above.)";
__END__
Comment