So I coded this last night and been playing around and tweaking the concept. Before anyone asks my purposes are academic. I wanted something that worked like I have been doing so manually and wanted more hands on time with python so I figured why not start on something like this that would be useful to me.
Posting the script code as I would love comments, critique, or good ideas for addition, and of course if it is useful to you, have at it.
Ken
Posting the script code as I would love comments, critique, or good ideas for addition, and of course if it is useful to you, have at it.
Code:
#!/usr/bin/env python
#
# sysupdate.py
#
# Version 0.1
#
# Author: Ken MacKenzie
#
# This script uses portsnap to fetch and update the ports tree. The pkg_version to determine packages that are out of date.
# The user can then specify a line number, No or Yes (to upgrade all) to upgrade packages in the system. Portupgrade is called
# to perform the upgrade.
#
# Requires:
#
# 1. Portsnap
# 2. Portupgrade
# 3. Python 2.6+
#
# To do:
#
# 1. Check for sudo/root privledges
# 2. Add pkg search and listing features as well as installation of new patches
# 3. Add version numbers old and new
# 4, Once a fully functioning and stable CLI tool a GUI tool will be added.
import os, shlex, subprocess
def PkgsToUpdate():
print "Checking Package Versions..."
pvList = []
pvCmd = "pkg_version -l \"<\""
pvReturn, pvError = subprocess.Popen([pvCmd], shell=True, stdout=subprocess.PIPE).communicate()
pvReturn = pvReturn.strip()
if len(pvReturn):
pvList = pvReturn.split("<")
pvCount = len(pvList)
del pvList[pvCount-1]
for pvItem in pvList:
pvItem = pvItem.strip()
return pvList
os.chdir("/usr/ports")
print "Refreshing Ports Tree..."
psCmd = "portsnap fetch update"
psArgs = shlex.split(psCmd)
psReturn = subprocess.call(psArgs)
#print psReturn
pkList = []
pkgList = PkgsToUpdate()
puExit = 0
while not(puExit):
if len(pkgList) == 0:
print "All packages current."
puExit = 1
else:
print "Packages to upgrade:"
for x in range ( len(pkgList)):
print (x+1), " : ", pkgList[x]
uCmd = input("Enter all (Y)es or (N)o, or enter line number to upgrade individually:")
if isinstance(uCmd, str):
uCmd = uCmd.upper
if uCmd == "N":
puExit = 1
elif uCmd == "Y":
for pkgItem in pkgList:
print "Upgrading package: ", pkgItem
pkgCmd = "portupgrade " + pkgItem
pkgArgs = shlex.split(pkgCmd)
pkgReturn = subprocess.call(pkgArgs)
#pkgReturn, pkgError = subprocess.Popen([pkgCmd], shell=True, stdout=subprocess.PIPE).communicate()
#print pkgReturn
pkgList = PkgsToUpdate()
elif uCmd in range (1, len(pkgList)+1):
print "Upgrading package: ", pkgList[uCmd - 1]
pkgCmd = "portupgrade " + pkgList[uCmd - 1]
pkgArgs = shlex.split(pkgCmd)
pkgReturn = subprocess.call(pkgArgs)
#pkgReturn, pkgError = subprocess.Popen([pkgCmd], shell=True, stdout=subprocess.PIPE).communicate()
#print pkgReturn
pkgList = PkgsToUpdate()
else:
print "Invalid Input!!!"
Ken