]> git.pld-linux.org Git - projects/distfiles.git/blobdiff - specparser.pl
- nice -n 20 when running
[projects/distfiles.git] / specparser.pl
index 94ac6818932258cf4703106a3f995230cf478e9d..3ecb168380d47f57c921265b52308addc490fba4 100644 (file)
@@ -7,31 +7,85 @@
 #     list of:
 #       <md5><space><url>\n
 #     for given specfile.
-#   stderr:
-#     diagnostics
+#     except for lines prefixed with: ERROR:
 #   exit status:
 #     0 - normal
 #     2 - cannot open spec file
 #
 
-%macro = ();
+sub next_spec($)
+{
+  $spec = shift;
+  $base_spec = $spec;
+  $base_spec =~ s|.*/||;
+  %macro = ( "nil" => "" );
+  $err_cnt = 0;
+}
+
+sub error($)
+{
+  $err_cnt++;
+  print "ERROR: $base_spec: $_[0]\n";
+}
+
+sub warning($)
+{
+  print "ERROR: $base_spec: $_[0]\n";
+}
+
+sub trim_spaces($)
+{
+  my $v = shift;
+  
+  $v =~ s/\s+$//;
+  $v =~ s/^\s+//;
+  
+  return $v;
+}
 
 # expand macros in string
 sub expand($)
 {
-  my $v = shift;
+  my $v = trim_spaces(shift);
   my $cnt = 20;
-  
-  while ($cnt-- > 0 && $v =~ /\%\{([^\{]+)\}/) {
+
+  while ($v =~ /\%\{([^\}]+)\}/) {
     my $value;
     if (defined $macro{$1}) {
       $value = $macro{$1};
     } else {
-      $value = "\@undef:$1\@";
+      error("undefined macro $1");
+      $value = "UNDEFINED";
+    }
+    $v =~ s/\%\{([^\}]+)\}/$value/;
+
+    return $v if (length $v > 1000 or $cnt-- <= 0)
+  }
+
+  while ($v =~ s/\%\(\s*echo\s+([^\|]+?)\s*\|\s*tr\s*(-d|)\s+([^\)]+?)\s*\)/\@\@tr-me\@\@/) {
+    my ($what, $d_opt, $how) = ($1, $2, $3);
+    my ($from, $to) = ($how, "");
+    ($from, $to) = ($1, $2) 
+      if $how =~ /^([^\s]+)\s+([^\s]+)$/;
+    if ($d_opt and $to ne "") {
+      error("tr -d with second string)");
+    } elsif (($from . $to) =~ /^[a-zA-Z0-9\+_\-\.]+$/) {
+      if ($d_opt) {
+        eval "\$what =~ tr/$from//d;";
+      } else {
+        eval "\$what =~ tr/$from/$to/;";
+      }
+    } else {
+      error("illegal characters in tr string(s) '$from' '$to'");
     }
-    $v =~ s/\%\{([^\{]+)\}/$value/;
-    last if (length $v > 1000);
+    $v =~ s/\@\@tr-me\@\@/$what/;
+    
+    return $v if (length $v > 1000 or $cnt-- <= 0)
   }
+
+  error("unexpanded macros in $v")
+    if ($v =~ /\%[^0-9]/);
+  
   return $v;
 }
 
@@ -39,7 +93,7 @@ sub expand($)
 sub define($$)
 {
   my ($n, $v) = @_;
-  $macro{$n} = expand($v);
+  $macro{$n} = trim_spaces($v);
 }
 
 # sets hash of macros defined with %define or %global
@@ -56,7 +110,11 @@ sub parse_defines($)
     } elsif (/^Name\s*:\s*(.*)/i) {
       define("name", $1);
     } elsif (/^Source(\d+)\s*:\s*(.*)/i) {
-      define("source_$1", $2);
+      define("source_$1", expand($2));
+    } elsif (/^Patch(\d+)\s*:\s*(.*)/i) {
+      define("patch_$1", expand($2));
+    } elsif (/^NoSource\s*:\s*(\d+)\s*$/i) {
+      define("no_source_$1", "1");
     }
   }
   close(F);
@@ -68,24 +126,40 @@ sub print_md5($)
   while (<F>) {
     chomp;
     if (/^\s*#\s*source(\d+)-md5\s*:\s*([a-f0-9]{32})/i) {
-      if (defined $macro{"source_$1"}) {
-        my $s = $macro{"source_$1"};
-       my $md5 = $2;
-       if ($s =~ / / || $s =~ /^\%\{/ || $s =~ /\@undef/) {
-         print STDERR "$_[0]: source url $s is ill-formatted\n";
+      my $no = $1;
+      my $md5 = $2;
+      if (defined $macro{"no_source_$no"}) {
+        error("both NoSource: $no and md5 given");
+      } elsif (defined $macro{"source_$no"}) {
+        my $s = $macro{"source_$no"};
+       if ($s =~ /^([a-z0-9A-Z:\=\?\@\+\~\.\-\/_]|\%[0-9])+$/) {
+         if ($s =~ /^(ftp|http):\/\//) {
+           if ($s =~ /\/$/) {
+             error("source $no ($s) is directory");
+           } else {
+             if ($s =~ /:\/\/distfiles\.pld-linux\.org\/src/) {
+               $s =~ s|.*/||;
+               print "$md5 no-url-copy://$s\n";
+             } else {
+                print "$md5 $s\n";
+              }
+           }
+         } else {
+           $s =~ s|.*/||;
+           print "$md5 no-url://$s\n";
+         }
        } else {
-          print "$md5 $s\n";
+         error("source $no url $s is ill-formatted");
        }
       } else {
-        print STDERR "$_[0]: source $1 not defined\n";
+        error("source $no not defined");
       }
     }
   }
   close(F);
 }
 
-$spec = shift;
-
+next_spec(shift);
 parse_defines($spec);
 print_md5($spec);
 
This page took 0.03922 seconds and 4 git commands to generate.