SVN: toys/rsget.pl/RSGet/Curl.pm

sparky sparky at pld-linux.org
Sat Jun 26 23:56:53 CEST 2010


Author: sparky
Date: Sat Jun 26 23:56:52 2010
New Revision: 11597

Modified:
   toys/rsget.pl/RSGet/Curl.pm
Log:
- added postdownload callback support - shell code executed after finished
  download


Modified: toys/rsget.pl/RSGet/Curl.pm
==============================================================================
--- toys/rsget.pl/RSGet/Curl.pm	(original)
+++ toys/rsget.pl/RSGet/Curl.pm	Sat Jun 26 23:56:52 2010
@@ -50,6 +50,10 @@
 		type => "PATH",
 		user => 1,
 	},
+	postdownload => {
+		desc => "Command executed after finishing download.",
+		type => "COMMAND",
+	},
 );
 
 
@@ -467,16 +471,17 @@
 	}
 
 	if ( $supercurl->{file} ) {
-		rename_done: {
+		my $outfile;
+		do_rename: {
 			my $infile = $supercurl->{filepath};
-			my $outfile = filepath( setting("outdir"), $get_obj->{_opts}->{dir}, $supercurl->{fname} );
+			$outfile = filepath( setting("outdir"), $get_obj->{_opts}->{dir}, $supercurl->{fname} );
 			if ( -e $outfile ) {
 				my @si = stat $infile;
 				my @so = stat $outfile;
 				if ( $si[0] == $so[0] and $si[1] == $so[1] ) {
 					p "$infile and $outfile are the same file, not renaming"
 						if verbose( 2 );
-					last rename_done;
+					last do_rename;
 				}
 
 				my $out_rename = file_backup( $outfile, "done" );
@@ -499,6 +504,15 @@
 			$supercurl->{fname},
 			bignum( $supercurl->{size_got} ),
 			bignum( $supercurl->{size_total} );
+
+		if ( my $post = setting( "postdownload" ) ) {
+			callback( $post,
+				file => $outfile,
+				name => $supercurl->{fname},
+				size => $supercurl->{size_total},
+				source => $get_obj->{_uri},
+			);
+		}
 	} else {
 		$get_obj->{body} = $supercurl->{ $supercurl->{headonly} ? "head" : "body" };
 	}
@@ -618,6 +632,28 @@
 	return bignum( $supercurl->{size_got} ) . "; ${speed}KB/s $eta";
 }
 
+sub callback
+{
+	my $hook = shift;
+	my %opts = @_;
+
+	$hook =~ s/(\@{([a-z]*)})/$opts{ $2 } || $1/eg;
+
+	my $pid = fork;
+	unless ( defined $pid ) {
+		warn "Fork failed\n";
+	}
+	if ( $pid ) {
+		p "Executing '$hook'\n" if verbose( 1 );
+	} else {
+		close STDIN;
+		close STDOUT;
+		close STDERR;
+		exec $hook;
+		die "Exec failed: $@\n";
+	}
+}
+
 
 1;
 


More information about the pld-cvs-commit mailing list