-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmakeGraphvizFromDirTree
executable file
·181 lines (138 loc) · 4.15 KB
/
makeGraphvizFromDirTree
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/usr/bin/env perl -w
#
# Make a Graphviz dot file for your file system
#
# Notes on graphviz syntax:
# // comment
# "node1" -> "node2";
# "node1" -- "node2";
# node [ a=v, a=v];
# "nodename" [props];
#
# Some attributes/properties:
# shape, sides, orientation, color, fillcolor, fontcolor, fontname,
# fontsize, height, label, style, toplabel, URL,
#
use strict;
use Getopt::Long;
our $VERSION_DATE = "2012-09-12";
my $docvs = 0;
my $doFiles = 0;
my $quiet = 0;
my $verbose = 0;
###############################################################################
# Process options
#
Getopt::Long::Configure ("ignore_case");
my $result = GetOptions(
"cvs" => \$docvs,
"f" => \$doFiles,
"h|help|?" => sub { system "perldoc $0"; exit; },
"q|quiet!" => \$quiet,
"v|verbose+" => \$verbose,
"version" => sub {
die "Version of $VERSION_DATE, by Steven J. DeRose.\n";
}
);
($result) || die "Bad options.\n";
###############################################################################
###############################################################################
# Main
#
my $file = $ARGV[0];
(-d $file) || die "Couldn't find directory '$file'.\n";
my %namesUsed = ();
print "// " . glob($file) . "\n";
print "
// GraphViz dot file for '$file', generated by dirtree.
digraph file_system {
//rankdir=LR;
size=\"8,11\";
";
my $depth = 0;
dirTraverse("$file/");
print "}\n";
exit;
###############################################################################
###############################################################################
#
sub dirTraverse {
my $theDir = $_[0];
my $shortDir = $theDir;
if (substr($shortDir,length($shortDir)-1,1) eq "/") { chop($shortDir); }
$shortDir =~ s/.*\///;
$depth += 3;
#print "\n";
#print "// entering dirtraverse for '$theDir'\n";
my $cmd = "ls -dF $theDir" . "*";
#print " command is '$cmd'\n";
my @subdirs = `$cmd`;
my $sd;
#print "// ls got: " . scalar @subdirs . " items.\n";
#print "subdirs now " . join(", ", @subdirs) . "\n";
# Issue the dot file definition of the arc
foreach my $sd (@subdirs) {
chomp($sd);
my $isDir = (substr($sd,length($sd)-1,1) eq "/") ? 1:0;
if ($isDir or $doFiles) {
my $c = index($sd,"CVS");
if ($c > -1 and $c == length($sd)-4) { next; }
my $shortName = $sd;
chop($shortName); # nuke the slash
$shortName =~ s/.*\///;
if ($namesUsed{$shortName}) {
#$shortName .= "(" . int(rand(1000)) . ")";
}
print " " x $depth;
($isDir) || print "\"$shortName\" [ color=red ]; ";
print "\"$shortDir\" -> \"$shortName\";\n";
$namesUsed{$sd} = $shortName;
}
}
# Recurse for each subdirectory
my $i = 0;
foreach my $sd (@subdirs) {
chomp($sd);
$i++;
my $isDir = (substr($sd,length($sd)-1,1) eq "/") ? 1:0;
if ($isDir) {
my $c = index($sd,"CVS");
if ($c > -1 and $c == length($sd)-4) { next; }
#print " " x $depth;
#print "// Recursing on '$sd' ($i of " . scalar @subdirs . ")\n";
dirTraverse("$sd");
}
else {
#print "// No traverse for '$sd'\n";
}
}
$depth -= 3;
} # dirTraverse
###############################################################################
###############################################################################
###############################################################################
#
=pod
=head1 Usage
dirtree [dir]
Make a 'dot' file for Graphviz, showing your directory structure.
The dot file is written to stdout.
See L<http://www.graphviz.org/Gallery.php>
=head1 Options
=over
=item * B<--cvs>
include CVS directories (normally omitted)
=item * B<--quiet> OR B<-q>
suppresses most messages.
=item * B<-f>
include files, not just directories (but show in a different color).
=back
=head1 Related commands
C<lsoutline> -- does an C<ls -r> but arranges the results as an indented
outline.
=head1 Ownership
This work by Steven J. DeRose is licensed under a Creative Commons
Attribution-Share Alike 3.0 Unported License. For further information on
this license, see L<http://creativecommons.org/licenses/by-sa/3.0/>.
For the most recent version, see L<http://www.derose.net/steve/utilities/>.
=cut