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

Thank you for that insight.

the script called `python content2.py`

when ran from the terminal, clobers and then creates a directory called /cachedata

but when this script is ran inside the perl script,

sub print_content { `python content2.py`; print header; print start_html(-title=>'Strain entry', -style=>{'src'=>'/Attempt_2.css', 'type'=>'text/css'}); start_form(-action=>'forensics.cgi?button=pushed'); print table({-'align'=>'left',-'border'=>'1', -'bordercolor'=>'CCC +CCC', -'width'=>'45%', -'cellspacing'=>'0', -'cellpadding'=>'0'}); opendir CACHE_DIR, $cache_dir or die "Cannot open $cache_dir: $!"; my $file; foreach $file (readdir CACHE_DIR) { my $current_file = $file; next if ($file =~ /^\./); $file = "peregrine.local/cgi-bin/final/$cache_dir/$file"; print Tr(td({-'bgcolor'=>'#D4E4F0'}, "<a href=http://$file>$cu +rrent_file</a>")) ."\n"; } }

the /cachedata is indeed clobered, but the new directory is not written?
any insight as to why?
my first guess was that the perl script was not waiting for the python script to finish.

Replies are listed 'Best First'.
Re^3: wait command syntax
by ikegami (Patriarch) on Mar 20, 2008 at 01:39 UTC

    This kind of problem from a CGI script yells out to pay attention to the current directory. It's often "/" instead of the directory in which the script resides. If that's the problem, adding the following will do the trick.

    use File::Spec::Functions qw( rel2abs ); use File::Basename qw( dirname ); BEGIN { chdir(dirname(rel2abs($0))); }

    Update: Oops, typed basename instead of rel2abs. Fixed.

      You forgot to use the rel2abs function.
      $0 or __FILE__
        What about them?
Re^3: wait command syntax
by MonkE (Hermit) on Mar 19, 2008 at 23:56 UTC
    If you're running the python script from a perl CGI script, then it may be a permissions problem. The CGI script will run as a different user than when you run it in a terminal.
Re^3: wait command syntax
by nefigah (Monk) on Mar 20, 2008 at 00:14 UTC

    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.

      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')
        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.

        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.

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