parse-maintainers.pl 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. #!/usr/bin/perl -w
  2. # SPDX-License-Identifier: GPL-2.0
  3. use strict;
  4. use Getopt::Long qw(:config no_auto_abbrev);
  5. my $input_file = "MAINTAINERS";
  6. my $output_file = "MAINTAINERS.new";
  7. my $output_section = "SECTION.new";
  8. my $help = 0;
  9. my $order = 0;
  10. my $P = $0;
  11. if (!GetOptions(
  12. 'input=s' => \$input_file,
  13. 'output=s' => \$output_file,
  14. 'section=s' => \$output_section,
  15. 'order!' => \$order,
  16. 'h|help|usage' => \$help,
  17. )) {
  18. die "$P: invalid argument - use --help if necessary\n";
  19. }
  20. if ($help != 0) {
  21. usage();
  22. exit 0;
  23. }
  24. sub usage {
  25. print <<EOT;
  26. usage: $P [options] <pattern matching regexes>
  27. --input => MAINTAINERS file to read (default: MAINTAINERS)
  28. --output => sorted MAINTAINERS file to write (default: MAINTAINERS.new)
  29. --section => new sorted MAINTAINERS file to write to (default: SECTION.new)
  30. --order => Use the preferred section content output ordering (default: 0)
  31. Preferred ordering of section output is:
  32. M: Person acting as a maintainer
  33. R: Person acting as a patch reviewer
  34. L: Mailing list where patches should be sent
  35. S: Maintenance status
  36. W: URI for general information
  37. Q: URI for patchwork tracking
  38. B: URI for bug tracking/submission
  39. C: URI for chat
  40. P: URI or file for subsystem specific coding styles
  41. T: SCM tree type and location
  42. F: File and directory pattern
  43. X: File and directory exclusion pattern
  44. N: File glob
  45. K: Keyword - patch content regex
  46. If <pattern match regexes> exist, then the sections that match the
  47. regexes are not written to the output file but are written to the
  48. section file.
  49. EOT
  50. }
  51. # sort comparison functions
  52. sub by_category($$) {
  53. my ($a, $b) = @_;
  54. $a = uc $a;
  55. $b = uc $b;
  56. # This always sorts last
  57. $a =~ s/THE REST/ZZZZZZ/g;
  58. $b =~ s/THE REST/ZZZZZZ/g;
  59. return $a cmp $b;
  60. }
  61. sub by_pattern($$) {
  62. my ($a, $b) = @_;
  63. my $preferred_order = 'MRLSWQBCPTFXNK';
  64. my $a1 = uc(substr($a, 0, 1));
  65. my $b1 = uc(substr($b, 0, 1));
  66. my $a_index = index($preferred_order, $a1);
  67. my $b_index = index($preferred_order, $b1);
  68. $a_index = 1000 if ($a_index == -1);
  69. $b_index = 1000 if ($b_index == -1);
  70. if (($a1 =~ /^F$/ && $b1 =~ /^F$/) ||
  71. ($a1 =~ /^X$/ && $b1 =~ /^X$/)) {
  72. return $a cmp $b;
  73. }
  74. if ($a_index < $b_index) {
  75. return -1;
  76. } elsif ($a_index == $b_index) {
  77. return 0;
  78. } else {
  79. return 1;
  80. }
  81. }
  82. sub trim {
  83. my $s = shift;
  84. $s =~ s/\s+$//;
  85. $s =~ s/^\s+//;
  86. return $s;
  87. }
  88. sub alpha_output {
  89. my ($hashref, $filename) = (@_);
  90. return if ! scalar(keys %$hashref);
  91. open(my $file, '>', "$filename") or die "$P: $filename: open failed - $!\n";
  92. my $separator;
  93. foreach my $key (sort by_category keys %$hashref) {
  94. if ($key eq " ") {
  95. print $file $$hashref{$key};
  96. } else {
  97. if (! defined $separator) {
  98. $separator = "\n";
  99. } else {
  100. print $file $separator;
  101. }
  102. print $file $key . "\n";
  103. if ($order) {
  104. foreach my $pattern (sort by_pattern split('\n', %$hashref{$key})) {
  105. print $file ($pattern . "\n");
  106. }
  107. } else {
  108. foreach my $pattern (split('\n', %$hashref{$key})) {
  109. print $file ($pattern . "\n");
  110. }
  111. }
  112. }
  113. }
  114. close($file);
  115. }
  116. sub file_input {
  117. my ($hashref, $filename) = (@_);
  118. my $lastline = "";
  119. my $case = " ";
  120. $$hashref{$case} = "";
  121. open(my $file, '<', "$filename") or die "$P: $filename: open failed - $!\n";
  122. while (<$file>) {
  123. my $line = $_;
  124. # Pattern line?
  125. if ($line =~ m/^([A-Z]):\s*(.*)/) {
  126. $line = $1 . ":\t" . trim($2) . "\n";
  127. if ($lastline eq "") {
  128. $$hashref{$case} = $$hashref{$case} . $line;
  129. next;
  130. }
  131. $case = trim($lastline);
  132. exists $$hashref{$case} and die "Header '$case' already exists";
  133. $$hashref{$case} = $line;
  134. $lastline = "";
  135. next;
  136. }
  137. if ($case eq " ") {
  138. $$hashref{$case} = $$hashref{$case} . $lastline;
  139. $lastline = $line;
  140. next;
  141. }
  142. trim($lastline) eq "" or die ("Odd non-pattern line '$lastline' for '$case'");
  143. $lastline = $line;
  144. }
  145. $$hashref{$case} = $$hashref{$case} . $lastline;
  146. close($file);
  147. }
  148. my %hash;
  149. my %new_hash;
  150. file_input(\%hash, $input_file);
  151. foreach my $type (@ARGV) {
  152. foreach my $key (keys %hash) {
  153. if ($key =~ /$type/ || $hash{$key} =~ /$type/) {
  154. $new_hash{$key} = $hash{$key};
  155. delete $hash{$key};
  156. }
  157. }
  158. }
  159. alpha_output(\%hash, $output_file);
  160. alpha_output(\%new_hash, $output_section);
  161. exit(0);