Updated apt.py to match pacman.py

This commit is contained in:
Matthew Printz 2011-06-01 15:29:41 -05:00
parent cb56a1bf0a
commit a4531e5e30

View file

@ -6,33 +6,85 @@ import subprocess
def __virtual__():
'''
Confine this module is on a Debian based system
Confirm this module is on a Debian based system
'''
return 'pkg' if __grains__['os'] == 'Debian' else False
def update():
def available_version(name):
'''
The available version of the package in the repository
CLI Example:
salt '*' pkg.available_version <package name>
'''
version = ''
cmd = 'apt-cache show ' + name + ' | grep Version'
out = subprocess.Popen(cmd,
shell=True,
stdout=subprocess.PIPE).communicate()[0]
version_list = out.split()
if len(version_list) >= 2:
version = version_list[1]
return version
def version(name):
'''
Returns a string representing the package version or an empty string if not
installed
CLI Example:
salt '*' pkg.version <package name>
'''
pkgs = list_pkgs(name)
if pkgs.has_key(name):
return pkgs[name]
else:
return ''
def refresh_db():
'''
Updates the apt database to latest packages based upon repositories
Returns True/False based upon successful completion of update
Returns a dict: {'<database name>': Bool}
CLI Example:
salt '*' pkg.refresh_db
'''
cmd = 'apt-get update'
result = subprocess.call(cmd, shell=True)
out = subprocess.Popen(cmd,
shell=True,
stdout=subprocess.PIPE).communicate()[0].split('\n')
return result == 0
servers = {}
for line in out:
cols = line.split()
if not len(cols):
continue
ident = " ".join(cols[1:4])
if cols[0].count('Get'):
servers[ident] = True
else:
servers[ident] = False
def install(pkg, update_repos=False):
return servers
def install(pkg, refresh=False):
'''
Install the passed package
Return a dict containing the new package names and versions:
{'<package>': {'old': '<old-version>',
'new': '<new-version>']}
CLI Example:
salt '*' pkg.install <package name>
'''
if(update_repos):
update()
if(refresh):
refresh_db()
ret_pkgs = {}
old_pkgs = list_pkgs()
@ -75,7 +127,37 @@ def remove(pkg):
return ret_pkgs
def dist_upgrade(update_repos=False):
def purge(pkg):
'''
Remove a package via apt-get along with all configuration files and
unused dependencies as determined by apt-get autoremove
Returns a list containing the names of the removed packages
CLI Example:
salt '*' pkg.purge <package name>
'''
ret_pkgs = []
old_pkgs = list_pkgs()
# Remove inital package
purge_cmd = 'apt-get -y purge ' + pkg
subprocess.call(purge_cmd, shell=True)
# Remove any dependencies that are no longer needed
autoremove_cmd = 'apt-get -y autoremove'
subprocess.call(purge_cmd, shell=True)
new = list_pkgs()
for pkg in old_pkgs:
if not new_pkgs.has_key(pkg):
ret_pkgs.append(pkg)
return ret_pkgs
def upgrade(refresh=True):
'''
Upgrades all packages via apt-get dist-upgrade
@ -86,10 +168,13 @@ def dist_upgrade(update_repos=False):
}',
...
]
CLI Example:
salt '*' pkg.upgrade
'''
if(update_repos):
update()
refresh_db()
ret_pkgs = {}
old_pkgs = list_pkgs()
@ -111,7 +196,7 @@ def dist_upgrade(update_repos=False):
return ret_pkgs
def list_pkgs():
def list_pkgs(regex_string=""):
'''
List the packages currently installed in a dict:
{'<package_name>': '<version>'}
@ -120,7 +205,7 @@ def list_pkgs():
salt '*' pkg.list_pkgs
'''
ret = {}
cmd = 'dpkg --list'
cmd = 'dpkg --list ' + regex_string
out = subprocess.Popen(cmd,
shell=True,