Sunday, 22 March 2009

Issue with Ant Copy task

This is something that I faced a few months ago. We had spent a few weeks developing build scripts for our deployment. We were entering the period where we prepared things for our final go live, and one of the suggestions was that we build a deployment script that could replace the current one. The existing one did not create a single tar file that could be extracted, it created two files - one with all the property files for all the services to be deployed and one with the jar files. Extracting these two one after the other was all that was needed, but it was not elegant.

So we asked a developer to merge these two scripts and thought that would be it. On the day we did the deployment to DEV, the services refused to start. Reason - corrupted files. We switched to plan B - used the old scripts and viola! the services start!

We duly fired a failure mail and decided to dissect things the next day. And after half a day when I figured it, I kicked myself.

Ant has a copy task that lets you perform token replacement. This basically lets you prepare files that can vary depending on environment etc and set the values when building the script. This is how it is used


< copy todir='../backup/dir'>
< fileset dir='src_dir'/>
< filterset>
< filter token='TITLE' value='Foo Bar'/>
< /filterset>
< /copy>


Apparently, this task does not differentiate between ASCII and binary files. It does the replacement on ANY file. Our original scripts were separate. So the jar files were copied in a copy task that did not do filtering. When we merged the scripts, we put all the files into a single task.

Once I moved the jar files and the property files into separate tasks, the scripts worked fine.


So it was a simple Ant task that was the issue!!

Sunday, 15 March 2009

Street photography

I was watching a programme called "The Genius of Photography"on BBC today. It gave me an insight into photography back in the old days. I loved all those black and white photos. It told me that I need to look at photography to capture life around me, how things are, how the world looks like. That's a good tip that I can use.

But there is a catch - I cant do street photography without getting into trouble. Just yesterday, I took my camera to work so that I could take a few shots around my office. I took my camera out in the evening and before I could take a picture, a security guard came to me and told me I could not take a picture without a pass. What? I was clicking pictures of a building and I got told off. What if I try to take a few pictures of people on the street? I am sure they will report me or take my film or my memory card. 

The reason why people do that is not important to me. I just am not comfortable with the fact that I cannot just go out there and capture the scenes that I like. I guess I live in a different time now and I might need a license to own a camera one day.

Friday, 13 March 2009

Python Script: Converting strings to camel case

A simple script to convert strings to camelCase


import sys

def processTokens(tokens):
result='';
for token in tokens:
if token is not None:
result=result+token.title()
return result

def processString(string,separator=' '):
li=string.split(separator)
if li is not []:
result=li[0].lower()
result=result+processTokens(li[1:])
return result

def getCamelCase(string,separator=' '):
return processString(string,separator)


if __name__=="__main__":
if sys.argv.__len__()<3:
print 'usage: camelcase [input filename] [output filename]'
else:
f=open(sys.argv[1],'r')
o=open(sys.argv[2],'w')
for line in f:
o.write(getCamelCase(line))
f.close()
o.close()

Tuesday, 10 March 2009

Python Script: Extract strings matching a regex from a file

A simple python script to extract strings matching a regex from a file.


import re
import sys

#initialize with some default pattern
mainpattern = re.compile('.*')


def matchLine(line):
result=mainpattern.search(line)
if result is not None:
if result.group() is not None:
print result.group()

def matchLines(inputfile):
for line in inputfile:
matchLine(line)

def processFile(inputfilename):
inputfile=open(inputfilename,'r')
matchLines(inputfile)

def main():
processFile(sys.argv[1])

if __name__=="__main__":
if sys.argv.__len__()<3:
print 'usage: match [infile] [pattern]'
else:
mainpattern=re.compile(sys.argv[2])
main()