Debian Codegraph @

doc/graph.pl

raw
1 #!/usr/bin/perl
2 #
3 # Pass this program a list of .pm files. It parses them (halfheartedly,
4 # it works on my code, may not on your code), and generates an inheritcance
5 # graph of the modules.
6 #
7 # Remember: I have a copy of this in debconf and a copy in stool. Keep them
8 # sync'd.
9
10 use warnings;
11 use strict;
12
13 my %kids;
14 my %iskid;
15 my %descs;
16
17 foreach my $file (@ARGV) {
18 my $package='';
19 my $desc='';
20 my @isa=();
21 open (my $in, "<", $file) || die "$file: $!";
22 while (<$in>) {
23 if (/^\s*package\s(\w+.*?);/) {
24 $package=$1;
25 }
26 # Gag. This just looks for @ISA= lines and use base.
27 if (/(?:use\s+base\s+|\@ISA\s*=\s*)(?:q(?:w|q)?(?:\(|{)|"|')(.*?)(?:}|\)|'|")/) {
28 push @isa, split(/\s+/, $1);
29 }
30 if (/.*::.*\s+-\s+(.*)/) {
31 $desc=$1;
32 }
33 }
34 close $in;
35
36 if ($package) {
37 $descs{$package}=$desc;
38 foreach (@isa) {
39 $kids{$_}{$package}=1;
40 $iskid{$package}=1;
41 }
42 }
43 }
44
45 my %seen;
46
47 # Print out one item.
48 sub printitem {
49 my $text=shift;
50 my $item=shift;
51 print $text . (' ' x (40 - length $text));
52 print $descs{$item} if exists $descs{$item};
53 print "\n";
54 }
55
56 # Recursively print out tree structure.
57 sub printkids {
58 my $parent=shift;
59 my $spacer=shift;
60
61 foreach my $kid (sort keys %{$kids{$parent}}) {
62 next if $seen{$kid};
63 $seen{$kid}=1;
64 # Strip off text in name that comes from any common parents.
65 $_=$kid;
66 foreach my $p (split(/::/,$parent)) {
67 s/^$p\:://;
68
69 }
70 printitem($spacer.$_, $kid);
71 printkids($kid, " $spacer");
72 }
73 }
74
75 # Print all parents with thier kids under them.
76 # It's important to only print toplevel parents, which is why
77 # %iskid comes into play.
78 foreach my $parent (sort keys %kids) {
79 next if $iskid{$parent};
80 printitem($parent, $parent);
81 printkids($parent, " ");
82 }