blokhead,
After an hour an a half, I convinced my co-worker he was full of contradictory statements and what he said he needed wasn't at all the correct definition of the problem. After figuring out what he really wanted to do, it turned out to be a simple matter of constructing a "depends on me" tree and then using the topological sort.
#!/usr/bin/perl
use strict;
use warnings;
my %all_pkg;
my %want_rm;
my %must_keep = map {$_ => 1} grep {! $want_rm{$_}} keys %all_pkg;
my %tree;
for my $pkg (keys %all_pkg) {
push @{$tree{$_}}, $pkg for dependencies($pkg);
}
PACKAGE:
for my $pkg (keys %want_rm) {
my @rm_order = how_to_uninstall($pkg);
for (@rm_order) {
if ($must_keep{$_}) {
print "Can't remove $pkg - depends on $_ which is a must k
+eep\n";
next PACKAGE;
}
}
print "Safe to remove $pkg\n";
for (@rm_order) {
print "\tRemoving $_\n";
remove_pkg($_);
}
}
sub dependencies {
my ($pkg) = @_;
# Functionality to return list of dependencies
}
sub remove_pkg {
my ($pkg) = @_;
# Functionality to remove a package
}
{
my %seen;
my %onstack;
my @list;
sub how_to_uninstall {
my $target = shift;
(@list, %seen, %onstack) = ();
_traverse($target);
return @list;
}
sub _traverse {
my $x = shift;
$seen{$x} = $onstack{$x} = 1;
for my $y (@{$tree{$x}}) {
die "cyclic!" if $onstack{$y};
_traverse($y) unless $seen{$y};
}
push @list, $x;
$onstack{$x} = 0;
}
}
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|