Advertisement
Guest User

Untitled

a guest
May 27th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.09 KB | None | 0 0
  1. #!/usr/bin/perl -I lib
  2.  
  3. # tests should be run from
  4. # t/12-sysconfig.t
  5. use LedgerSMB::Sysconfig;
  6. use strict;
  7. use warnings;
  8. use Cwd ;
  9. use Test::More; # plan automatically generated below
  10. use File::Find;
  11.  
  12. my $ignore_regex = qr/EntryStore|environment\.PATH|database\.host|database\.port|database\.sslmode|log4perl_.*|printers\./;
  13. my $old_sysconfig = 0;
  14.  
  15. # We assume we are inside a git repository when running this.
  16. # but the current working dir could be at any level of the tree so findout what the top level is.
  17. #my $repodir = `git rev-parse --show-toplevel`;
  18. #chomp($repodir); # get rid of the trailing newline that the backtick call returns
  19. #if ( ! -d $repodir ) { $repodir = getcwd; } # if we aren't in a git repo or something went wrong, assume we are running from the top level dir and continue
  20. #if ( ! -r "lib/LedgerSMB.pm" ) { $repodir = Cwd::abs_path( "$repodir/../../" ); } # we probably aren't in a repo and are running from utils/devel or t/data*
  21. if ( ! -r "lib/LedgerSMB.pm" ) {
  22. print "\n\nERROR: we don't seem to be able to find the top level of the src tree.\n\n";
  23. exit;
  24. }
  25.  
  26. my $search_regex;
  27. my @matchingstrings;
  28. sub search {
  29. #$matchingstrings = '';
  30. return if $File::Find::name !~ m/\.([pP][lmLM])$/;
  31. return if $File::Find::dir !~ m%.*/doc/|.*/t/|blib%;
  32. my $tmpfilename = $_;
  33.  
  34. my $input;
  35. open $input, '<', $tmpfilename
  36. or die "$!";
  37. my @lines = <$input>;
  38. push @matchingstrings, grep { $_ =~ $search_regex } @lines ;
  39. close $input;
  40. }
  41.  
  42. # $match_pattern is used to filter the files with system grep
  43. # $file is a shell file glob relative to $repodir. it would normally be a specific path/name or '**' for all files
  44. # $substitution_pattern should have at least 1 sub expressions, only $1 (the result of the first sub expression is returned, everything else is deleted.
  45. sub code_grep {
  46. my ( $match_pattern, $file, $substitution_pattern, $ignore_regex ) = @_;
  47. $search_regex = $match_pattern;
  48. find(\&search, '.');
  49. my $keys = join("\n", @matchingstrings);
  50. $keys =~ s|$substitution_pattern|$1|g; # strip everything except the portion matching the first subexpression
  51. $keys =~ s/$ignore_regex//g if ( ! defined $ignore_regex ); # strip all keys these keys from the result as we want to ignore them for various reasons
  52. my %keys = map { $_ => 1 } split(/\n/, $keys); # create a hash from the result disposing of duplicates
  53. my @result = sort { "\L$a" cmp "\L$b" } keys %keys; # sort the keys case insensitively
  54. return @result;
  55. }
  56.  
  57. # A list of section.key available from Sysconfig
  58. my @available = '';
  59. if ( defined &LedgerSMB::Sysconfig::available_keys ) {
  60. @available = LedgerSMB::Sysconfig::available_keys();
  61. # remove any keys that match $ignore_regex
  62. my @del_indexes = reverse(grep { $available[$_] =~ $ignore_regex } 0..$#available);
  63. foreach my $item (@del_indexes) {
  64. splice (@available,$item,1);
  65. }
  66. } else {
  67. print "Old Sysconfig.pm being used\n";
  68. $old_sysconfig = 1;
  69. }
  70.  
  71. # find all "our [$@%]key" declarations and return an array the key names
  72. # this is to cover legacy keys still declared in Sysconfig.pm
  73. my @available_legacy = code_grep( '^[[:space:]]*[^#]*our[[:space:]][\$%@]', "lib/LedgerSMB/Sysconfig.pm", '.*our[[:space:]][\$@%]([0-9a-zA-Z_-]*).*', $ignore_regex );
  74.  
  75. my @allavailable = @available;
  76. push(@allavailable, @available_legacy);
  77.  
  78. # find all the LegacyKeys used in the source and return an array
  79. my @usedkeys_legacy = code_grep( 'LedgerSMB::Sysconfig::', '**', '.*LedgerSMB::Sysconfig::([0-9a-zA-Z_-]*).*', '' );
  80.  
  81. # The list of Keys missing from Sysconfig.pm
  82. my $missingkeys = '';
  83. foreach my $key ( @usedkeys_legacy ) {
  84. my $pattern = quotemeta( $key ); # quote meta chars so any unexpected chars don't crash the code, instead we simply won't match anything
  85. if ( ! grep( /\b$pattern$/, @allavailable) ) { # match against the end of each key so we ignore the section which isn't available in usedkeys_legacy
  86. $missingkeys .= " - $key\n";
  87. }
  88. }
  89.  
  90. #The list of Keys not found in the source
  91. my $unusedkeys;
  92. foreach my $key ( sort { "\L$a" cmp "\L$b" } @allavailable ) {
  93. my $pattern = quotemeta ( $key );
  94. $pattern =~ s/.*[.]//;
  95. $unusedkeys .= " - $key\n" if "@usedkeys_legacy" !~ m/\b$pattern\b/;
  96. }
  97.  
  98. # Generate some output
  99. if ( $old_sysconfig == 0 ) {
  100. # start by listing the legacy keys still provided by Sysconfig.pm
  101. print "========= legacy keys still in use ==========\n";
  102. foreach my $key (@available_legacy) {
  103. print " - $key\n" if $key !~ m/EntryStore/;
  104. }
  105. print "===============================================\n\n";
  106. }
  107. # now report any keys that are used but missing from Sysconfig.pm
  108. if ( defined $missingkeys) {
  109. print "These Keys are Used in the src but Missing from Sysconfig.pm\n";
  110. print "$missingkeys\n";
  111. }
  112.  
  113. # now report any keys that are provided by Sysconfig.pm but not used by or missing from the src
  114. if ( defined $unusedkeys) {
  115. print "These Keys are in Sysconfig.pm but Missing from the src\n";
  116. print "$unusedkeys\n";
  117. }
  118.  
  119. exit
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement