Blame view

scripts/parse-maintainers.pl 4.54 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;
1e6270d07   Joe Perches   parse-maintainers...
5
6
7
8
9
10
  use Getopt::Long qw(:config no_auto_abbrev);
  
  my $input_file = "MAINTAINERS";
  my $output_file = "MAINTAINERS.new";
  my $output_section = "SECTION.new";
  my $help = 0;
5cdbec108   Joe Perches   parse-maintainers...
11
  my $order = 0;
fe9090301   Joe Perches   parse-maintainers...
12
  my $P = $0;
7683e9e52   Linus Torvalds   Properly alphabet...
13

1e6270d07   Joe Perches   parse-maintainers...
14
15
16
17
  if (!GetOptions(
  		'input=s' => \$input_file,
  		'output=s' => \$output_file,
  		'section=s' => \$output_section,
5cdbec108   Joe Perches   parse-maintainers...
18
  		'order!' => \$order,
1e6270d07   Joe Perches   parse-maintainers...
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
  		'h|help|usage' => \$help,
  	    )) {
      die "$P: invalid argument - use --help if necessary
  ";
  }
  
  if ($help != 0) {
      usage();
      exit 0;
  }
  
  sub usage {
      print <<EOT;
  usage: $P [options] <pattern matching regexes>
  
    --input => MAINTAINERS file to read (default: MAINTAINERS)
    --output => sorted MAINTAINERS file to write (default: MAINTAINERS.new)
    --section => new sorted MAINTAINERS file to write to (default: SECTION.new)
5cdbec108   Joe Perches   parse-maintainers...
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
    --order => Use the preferred section content output ordering (default: 0)
      Preferred ordering of section output is:
        M:  Person acting as a maintainer
        R:  Person acting as a patch reviewer
        L:  Mailing list where patches should be sent
        S:  Maintenance status
        W:  URI for general information
        Q:  URI for patchwork tracking
        B:  URI for bug tracking/submission
        C:  URI for chat
        P:  URI or file for subsystem specific coding styles
        T:  SCM tree type and location
        F:  File and directory pattern
        X:  File and directory exclusion pattern
        N:  File glob
        K:  Keyword - patch content regex
1e6270d07   Joe Perches   parse-maintainers...
53
54
55
56
57
58
59
  
  If <pattern match regexes> exist, then the sections that match the
  regexes are not written to the output file but are written to the
  section file.
  
  EOT
  }
61f741645   Joe Perches   parse-maintainers...
60
  # sort comparison functions
7683e9e52   Linus Torvalds   Properly alphabet...
61
62
63
64
65
66
67
68
69
  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...
70
71
72
73
74
      return $a cmp $b;
  }
  
  sub by_pattern($$) {
      my ($a, $b) = @_;
5cdbec108   Joe Perches   parse-maintainers...
75
      my $preferred_order = 'MRLSWQBCPTFXNK';
61f741645   Joe Perches   parse-maintainers...
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
  
      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...
98
  }
fe9090301   Joe Perches   parse-maintainers...
99
100
101
102
103
104
  sub trim {
      my $s = shift;
      $s =~ s/\s+$//;
      $s =~ s/^\s+//;
      return $s;
  }
7683e9e52   Linus Torvalds   Properly alphabet...
105
  sub alpha_output {
fe9090301   Joe Perches   parse-maintainers...
106
      my ($hashref, $filename) = (@_);
1e6270d07   Joe Perches   parse-maintainers...
107
      return if ! scalar(keys %$hashref);
fe9090301   Joe Perches   parse-maintainers...
108
109
      open(my $file, '>', "$filename") or die "$P: $filename: open failed - $!
  ";
1e6270d07   Joe Perches   parse-maintainers...
110
      my $separator;
fe9090301   Joe Perches   parse-maintainers...
111
      foreach my $key (sort by_category keys %$hashref) {
61f741645   Joe Perches   parse-maintainers...
112
  	if ($key eq " ") {
fe9090301   Joe Perches   parse-maintainers...
113
  	    print $file $$hashref{$key};
61f741645   Joe Perches   parse-maintainers...
114
  	} else {
1e6270d07   Joe Perches   parse-maintainers...
115
116
117
118
119
120
121
122
  	    if (! defined $separator) {
  		$separator = "
  ";
  	    } else {
  		print $file $separator;
  	    }
  	    print $file $key . "
  ";
5cdbec108   Joe Perches   parse-maintainers...
123
124
125
126
127
128
129
130
131
132
133
134
  	    if ($order) {
  		foreach my $pattern (sort by_pattern split('
  ', %$hashref{$key})) {
  		    print $file ($pattern . "
  ");
  		}
  	    } else {
  		foreach my $pattern (split('
  ', %$hashref{$key})) {
  		    print $file ($pattern . "
  ");
  		}
61f741645   Joe Perches   parse-maintainers...
135
136
  	    }
  	}
7683e9e52   Linus Torvalds   Properly alphabet...
137
      }
fe9090301   Joe Perches   parse-maintainers...
138
      close($file);
7683e9e52   Linus Torvalds   Properly alphabet...
139
140
141
  }
  
  sub file_input {
fe9090301   Joe Perches   parse-maintainers...
142
      my ($hashref, $filename) = (@_);
7683e9e52   Linus Torvalds   Properly alphabet...
143
144
      my $lastline = "";
      my $case = " ";
fe9090301   Joe Perches   parse-maintainers...
145
146
147
148
      $$hashref{$case} = "";
  
      open(my $file, '<', "$filename") or die "$P: $filename: open failed - $!
  ";
7683e9e52   Linus Torvalds   Properly alphabet...
149

fe9090301   Joe Perches   parse-maintainers...
150
      while (<$file>) {
7683e9e52   Linus Torvalds   Properly alphabet...
151
152
153
154
155
156
157
          my $line = $_;
  
          # Pattern line?
          if ($line =~ m/^([A-Z]):\s*(.*)/) {
              $line = $1 . ":\t" . trim($2) . "
  ";
              if ($lastline eq "") {
fe9090301   Joe Perches   parse-maintainers...
158
                  $$hashref{$case} = $$hashref{$case} . $line;
7683e9e52   Linus Torvalds   Properly alphabet...
159
160
161
                  next;
              }
              $case = trim($lastline);
fe9090301   Joe Perches   parse-maintainers...
162
163
              exists $$hashref{$case} and die "Header '$case' already exists";
              $$hashref{$case} = $line;
7683e9e52   Linus Torvalds   Properly alphabet...
164
165
166
167
168
              $lastline = "";
              next;
          }
  
          if ($case eq " ") {
fe9090301   Joe Perches   parse-maintainers...
169
              $$hashref{$case} = $$hashref{$case} . $lastline;
7683e9e52   Linus Torvalds   Properly alphabet...
170
171
172
173
174
175
              $lastline = $line;
              next;
          }
          trim($lastline) eq "" or die ("Odd non-pattern line '$lastline' for '$case'");
          $lastline = $line;
      }
fe9090301   Joe Perches   parse-maintainers...
176
177
      $$hashref{$case} = $$hashref{$case} . $lastline;
      close($file);
7683e9e52   Linus Torvalds   Properly alphabet...
178
  }
fe9090301   Joe Perches   parse-maintainers...
179
  my %hash;
b95c29a20   Joe Perches   parse-maintainers...
180
  my %new_hash;
fe9090301   Joe Perches   parse-maintainers...
181

1e6270d07   Joe Perches   parse-maintainers...
182
  file_input(\%hash, $input_file);
b95c29a20   Joe Perches   parse-maintainers...
183
184
185
186
187
188
189
190
191
  
  foreach my $type (@ARGV) {
      foreach my $key (keys %hash) {
  	if ($key =~ /$type/ || $hash{$key} =~ /$type/) {
  	    $new_hash{$key} = $hash{$key};
  	    delete $hash{$key};
  	}
      }
  }
1e6270d07   Joe Perches   parse-maintainers...
192
193
  alpha_output(\%hash, $output_file);
  alpha_output(\%new_hash, $output_section);
61f741645   Joe Perches   parse-maintainers...
194

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