gnu@perl has asked for the wisdom of the Perl Monks concerning the following question:
The program is working fine and it does most things that I want, but it's not 'pretty'. I am to the point where I can do most anything I want/need to in perl, but not always in the best/most effecient way.
So anyway, here's the code, fire at will:
use strict; use Archive::Zip qw( :ERROR_CODES :CONSTANTS ); use Net::FTP; use File::Basename; use Getopt::Long; # Vars my $progname = $0; $progname =~ s/^.*\///; my $version = "1.01"; my $DEBUG = 0; $|++; my $file; my $ftp_host = 'ftp.uslec.net'; my $ftp_user = 'cheese'; my $ftp_pass = 'whiz'; my $log_method = 'both'; # both, email or file my $def_email = 'someone@somewhere.com'; my $emails; my $LOGFILE = "/u20/home/gvc/log/$progname.log"; my $ALT_LOGF; my @LOG_MSGS; # helpful and generic info go to log only. my $set_error = 0; # only send an email if something goes wrong. # this will get set in the event of an error # and an email with the output of this program + is sent. # to the unix group. GetOptions("send=s" => \$log_method, "email=s" => \$emails, "logfile=s" => \$ALT_LOGF, "ftpserver=s" => \$ftp_host, "file=s" => \$file, "user=s" => \$ftp_user, "pass=s" => \$ftp_pass, "version" => \&display_version, "help" => \&show_help, ); if ( ( ($log_method eq 'both') and (! defined $emails) ) or ( ($log_method eq 'both') and (! defined $ALT_LOGF) ) ) { print "Both --email and --logfile must be set when 'both'\n", "is selected as the notification method with --send\n", "NOTE: 'both' is the default if --send is not set on\n" +, "the command line\n"; print "$progname --send <both|email|file>\n"; exit; } if (! defined $file) { print "You did not specify a file to compress and send\n", "Specify a file with --file <full path to file>\n"; exit; } ############### # MAIN $LOG_MSGS[0] = "-----------------------------------------\n"; $LOG_MSGS[1] = scalar localtime()."\n"; # date and time are firs +t in message. my $dest_size = 0; my $zip_size = make_zip("$file"); if ($zip_size > 0) { $dest_size = ftp_file("$file.ZIP") ; if ($dest_size != $zip_size) { push(@LOG_MSGS, "FTP Error, dest file incorrect size\nRetrying F +TP.\n"); $set_error = 1; $dest_size = ftp_file("$file.ZIP"); if ( $dest_size != $zip_size) { push(@LOG_MSGS,( "Second FTP attempt of $file.ZIP failed!\n", "This is your only notice! No further\n", "ftp attempts will be tried with this file!\n\n" +)); $set_error = 1; } } else #file transferred correctly { unlink "$file"; unlink "$file.ZIP"; } } else { push(@LOG_MSGS,"Error on zip of $file!\n"); $set_error = 1; } push(@LOG_MSGS,"SUCCESS!!\n") if (!$set_error); # Send the output to the log file log_output($ALT_LOGF) if ( ($log_method eq 'file') or ($log_method eq 'both') ); # Send the output to the email address specified on the command line. email_output($emails) if ( ($log_method eq 'email') or ($log_method eq 'both') ); ####################################################### # ADMINISTRATIVE ALERTS # ####################################################### # this will log to gvc's log directory no matter what. # kind of a fallback for us to check on things. log_output($LOGFILE); # emails me if any errors, this should always be the # last alert so we can even catch alert errors. email_output($def_email) if ( $set_error ); ####################################################### ############### # Subs sub make_zip { my $big_file = shift; my $zip = Archive::Zip->new(); if (-r $big_file and -f $big_file) { push(@LOG_MSGS,"Zipping: $big_file\n"); $zip->addFile($big_file); my $zip_status = $zip->writeToFileNamed("$big_file.ZIP"); if ( $zip_status != AZ_OK ) { push(@LOG_MSGS,"ZIP Error on file: $big_file\n"); $set_error = 1; return -1; } else { return -s "$big_file.ZIP"; # returns the size of the zip f +ile }; } else { push(@LOG_MSGS,( "Cannot read file: $big_file\n", "File $big_file will not be processed!\n")); $set_error = 1; return -2; } } sub ftp_file { my $ftp_file = shift; push(@LOG_MSGS,"FTP-ing $ftp_file\n"); my $ftp = Net::FTP->new($ftp_host); $ftp->login($ftp_user,$ftp_pass) if defined $ftp; $ftp->type("I") if defined $ftp; # ZIP file is a binary file. $ftp->put($ftp_file) if defined $ftp; my $return = $ftp->size(basename($ftp_file)) if defined $ftp; $ftp->quit() if defined $ftp; $return = -1 if not defined $return; # if for some reason it # cannot find the remote # file. return $return; } sub email_output { my $local_emails = shift; open(MAIL,"|/usr/bin/mailx -s \"SLINC FTP PROCESSES\" $local_email +s"); print MAIL @LOG_MSGS; close(MAIL); } sub log_output { my $log_file = shift; my $open_stat = open(LOG,">>$log_file"); if (!defined $open_stat) { push(@LOG_MSGS,"Problem opening log file $log_file: $!\n") if +($!); warn "Problem opening log file $log_file: $!\n"; $set_error = 1; return; } print LOG @LOG_MSGS; close(LOG); } sub display_version { print "$progname version $version\n"; print "Written by Chad M. Johnson\n"; print "12/02/2002\n"; exit; } sub show_help { print "$progname --send <both|email|file> --email <someone\@somewh +ere.com> --logfile </path/to/file.log> --file </path/to/file/to/compr +ess/and/send>\n"; print "\n< and > are just for clarity in this example.\n", "Do not use them when entering options.\n"; exit; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Compress and FTP files
by pg (Canon) on Dec 04, 2002 at 16:34 UTC | |
by gnu@perl (Pilgrim) on Dec 04, 2002 at 16:48 UTC | |
by paulbort (Hermit) on Dec 04, 2002 at 19:35 UTC | |
by gnu@perl (Pilgrim) on Dec 05, 2002 at 13:48 UTC | |
|
Configuration File
by Wally Hartshorn (Hermit) on Dec 05, 2002 at 20:30 UTC | |
by gnu@perl (Pilgrim) on Dec 06, 2002 at 16:44 UTC |