Thursday, November 26, 2009

Automatic builds with some inotify magic

The inotify feature in the linux kernel allows you to receive events about changed files. Using pyinotify, here is a simple script to watch for changes to source code and run "make". Everything is hard coded right now, and since I was using CMake with out-of-source builds, the directory structure is that way. It also uses the command line notify-send tool to display notifications. Perhaps some day I will make it configurable and more useful.

Cheers.



# Notification tool
# Run with the directory to watch
# currently hardcoded to use
#
# +
# |- src - contains source code and is monitored
# |- build - assumes build to be here
#
# (c) 2009, Nikhil Marathe
# Licensed under the MIT License

import sys
import os
import subprocess
import re
from pyinotify import WatchManager, Notifier, ProcessEvent, IN_MODIFY

wm = WatchManager()

class Compile( ProcessEvent ):
def process_IN_MODIFY( self, event ):
print event.path, event.name, event.mask & IN_MODIFY

if re.match( '[a-zA-Z]*.cpp', event.name ):
os.chdir( event.path.replace( 'src', 'build' ) )
ex = subprocess.call( 'make' )
subprocess.call( ['notify-send', "Build output", "%s" % ( ex == 0 and "Success" or "Error" )] )

c = Compile()
notifier = Notifier( wm, c )
wm.add_watch( sys.argv[1], IN_MODIFY, rec=True )

notifier.loop()


Saturday, November 07, 2009

Chrome: Little UI touches

I forgot to mention that I made a life changing decision change of browser about 2 weeks ago. I've been exclusively on Chrome ever since. I love the minimalist look, the great search features for downloads and the best feature is perhaps Ctrl+PgUp/Dn to switch tabs. Its also very fast on shutdown and startup compared to my firefox, almost Firebug like Developer Tools, really awesome view-source: component ( inspired by kio? ). I love the fact that I can kill some flash/java applet without crashing the browser. So fellow archers go and grab the PKGBUILD

But the really interesting thing I noticed today is this:

Hide the bookmarks toolbar. Open a new tab. In SpeedDial, notice how the bookmarks are shown at the top. Now show the bookmarks toolbar. Voila, bookmarks are hidden.

As for the firefox plugins, well I don't miss any of them yet, except for the Delicious plugin, which I try to compensate for using the bookmarklet.

Monday, November 02, 2009

Macros for Observers

Here are two little macros too make implementing the Observer design pattern less repetitive. This is C++

File: observermacros.h



#ifndef DDM_OBSERVERMACRO_H
#define DDM_OBSERVERMACRO_H

#define MAKE_OBSERVABLE( obscls ) \
private:\
std::list< obscls *> m_observers;\
public:\
void addObserver( obscls *obs ) {\
m_observers.push_back( obs );\
}\
void delObserver( obscls *obs ) {\
m_observers.remove( obs );\
}

#define NOTIFY( obscls, obsmethod, argument ) \
for( std::list::const_iterator it = m_observers.begin();\
it != m_observers.end(); it++ )\
(*it)->obsmethod( argument )

#endif




Now if you want a class to be observable do



#include "observermacros.h"

class NeedsObserving
{
MAKE_OBSERVABLE( Observes );
};



Observes is the name of the class which will observe NeedsObserving.



// add/del observers
NeedsObserving no;
Observes o = new Observes;
no.addObserver( o );
no.delObserver( o );

// To notify observers of changes

void NeedsObserving::didSomething()
{
// CoolThing *coolThing is modified/used here
// ...
NOTIFY( Observes, coolThingDone, coolThing );
// Calls Observes::coolThingDone( CoolThing * );
}


The code is pretty naive, but it might help. Cheers