From 1bf4438e60e1a7eed541cee2a84852d1f635c031 Mon Sep 17 00:00:00 2001
From: "martin f. krafft" <madduck@madduck.net>
Date: Wed, 19 Jun 2013 08:37:14 +0200
Subject: [PATCH 1/3] Fix purging of packages

A small error in the reuse of a variable caused packages to never get
purged. This commit fixes that.

Signed-off-by: martin f. krafft <madduck@madduck.net>
---
 packaging/apt | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/packaging/apt b/packaging/apt
index d0e86e80827..37856074c96 100644
--- a/packaging/apt
+++ b/packaging/apt
@@ -210,9 +210,10 @@ def remove(m, pkgspec, cache, purge=False):
     if len(packages) == 0:
         m.exit_json(changed=False)
     else:
-        purge = ''
         if purge:
             purge = '--purge'
+        else:
+            purge = ''
         cmd = "%s -q -y %s remove %s" % (APT_GET_CMD, purge,packages)
 
         if m.check_mode:

From 3d1db80fe03579a2ca3134713592e6772236c926 Mon Sep 17 00:00:00 2001
From: "martin f. krafft" <madduck@madduck.net>
Date: Wed, 19 Jun 2013 09:06:33 +0200
Subject: [PATCH 2/3] Introduce non-purged package status

A package may be removed but not purged with APT. The only way to
identify this state is by looking at the list of installed files of
a package. Even if the package has no files installed, this list will be
non-empty until the package is removed:

  # python -c "import apt; c=apt.Cache(); c.update(); c.open(); p=c['ruby1.8']; print p, p.installed, p.installed_files"
  <Package: name:'ruby1.8' id:1425> None [u'']

  # dpkg --purge ruby1.8
  (Reading database ... 27904 files and directories currently installed.)
  Removing ruby1.8 ...
  Purging configuration files for ruby1.8 ...

  # python -c "import apt; c=apt.Cache(); c.update(); c.open(); p=c['ruby1.8']; print p, p.installed, p.installed_files"
  <Package: name:'ruby1.8' id:1425> None []

See http://bugs.debian.org/712749 too.

If a package is not marked installed but it still 'has_files', then it
should be processed if the request is to purge it.

Signed-off-by: martin f. krafft <madduck@madduck.net>
---
 packaging/apt | 17 +++++++++--------
 1 file changed, 9 insertions(+), 8 deletions(-)

diff --git a/packaging/apt b/packaging/apt
index 37856074c96..a882c43c4aa 100644
--- a/packaging/apt
+++ b/packaging/apt
@@ -151,25 +151,26 @@ def package_status(m, pkgname, version, cache, state):
         if state == 'install':
             m.fail_json(msg="No package matching '%s' is available" % pkgname)
         else:
-            return False, False
+            return False, False, False
+    has_files = len(pkg.installed_files) > 0
     if version:
         try :
-            return pkg.is_installed and fnmatch.fnmatch(pkg.installed.version, version), False
+            return pkg.is_installed and fnmatch.fnmatch(pkg.installed.version, version), False, has_files
         except AttributeError:
             #assume older version of python-apt is installed
-            return pkg.isInstalled and fnmatch.fnmatch(pkg.installedVersion, version), False
+            return pkg.isInstalled and fnmatch.fnmatch(pkg.installedVersion, version), False, has_files
     else:
         try :
-            return pkg.is_installed, pkg.is_upgradable
+            return pkg.is_installed, pkg.is_upgradable, has_files
         except AttributeError:
             #assume older version of python-apt is installed
-            return pkg.isInstalled, pkg.isUpgradable
+            return pkg.isInstalled, pkg.isUpgradable, has_files
 
 def install(m, pkgspec, cache, upgrade=False, default_release=None, install_recommends=True, force=False):
     packages = ""
     for package in pkgspec:
         name, version = package_split(package)
-        installed, upgradable = package_status(m, name, version, cache, state='install')
+        installed, upgradable, has_files = package_status(m, name, version, cache, state='install')
         if not installed or (upgrade and upgradable):
             packages += "'%s' " % package
 
@@ -203,8 +204,8 @@ def remove(m, pkgspec, cache, purge=False):
     packages = ""
     for package in pkgspec:
         name, version = package_split(package)
-        installed, upgradable = package_status(m, name, version, cache, state='remove')
-        if installed:
+        installed, upgradable, has_files = package_status(m, name, version, cache, state='remove')
+        if installed or (has_files and purge):
             packages += "'%s' " % package
 
     if len(packages) == 0:

From a52f531c5bcf0fe685095e9a69dd3c41dfe7e0dd Mon Sep 17 00:00:00 2001
From: "martin f. krafft" <madduck@madduck.net>
Date: Wed, 19 Jun 2013 10:56:43 +0200
Subject: [PATCH 3/3] Always assume purged for older python-apt

Older python-apt modules don't export Package.installed_files and there
seems to be no other way to figure out if a package is
removed-but-not-purged, so we just always assume it's purged.

Signed-off-by: martin f. krafft <madduck@madduck.net>
---
 packaging/apt | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/packaging/apt b/packaging/apt
index a882c43c4aa..49a3d7f9ab5 100644
--- a/packaging/apt
+++ b/packaging/apt
@@ -152,7 +152,11 @@ def package_status(m, pkgname, version, cache, state):
             m.fail_json(msg="No package matching '%s' is available" % pkgname)
         else:
             return False, False, False
-    has_files = len(pkg.installed_files) > 0
+    try:
+        has_files = len(pkg.installed_files) > 0
+    except AttributeError:
+        has_files = False  # older python-apt cannot be used to determine non-purged
+
     if version:
         try :
             return pkg.is_installed and fnmatch.fnmatch(pkg.installed.version, version), False, has_files