<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/css" href="http://www.theoldmonk.net/blog/styles/feed.css"?>
<rss version="2.0" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:admin="http://webns.net/mvcb/">
<channel>
<title>The Old Monk</title>
<link>http://www.theoldmonk.net/blog/archives/code/</link>
<description>dark matter</description>
<dc:language>en-us</dc:language>
<dc:creator>gera</dc:creator>
<dc:date>2008-03-13T01:39:20+05:30</dc:date>
<admin:generatorAgent rdf:resource="http://nanoblogger.sourceforge.net" />
<item>
<link>http://www.theoldmonk.net/blog/archives/2008/03/08/git-bugzilla_integration/</link>
<title>Git-Bugzilla integration</title>
<dc:date>2008-03-08T19:47:35+05:30</dc:date>
<dc:creator>gera</dc:creator>
<dc:subject>tricks, perl, code, hacks</dc:subject>
<description>
<![CDATA[<p>There's always <a href="http://www.mkgnu.net/?q=scmbug">SCMBug</a>, but you can cook your own Git-Bugzilla integration very easily. SCMBug's fine, but it's a lot of code if all you want is simple cross-linking between Git, Bugzilla and <a href="http://trac.edgewall.org/">Trac</a> (btw - <a href="http://trac-hacks.org/wiki/GitPlugin">GitPlugin</a> for Trac would throw up an error unless you have at least *two* commits in your repository - talk about undocumented easter bugs!).</p>

<p>Anyhoo - here's what I want : <br />
1. Git should disallow any commit where the commit message does not have a bug number. <br />
2. Git should add a comment to the corresponding bug on a commit, mentioning the author, the Trac changeset link, the commit message and the list of files which changed.</p>

<p>And here's the code to do it. First, the post-receive hook :</p>
<pre>
#!/usr/bin/perl -w
use strict;

# A hook script which integrates with bugzilla. It looks for bug IDs in
# commit messages and adds the commit message as well as a link to the
# changeset as a comment on the bug.

# This program is released under the terms of the GNU General Public License
# version 2. A copy of the license may be obtained by emailing the author,
# or at http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
#
# The absolute lack of warranty and other disclaimers as per the license
# apply.
#
# Copyright 2008, Devendra Gera. All rights reserved.
#
# Author : Devendra Gera <gera@theoldmonk.net>

### user configurable section

# The bugzilla has contains the server, username and password for the targeted
# bugzilla installation. There's NO 'http://' in the server line.
my $bugzilla = {
        server          => "10.147.251.90/bugzilla",
        user            => "demonuser",
        password        => "daemonuserspassword",
};

# __PATH__ and __REVISION__ are replaced in $browser->{changeset} and
# $browser->{revision} to get the changeset and revision URLs respectively. 
my $browser = {
        changeset       =>
        "http://10.147.251.90/projname/changeset/__REVISION__",
        revision        =>
        "http://10.147.251.90/projname/browser/__PATH__?rev=__REVISION__",
};

# The bug_regex should extract the bug id from the commit message and place
# it in $1
my $bug_regex = 'bug #(\d+)';


##### End user configurable section

use WWW::Bugzilla;

my $input = <>;
chomp $input;

my ($oldrev, $newrev, $refname) = split /\s+/, $input;
my $commit_msg = `git-whatchanged $oldrev..$newrev`;

# prepare the changeset URL
my $changeset_url = $browser->{ changeset };
$changeset_url =~ s/__REVISION__/$newrev/g;

# author
my ($author) = ( $commit_msg =~ /^Author:\s+(.*)$/m );

# files
my @filelist = grep ( /^:/, split( /\n/, $commit_msg ) );

# prepare comment
$commit_msg =~ s/^.*?Date://s;  # eat everything till the Date: heder
$commit_msg =~ s/^.*?\n//m;     # eat the date line completely
$commit_msg =~ s/^:.*?$//mg;    # eat the file list from the msg.
chomp $commit_msg;
my ($bug_number) = ( $commit_msg =~ /$bug_regex/ );

my $comment = &lt;&lt;END_COMMENT;

------------------------------------
changeset $newrev [ $changeset_url ]
    by $author :

$commit_msg
------------------------------------

Files changed :
END_COMMENT

$comment .= join("", @filelist) . "\n";

my $bz = WWW::Bugzilla->new(
        server          => $bugzilla->{ server },
        email           => $bugzilla->{ user },
        password        => $bugzilla->{ password },
        bug_number      => $bug_number
);

die "cannot connect to bugzilla" unless defined $bz;

$bz->additional_comments( $comment );

$bz->commit;
</pre>

<p>And here's the update hook :</p>
<pre>
#!/usr/bin/perl -w
use strict;

my $refname = shift;
my $oldrev = shift;
my $newrev = shift;

my $commit_msg = `git-whatchanged $oldrev..$newrev`;

# check if the commit message contains a bug number
if($commit_msg !~ /bug #\d+/) {
        exit -1;
}

exit 0;
</pre>

<p>The TODO : <br />
1. Push every configurable thing to git's config file and access it via git-config. <br />
2. Code cleanups. One definition of the bug regex (which is hardcoded in the update for now).</p>

<p><em>edit</em> : the here doc in the code was causing a problem with formatting, eating up some of the code. Fixed now.</p>]]>
</description>
</item>
<item>
<link>http://www.theoldmonk.net/blog/archives/2008/01/29/datemanip_remind_and_remindme/</link>
<title>Date::Manip, remind and remindme</title>
<dc:date>2008-01-29T17:00:44+05:30</dc:date>
<dc:creator>gera</dc:creator>
<dc:subject>tricks, perl, code</dc:subject>
<description>
<![CDATA[<p>I use <a href="http://www.roaringpenguin.com/products/remind">remind</a>(<a href="http://lifehacker.com/software/top/geek-to-live--keep-your-calendar-in-plain-text-with-remind-186661.php">short howto</a>) to keep my calendar of events and reminders. Its a wonderful utility - and all commandline. I split up the birthdays, anniversaries, personally important days, and the usual stuff into different files, and include them in my ~/.reminders file. Here's what my .reminders looks like :</p>

<pre>
gera@gera-laptop:~$ cat .reminders 
include /home/gera/reminders/birthdays
include /home/gera/reminders/anniversaries
include /home/gera/reminders/impdates
include /home/gera/reminders/stuff
</pre>

<p>All this works well, except for a small issue. It takes too much time for me to add reminders for my day-to-day tasks. Firing up an editor and typing all that remind syntax is something that can be avoided. Also, it really hurts to look at the calendar to figure out fuzzy dates like "tomorrow", or "sat". I was planning to write this big Perl script which would do the heavy lifting for me. Instead, I found Date::Manip which already does the heavy lifting. It understands stuff like "tomorrow" and "sun". I just needed a small 'remindme' script then :</p>

<pre>
#!/usr/bin/perl -w
use strict;

use Date::Manip;
use Fcntl qw(:flock);

my $reminders = "$ENV{HOME}/reminders/stuff";

my $argstring = join (" ", @ARGV);
my ($when, $what);

(undef, $when, $what) = ($argstring =~ /^(on |)(.*?) to (.*)$/);
quit("what?") unless $what;
quit("when?") unless $when;

# form the line to be appended
my $date = ParseDate($when);
quit("cannot parse date") unless $date;

my $line = UnixDate($date, "REM %b %e %Y +1 MSG");
$line .= " $what %b.%\n";

# write in file

# open lock file
open LOCK, ">$reminders.lock" or quit("cannot open $reminders.lock : $!");
flock LOCK, LOCK_EX or quit("cannot obtain lock : $!");

# open data file, write and close data file
open REMINDERS, ">>$reminders" or quit("cannot open $reminders : $!");
print REMINDERS $line;
close REMINDERS;

# release locks
flock LOCK, LOCK_UN;
close LOCK;

exit (0);

sub quit
{
        my $msg = shift;
        print STDERR $msg, "\n";
        exit (-1);
}
</pre>

<p>Now, I can use it like this :</p>

<pre>
$ remindme on sat to eat everything I can
$ remindme tomorrow to pay the phone bill
$ remindme next week to think up of something useful to say
$ remindme next thursday to find something better to do
</pre>

<br />
nifty, eh?

<p><em>update</em>: bugfixed the script. An extra space creeped up between the '+' and the '1'. Thanks to <a href="http://www.amitu.com/blog/">AmitU</a> for pointing that out.
<br />
<em>update 2</em>: added the capability to get reminded n days in advance - a feature suggested by AmitU in the comments, but with a different syntax ("remindme next week to do this <em>for 2 days</em>"). Also, remindme's <a href="http://repo.or.cz/w/remindme.git">git repository</a> and <a href="http://www.theoldmonk.net/remindme/">home page</a>. The repository contains the bash-completion script as well.</p>]]>
</description>
</item>
<item>
<link>http://www.theoldmonk.net/blog/archives/2008/01/28/commandline_gtd_with_gtdo/</link>
<title>Commandline GTD with gtdo</title>
<dc:date>2008-01-28T15:17:57+05:30</dc:date>
<dc:creator>gera</dc:creator>
<dc:subject>tricks, perl, code, hacks</dc:subject>
<description>
<![CDATA[I was introduced to <a href="http://en.wikipedia.org/wiki/Getting_Things_Done">GTD</a> around a year ago, but wanted to manage mine using a commandline. I discovered <a href="http://www.todotxt.com/">todotxt.com</a> and modified a <a href="http://www.todotxt.com/download.php?p=%2Flibrary%2Ftodo.sh%2F&f=todo.py">todo.py</a> to suit my needs (removed priorities, added some commands, bash-completion etc).
<br /><br />
But I stopped doing it.
<br /><br />
Because I had nothing to manage my "projects" (tasks with multiple action items). I maintained a list of projects out-of-band, as a directory structure, which required me to identify the next task for a project and move it to my todo list. Of course it didn't work!
<br /><br />
So I wrote gtdo. gtdo has a similar interface - todo.py was the inspiration - but in addition to the contexts (marked by a '@'), it supports groups (or "projects" - marked by a '/'). So, you might have a project about starting using GNUcash. Of course, the first step is to install/set-up GNUcash. Adding your portfolio comes later. If you have both these items in your todo list, the second one does nothing but adds noise and makes the list longer. The answer is to add them in a group /gnucash. If there are multiple tasks in a group, only the first one is displayed - till its marked done. By default, new tasks are added at the end of the queue in a group, but may be added at a specific position.
<br /><br />
An example would be better than the ramble above (gtdo being aliased to 't'):
<pre>
$ t add /gnucash @online install gnucash
$ t add /frames @errands buy wood
$ t add /email @online set up spamassassin
$ t add /gnucash @online add portfolio to gnucash
<br /><br />
# only the immediate next task is displayed when you 'ls'
$ t ls
1 : /gnucash @online install gnucash
2 : /frames @errands buy wood
3 : /email @online set up spamassassin
<br /><br />
$ t ls @online
1 : /gnucash @online install gnucash
3 : /email @online set up spamassassin
<br /><br />
# except when you specifically ask for a group
$ t ls /gnucash
1 : /gnucash @online install gnucash
4 : /gnucash @online add portfolio to gnucash
<br /><br />
# well, we need to fix F::Q::IndiaMutual before we add our portfolio
# we'll try adding that as the second step in the /gnucash group
$ t add /gnucash.2 fix F::Q::IndiaMutual
<br /><br />
$ t ls /gnucash
1 : /gnucash @online install gnucash
4 : /gnucash fix F::Q::IndiaMutual
5 : /gnucash @online add portfolio to gnucash
</pre>
Comes with standard todo.sh style bash-completion. The Git repository is available at <a href="http://repo.or.cz/w/gtdo.git">http://repo.or.cz/w/gtdo.git</a>. You can obtain a tarball by clicking <a href="http://repo.or.cz/w/gtdo.git?a=snapshot;h=HEAD;sf=tgz">here</a>.]]>
</description>
</item>
<item>
<link>http://www.theoldmonk.net/blog/archives/2008/01/23/configd__the_configuration_daemon/</link>
<title>configd : the configuration daemon</title>
<dc:date>2008-01-23T13:31:40+05:30</dc:date>
<dc:creator>gera</dc:creator>
<dc:subject>perl, code</dc:subject>
<description>
<![CDATA[Reading configuration items is a task that almost every application/utility does. Writing code for all those is stupid. Even writing code once and linking it in leaves the language binding issues open. What encouraged me to write this is a frequently invoked piece of (network) code which ideally would check its configuration for each invocation. The disk I/O seemed unnecessary.
<br /><br />
Enter configd - the configuration daemon. Reads configuration items (key=value pairs) for "domains" (applications), and gives them out to anyone who cares to ask over HTTP. Supports HTTP GET/HEAD, with the "domain" and "key" headers, handing out the value as the "value" header in the response. Returns a <a href="http://en.wikipedia.org/wiki/HTTP_404">404 Not Found</a> if it can't find a value, or a <a href="http://en.wikipedia.org/wiki/HTTP_400#4xx_Client_Error">400 Bad Request</a> if the request is malformed. A SIGHUP to configd makes it re-read the (possibly changed) values from the configuration.
<br /><br />
configd is consistent with the <a href="http://en.wikipedia.org/wiki/Unix_philosophy#McIlroy:_A_Quarter_Century_of_Unix">Unix Philosophy</a>. A simple tool that does only one job, and does it well.
<br /><br />
Although configd advertises configurations, it doesn't stop anyone from using it as a trivial key=value "database" (for a stretched definition of the term). It's HTTP - something that's very well understood and supported, and can scale extremely well behind a load balancer, offering caching and remote configuration.
<br /><br />
PUT requests for storing values, and some form of authentication are something that might be added some day, although I don't have the need for them right now.
<br /><br />
<s>The <a href="http://en.wikipedia.org/wiki/Git_%28software%29">Git</a> repository will shortly be online.</s>
<br /><br />
<em>Edit:</em> The <a href="http://repo.or.cz/w/configd.git">configd repository</a> is now online.
<br /><br />
<em>Something that I thought just now</em>: On the trivial database thread, configd can present a consistent interface, once it implements PUT. Reads are cached, load balanced, yadda yadda. And writes are PUT requests. The backend doesn't matter to the application. One can take out the flat files, and replace it with MySQL (with some type checking at the configd/application end), and the application wouldn't know better. A connection can be equated to a transaction. All PUT requests over a connection are rolled as a transaction. How cool is that?
<br /><br />
<em>Also</em>: If you use it as a "database", you cleanly separate the scale layer from the business logic layer. Right now, even prototypes use an SQL backend because people want to scale in the future. There's a lot of DBI/SQL where it doesn't belong. Engineers who write prototypes also write SQL instead of a DBA ("its just a prototype"), which they have to maintain as legacy code/design. All these problems vanish away when you use the simple approach. Engineers don't have to worry about SQL or scale. They can be up and running with a flat file backend which can be changed to SQL without the app noticing. If and when you swap in a real database behind it, SQL queries can be written for the engine by real DBAs (you're scaling this up - its no longer a prototype - it makes sense to assign a DBA for this). To top it all, your *hardware* can do sharding. HTTP load balancers can load balance based on headers - and domains/keys are headers here. The applications need not worry about scaling the database _at_all_.]]>
</description>
</item>
<item>
<link>http://www.theoldmonk.net/blog/archives/2008/01/18/mutt_addressbook_collector/</link>
<title>mutt addressbook collector</title>
<dc:date>2008-01-18T15:13:02+05:30</dc:date>
<dc:creator>gera</dc:creator>
<dc:subject>tricks, perl, code, hacks</dc:subject>
<description>
<![CDATA[I have a tiny helper script to collect addresses from my outgoing emails, and add them to my .aliases file. I only care about people who I send out email to, and I've noticed that collecting addresses from incoming emails leads to too much noise. Although I could set it up to *not* pick up addresses from spam and mails which have my firstname (ecommerce stuff which is NOT spam). But that's for later.

<p />
Here's the helper (I use msmtp for sending out email as evindent in my <a href="http://www.theoldmonk.net/blog/archives/2008/01/16/email_setup/">mutt config</a>) :
<p />
<pre>
#!/usr/bin/perl -w
use strict;

my $home = "/home/gera";
my $msmtp = "/usr/bin/msmtp";
my $aliasfile = "/home/gera/.aliases";

open ALIASES, "<$aliasfile" or quit("cannot open $aliasfile : $!");
my %aliases = ();
my %collected = ();
while(<ALIASES>) {
        s/#.*$//;
        chomp;
        next unless $_;
        my ($nick, $name, $email) = ($_ =~ /^alias\t+(\S+)\t+(.+?)\t+<(\S+)>/);
        quit("cannot parse $_") unless ($nick && $name && $email);
        $aliases{$email} = {
                "nick"  => $nick,
                "name"  => $name,
        };
}
close ALIASES;

# now read the email contents and try to figure out the addresses
local $/ = undef;       # slurp mode

my $email = <STDIN>;
my ($to) = ($email =~ /^To:(.*)$/m);
my ($cc) = ($email =~ /^Cc:(.*)$/m);
my @addresses = split( /,/, "$to,$cc");
foreach my $address (@addresses) {
        chomp;
        next unless $address;
        # the address is of the form of "Firstname Lastname <email@address>"
        # or "<email@address>" or "email@address"
        my ($name, undef, $email) = ($address =~ /^(.*?)\s*(<|)([^<>]*?)(>|)$/);
        quit("cannot parse $address") unless $email;
        next if exists $aliases{$email};
        # else, add it to the aliases.
        # See if everything is populated. We'll have to invent a nickname here
        if(! $name) {
                ($name = $email) =~ s/\@.*//;
        }
        my $nick;
        ($nick = lc $name) =~ tr/ //d;
        $collected{$email} = {
                "nick"  => $nick,
                "name"  => $name,
        };
}

if(scalar(keys %collected) != 0) {
        # save to tmp file
        open ALIASES, ">$aliasfile.new.$$" or quit("cannot open $aliasfile.new.$$ : $!");
        foreach my $email (sort keys %aliases) {
                my $nick = $aliases{$email}->{nick};
                my $name = $aliases{$email}->{name};
                print ALIASES "alias    $nick   $name   <$email>\n";
        }
        print ALIASES "\n\n#collected email addresses\n\n";
        foreach my $email (sort keys %collected) {
                my $nick = $collected{$email}->{nick};
                my $name = $collected{$email}->{name};
                print ALIASES "alias    $nick   $name   <$email>\n";
        }
        close ALIASES;

        # move tmp file to $aliasfile
        rename("$aliasfile.new.$$", $aliasfile);
}

# send mail onward
open(MSMTP, "|$msmtp @ARGV") or quit("cannot open pipe to $msmtp @ARGV : $!");
print MSMTP $email;
close(MSMTP) or quit("cannot close pipe to MSMTP : $!");

exit(0);


sub quit
{
        my $msg = shift;
        open LOGFILE, ">$home/.msmtp_helper.log" || die "cannot open log file : $!";
        print LOGFILE "$msg\n";
        close LOGFILE;
        exit(127);
}
</pre>
<p />
Edit : fixed formatting.]]>
</description>
</item>
<item>
<link>http://www.theoldmonk.net/blog/archives/2008/01/17/financequoteindiamutual_bugfix/</link>
<title>Finance::Quote::IndiaMutual bugfix</title>
<dc:date>2008-01-17T22:25:39+05:30</dc:date>
<dc:creator>gera</dc:creator>
<dc:subject>perl, code, hacks</dc:subject>
<description>
<![CDATA[I used GnuCash for a while and maybe I'll start using it again. A neat thing that one can do with it is to track one's portfolio. Stocks are easy (GnuCash can use Finance::Quote::Yahoo where NSE stocks have symbols ending in '.NS', and BSE stocks are numbers). Mutual funds in India do not have ticker symbols, but prices/NAV etc. can be obtained via the <a href="http://search.cpan.org/~hampton/Finance-Quote-1.13/lib/Finance/Quote/IndiaMutual.pm">Finance::Quote::IndiaMutual</a> module, which fetches them from the <a href="http://amfiindia.com/">AMFI</a> website.
<br /><br />
To use the module, one can grab the code corresponding to the fund from the table at <a href="http://amfiindia.com/downloadnavopen.asp">http://amfiindia.com/downloadnavopen.asp</a> and supply that to the module.
<br /><br />
All that is fine, but there's a tiny regex bug in the module which chokes on many rows. The fix is trivially simple and the diff totals to 
<pre>
74c74
<       my ($symbol, @data) = split /\;/;
---
>       my ($symbol, @data) = split /\s*\;\s*/;
</pre>
I hope that helps someone.]]>
</description>
</item>
</channel>
</rss>
