/ Published in: Perl
Processe a directory of JSP source files, looks for regex matches, and reports on the number and location of matches. For detecting deprecated attributes in XHTML.
Uses Mail::Outlook to send an email. Requires you to click "ok" each time a notification is sent, but gets around firewalls.
Expand |
Embed | Plain Text
#! /usr/bin/perl -w use strict; use Mail::Outlook 'Attach'; #Use Outlook for notification, since port 25 is blocked. =item usage for MyEcom Redesign: cd /d X:\storm\main\webapps\neb\src\webapp\WEB-INF\jsp find \( -name "*jsp" -o -name "*jspf" \) | xargs perl z:\noah\standards\cssSelector\csel.pl for MyDev Redesign: cd /d X:\storm\nw2.0\webapps\neb\src\webapp\WEB-INF\jsp find \( -name "*jsp" -o -name "*jspf" \) | xargs perl z:\noah\standards\cssSelector\csel.pl =cut =item toDo - do the whole find-pipe-xargs thing inside this script --- check that we are searching the right directory - exclude directories or files by regex - name the output file after today's date - if there is a previous output file, compare the failure counts in that file to the current failure count; and notify of the difference. - make verbose command line option - toggle "send email" from the command line --- print the email body to STDOUT if not sending email - with a command line switch, append to or replace the list of "send to" addresses - optionally generate only ONE list, of all files that failed ANY test - generate the report in a more readable format, like HTML =cut #valid Outlook addresses: my $recipients = "MyDev Redesign Team;nsussman"; my (%longClass, %digits, %cssPropName, %tooShort, %JSprotocol, %obtrusiveJS); my $failed = 0; my $warned = 0; my $failCount = 0; my $warnCount = 0; my $failedLines = 0; my $warnedLines = 0; my $fileHasFailed = 0; my $fileWasWarned = 0; my $verbose = 1; my $send_email = 1; my $outfile = "z:\\Noah\\esCssRefactor\\CSS_best_practices_autoEvaluation.txt"; my $zipped_outfile = "z:\\Noah\\esCssRefactor\\CSS_best_practices_autoEvaluation.zip"; #my appname = "MyEcom Redesign"; my $appname = "MyDev Redesign"; checking_files: foreach my $input_file (@ARGV) { $. = 0; $fileHasFailed = 0; $fileWasWarned = 0; while (<FILE>) { #FAILURE CHECKS if (m/class=("[^"<]*(fnt|pad|mar|txt|teal|aqua)[^"<]*")/) { $cssPropName{ $input_file } .= "ln:$.: $1\n "; $failedLines++ unless ($failed == 1); $failed = 1; } if (m/class=("[^"<]{1,2}(\s+[^"<]+)*")/) { $tooShort{ $input_file } .= "ln:$.: $1\n "; $failedLines++ unless ($failed == 1); $failed = 1; } if (m/class=("[^"<]*\d\d[^"<]*")/) { $digits{ $input_file } .= "ln:$.: $1\n "; $failedLines++ unless ($failed == 1); $failed = 1; } if (m/class=("([^"<]+\s){2,}[^<"]+")/) { $longClass{ $input_file } .= "ln:$.: $1\n "; $failedLines++ unless ($failed == 1); $failed = 1; } #WARNING CHECKS if ((m/^\s*(.*javascript:.*)\s*$/) && $warned !=1) { $JSprotocol{ $input_file } .= "ln:$.: $1\n "; #should trim whitespace when including entire line $warnedLines++; $warned = 1; } if ((m/^\s*(.*\W(onabort|onblur|onchange|onclick|ondblclick|onerror|onfocus|onkeydown|onkeypress|onkeyup|onload|onmousedown|onmousemove|onmouseout|onmouseover|onmouseup|onreset|onresize|onselect|onsubmit|onunload).*)\s*$/i) && $warned !=1) { $obtrusiveJS{ $input_file } .= "ln:$.: $1\n "; $warnedLines++; $warned = 1; } #Keep track of failures if ($failed == 1) { if ($fileHasFailed == 0) { $failCount++; $fileHasFailed = 1; } $failedLines++; $failed = 0; } #Keep track of warnings if ($warned == 1) { if ($fileWasWarned == 0) { $warnCount++; $fileWasWarned = 1; } $warnedLines++; $warned = 0; } } } my $summary = <<"SUMMARY"; $appname automatic report: Spaghetti Code alerts: Files flagged: $failCount Lines flagged: $failedLines Deprecation warnings: Files warned: $warnCount Lines warned: $warnedLines This validator enforces browser code best practices as developed by the JAG Web 2.0 team. Note that the validator checks every file in the source directory, so some of the files flagged here may not actually be part of the live app. If you notice superfluous files in the list, please let us know so we can correct the problem. Spaghetti Code alerts (very bad): - More than three selectors in one CLASS declaration. - Selector names descriptive of CSS properties, for example: 'fnt, pad, teal' - Digits (0-9) in a selector name. Currently we think _one_ digit is OK. - selectors of less than 3 characters in length. Deprecation warnings (arguably not so bad): - "Obtrusive JavaScript". See http://jibbering.com/faq/#FAQ4_24 and http://www.kryogenix.org/code/browser/aqlists/ --- Use of the "javascript:" protocol. --- Some, but not all, uses of inline event handlers (onclick, onload, onmouseover, etc.) instead of event listeners. SUMMARY print OUTFILE $summary; print OUTFILE "Files that failed to validate are listed below. "; print OUTFILE "Each file is followed by a list of the lines that were problematic." if ($verbose == 1); print OUTFILE "\n"; print OUTFILE "\n========================\ntoo many selectors in a CLASS declaration:\n"; print OUTFILE " $key \n"; if ($verbose == 1) { print OUTFILE " $longClass{$key}\n"; } } print OUTFILE "\n========================\nSelector is less than 3 characters long:\n"; print OUTFILE " $key \n"; if ($verbose == 1) { print OUTFILE " $tooShort{$key}\n"; } } print OUTFILE "\n========================\n'mar, pad, teal' or other string that describes a CSS property, in a selector:\n"; print OUTFILE " $key \n"; if ($verbose == 1) { print OUTFILE " $cssPropName{$key}\n"; } } print OUTFILE "\n========================\ndigits (0-9) in a selector:\n"; print OUTFILE " $key \n"; if ($verbose == 1) { print OUTFILE " $digits{$key}\n"; } } print OUTFILE "\n|||||||||||||||||||| Only warnings below here |||||||||||||||||||||\n"; print OUTFILE "\n||||||||||||||||||||||||\nUses of the \"javascript:\" protocol:\n"; print OUTFILE " $key \n"; if ($verbose == 1) { print OUTFILE " $JSprotocol{$key}\n"; } } print OUTFILE "\n||||||||||||||||||||||||\nUses of inline event handlers:\n"; print OUTFILE " $key \n"; if ($verbose == 1) { print OUTFILE " $obtrusiveJS{$key}\n"; } } #manage notification if ($send_email) { my $outlook = new Mail::Outlook(); my @attachments; $attachments[0] = $zipped_outfile; # create a message for sending my $message = $outlook->create(); $message->To($recipients); $message->Cc(''); $message->Bcc(''); $message->Subject("$failCount files in $appname are becoming Spaghetti Code."); $message->Body("$summary\n\nA full report is attached."); $message->Attach(@attachments); $message->send; } #here are useful greps # # CLASS declaration with 3 or more selectors # grep -niP 'class="([^("<)]+\s){2,}[^(<")]+"' advsearch.jsp #
You need to login to post a comment.
