in reply to Re^3: wait command syntax
in thread wait command syntax

The python script mentioned was written by someone other than myself. They use several python tricks -- which may or may-not have equivelents in perl (i am no expert in cracking open binary files)

Below is the python code as mentioned.

#!/usr/bin/env python import sys # sys.argv[1] import os # os.walk(home) import gzip # gzip.open(filename) import commands # commands.getoutput home = "./FFCache/" if sys.argv[1:]: home = sys.argv[1] cachedata = "../../Documents/cachedata/" #commands.getoutput('chmod -R ../../Documents') #if sys.argv[2:]: cachedata = sys.argv[2] #commands.getoutput('rm -R cachedata') #commands.getoutput('rmdir cachedata') #os.mkdir(cachedata) #print "Making directory ", cachedata, "for copying cached files.\n" for root, dirs, files in os.walk(home): for filename in files: if "_CACHE_" in filename: continue content = None try: f = gzip.open(root + filename,"rb") data = f.read() if 'PNG' in data[:10]: content = "png" elif 'GIF89a' in data[:10]: content = "gif" elif 'JFIF' in data[:10]: content = "jpeg" elif 'HTML' in data[:20]: content = "html" elif 'html' in data[:20]: content = "html" else: continue #print "Copying ",filename," -> ", cachedata + filename +" +."+ content #print data[:20] f_out = open(cachedata + filename +"."+ content, "wb") f_out.write(data) #os.chmod(f_out, 0777) except IOError: f = open(root + filename,"rb") data = f.read() if 'PNG' in data[:10]: content = "png" elif 'GIF89a' in data[:10]: content = "gif" elif 'JFIF' in data[:10]: content = "jpeg" elif 'HTML' in data[:20]: content = "html" elif 'html' in data[:20]: content = "html" else: continue #print "Copying ",filename," -> ", cachedata + filename +" +."+ content #print data[:50] f_out = open(cachedata + filename +"." + content, "wb") f_out.write(data) #os.chmod(f_out, 0777) commands.getoutput('chmod -R 777 cachedata')

Replies are listed 'Best First'.
Re^2: wait command syntax
by Corion (Patriarch) on Mar 20, 2008 at 07:00 UTC
    home = "./FFCache/"

    The current directory is not what you think. Change (for example, in your Perl script) the current directory to an absolute, known path.

    Alternatively, from reading the Python program, you can give it the base directory as the first argument, so you might want to try that.

    Also, you might want to replace

    commands.getoutput('chmod -R 777 cachedata') </c>

    with the native Python function calls instead of a (brittle) system call.

Re^2: wait command syntax
by nefigah (Monk) on Mar 20, 2008 at 03:50 UTC

    Yeah, I'm not seeing anything here that looks too crazy, much less unique to Python. I can try my hand at a Perl version tomorrow if nobody cooler does it before then :)


    I'm a peripheral visionary... I can see into the future, but just way off to the side.

Re^2: wait command syntax
by nefigah (Monk) on Mar 20, 2008 at 07:52 UTC

    Alright, so here's my shot at a Perl version of the above. Note that there is quite a bit of commented-out code in the Python version, so I didn't reproduce that.

    #!/usr/bin/env perl use warnings; use strict; use feature ':5.10'; use File::Find; use File::Basename; use IO::Uncompress::AnyUncompress; # Note: if this script isn't being run from the same directory as the # python one was, chdir to it! Or convert everything to absolute paths +. my $home = $ARGV[0] // q{./FFCache/}; # 5.10 idiom my $cachedata = q{../../Documents/cachedata/}; # The python script has some commented out stuff here, so I won't rewr +ite it find( { wanted => \&process_file, no_chdir => 1 }, $home ); sub process_file { my $name = basename( $File::Find::name ); # Needed because we are +running no_chdir return if $name =~ /_CACHE_/; my $content; my $data; my $gzip; if ( $gzip = new IO::Uncompress::AnyUncompress( $File::Find::name + ) ) { my $status = $gzip->read( $data ); return unless $status > 0; } else { open my $file, '<', $File::Find::name or return; $data = do { local $/; <$file> }; close $file; } # This seems like an odd way of doing the following, but it's # how the python script does it, so who am I to argue? if ( substr($data, 0, 10) =~ /PNG/ ) { $content = 'png'; } elsif ( substr($data, 0, 10) =~ /GIF89a/ ) { $content = 'gif'; } elsif ( substr($data, 0, 10) =~ /JFIF/ ) { $content = 'jpeg'; } elsif ( substr($data, 0, 20) =~ /HTML/i ) { $content = 'html'; } else { return; } my $fname = $cachedata . $name . '.' . $content; open my $f_out, '>', $fname or return; print $f_out $data; chmod 0777, $f_out; close $f_out; }