Click to See Complete Forum and Search --> : Source tarball grabber


Moonglobe
11-09-2003, 12:50 AM
this is an untested CLI script designed to grab tarballs, rpms, and zips (.tar.(gz|bz2),.tgz,.rpm,.zip, other suggestions welcome) from one dir and copies them into another. this may sound stupid, but it is rather cool because it includes so much error checking. this is my first CLI script, and there may be problems with my version of read(). i havent tested or commented it yet because i just hacked this up in half an hour at a friends house.

it requires the following to be at /usr/local/lib/php/common.shell.lib.php:<?php
define('N',"\n"); //newline so we dont have to go into double-quotes
define('TARBALL_DIR','/winxp'); //this is where your sources are kept
define('DEST_DIR','/usr/local/src/'); //this is where you want things to go
$sdir = TARBALL_DIR; //set defaults
$ddir = DEST_DIR;
function read($length=255)
{
/*****************************\
* Grabs from stdin up to *
* $length, \n, or EOF. *
\*****************************/
if (!is_int($length) || !$length) //if length is invalid
{
$length = 255; //fall back to default
}
$f = STDIN; //stdin link
if (!$f)
{
die('Error opening STDIN. Dying.');
}
$data = fgets($f,1024); //first, get up to 1K of data
$data = str_split($data); //PHP5-only function same as preg_split with empty delimeter
$outdata = ''; //what we return
for ($i = 1; $i < $length; $i++)
{
$outdata .= str_replace(N, '', $data[$i-1]); //no newlines
}
return str_replace('\n', N, $outdata); // unless you REALLY want them...
} //end read()
function user_abort($message = 'User aborted', $allow_abort = true)
{
/*****************************\
* Echo a dying message, but *
* give the user a choice *
* (disableable) *
\*****************************/
if ($allow_abort)
{
echo 'Die?';
if (strtolower(read(1)) == 'y')
{
die($message.N);
}
else
{
echo 'Continuing script...'.N;
return 0;
}
}
else
{
echo 'Dying...'.N.N;
die($message.N);
}
}
?>
put the main script inside your PATH (eg /usr/local/bin) and you're set to go!

edit: added comments

LordShryku
11-09-2003, 01:08 AM
You forgot .deb's :D

Moonglobe
11-09-2003, 01:29 AM
done :D

Moonglobe
11-09-2003, 07:53 PM
ok here's a third version, which has now been tested rather thoroughly :D

on thing that i thought was cool was the idea of the whole thing being a loop..... but then im sure that's how most good programms work. it was new to me, though, and was fun to use. finally something i once read about the ability to use stuff like 'continue 2;' came in handy :cool:


/usr/local/lib/php/common.shell.lib.php:<?php
define('N',"\n"); //newline so we dont have to go into double-quotes
define('TARBALL_DIR','/winxp'); //this is where your sources are kept
define('DEST_DIR','/usr/local/src/'); //this is where you want things to go
$sdir = TARBALL_DIR; //set defaults
$ddir = DEST_DIR;
function read($length=255)
{
/*****************************\
* Grabs from stdin up to *
* $length, \n, or EOF. *
\*****************************/

$f = fopen('/dev/stdin','r'); //stdin link
if (!$f)
{
die('Error opening STDIN. Dying.');
}
$data = fgets($f,255); //first, get up to .25K of data
$outdata = str_replace(N, '', $data);
return $outdata;
} //end read()
function user_abort($message = 'User aborted', $allow_abort = true)
{
/*****************************\
* Echo a dying message, but *
* give the user a choice *
* (disableable) *
\*****************************/
if ($allow_abort)
{
echo 'Die? ';
if (strtolower(substr(read(),0,1) == 'y'))
{
die($message.N);
}
else
{
echo 'Continuing script...'.N;
return 0;
}
}
else
{
echo 'Dying...'.N.N;
die($message.N);
}
}
?>

/usr/local/bin/gsrc:

Moonglobe
11-10-2003, 01:05 PM
ok, there's now modification time checking, and checking of the source directory before the copy (not only on 'c' in the main switch). a bunch of notification bugs fixed.

btw, i'd appreciate some more feedback, perhaps by telling me how stupid this is? :)

Moonglobe
11-10-2003, 01:26 PM
oops forgot to attach it