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

dlpage = urllib2.urlopen("http://www.stsci.edu/institute/software_hardware/pyfits/Download")
versions = re.findall(r'''https://pypi\.python\.org/packages/source/p/pyfits/pyfits-([0-9.]+)\.tar\.gz''', dlpage.read())

versions.sort(key=lambda x: map(int, x.split(".")) )
version = versions[-1]

newBuildFile = "pyfits-" + version + ".build"
buildFile, = glob.glob("pyfits-*.build")

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