ANNOUNCE: checkdeps (script to check for unused libraries)



Hi all,

I noticed that I had about a gig (!) of old versions of gnome libraries
hanging around.

I attached a perl script to check which libraries are unused.  Is this
useful to anyone?
Should it go in the CVS' build scripts module?

Andrew Clausen

#! /usr/bin/perl

@PROGRAM_PATHS =
(
    "/usr/local/bin"
);

@LIBRARY_PATHS =
(
    "/usr/local/lib"
);

# main code
{
    build_lib_hash ();
    check_programs ();

    print_unused_libs ();
    calc_wasted_space ();
}

sub get_files
{
    (my $dir) = @_;

    opendir (DIR, $dir) || die "directory $dir not found";
    my @dir_entries = readdir DIR;
    closedir DIR;

    return @dir_entries;
}

sub build_lib_hash
{
    for my $dir (@LIBRARY_PATHS)
    {
	for my $file (get_files ($dir))
	{
	    my $path = $dir . "/" . $file;
	    if (!-d $path && !-l $path)	    # exclude directories and sym-links
	    {
		$_ = $path;
		if (/.*\.so\..*/)	    # only include shared libraries
		{
		    $libraries{$path} = 0;
		}
	    }
	}
    }
}

sub check_programs
{
    for my $dir (@PROGRAM_PATHS)
    {
	for my $file (get_files ($dir))
	{
	    open (DEPS, "ldd $dir/$file |") || die "can't open $dir/$file";
	    while (<DEPS>)
	    {
		(my $short_lib, my $symsign, my $lib) = split (/ +/, $_);
		while (-l $lib)
		{
		    $_ = $lib;
		    (my $libdir) = /(.*\/)[^\/]*$/;
		    
		    $lib = $libdir . readlink $lib;
		}

		$libraries{$lib}++;
	    }
	    close DEPS;
	}
    }
}

sub print_unused_libs ()
{
    for $lib (sort keys %libraries)
    {
	if ($libraries{$lib} == 0)
	{
	    print "$lib\n";
	}
    }
}

sub calc_wasted_space ()
{
    my $total = 0;
    
    for $lib (keys %libraries)
    {
	if ($libraries{$lib} == 0)
	{
	    @file_info = stat $lib;
	    $total += $file_info[7];
	}
    }

    printf ("total space occupied by unused libraries: %d Mb\n",
	    $total / 1024 / 1024);
}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]