From 2d8b956248d5c60154e9ab6fc567db1ff281827a Mon Sep 17 00:00:00 2001
From: Stephen Hemminger <stephen.hemminger@vyatta.com>
Date: Mon, 21 Dec 2009 16:27:57 -0800
Subject: Rewrite priority script to use File::find

Use perl library rather than external find/grep.  Cleaner and
more efficient.
---
 scripts/priority.pl | 65 +++++++++++++++++++++++++++++++----------------------
 1 file 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";
     }
-} 
+}
-- 
cgit v1.2.3