#!/usr/bin/env python
import urllib2, tarfile, re, glob, shutil

rawTar = urllib2.urlopen("ftp://ftp.atnf.csiro.au/pub/software/wcslib/wcslib.tar.bz2")
tar = tarfile.open(fileobj=rawTar, mode="r|bz2")
info = tar.next()
dirname = info.name.split("/")[0]
version = re.match("wcslib-([0-9]+(?:\.[0-9]+)*)$", dirname).group(1)
newBuildFile = "wcslib-" + version + ".build"

buildFile, = glob.glob("wcslib-*.build")

if buildFile != newBuildFile:
    print "mv {old} {new}".format(old=buildFile, new=newBuildFile)
    shutil.move(buildFile, newBuildFile)
