extract-dependencies 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #! /usr/bin/perl
  2. # $OpenBSD: extract-dependencies,v 1.3 2017/04/05 12:08:20 espie Exp $
  3. #
  4. # Copyright (c) 2005 Marc Espie <espie@openbsd.org>
  5. #
  6. # Permission to use, copy, modify, and distribute this software for any
  7. # purpose with or without fee is hereby granted, provided that the above
  8. # copyright notice and this permission notice appear in all copies.
  9. #
  10. # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  11. # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  12. # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  13. # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  14. # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  15. # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  16. # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  17. # Usage: extract-dependencies < 'tsort-pairs' seed
  18. # extracts all the dependencies for seed from a list that contains more
  19. # than that.
  20. #
  21. # THIS IS CURRENTLY A HELPER SCRIPT FOR show-required-by
  22. use strict;
  23. use warnings;
  24. use Getopt::Std;
  25. my %opts;
  26. getopts('r', \%opts);
  27. # build dependency graph
  28. my $dep = {};
  29. while (<STDIN>) {
  30. chomp;
  31. my ($a, $b) = split(/\s+/, $_);
  32. if ($opts{r}) {
  33. ($a, $b) = ($b, $a);
  34. }
  35. $dep->{$a} = {} unless defined $dep->{$a};
  36. $dep->{$a}->{$b} = 1;
  37. }
  38. # get the starting points
  39. my %pkgpath = map {($_, 1)} @ARGV;
  40. my @todo = ();
  41. my $done = {};
  42. # walk the graph repeatedly starting from it
  43. push(@todo, keys %pkgpath);
  44. while (my $x = shift @todo) {
  45. next if $done->{$x};
  46. $done->{$x} = 1;
  47. next unless defined $dep->{$x};
  48. push(@todo, keys %{$dep->{$x}});
  49. }
  50. # display all nodes, except for the seeds
  51. for my $d (keys %$done) {
  52. next if $pkgpath{$d};
  53. print "$d\n";
  54. }