Blame view

scripts/parse-maintainers.pl 2.75 KB
7683e9e52   Linus Torvalds   Properly alphabet...
1
  #!/usr/bin/perl -w
b24413180   Greg Kroah-Hartman   License cleanup: ...
2
  # SPDX-License-Identifier: GPL-2.0
7683e9e52   Linus Torvalds   Properly alphabet...
3
4
  
  use strict;
fe9090301   Joe Perches   parse-maintainers...
5
  my $P = $0;
7683e9e52   Linus Torvalds   Properly alphabet...
6

61f741645   Joe Perches   parse-maintainers...
7
  # sort comparison functions
7683e9e52   Linus Torvalds   Properly alphabet...
8
9
10
11
12
13
14
15
16
  sub by_category($$) {
      my ($a, $b) = @_;
  
      $a = uc $a;
      $b = uc $b;
  
      # This always sorts last
      $a =~ s/THE REST/ZZZZZZ/g;
      $b =~ s/THE REST/ZZZZZZ/g;
61f741645   Joe Perches   parse-maintainers...
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
      return $a cmp $b;
  }
  
  sub by_pattern($$) {
      my ($a, $b) = @_;
      my $preferred_order = 'MRPLSWTQBCFXNK';
  
      my $a1 = uc(substr($a, 0, 1));
      my $b1 = uc(substr($b, 0, 1));
  
      my $a_index = index($preferred_order, $a1);
      my $b_index = index($preferred_order, $b1);
  
      $a_index = 1000 if ($a_index == -1);
      $b_index = 1000 if ($b_index == -1);
  
      if (($a1 =~ /^F$/ && $b1 =~ /^F$/) ||
  	($a1 =~ /^X$/ && $b1 =~ /^X$/)) {
  	return $a cmp $b;
      }
  
      if ($a_index < $b_index) {
  	return -1;
      } elsif ($a_index == $b_index) {
  	return 0;
      } else {
  	return 1;
      }
7683e9e52   Linus Torvalds   Properly alphabet...
45
  }
fe9090301   Joe Perches   parse-maintainers...
46
47
48
49
50
51
  sub trim {
      my $s = shift;
      $s =~ s/\s+$//;
      $s =~ s/^\s+//;
      return $s;
  }
7683e9e52   Linus Torvalds   Properly alphabet...
52
  sub alpha_output {
fe9090301   Joe Perches   parse-maintainers...
53
54
55
56
57
      my ($hashref, $filename) = (@_);
  
      open(my $file, '>', "$filename") or die "$P: $filename: open failed - $!
  ";
      foreach my $key (sort by_category keys %$hashref) {
61f741645   Joe Perches   parse-maintainers...
58
  	if ($key eq " ") {
fe9090301   Joe Perches   parse-maintainers...
59
60
  	    chomp $$hashref{$key};
  	    print $file $$hashref{$key};
61f741645   Joe Perches   parse-maintainers...
61
  	} else {
fe9090301   Joe Perches   parse-maintainers...
62
63
64
65
66
67
68
  	    print $file "
  " . $key . "
  ";
  	    foreach my $pattern (sort by_pattern split('
  ', %$hashref{$key})) {
  		print $file ($pattern . "
  ");
61f741645   Joe Perches   parse-maintainers...
69
70
  	    }
  	}
7683e9e52   Linus Torvalds   Properly alphabet...
71
      }
fe9090301   Joe Perches   parse-maintainers...
72
      close($file);
7683e9e52   Linus Torvalds   Properly alphabet...
73
74
75
  }
  
  sub file_input {
fe9090301   Joe Perches   parse-maintainers...
76
      my ($hashref, $filename) = (@_);
7683e9e52   Linus Torvalds   Properly alphabet...
77
78
      my $lastline = "";
      my $case = " ";
fe9090301   Joe Perches   parse-maintainers...
79
80
81
82
      $$hashref{$case} = "";
  
      open(my $file, '<', "$filename") or die "$P: $filename: open failed - $!
  ";
7683e9e52   Linus Torvalds   Properly alphabet...
83

fe9090301   Joe Perches   parse-maintainers...
84
      while (<$file>) {
7683e9e52   Linus Torvalds   Properly alphabet...
85
86
87
88
89
90
91
          my $line = $_;
  
          # Pattern line?
          if ($line =~ m/^([A-Z]):\s*(.*)/) {
              $line = $1 . ":\t" . trim($2) . "
  ";
              if ($lastline eq "") {
fe9090301   Joe Perches   parse-maintainers...
92
                  $$hashref{$case} = $$hashref{$case} . $line;
7683e9e52   Linus Torvalds   Properly alphabet...
93
94
95
                  next;
              }
              $case = trim($lastline);
fe9090301   Joe Perches   parse-maintainers...
96
97
              exists $$hashref{$case} and die "Header '$case' already exists";
              $$hashref{$case} = $line;
7683e9e52   Linus Torvalds   Properly alphabet...
98
99
100
101
102
              $lastline = "";
              next;
          }
  
          if ($case eq " ") {
fe9090301   Joe Perches   parse-maintainers...
103
              $$hashref{$case} = $$hashref{$case} . $lastline;
7683e9e52   Linus Torvalds   Properly alphabet...
104
105
106
107
108
109
              $lastline = $line;
              next;
          }
          trim($lastline) eq "" or die ("Odd non-pattern line '$lastline' for '$case'");
          $lastline = $line;
      }
fe9090301   Joe Perches   parse-maintainers...
110
111
      $$hashref{$case} = $$hashref{$case} . $lastline;
      close($file);
7683e9e52   Linus Torvalds   Properly alphabet...
112
  }
fe9090301   Joe Perches   parse-maintainers...
113
  my %hash;
b95c29a20   Joe Perches   parse-maintainers...
114
  my %new_hash;
fe9090301   Joe Perches   parse-maintainers...
115
116
  
  file_input(\%hash, "MAINTAINERS");
b95c29a20   Joe Perches   parse-maintainers...
117
118
119
120
121
122
123
124
125
  
  foreach my $type (@ARGV) {
      foreach my $key (keys %hash) {
  	if ($key =~ /$type/ || $hash{$key} =~ /$type/) {
  	    $new_hash{$key} = $hash{$key};
  	    delete $hash{$key};
  	}
      }
  }
fe9090301   Joe Perches   parse-maintainers...
126
  alpha_output(\%hash, "MAINTAINERS.new");
b95c29a20   Joe Perches   parse-maintainers...
127
  alpha_output(\%new_hash, "SECTION.new");
61f741645   Joe Perches   parse-maintainers...
128

7683e9e52   Linus Torvalds   Properly alphabet...
129
  exit(0);