Created
September 4, 2009 21:56
-
-
Save stubbetje/181172 to your computer and use it in GitHub Desktop.
autorun.php: run a command on file modifications
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env php | |
<?php | |
/* original version can be found at https://gist.github.com/181172 */ | |
if( ! function_exists( 'inotify_init' ) ) { | |
die_with_error( 'This script needs the inotify module' ); | |
} | |
$excludePatterns = array( | |
'^\..*\.swp$' , | |
'^.*~$' , | |
'^.git$' , | |
); | |
// usage: $0 path [path ...] -- command | |
$args = $_SERVER['argv']; | |
array_shift( $args ); | |
$toWatch = array(); | |
while( false !== ( $arg = array_shift( $args ) ) ) { | |
if( $arg === '--' ) { | |
break; | |
} else { | |
if( ! file_exists( $arg ) ) { | |
die_with_error( 'Cannot watch "%s", file or directory doest not exist' , $arg ); | |
} | |
if( ! isExcludedFile( $arg , $excludePatterns ) ) { | |
$toWatch[] = $arg; | |
echo 'will be watching: ' , $arg , PHP_EOL; | |
} | |
} | |
} | |
$command = implode( ' ' , array_map( 'escapeshellarg' , $args ) ); | |
if( false === ( $fd = inotify_init() ) ) { | |
die_with_error( 'inotify_init() failed' ); | |
} | |
$countFile = $countDir = 0; | |
foreach( $toWatch as $watchThis ) { | |
//echo 'watching: ' , $watchThis , PHP_EOL; | |
if( ! isExcludedFile( $watchThis , $excludePatterns ) ) { | |
inotify_add_watch( $fd , $watchThis , IN_MODIFY ); | |
} | |
if( is_dir( $watchThis ) ) { | |
$countDir++; | |
foreach( new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $watchThis ) , RecursiveIteratorIterator::SELF_FIRST ) as $it ) { | |
if( $it->isDir() && ! isExcludedFile( $it->getPathName() , $excludePatterns ) ) { | |
//echo 'watching: ' , $it->getPathname() , PHP_EOL; | |
inotify_add_watch( $fd , $it->getPathname() , IN_MODIFY ); | |
$countDir++; | |
} | |
} | |
} else { | |
$countFile++; | |
} | |
} | |
echo 'watching ' , ( $countDir ? $countDir . ' directories' : null ) , ( $countFile ? ( $countDir ? ' and ' : null ) . $countFile . ' files' : null ) , PHP_EOL; | |
echo 'waiting for changes...' , PHP_EOL; | |
while( false !== ( $events = inotify_read( $fd ) ) ) { | |
foreach( $events as $event ) { | |
$executedCommand = false; | |
if( ! isExcludedFile( $event['name'] , $excludePatterns ) ) { | |
// echo '[' , translate_inotify_constant( $event['mask'] ) , '] ' , $event['name'] , ' : ' , json_encode( $event ) , PHP_EOL; | |
// echo 'executing: ' , $command , PHP_EOL; | |
logPrint( '$[blue]File modified :$[normal] $[yellow]' . $event['name'] . '$[normal]' ); | |
logPrint( '$[blue]Executing command:$[normal] $[yellow]' . $command . '$[normal]' ); | |
system( $command ); | |
$executedCommand = true; | |
} | |
/* eat up events that may have been triggered by the command */ | |
while( inotify_queue_len( $fd ) > 0 ) { | |
$events = inotify_read( $fd ); | |
} | |
} | |
if( $executedCommand ) { | |
logPrint( '$[blue]waiting for changes...' ); | |
} | |
} | |
fclose( $fd ); | |
function die_with_error( $msg ) | |
{ | |
$args = func_get_args(); | |
array_shift( $args ); | |
vprintf( $msg . PHP_EOL , $args ); | |
exit -1; | |
} | |
function translate_inotify_constant( $value ) | |
{ | |
static $const = null; | |
if( null === $const ) { | |
$const = get_defined_constants( true ); | |
$const = array_flip( $const['inotify'] ); | |
} | |
return array_key_exists( $value , $const ) ? $const[ $value ] : $value; | |
} | |
function isExcludedFile( $filename , $excludePatterns ) | |
{ | |
$name = basename( $filename ); | |
foreach( $excludePatterns as $pattern ) { | |
$pattern = '#' . str_replace( '#' , '\#' , $pattern ) . '#'; | |
if( preg_match( $pattern , $name ) ) { | |
return true; | |
} | |
} | |
return false; | |
} | |
function logPrint( $message ) | |
{ | |
$replace = array( | |
'$[blue]' => chr(033) . '[0;30;44m' , | |
'$[normal]' => chr(033) . '[0m' , | |
'$[yellow]' => chr(033) . '[33;40m' , | |
'$[red]' => chr(033) . '[31m' , | |
); | |
echo str_replace( array_keys( $replace ) , array_values( $replace ) , $message ) , PHP_EOL; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment