Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^3: wait command syntax

by nefigah (Monk)
on Mar 20, 2008 at 00:14 UTC ( #675133=note: print w/ replies, xml ) Need Help??


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

I'm sure the python script probably does other stuff than what you mentioned, but if the directory not being made is an issue... I mean, couldn't you always just make it in Perl?

Just my 2 cents... it's usually easier to not have to try and daisy chain different stuff together if it is possible to do it all with Tool X


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


Comment on Re^3: wait command syntax
Re: wait command syntax
by jperlq (Acolyte) on Mar 20, 2008 at 00:39 UTC
    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')

      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.

      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.

      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; }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://675133]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (13)
As of 2014-08-27 22:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (253 votes), past polls