diff options
author | Stephen Hemminger <stephen.hemminger@vyatta.com> | 2009-12-21 16:27:57 -0800 |
---|---|---|
committer | Stephen Hemminger <stephen.hemminger@vyatta.com> | 2009-12-21 16:27:57 -0800 |
commit | 2d8b956248d5c60154e9ab6fc567db1ff281827a (patch) | |
tree | 975cf8fc408e85a25fff567dde949152d573719b /scripts/priority.pl | |
parent | 06c898a1c4019a7d72338af9c746bf4ebe8d0d77 (diff) | |
download | vyatta-cfg-2d8b956248d5c60154e9ab6fc567db1ff281827a.tar.gz vyatta-cfg-2d8b956248d5c60154e9ab6fc567db1ff281827a.zip |
Rewrite priority script to use File::find
Use perl library rather than external find/grep. Cleaner and
more efficient.
Diffstat (limited to 'scripts/priority.pl')
-rw-r--r-- | scripts/priority.pl | 65 |
1 files changed, 38 insertions, 27 deletions
diff --git a/scripts/priority.pl b/scripts/priority.pl index 39f6220..3f5804f 100644 --- a/scripts/priority.pl +++ b/scripts/priority.pl @@ -1,39 +1,50 @@ #!/usr/bin/perl +# Read all the configuration templates in the configuration +# template directory and produce an ordered list of the priority +# of configuration actions + +use strict; +use warnings; +use File::Find; + my %pri; -# Look at all node.def files in the configuration template tree -my @files = `find /opt/vyatta/share/vyatta-cfg -name 'node.def'`; -foreach my $f (@files) { - my $result = `grep 'priority:' $f`; - if (defined $result && length($result) != 0) { - my @r = split " ", $result; - if (defined $r[1]) { - # Strip off trailing "/node.def\n" from file pathname - my $line = substr($f, 0, -10); - - # Strip off leading "/opt/vyatta/share/vyatta-cfg/templates/" - $line = substr($line, 39); - - # See if there is a comment in entry - my ($entry, $comment) = split /#/, $result; - if (defined $comment) { - $comment =~ s/\n//; - $line = $line . " #" . $comment; - } - - # stuff resulting line into hash - push @{$pri{$r[1]}}, $line; - } +sub get_priority { + open( my $f, '<', $_ ) + or return; + my $priority; + + while (<$f>) { + chomp; + next unless m/^priority:\s(\d+)/; + $priority = $1; + last; } + close $f; + return $priority; } +sub wanted { + return unless ( $_ eq 'node.def' ); + + my $p = get_priority($File::Find::name); + return unless $p; -#now sort + my $dir = $File::Find::dir; + $dir =~ s/^.*\/templates\///; + + push @{ $pri{$p} }, $dir; + return 1; +} + +my $cfgdir = '/opt/vyatta/share/vyatta-cfg/templates'; +die "$cfgdir does not exist!" unless -d $cfgdir; +find( \&wanted, $cfgdir ); foreach my $key ( sort { $a <=> $b } keys %pri ) { - my @a = @{$pri{$key}}; + my @a = @{ $pri{$key} }; foreach my $val (@a) { - print $key," ",$val,"\n"; + print $key, " ", $val, "\n"; } -} +} |