#!/usr/bin/env python # # Upgrade script # import sys, os, time from shutil import copy2 from commands import getstatusoutput, getoutput mountDir = "/var/ports" binDir = "/usr/local/bin" bins = ["php5-cli", "php5-cgi"] packageDir = "/var/ports/packages/All" def getCurInfo(): status, output = getstatusoutput("php5 -v | head -n1") if status == 0: version = output.split()[1] status, output = getstatusoutput("php5 -i | grep extension_dir") if status == 0: extension_dir = output.split()[2] return dict(ext_dir = extension_dir, ver = version) def portsManager(action): cmd = {'u': 'umount', 'm': 'mount'}[action] # check if it's already mounted status = getstatusoutput("mount | grep %s" % mountDir)[0] if status == 0 and cmd == 'mount': return True elif not status == 0 and cmd == 'umount': return True else: status = getstatusoutput("%s %s" % (cmd, mountDir))[0] if status == 0: return True else: return False def updatePHP(newVer): # make backups of the binaries for bin in bins: copy2("%s/%s" % (binDir, bin), "%s/%s-%s" % (binDir, bin, cur['ver'])) # move the ext dir os.rename(cur['ext_dir'], "%s-%s" % (cur['ext_dir'], cur['ver'])) # update curl curlCur = getoutput("pkg_version -vs curl-7 | awk '{print $1}'") if not curlCur == "curl-7.16.1": os.system("portupgrade -PPi %s" % curlCur) dirs = [dirs for root,dirs,files in os.walk("%s/%s" % (packageDir, newVer))][0] for dir in dirs: os.system("rsync -a %s/%s/%s/* /usr/local/%s" % (packageDir, newVer, dir, dir)) print "------" print "Done!" print "------" os.system("php5 -v") print if __name__ == "__main__": cur = getCurInfo() # mount ports if not portsManager('m'): print "Failed to mount %s, exiting!" % mountDir sys.exit() # check the newest version available # from the packages directory newVer = getoutput("ls %s | grep .php-5 | grep -v %s | sort | tail -n1" % (packageDir, cur['ver'])) response = raw_input("Are you sure you want to upgrade php-%s to %s? (y/n) " % (cur['ver'], newVer.lstrip('.'))) if response.lower() == 'n': print "Cancelled!" elif response.lower() == 'y': updatePHP(newVer) else: print "Response not understood!" # bye ! while True: if not portsManager('u'): print "Failed to unmount %s, trying again in 20 seconds!" % mountDir time.sleep(20) else: break