classaudit.pl 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #!/usr/bin/perl
  2. #
  3. # Audit tool for OpenSim class and namespace definitions.
  4. #
  5. # Copyright 2007 IBM
  6. #
  7. # Authors: Sean Dague
  8. #
  9. # Redistribution and use in source and binary forms, with or without
  10. # modification, are permitted provided that the following conditions are met:
  11. # * Redistributions of source code must retain the above copyright
  12. # notice, this list of conditions and the following disclaimer.
  13. # * Redistributions in binary form must reproduce the above copyright
  14. # notice, this list of conditions and the following disclaimer in the
  15. # documentation and/or other materials provided with the distribution.
  16. # * Neither the name of the OpenSim Project nor the
  17. # names of its contributors may be used to endorse or promote products
  18. # derived from this software without specific prior written permission.
  19. #
  20. # THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  21. # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  22. # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  23. # DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  24. # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  25. # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  26. # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  27. # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  29. # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. use strict;
  31. use File::Find;
  32. use Data::Dumper;
  33. use constant YELLOW => "\033[33m";
  34. use constant RED => "\033[31m";
  35. use constant CLEAR => "\033[0m";
  36. our %totals;
  37. find(\&test, "../../OpenSim");
  38. print Dumper(\%totals);
  39. sub test {
  40. my $file = $File::Find::name;
  41. my $dir = $File::Find::dir;
  42. $file =~ s{^../../}{}; #strip off prefix
  43. $dir =~ s{^../../}{}; #strip off prefix
  44. return if ($file !~ /\.cs$/);
  45. return if ($file =~ /AssemblyInfo\.cs$/);
  46. print "Processing File: $file\n";
  47. my $namespace = find_namespace($_);
  48. my $class = find_class($_);
  49. if(cmp_namespace($namespace, $dir) == 1) {
  50. $totals{goodns}++;
  51. } else {
  52. $totals{badns}++;
  53. }
  54. if(cmp_class($namespace, $class, $file) == 1) {
  55. $totals{goodclass}++;
  56. } else {
  57. $totals{badclass}++;
  58. }
  59. print "\n";
  60. }
  61. sub find_class {
  62. my $file = shift;
  63. my $content = slurp($file);
  64. if ($content =~ /\n\s*(public|private|protected)?\s*(class|interface)\s+(\S+)/) {
  65. return $3;
  66. }
  67. return "";
  68. }
  69. sub find_namespace {
  70. my $file = shift;
  71. my $content = slurp($file);
  72. if ($content =~ /\bnamespace\s+(\S+)/s) {
  73. return $1;
  74. }
  75. return "";
  76. }
  77. sub slurp {
  78. my $file = shift;
  79. local(*IN);
  80. local $/ = undef;
  81. open(IN, "$file") or die "Can't open '$file': $!";
  82. my $content = <IN>;
  83. close(IN);
  84. return $content;
  85. }
  86. sub cmp_class {
  87. my ($ns, $class, $file) = @_;
  88. $class = "$ns.$class";
  89. my $classtrans = $class;
  90. $classtrans =~ s{\.}{/}g;
  91. $classtrans .= ".cs";
  92. if($classtrans ne $file) {
  93. error(YELLOW, "CLASS: $class != $file");
  94. return -1;
  95. }
  96. return 1;
  97. }
  98. sub cmp_namespace {
  99. my ($ns, $dir) = @_;
  100. my $nstrans = $ns;
  101. $nstrans =~ s{\.}{/}g;
  102. if($nstrans ne $dir) {
  103. error(RED, "NS: $ns != $dir");
  104. return -1;
  105. }
  106. return 1;
  107. }
  108. sub error {
  109. print @_, CLEAR, "\n";
  110. }