dbdigest.pp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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. coCategory,
  54. coVersion,
  55. coDate,
  56. coSubmitter,
  57. coMachine,
  58. coComment,
  59. coTestSrcDir,
  60. coRelSrcDir,
  61. coVerbose
  62. );
  63. Const
  64. ConfigStrings : Array [TConfigOpt] of string = (
  65. 'databasename',
  66. 'host',
  67. 'username',
  68. 'password',
  69. 'logfile',
  70. 'os',
  71. 'cpu',
  72. 'category',
  73. 'version',
  74. 'date',
  75. 'submitter',
  76. 'machine',
  77. 'comment',
  78. 'testsrcdir',
  79. 'relsrcdir',
  80. 'verbose'
  81. );
  82. ConfigOpts : Array[TConfigOpt] of char
  83. = ('d','h','u','p','l','o','c','a','v','t','s','m','C','S','r','V');
  84. Var
  85. TestOS,
  86. TestCPU,
  87. TestVersion,
  88. TestCategory,
  89. DatabaseName,
  90. HostName,
  91. UserName,
  92. Password,
  93. LogFileName,
  94. Submitter,
  95. Machine,
  96. Comment : String;
  97. TestDate : TDateTime;
  98. Procedure SetOpt (O : TConfigOpt; Value : string);
  99. var
  100. year,month,day,min,hour : word;
  101. begin
  102. Case O of
  103. coDatabaseName : DatabaseName:=Value;
  104. soHost : HostName:=Value;
  105. coUserName : UserName:=Value;
  106. coPassword : Password:=Value;
  107. coLogFile : LogFileName:=Value;
  108. coOS : TestOS:=Value;
  109. coCPU : TestCPU:=Value;
  110. coCategory : TestCategory:=Value;
  111. coVersion : TestVersion:=Value;
  112. coDate :
  113. begin
  114. { Formated like YYYYMMDDhhmm }
  115. if Length(value)=12 then
  116. begin
  117. year:=StrToInt(Copy(value,1,4));
  118. month:=StrToInt(Copy(value,5,2));
  119. day:=StrToInt(Copy(Value,7,2));
  120. hour:=StrToInt(Copy(Value,9,2));
  121. min:=StrToInt(Copy(Value,11,2));
  122. TestDate:=EncodeDate(year,month,day)+EncodeTime(hour,min,0,0);
  123. end
  124. else
  125. Verbose(V_Error,'Error in date format, use YYYYMMDDhhmm');
  126. end;
  127. coSubmitter : Submitter:=Value;
  128. coMachine : Machine:=Value;
  129. coComment : Comment:=Value;
  130. coVerbose : DoVerbose:=true;
  131. coTestSrcDir :
  132. begin
  133. TestSrcDir:=Value;
  134. if (TestSrcDir<>'') and (TestSrcDir[length(TestSrcDir)]<>'/') then
  135. TestSrcDir:=TestSrcDir+'/';
  136. end;
  137. coRelSrcDir :
  138. begin
  139. RelSrcDir:=Value;
  140. if (RelSrcDir<>'') and (RelSrcDir[length(RelSrcDir)]<>'/') then
  141. RelSrcDir:=RelSrcDir+'/';
  142. if (RelSrcDir<>'') and (RelSrcDir[1]='/') then
  143. RelSrcDir:=copy(RelSrcDir,2,length(RelSrcDir)-1);
  144. end;
  145. end;
  146. end;
  147. Function ProcessOption(S: String) : Boolean;
  148. Var
  149. N : String;
  150. I : Integer;
  151. co : TConfigOpt;
  152. begin
  153. Verbose(V_DEBUG,'Processing option: '+S);
  154. I:=Pos('=',S);
  155. Result:=(I<>0);
  156. If Result then
  157. begin
  158. N:=Copy(S,1,I-1);
  159. Delete(S,1,I);
  160. For co:=low(TConfigOpt) to high(TConfigOpt) do
  161. begin
  162. Result:=CompareText(ConfigStrings[co],N)=0;
  163. If Result then
  164. Break;
  165. end;
  166. end;
  167. If Result then
  168. SetOpt(co,S)
  169. else
  170. Verbose(V_ERROR,'Unknown option : '+n+S);
  171. end;
  172. Procedure ProcessConfigfile(FN : String);
  173. Var
  174. F : Text;
  175. S : String;
  176. I : Integer;
  177. begin
  178. // Set the default value for old digests without RelSrcDir to the rtl/compiler
  179. // testsuite
  180. RelSrcDir:='tests/';
  181. If Not FileExists(FN) Then
  182. Exit;
  183. Verbose(V_DEBUG,'Parsing config file: '+FN);
  184. Assign(F,FN);
  185. {$i-}
  186. Reset(F);
  187. If IOResult<>0 then
  188. Exit;
  189. {$I+}
  190. While not(EOF(F)) do
  191. begin
  192. ReadLn(F,S);
  193. S:=trim(S);
  194. I:=Pos('#',S);
  195. If I<>0 then
  196. S:=Copy(S,1,I-1);
  197. If (S<>'') then
  198. ProcessOption(S);
  199. end;
  200. Close(F);
  201. end;
  202. Procedure ProcessCommandLine;
  203. Var
  204. I : Integer;
  205. O : String;
  206. c,co : TConfigOpt;
  207. Found : Boolean;
  208. begin
  209. I:=1;
  210. While I<=ParamCount do
  211. begin
  212. O:=Paramstr(I);
  213. Found:=Length(O)=2;
  214. If Found then
  215. For co:=low(TConfigOpt) to high(TConfigOpt) do
  216. begin
  217. Found:=(O[2]=ConfigOpts[co]);
  218. If Found then
  219. begin
  220. c:=co;
  221. Break;
  222. end;
  223. end;
  224. If Not Found then
  225. Verbose(V_ERROR,'Illegal command-line option : '+O)
  226. else
  227. begin
  228. Found:=(I<ParamCount);
  229. If Not found then
  230. Verbose(V_ERROR,'Option requires argument : '+O)
  231. else
  232. begin
  233. inc(I);
  234. O:=Paramstr(I);
  235. SetOpt(c,o);
  236. end;
  237. end;
  238. Inc(I);
  239. end;
  240. end;
  241. Var
  242. TestCPUID : Integer;
  243. TestOSID : Integer;
  244. TestVersionID : Integer;
  245. TestCategoryID : Integer;
  246. TestRunID : Integer;
  247. Procedure GetIDs;
  248. begin
  249. TestCPUID := GetCPUId(TestCPU);
  250. If TestCPUID=-1 then
  251. Verbose(V_Error,'NO ID for CPU "'+TestCPU+'" found.');
  252. TestOSID := GetOSID(TestOS);
  253. If TestOSID=-1 then
  254. Verbose(V_Error,'NO ID for OS "'+TestOS+'" found.');
  255. TestCategoryID := GetCategoryID(TestCategory);
  256. If TestCategoryID=-1 then
  257. begin
  258. // Verbose(V_Error,'NO ID for Category "'+TestCategory+'" found.');
  259. TestCategoryID:=1;
  260. end;
  261. TestVersionID := GetVersionID(TestVersion);
  262. If TestVersionID=-1 then
  263. Verbose(V_Error,'NO ID for version "'+TestVersion+'" found.');
  264. If (Round(TestDate)=0) then
  265. Testdate:=Now;
  266. TestRunID:=GetRunID(TestOSID,TestCPUID,TestVersionID,TestDate);
  267. If (TestRunID=-1) then
  268. begin
  269. TestRunID:=AddRun(TestOSID,TestCPUID,TestVersionID,TestCategoryID,TestDate);
  270. If TestRUnID=-1 then
  271. Verbose(V_Error,'Could not insert new testrun record!');
  272. end
  273. else
  274. CleanTestRun(TestRunID);
  275. end;
  276. Function GetLog(FN : String) : String;
  277. begin
  278. FN:=ChangeFileExt(FN,'.log');
  279. If FileExists(FN) then
  280. Result:=GetFileContents(FN)
  281. else
  282. Result:='';
  283. end;
  284. Function GetExecuteLog(FN : String) : String;
  285. begin
  286. FN:=ChangeFileExt(FN,'.elg');
  287. If FileExists(FN) then
  288. Result:=GetFileContents(FN)
  289. else
  290. Result:='';
  291. end;
  292. Procedure Processfile (FN: String);
  293. var
  294. logfile : text;
  295. line : string;
  296. TS : TTestStatus;
  297. ID : integer;
  298. Testlog : string;
  299. begin
  300. Assign(logfile,FN);
  301. {$i-}
  302. reset(logfile);
  303. if ioresult<>0 then
  304. Verbose(V_Error,'Unable to open log file'+logfilename);
  305. {$i+}
  306. while not eof(logfile) do
  307. begin
  308. readln(logfile,line);
  309. If analyse(line,TS) then
  310. begin
  311. Verbose(V_NORMAL,'Analysing result for test '+Line);
  312. Inc(StatusCount[TS]);
  313. If Not ExpectRun[TS] then
  314. begin
  315. ID:=RequireTestID(Line);
  316. If (ID<>-1) then
  317. begin
  318. If Not (TestOK[TS] or TestSkipped[TS]) then
  319. begin
  320. TestLog:=GetExecuteLog(Line);
  321. if pos(failed_to_compile,TestLog)=1 then
  322. TestLog:=GetLog(Line);
  323. end
  324. else
  325. TestLog:='';
  326. AddTestResult(ID,TestRunID,Ord(TS),TestOK[TS],TestSkipped[TS],TestLog);
  327. end;
  328. end
  329. end
  330. else
  331. Inc(UnknownLines);
  332. end;
  333. close(logfile);
  334. end;
  335. procedure UpdateTestRun;
  336. var
  337. i : TTestStatus;
  338. qry : string;
  339. res : TQueryResult;
  340. begin
  341. qry:='UPDATE TESTRUN SET ';
  342. for i:=low(TTestStatus) to high(TTestStatus) do
  343. qry:=qry+format('%s=%d, ',[SQLField[i],StatusCount[i]]);
  344. qry:=qry+format('TU_SUBMITTER="%s", TU_MACHINE="%s", TU_COMMENT="%s", TU_DATE="%s"',[Submitter,Machine,Comment,SqlDate(TestDate)]);
  345. qry:=qry+' WHERE TU_ID='+format('%d',[TestRunID]);
  346. RunQuery(Qry,res)
  347. end;
  348. begin
  349. ProcessConfigFile('dbdigest.cfg');
  350. ProcessCommandLine;
  351. If LogFileName<>'' then
  352. begin
  353. ConnectToDatabase(DatabaseName,HostName,UserName,Password);
  354. GetIDs;
  355. ProcessFile(LogFileName);
  356. UpdateTestRun;
  357. end
  358. else
  359. Verbose(V_ERROR,'Missing log file name');
  360. end.