gitweb on Svarog

projekti pod git sistemom za održavanje verzija -- projects under the git version control system
version 0.2.2 - minor improvements
[mjc2wsl.git] / src-wsl / metrics_csv.wsl
1 C:"Doni Pracner (c) 2015,2017,2018";
2 C:"
3 This program is free software; you can redistribute it
4 and/or modify it under the terms of the GNU General Public
5 License as published by the Free Software Foundation; either
6 version 3 of the License, or (at your option) any later
7 version.
9 This program is distributed in the hope that it will be
10 useful, but WITHOUT ANY WARRANTY; without even the implied
11 warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12 PURPOSE. See the GNU General Public License for more
13 details.
15 You should have received a copy of the GNU General Public
16 License along with this program. If not, see
17 <http://www.gnu.org/licenses/>.
18 ==========================================================";
20 C:"This program generates metrics for WSL programs,
21 including options for comparing two programs (mainly ment to
22 compare different versions) and output differences as direct
23 numbers and percentages in a CSV type of format.
25 One of the goals was to enable this to be used both in the
26 command line for quick checks but also to be used for
27 automation and creation of CSV files for collections of
28 programs.";
30 C:"usage {option} {filename}";
32 C:"Options";
33 C:" -h or --help - help screen";
34 C:" -H or --header - print header for metrics";
35 C:" -c 'file1' 'file2' - compare two files and print the metrics";
36 C:" -HC print header for comparison";
37 C:" -o 'file' - set output for the print";
38 C:" -s 'separator' - set the separator in outputs (default comma)";
39 C:" otherwise print the metrics of the given file(s)";
41 C:"options are processed in sequence, so you can have
42 for instance multiple outputs during the execution and
43 run it for different programs";
45 C:"examples;
47 'filename' 'file2' 'file3'
48 - just output the metrics all files on the screen,
49 each in it's own row
51 -c 'f1' 'f2'
52 - compare two files and output the differences on the
53 screen
55 -o res.tmp -HC -c 'f1' 'f1a' -c 'f2' 'f2a'
56 - output a header row and two rows comparing the given
57 files in the specified file
58 ";
60 Field_Separator := ",";
62 MW_PROC @Get_New_Name(VAR str) ==
63 IF @Ends_With?(str, ".wsl") THEN
64 str := SUBSTR(str, 0, SLENGTH(str)-4)
65 FI;
66 str := str ++ ".met"
67 END;
69 MW_PROC @Process_File(filename VAR metricslist) ==
70 metricslist := < >;
71 IF @File_Exists?(filename) THEN
72 @New_Program(@Parse_File(filename, T_Statements));
74 C:"add them in reverse to the start of the list";
75 metricslist := < @Struct_Metric(@Program) > ++ metricslist ;
76 metricslist := < @Total_Size(@Program) > ++ metricslist ;
77 metricslist := < @CFDF_Metric(@Program) > ++ metricslist ;
78 metricslist := < @Gen_Type_Count("T_Expression",@Program) > ++ metricslist ;
79 metricslist := < @Stat_Count(@Program) > ++ metricslist ;
80 metricslist := < @Essential(@Program) > ++ metricslist ;
81 metricslist := < @McCabe(@Program) > ++ metricslist
82 ELSE
83 PRINT("ERROR: File ",filename," not found");
84 FI
85 END;
87 MW_PROC @Write_Metrics(metrics VAR) ==
88 FOR met IN metrics DO
89 @WS(Field_Separator);
90 @WN(met)
91 OD
92 END;
94 MW_PROC @Write_Metrics_List(prefix VAR) ==
95 @WS(prefix);@WS("McCabe Cyclo");@WS(Field_Separator);
96 @WS(prefix);@WS("McCabe Essential");@WS(Field_Separator);
97 @WS(prefix);@WS("Statements");@WS(Field_Separator);
98 @WS(prefix);@WS("Expressions");@WS(Field_Separator);
99 @WS(prefix);@WS("CFDF");@WS(Field_Separator);
100 @WS(prefix);@WS("Size");@WS(Field_Separator);
101 @WS(prefix);@WS("Structure")
102 END;
104 MW_PROC @Metrics_Main() ==
105 VAR< prog := < >,
106 filename:="", filename2 := "",
107 metrics := < >, met2 := < >,
108 opened := 0,
109 Argv := ARGV
110 >:
111 C:"First one is the script name that is being executed";
112 Argv := TAIL(Argv);
114 IF Argv = < > THEN
115 PRINT("no arguments passed; supply a filename to make metrics for ");
116 ELSE
117 WHILE Argv <> < > DO
118 POP(filename,Argv);
119 IF filename = "-h" OR filename = "--help" THEN
120 PRINT("HELP - for now in the comments at the start of the script");
121 PRINT("options: --header or -H | -c | -HC | -o | -s");
122 SKIP
123 ELSIF filename = "-H" OR filename = "--header" THEN
124 @WS("filename");@WS(Field_Separator);
125 @Write_Metrics_List("");
126 @WL("");
127 ELSIF filename = "-HC" THEN
128 C:"Header for comparison";
129 @WS("filename");@WS(Field_Separator);
130 @Write_Metrics_List("P1-");@WS(Field_Separator);
131 @Write_Metrics_List("P2-");@WS(Field_Separator);
132 @Write_Metrics_List("DIFF-");@WS(Field_Separator);
133 @Write_Metrics_List("%-");
134 @WL("");
135 ELSIF filename = "-o" THEN
136 C:"set output";
137 IF Argv = < > THEN
138 PRINT("argument needed after -o")
139 ELSE
140 POP(filename, Argv);
141 opened := opened + 1;
142 @Write_To(filename)
143 FI
144 ELSIF filename = "-s" THEN
145 C:"set separator";
146 IF Argv = < > THEN
147 PRINT("argument needed after -s")
148 ELSE
149 POP(Field_Separator, Argv);
150 FI
151 ELSIF filename = "-c" THEN
152 C:"compare two files and dump the comparison";
153 IF LENGTH(Argv) < 2 THEN
154 PRINT("two arguments needed after -c")
155 ELSE
156 POP(filename, Argv);
158 @Process_File(filename VAR metrics);
159 @WS(filename);
160 @Write_Metrics(metrics);
162 POP(filename2,Argv);
163 @Process_File(filename2 VAR met2);
164 @Write_Metrics(met2);
166 C:"calculate the differences";
167 FOR i := 1 TO LENGTH(metrics) STEP 1 DO
168 met2[i] := metrics[i] - met2[i];
169 IF metrics[i] <> 0 THEN
170 metrics[i] := met2[i] * 100 DIV metrics[i]
171 FI
172 OD;
173 @Write_Metrics(met2);
174 @Write_Metrics(metrics);
176 @WL("");
177 FI;
178 SKIP
179 ELSE
180 @Process_File(filename VAR metrics);
181 @WS(filename);
182 @Write_Metrics(metrics);
183 @WL("");
184 SKIP
185 FI
186 OD;
187 C:"be nice and close all the opened writes";
188 FOR count := 1 TO opened STEP 1 DO
189 @End_Write
190 OD
191 FI
192 ENDVAR
193 END;
195 @Metrics_Main()
Svarog.pmf.uns.ac.rs/gitweb maintanance Doni Pracner