Apr/080
Bash: PHP Code Statistik Script
Heute geht es um ein kleines Script, welches ich mir vor einigen Monaten geschrieben habe um eine einfache Statistik über ein paar PHP-Scripte machen zu können. In erster Linie ging es mir darum ein Gefühl dafür zu bekommen, wie das Verhältnis von Code zu Kommentaren ist. Ganz nebenbei habe ich dann aber auch eine Zahl darüber bekommen, wie sich die Menge des Codes mit der Zeit entwickelt hat.
Das Script durchsucht ein Verzeichnis nach Dateien mit der Endung .php, diese Dateien werden zeilenweise durchlaufen. Bei diesem Durchlaufen des Codes wird jede Zeile in eine der folgenden Schubladen gesteckt:
1. Programm-Code
2. Kommentar
3. Leere Zeile
Nachdem alle Dateien durchlaufen wurden, wird die Gesamt-Summe der einzelnen Schubladen ausgegeben. Das Script kann man somit auf die einzelnen Tags oder Branches eines Repositories loslassen und damit eine schöne Code Historie erstellen.
Hier aber nun das Script:
#!/bin/bash ############################################################################## # analyze-nagvis-code.sh - Script for counting code and comments in PHP files # # Copyright (c) 2004-2008 NagVis Project (Contact: lars@vertical-visions.de) # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License version 2 as # published by the Free Software Foundation. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. ############################################################################## # Summary Count sumLines=0 sumComments=0 sumCode=0 label= if [ "$1" = "" ]; then echo "Error: Give the path to source directory as parameter" exit 1 fi nagvisPath=$1 if [ ! "$2" = "" ]; then label="$2," fi # Loop all PHP files for file in $(find $nagvisPath -name "*.php" ! -name "nagvis.ini.php" ! -name "config.ini.php" -type f -print); do # Count all lines lines=`egrep '^.*$' -c $file` # Count all lines which are comments (begin with #, //, \*, *) comments=`egrep '^[^A-Za-z0-9]*(#|\/\/|\/\*|\*)' -c $file` # Count all lines which are code (not empty and no comments) code=`egrep '^([^A-Za-z0-9]*(#|\/\/|\/\*|\*)|[^A-Za-z0-9]*$)' -v -c $file` # Summarize the lines sumLines=$(($sumLines+$lines)) sumComments=$(($sumComments+$comments)) sumCode=$(($sumCode+$code)) done # Summary Output echo "${label}$sumLines,$sumCode,$sumComments" exit 0
Wie man schon sehen kann, habe ich das Script für das NagVis-Projekt geschrieben. Dieses Script lasse ich auf jedes Release los um so den aktuellen Stand festzuhalten. Initial habe ich diese Statistik auch rückwirkend für alle vorherigen Releases erstellt. Dazu habe ich mir ein weiteres kleines Script geschrieben, welches die einzelnen Tags in meiner Arbeitskopie des Subversion-Repositories durchläuft und für jede getaggte Version eine Auswertung macht. Diese Auswertung wird dann in eine CSV-Datei geschrieben. Hier das zweite Script:
#!/bin/bash ############################################################################## # analyze-all-nagvis-versions.sh - Script for getting code statistics of all # available NagVis releases # # Copyright (c) 2004-2008 NagVis Project (Contact: lars@vertical-visions.de) # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License version 2 as # published by the Free Software Foundation. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. ############################################################################## WORK_DIR=/home/lami/Daten/nagvis TOOLS_DIR=/home/lami/Daten/nagvis-tools for version in `ls -1 ${WORK_DIR}/tags`; do ${TOOLS_DIR}/analyze-nagvis-code.sh ${WORK_DIR}/tags/$version $version >> ${TOOLS_DIR}/nagvis-stats.csv done
Mit Hilfe des zweiten Scripts wird nun eine CSV-Datei mit folgendem Inhalt erzeugt:
0.9a5,3887,2205,362 0.9b2,5727,3039,1093 0.9b3,5821,3128,1127 0.9b3+,5655,3180,1069 0.9b-workshop,5114,2752,785 0.9rc1-pre,5578,3130,1062 1.0,8378,4611,2039 1.0.1,8386,4621,2034 1.0.2,8396,4623,2041 1.0a1,5578,3130,1062 1.1,9800,5586,2244 1.1.1,9804,5589,2245 1.1.2,9804,5589,2245 1.1b1,9675,5519,2212 1.1b2,9833,5611,2250 1.1rc1,9846,5618,2251 1.1rc2,9867,5620,2260 1.1rc3,9750,5543,2244 1.2,11513,6574,2520 1.2.1,9851,5610,2318 1.2.2,9861,5615,2323 1.2b1,9968,5718,2272 1.2b2,9780,5575,2298 1.2rc1,9853,5611,2318 1.3b1,13407,6709,3958 1.3b2,13417,6713,3963 1.3b3,13471,6733,3976 1.3rc1,14628,6769,5040
Die Zahlen können nun z.B. mit Excel oder OpenOffice Spreadsheet grafisch noch etwas aufbereitet werden:
Mit ein paar kleinen Anpassungen lässt sich das Script sicherlich auf viele andere Projekte loslassen. Durch Anpassen der regulären Ausdrücke könnte das Script auch andere Sprachen verstehen…







