~ overflow ~

Tag: cli

SVN folder cleaning (svn wipe)

by z3n on Dec.04, 2010, under Coding

Problem:

You did a svn checkout on this nice project but now you don’t want that .svn folders all over the place, after all you won’t be commiting it. Suddenly you find out that there’s thousands of folders with thousands of files.

Solution:

I wrote this little php cli script to fix this:

<?php

// (c) z3n - R1V1@101203 - www.overflow.biz - rodrigo.orph@gmail.com

function _o($msg) {
	echo "\r" . substr($msg,0, 79) . str_repeat(" ", 79 - strlen($msg));
}

function rddir($dir) { /* v2.17 */
	global $tfile,$tdir;

	if (is_dir($dir)) {
		if ($dh = opendir($dir)) {
			while (($file = readdir($dh)) !== false) {
				if ($file != "." && $file != "..") {
					if (is_dir($dir . $file))
						$tdir[] = $dir . $file;
					else
						$tfile[] = $file;
				}
			}
			closedir($dh);
		}
	}
}

if (!isset($argv[1]))
	die("Usage: " . $_SERVER['PHP_SELF'] . " <path/to/svn_root/>");

// browse dirs searching for .svn

for (
	$tdir = array($argv[1]),
	$tfile = array(),
	$svn = array(),
	$i = 0;$i < count($tdir);$i++)
{
	rddir($tdir[$i]);
	_o("Reading: ".$tdir[$i]." ...");

	foreach ($tfile as $entry) {
		if ($entry != "." && $entry != "..") {
			$fn = $tdir[$i] . (PHP_OS == "WINNT" ? "\\" : "/") . $entry;
			if ($entry == ".svn" && !in_array($fn, $svn))
				$svn[] = $fn;

			if (is_dir($fn) && $entry != ".svn")
				$tdir[] = $fn;
		}
	}

	$tfile = array();
}

_o("Found: " . count($svn) . " .svn folders, collecting files...");

$files = array();
$dirs = array();
$t = 0;

// collect files from .svn folders

foreach ($svn as $dir) {
	for ($tdir = array($dir), $tfile = array(),$i = 0;$i < count($tdir);$i++) {
		rddir($tdir[$i]);
		_o("[" . $t . "] - Collecting: ". $tdir[$i] ."...");

		foreach ($tfile as $entry) {
			if ($entry != "." && $entry != "..") {
				$fn = $tdir[$i] . (PHP_OS == "WINNT" ? "\\" : "/") . $entry;
				$t++;

				if (is_dir($fn)) {
					$tdir[] = $fn;
					$dirs[] = $fn;
				} else {
					$files[] = $fn;
				}
			}
		}
	}
}

$i = count($files) + count($dirs) + count($svn);

// delete files

foreach ($files as $file) {
	_o("[" . $i . "] Deleting Files: " . $file);
	unlink($file);
	$i--;
}

// delete subfolders

rsort($dirs);

foreach ($dirs as $dir) {
	_o("[" . $i . "] Deleting Dir: " . $dir);
	rmdir($dir);
	$i--;
}

// delete root .svn folders

foreach ($svn as $dir) {
	_o("[" . $i . "] Deleting Roots: " . $dir);
	rmdir($dir);
	$i--;
}

echo "\nDone!";

?>
Leave a Comment :, , , more...

Display MySQL tables detailed information

by z3n on Jun.18, 2009, under Coding, Tips & Hints

Problem:

How to display detailed information about each table on a database?


Solution:

show table status;


Source:

MySQL

Leave a Comment :, , , , more...

md5 File Comparsion PHP Script

by z3n on Jun.05, 2009, under Coding, Tips & Hints

Problem:

I had a bunch of files but i didn’t knew which ones were doubles and which ones were unique, they had different names and belong to different subfolders.

Solution:

A PHP script that reads and do a md5 hash on every file then do a comparsion.

<?
  1.  
  2. // (c) z3n – R1V1@090605 – z3n666@gmail.com – www.overflow.biz
  3.  
  4. define("is_debug",1);
  5. define("_dbp","_debug.log");
  6. define('_bf',512288); // debug buffer
  7. define('_dw',250); // maximum debug file size, _bf*_dw bytes
  8. define('_cr',chr(13).chr(10)); // CRLF definition
  9. define('_dt_sml','Y-m-d h:i:s'); // date small format (for debug)
  10. ini_set("memory_limit","128M"); // avoid happyness
  11.  
  12. // functions
  13.  
  14. function _o($x) { if (is_debug) { _dbg($x); } echo $x."\n"; }
  15. function _fw($fn,$txt,$m) { /* v1.5 with auto mkdir */ global $__fw,$__fwb;$__fw++;$__fwb+=strlen($txt);$x=1;$fn=str_replace("\\","/",$fn);if (strpos($fn,"/") !== false) { $p=substr($fn,0,strrpos($fn,"/"));if (!is_dir($p)) { _o("Mkdir: ".$p);mkdir($p,777,true); } } if ((is_writable($fn)) || (!file_exists($fn))) { if (!$fp=fopen($fn,$m)) { $x=0;break; } if (!fwrite($fp,$txt)) { $x=0;break; } fclose($fp); } _o("Wrote: ".$fn);return $x; }
  16. function _dbg($x,$f=0) { /* v1.9 */ global $_bg,$_dw;if ((is_debug) &amp;&amp; ($_dw &lt; _dw)) { if ($x != "") { $_bg.=date(_dt_sml).":".$x._cr; } if ((strlen($_bg) &gt; _bf) || ($f)) { $x=$_bg;$_dw++;$_bg="";_fw(_dbp,$x,"a"); } } }
  17. function rddir($dir) { /* v2.11 */
  18. global $tfile,$tdir,$ij;$i=0;$j=count($tdir);
  19. if (is_dir($dir)) {
  20. if ($dh=opendir($dir)) {
  21. while (($file=readdir($dh)) !== false) {
  22. if (is_dir($dir.$file)) {
  23. if (($file != ".") &amp;&amp; ($file != "..")) {
  24. $tdir[$j]=$dir.$file;$j++;
  25. } } else { $tfile[$i]=$file;$i++;
  26. } } closedir($dh); } } $ij=$i; }
  27.  
  28. // main
  29.  
  30. $_bg="";$_dw=$__fw=$__fwb=0;$_x=array();$_f=array();
  31.  
  32. if (!isset($argv[1])) { _o("Usage: ".$_SERVER['PHP_SELF']." &lt;path/to/&gt;");die; }
  33.  
  34. for ($tdir[0]=$argv[1],$k=$i=0,rddir($tdir[$i]);$i&lt;count($tdir);$i++) {
  35.  $tdir[$i].="/";
  36.  for ($j=0,$m=count($tfile),_o("Found: ".$m." files @ ".$tdir[$i]."\nBuilding md5 hash…");$j&lt;$m;$j++) {
  37.   if (($tfile[$j] != ".") &amp;&amp; ($tfile[$j] != "..")) {
  38.    $h=md5(file_get_contents($tdir[$i].$tfile[$j]));
  39.    $_x[$k]=$h;
  40.    $_f[$k]=$tdir[$i].$tfile[$j];
  41.    $x=array_keys($_x,$h);
  42.    for ($o=0,$p=count($x);$o&lt;$p;$o++) {
  43.     if ($x[$o] != $k) {
  44.      _o($_f[$o]." = ".$_f[$k]." (".$h." – ".$j.")");
  45.     }
  46.    }
  47.    $k++;
  48.   }
  49.  }
  50. }
  51.  
  52. _dbg("",1);_o("Done!");
  53.  
  54. ?&gt;
Leave a Comment :, , , , more...

Testing email server with telnet

by z3n on Apr.28, 2009, under Tips & Hints

I keep forgeting about this so i decided to write a post.

Problem:

An easy way to test a pop3 email server.

Solution:

telnet mail.server.com 110

user username

pass yourpassword

list to list messages

stat to show status of your mailbox

retr # to show the message

dele # to delete a message

Source:

Mike Tech

Leave a Comment :, , , , more...

JS and CSS Packer in PHP

by z3n on Apr.03, 2009, under Coding

Problem:

No decent css/js packers in php

Solution:

Source Here

This CLI based script will compress the input files detecting their formats and placing on a defined folder, easying the automation of multiple files compression.

have fun!

1 Comment :, , , , more...

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!