2
0

dbdigest.pp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. {
  2. This file is part of the Free Pascal test suite.
  3. Copyright (c) 2002 by the Free Pascal development team.
  4. This program generates a digest
  5. of the last tests run.
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. {$mode objfpc}
  13. {$h+}
  14. program digest;
  15. uses
  16. sysutils,teststr,testu,tresults,dbtests;
  17. Var
  18. StatusCount : Array[TTestStatus] of Integer;
  19. UnknownLines : integer;
  20. Procedure ExtractTestFileName(Var Line : string);
  21. Var I : integer;
  22. begin
  23. I:=Pos(' ',Line);
  24. If (I<>0) then
  25. Line:=Copy(Line,1,I-1);
  26. end;
  27. Function Analyse(Var Line : string; Var Status : TTestStatus) : Boolean;
  28. Var
  29. TS : TTestStatus;
  30. begin
  31. Result:=False;
  32. For TS:=FirstStatus to LastStatus do
  33. begin
  34. Result:=Pos(StatusText[TS],Line)=1;
  35. If Result then
  36. begin
  37. Status:=TS;
  38. Delete(Line,1,Length(StatusText[TS]));
  39. ExtractTestFileName(Line);
  40. Break;
  41. end;
  42. end;
  43. end;
  44. Type
  45. TConfigOpt = (
  46. coDatabaseName,
  47. soHost,
  48. coUserName,
  49. coPassword,
  50. coLogFile,
  51. coOS,
  52. coCPU,
  53. coVersion,
  54. coDate,
  55. coSubmitter,
  56. coMachine,
  57. coComment,
  58. coTestSrcDir,
  59. coVerbose
  60. );
  61. Const
  62. ConfigStrings : Array [TConfigOpt] of string = (
  63. 'databasename',
  64. 'host',
  65. 'username',
  66. 'password',
  67. 'logfile',
  68. 'os',
  69. 'cpu',
  70. 'version',
  71. 'date',
  72. 'submitter',
  73. 'machine',
  74. 'comment',
  75. 'testsrcdir',
  76. 'verbose'
  77. );
  78. ConfigOpts : Array[TConfigOpt] of char
  79. = ('d','h','u','p','l','o','c','v','t','s','m','C','S','V');
  80. Var
  81. TestOS,
  82. TestCPU,
  83. TestVersion,
  84. DatabaseName,
  85. HostName,
  86. UserName,
  87. Password,
  88. LogFileName,
  89. Submitter,
  90. Machine,
  91. Comment : String;
  92. TestDate : TDateTime;
  93. Procedure SetOpt (O : TConfigOpt; Value : string);
  94. var
  95. year,month,day,min,hour : word;
  96. begin
  97. Case O of
  98. coDatabaseName : DatabaseName:=Value;
  99. soHost : HostName:=Value;
  100. coUserName : UserName:=Value;
  101. coPassword : Password:=Value;
  102. coLogFile : LogFileName:=Value;
  103. coOS : TestOS:=Value;
  104. coCPU : TestCPU:=Value;
  105. coVersion : TestVersion:=Value;
  106. coDate :
  107. begin
  108. { Formated like YYYYMMDDhhmm }
  109. if Length(value)=12 then
  110. begin
  111. year:=StrToInt(Copy(value,1,4));
  112. month:=StrToInt(Copy(value,5,2));
  113. day:=StrToInt(Copy(Value,7,2));
  114. hour:=StrToInt(Copy(Value,9,2));
  115. min:=StrToInt(Copy(Value,11,2));
  116. TestDate:=EncodeDate(year,month,day)+EncodeTime(hour,min,0,0);
  117. end
  118. else
  119. Verbose(V_Error,'Error in date format, use YYYYMMDDhhmm');
  120. end;
  121. coSubmitter : Submitter:=Value;
  122. coMachine : Machine:=Value;
  123. coComment : Comment:=Value;
  124. coVerbose : DoVerbose:=true;
  125. coTestSrcDir :
  126. begin
  127. TestSrcDir:=Value;
  128. if (TestSrcDir<>'') and (TestSrcDir[length(TestSrcDir)]<>'/') then
  129. TestSrcDir:=TestSrcDir+'/';
  130. end;
  131. end;
  132. end;
  133. Function ProcessOption(S: String) : Boolean;
  134. Var
  135. N : String;
  136. I : Integer;
  137. co : TConfigOpt;
  138. begin
  139. Verbose(V_DEBUG,'Processing option: '+S);
  140. I:=Pos('=',S);
  141. Result:=(I<>0);
  142. If Result then
  143. begin
  144. N:=Copy(S,1,I-1);
  145. Delete(S,1,I);
  146. For co:=low(TConfigOpt) to high(TConfigOpt) do
  147. begin
  148. Result:=CompareText(ConfigStrings[co],N)=0;
  149. If Result then
  150. Break;
  151. end;
  152. end;
  153. If Result then
  154. SetOpt(co,S)
  155. else
  156. Verbose(V_ERROR,'Unknown option : '+n+S);
  157. end;
  158. Procedure ProcessConfigfile(FN : String);
  159. Var
  160. F : Text;
  161. S : String;
  162. I : Integer;
  163. begin
  164. If Not FileExists(FN) Then
  165. Exit;
  166. Verbose(V_DEBUG,'Parsing config file: '+FN);
  167. Assign(F,FN);
  168. {$i-}
  169. Reset(F);
  170. If IOResult<>0 then
  171. Exit;
  172. {$I+}
  173. While not(EOF(F)) do
  174. begin
  175. ReadLn(F,S);
  176. S:=trim(S);
  177. I:=Pos('#',S);
  178. If I<>0 then
  179. S:=Copy(S,1,I-1);
  180. If (S<>'') then
  181. ProcessOption(S);
  182. end;
  183. Close(F);
  184. end;
  185. Procedure ProcessCommandLine;
  186. Var
  187. I : Integer;
  188. O : String;
  189. c,co : TConfigOpt;
  190. Found : Boolean;
  191. begin
  192. I:=1;
  193. While I<=ParamCount do
  194. begin
  195. O:=Paramstr(I);
  196. Found:=Length(O)=2;
  197. If Found then
  198. For co:=low(TConfigOpt) to high(TConfigOpt) do
  199. begin
  200. Found:=(O[2]=ConfigOpts[co]);
  201. If Found then
  202. begin
  203. c:=co;
  204. Break;
  205. end;
  206. end;
  207. If Not Found then
  208. Verbose(V_ERROR,'Illegal command-line option : '+O)
  209. else
  210. begin
  211. Found:=(I<ParamCount);
  212. If Not found then
  213. Verbose(V_ERROR,'Option requires argument : '+O)
  214. else
  215. begin
  216. inc(I);
  217. O:=Paramstr(I);
  218. SetOpt(c,o);
  219. end;
  220. end;
  221. Inc(I);
  222. end;
  223. end;
  224. Var
  225. TestCPUID : Integer;
  226. TestOSID : Integer;
  227. TestVersionID : Integer;
  228. TestRunID : Integer;
  229. Procedure GetIDs;
  230. begin
  231. TestCPUID := GetCPUId(TestCPU);
  232. If TestCPUID=-1 then
  233. Verbose(V_Error,'NO ID for CPU "'+TestCPU+'" found.');
  234. TestOSID := GetOSID(TestOS);
  235. If TestOSID=-1 then
  236. Verbose(V_Error,'NO ID for OS "'+TestOS+'" found.');
  237. TestVersionID := GetVersionID(TestVersion);
  238. If TestVersionID=-1 then
  239. Verbose(V_Error,'NO ID for version "'+TestVersion+'" found.');
  240. If (Round(TestDate)=0) then
  241. Testdate:=Now;
  242. TestRunID:=GetRunID(TestOSID,TestCPUID,TestVersionID,TestDate);
  243. If (TestRunID=-1) then
  244. begin
  245. TestRunID:=AddRun(TestOSID,TestCPUID,TestVersionID,TestDate);
  246. If TestRUnID=-1 then
  247. Verbose(V_Error,'Could not insert new testrun record!');
  248. end
  249. else
  250. CleanTestRun(TestRunID);
  251. end;
  252. Function GetLog(FN : String) : String;
  253. begin
  254. FN:=ChangeFileExt(FN,'.log');
  255. If FileExists(FN) then
  256. Result:=GetFileContents(FN)
  257. else
  258. Result:='';
  259. end;
  260. Function GetExecuteLog(FN : String) : String;
  261. begin
  262. FN:=ChangeFileExt(FN,'.elg');
  263. If FileExists(FN) then
  264. Result:=GetFileContents(FN)
  265. else
  266. Result:='';
  267. end;
  268. Procedure Processfile (FN: String);
  269. var
  270. logfile : text;
  271. line : string;
  272. TS : TTestStatus;
  273. ID : integer;
  274. Testlog : string;
  275. begin
  276. Assign(logfile,FN);
  277. {$i-}
  278. reset(logfile);
  279. if ioresult<>0 then
  280. Verbose(V_Error,'Unable to open log file'+logfilename);
  281. {$i+}
  282. while not eof(logfile) do
  283. begin
  284. readln(logfile,line);
  285. If analyse(line,TS) then
  286. begin
  287. Verbose(V_NORMAL,'Analysing result for test '+Line);
  288. Inc(StatusCount[TS]);
  289. If Not ExpectRun[TS] then
  290. begin
  291. ID:=RequireTestID(Line);
  292. If (ID<>-1) then
  293. begin
  294. If Not (TestOK[TS] or TestSkipped[TS]) then
  295. begin
  296. TestLog:=GetExecuteLog(Line);
  297. if pos(failed_to_compile,TestLog)=1 then
  298. TestLog:=GetLog(Line);
  299. end
  300. else
  301. TestLog:='';
  302. AddTestResult(ID,TestRunID,Ord(TS),TestOK[TS],TestSkipped[TS],TestLog);
  303. end;
  304. end
  305. end
  306. else
  307. Inc(UnknownLines);
  308. end;
  309. close(logfile);
  310. end;
  311. procedure UpdateTestRun;
  312. var
  313. i : TTestStatus;
  314. qry : string;
  315. res : TQueryResult;
  316. begin
  317. qry:='UPDATE TESTRUN SET ';
  318. for i:=low(TTestStatus) to high(TTestStatus) do
  319. qry:=qry+format('%s=%d, ',[SQLField[i],StatusCount[i]]);
  320. qry:=qry+format('TU_SUBMITTER="%s", TU_MACHINE="%s", TU_COMMENT="%s", TU_DATE="%s"',[Submitter,Machine,Comment,SqlDate(TestDate)]);
  321. qry:=qry+' WHERE TU_ID='+format('%d',[TestRunID]);
  322. RunQuery(Qry,res)
  323. end;
  324. begin
  325. ProcessConfigFile('dbdigest.cfg');
  326. ProcessCommandLine;
  327. If LogFileName<>'' then
  328. begin
  329. ConnectToDatabase(DatabaseName,HostName,UserName,Password);
  330. GetIDs;
  331. ProcessFile(LogFileName);
  332. UpdateTestRun;
  333. end
  334. else
  335. Verbose(V_ERROR,'Missing log file name');
  336. end.