PageRenderTime 23ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/external/chromium_org/android_webview/tools/find_copyrights.pl

https://gitlab.com/brian0218/rk3188_rk3066_r-box_android4.4.2_sdk
Perl | 161 lines | 150 code | 6 blank | 5 comment | 2 complexity | 6d81c37dcbf0f85e2e28a7cbc9e540e1 MD5 | raw file
  1. #!/usr/bin/perl -w
  2. # Copyright (c) 2013 The Chromium Authors. All rights reserved.
  3. # Use of this source code is governed by a BSD-style license that can be
  4. # found in the LICENSE file.
  5. # Use: find_copyrights.pl <start-from> [exclude-dir ...]
  6. use strict;
  7. use warnings;
  8. use File::Basename;
  9. sub check_is_generated_file($);
  10. sub start_copyright_parsing();
  11. my $progname = basename($0);
  12. my $root_dir = shift @ARGV;
  13. my @find_args = ();
  14. while (@ARGV) {
  15. my $path = shift @ARGV;
  16. push @find_args, qw'-not ( -path', "*/$path/*", qw'-prune )'
  17. }
  18. push @find_args, qw(-follow -type f -print);
  19. open FIND, '-|', 'find', $root_dir, @find_args
  20. or die "$progname: Couldn't exec find: $!\n";
  21. my $check_regex = '\.(asm|c(c|pp|xx)?|h(h|pp|xx)?|p(l|m)|xs|sh|php|py(|x)' .
  22. '|rb|idl|java|el|sc(i|e)|cs|pas|inc|js|pac|html|dtd|xsl|mod|mm?' .
  23. '|tex|mli?)$';
  24. my @files = ();
  25. while (<FIND>) {
  26. chomp;
  27. push @files, $_ unless (-z $_ || !m%$check_regex%);
  28. }
  29. close FIND;
  30. my $generated_file_scan_boundary = 25;
  31. while (@files) {
  32. my $file = shift @files;
  33. my $file_header = '';
  34. my %copyrights;
  35. open (F, "<$file") or die "$progname: Unable to access $file\n";
  36. my $parse_copyright = start_copyright_parsing();
  37. while (<F>) {
  38. $file_header .= $_ unless $. > $generated_file_scan_boundary;
  39. my $copyright_match = $parse_copyright->($_, $.);
  40. if ($copyright_match) {
  41. $copyrights{lc("$copyright_match")} = "$copyright_match";
  42. }
  43. }
  44. close(F);
  45. my $copyright = join(" / ", values %copyrights);
  46. print "$file\t";
  47. if (check_is_generated_file($file_header)) {
  48. print "GENERATED FILE";
  49. } else {
  50. print ($copyright or "*No copyright*");
  51. }
  52. print "\n";
  53. }
  54. sub check_is_generated_file($) {
  55. my $license = uc($_[0]);
  56. # Remove Python multiline comments to avoid false positives
  57. if (index($license, '"""') != -1) {
  58. $license =~ s/"""[^"]*(?:"""|$)//mg;
  59. }
  60. if (index($license, "'''") != -1) {
  61. $license =~ s/'''[^']*(?:'''|$)//mg;
  62. }
  63. # Quick checks using index.
  64. if (index($license, 'ALL CHANGES MADE IN THIS FILE WILL BE LOST') != -1) {
  65. return 1;
  66. }
  67. if (index($license, 'DO NOT EDIT') != -1 ||
  68. index($license, 'DO NOT DELETE') != -1 ||
  69. index($license, 'GENERATED') != -1) {
  70. return ($license =~ /(All changes made in this file will be lost' .
  71. 'DO NOT (EDIT|delete this file)|Generated (at|automatically|data)' .
  72. '|Automatically generated|\Wgenerated\s+(?:\w+\s+)*file\W)/i);
  73. }
  74. return 0;
  75. }
  76. sub are_within_increasing_progression($$$) {
  77. my $delta = $_[0] - $_[1];
  78. return $delta >= 0 && $delta <= $_[2];
  79. }
  80. sub start_copyright_parsing() {
  81. my $max_line_numbers_proximity = 3;
  82. # Set up the defaults the way that proximity checks will not succeed.
  83. my $last_a_item_line_number = -200;
  84. my $last_b_item_line_number = -100;
  85. return sub {
  86. my $line = $_[0];
  87. my $line_number = $_[1];
  88. # Remove C / C++ strings to avoid false positives.
  89. if (index($line, '"') != -1) {
  90. $line =~ s/"[^"\\]*(?:\\.[^"\\]*)*"//g;
  91. }
  92. my $uc_line = uc($line);
  93. # Record '(a)' and '(b)' last occurences in C++ comments.
  94. my $cpp_comment_idx = index($uc_line, '//');
  95. if ($cpp_comment_idx != -1) {
  96. if (index($uc_line, '(A)') > $cpp_comment_idx) {
  97. $last_a_item_line_number = $line_number;
  98. }
  99. if (index($uc_line, '(B)') > $cpp_comment_idx) {
  100. $last_b_item_line_number = $line_number;
  101. }
  102. }
  103. # Fast bailout, uses the same patterns as the regexp.
  104. if (index($uc_line, 'COPYRIGHT') == -1 &&
  105. index($uc_line, 'COPR.') == -1 &&
  106. index($uc_line, '\x{00a9}') == -1 &&
  107. index($uc_line, '\xc2\xa9') == -1) {
  108. my $c_item_index = index($uc_line, '(C)');
  109. return '' if ($c_item_index == -1);
  110. # Filter out 'c' used as a list item inside C++ comments.
  111. # E.g. "// blah-blah (a) blah\n// blah-blah (b) and (c) blah"
  112. if ($c_item_index > $cpp_comment_idx &&
  113. are_within_increasing_progression(
  114. $line_number,
  115. $last_b_item_line_number,
  116. $max_line_numbers_proximity) &&
  117. are_within_increasing_progression(
  118. $last_b_item_line_number,
  119. $last_a_item_line_number,
  120. $max_line_numbers_proximity)) {
  121. return '';
  122. }
  123. }
  124. my $copyright_indicator_regex =
  125. '(?:copyright|copr\.|\x{00a9}|\xc2\xa9|\(c\))';
  126. my $copyright_disindicator_regex =
  127. '\b(?:info(?:rmation)?|notice|and|or)\b';
  128. my $copyright = '';
  129. if ($line =~ m%\W$copyright_indicator_regex(?::\s*|\s+)(\w.*)$%i) {
  130. my $match = $1;
  131. if ($match !~ m%^\s*$copyright_disindicator_regex%i) {
  132. $match =~ s/([,.])?\s*$//;
  133. $match =~ s/$copyright_indicator_regex//ig;
  134. $match =~ s/^\s+//;
  135. $match =~ s/\s{2,}/ /g;
  136. $match =~ s/\\@/@/g;
  137. $copyright = $match;
  138. }
  139. }
  140. return $copyright;
  141. }
  142. }