Folks,
I posted this script to bug 8035, but I thought I'd also send it out
here, in case others are trying to clean up their /var/pkg metadata.
The script removes manifests for packages that are no longer installed,
as well as the contents of the download directory.  More details are
available in the bug:

        https://defect.opensolaris.org/bz/show_bug.cgi?id=8035

A copy of the script is there, but I'm attaching one to this e-mail,
too.

-j
#!/usr/bin/python
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License (the "License").
# You may not use this file except in compliance with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#

#
# Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
# Use is subject to license terms.
#

import getopt
import os
import shutil
import subprocess
import sys
import urllib
import urlparse

def usage():
        print "cleanup.py [-d pkgdir]"

def main():

        basedir = "/"

        try:
                opts, pargs = getopt.getopt(sys.argv[1:], "d:")
        except getopt.GetoptError, e:
                print "Illegal option: %s\n" % e.opt
                usage()
                return(2)

        for opt, arg in opts:
                if opt == "-d":
                        basedir = arg

        if not os.path.exists(basedir):
                print "Package directory: %s does not exist" % pkgdir
                return(1)

        dl_dir = os.path.join(basedir, "var", "pkg", "download")
        pkgdir = os.path.join(basedir, "var", "pkg", "pkg")

        installed_dirs = find_installed_dirs(basedir, pkgdir)

        prune_dirs = find_stale_pkgdirs(pkgdir, installed_dirs)

        for f in prune_dirs:
               shutil.rmtree(f, ignore_errors=True)

        cleanup_download_dir(dl_dir)

        return(0)

def find_installed_dirs(basedir, pkgdir):

        installed_list = []

        devnull = file(os.devnull, "wb")

        cmd = ["/usr/bin/pkg", "-R", basedir, "list", "-H", "-v"]
        p = subprocess.Popen(cmd, stdin=devnull, stdout=subprocess.PIPE,
            stderr=devnull)
        output = p.communicate()[0]
        devnull.close()

        for l in output.splitlines():
                lines = l.split()
                l = urlparse.urlparse(lines[0]).path
                if not l.startswith("//"):
                        continue

                l = l[2:]
                idx = l.find("/")
                l = l[idx+1:]
                pkg, vers = l.split("@")
                pkg = urllib.quote(pkg, safe='')
                vers = urllib.quote(vers)
                pkgpath = os.path.join(pkgdir, pkg, vers)
                installed_list.append(pkgpath)

        return installed_list


def find_stale_pkgdirs(pkgdir, installed_dirs):

        prune_list = []

        for pkgdirs in os.listdir(pkgdir):
                prune_files = []
                counted_files = 0
                for files in os.listdir(os.path.join(pkgdir, pkgdirs)):
                        counted_files += 1
                        curpath = os.path.join(pkgdir, pkgdirs, files)
                        if curpath in installed_dirs:
                                continue
                        prune_files.append(curpath)

                if len(prune_files) == counted_files:
                        prune_list.append(os.path.join(pkgdir, pkgdirs))
                else:
                        prune_list.extend(prune_files)

        return prune_list

def cleanup_download_dir(dl_dir):

        for subdir in os.listdir(dl_dir):
                shutil.rmtree(os.path.join(dl_dir, subdir), ignore_errors=True)

if __name__ == "__main__":
        try:
                rc = main()
        except KeyboardInterrupt:
                rc = 0
        sys.exit(rc)
_______________________________________________
pkg-discuss mailing list
[email protected]
http://mail.opensolaris.org/mailman/listinfo/pkg-discuss

Reply via email to