Saturday, July 16, 2005

Jython instead of Ant: sample code

Here is an example of how to use jython to deploy a web application under tomcat. The application is supposed here to be WAR packaged in a 'web' directory. Libraries used for the web-app are already in web/WEB-INF/lib, classes are compiled automatically by Eclipse under web/WEB-INF/classes, and resources are copied automatically by Eclipse. If necessary, it is very easy to script those actions in the python script, with very little code.

The longest code is the TreeCopier class, that should probably be part of any python make script. The reload method could be reworked to provide a generic method for reloading a tomcat web application. The following is just a working sample code as proof of concept.

# call jython make.py method1 method2, ...
# to invoke method1 and then method2, ...

import sys, os, os.path, shutil, urllib, base64, re
web_dir = 'web'
webapp_name = 'myrestaurant'
tomcat_dir = 'c:/java/Tomcat 5.5'

#copy web directory to tomcat app dir
def deploy():
target_dir = tomcat_dir+'/webapps'+'/'+webapp_name
copier=LatestTreeCopier()
copier.copytree(web_dir,target_dir)
print "copied %s file(s) to %s" % (copier.count,target_dir)

#reload tomcat web application
def reload():
url = "http://localhost:8080/manager/reload?path=/"+webapp_name
base64string = base64.encodestring("%s:%s"%('michelin','michelin'))[:-1]
opener = urllib.URLopener()
opener.addheader('Authorization',"Basic %s"%base64string)
h = opener.open(url)
print h.read()
h.close()

class TreeCopier:
def __init__(self):
self.count = 0

def filter(self, srcname, dstname):
return true

def filterDir(self, srcname):
return true

def copytree(self, src, dst, symlinks=0):
"""Recursively copy a directory tree using copy2().
"""
names = os.listdir(src)
if not os.path.exists(dst):
os.mkdir(dst)
count = 0
for name in names:
srcname = os.path.join(src, name)
dstname = os.path.join(dst, name)
try:
if symlinks and os.path.islink(srcname):
linkto = os.readlink(srcname)
os.symlink(linkto, dstname)
elif os.path.isdir(srcname):
if self.filterDir(srcname):
self.copytree(srcname, dstname, symlinks)
else:
if self.filter(srcname, dstname):
shutil.copy2(srcname, dstname)
print srcname
count += 1
except (IOError, os.error), why:
print "Can't copy %s to %s: %s"%('srcname', 'dstname', str(why))
self.count += count

class LatestTreeCopier(TreeCopier):
def __init__(self):
self.count = 0
self.excludePattern = re.compile(
'(^\.svn.*|.*\.swp$|.*\.bak$|.*~$|.*\.swo$)',re.I)
self.excludeDirPattern = re.compile('^\.svn.*',re.I)

def filter(self, srcname, dstname):
return (not self.excludePattern.match(os.path.basename(srcname)))
and (os.path.getmtime(srcname) > os.path.getmtime(dstname))

def filterDir(self, srcname):
return (not self.excludeDirPattern.match(os.path.basename(srcname)))


if __name__ == "__main__":
if (len(sys.argv)) >= 2:
methods = sys.argv[1:]
for m in methods:
method = m+'()'
print "launching %s" % method
eval(method)
else:
deploy()
reload()


technorati tags:

No comments :

Post a Comment