BaseTools: Use absolute import in Table
[mirror_edk2.git] / BaseTools / Source / Python / Table / TableReport.py
1 ## @file
2 # This file is used to create/update/query/erase table for ECC reports
3 #
4 # Copyright (c) 2008 - 2018, Intel Corporation. All rights reserved.<BR>
5 # This program and the accompanying materials
6 # are licensed and made available under the terms and conditions of the BSD License
7 # which accompanies this distribution. The full text of the license may be found at
8 # http://opensource.org/licenses/bsd-license.php
9 #
10 # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12 #
13
14 ##
15 # Import Modules
16 #
17 from __future__ import absolute_import
18 import Common.EdkLogger as EdkLogger
19 import Common.LongFilePathOs as os, time
20 from .Table import Table
21 from Common.StringUtils import ConvertToSqlString2
22 import EccToolError as EccToolError
23 import EccGlobalData as EccGlobalData
24 from Common.LongFilePathSupport import OpenLongFilePath as open
25
26 ## TableReport
27 #
28 # This class defined a table used for data model
29 #
30 # @param object: Inherited from object class
31 #
32 #
33 class TableReport(Table):
34 def __init__(self, Cursor):
35 Table.__init__(self, Cursor)
36 self.Table = 'Report'
37
38 ## Create table
39 #
40 # Create table report
41 #
42 # @param ID: ID of an Error
43 # @param ErrorID: ID of an Error TypeModel of a Report item
44 # @param OtherMsg: Other error message besides the standard error message
45 # @param BelongsToItem: The error belongs to which item
46 # @param Enabled: If this error enabled
47 # @param Corrected: if this error corrected
48 #
49 def Create(self):
50 SqlCommand = """create table IF NOT EXISTS %s (ID INTEGER PRIMARY KEY,
51 ErrorID INTEGER NOT NULL,
52 OtherMsg TEXT,
53 BelongsToTable TEXT NOT NULL,
54 BelongsToItem SINGLE NOT NULL,
55 Enabled INTEGER DEFAULT 0,
56 Corrected INTEGER DEFAULT -1
57 )""" % self.Table
58 Table.Create(self, SqlCommand)
59
60 ## Insert table
61 #
62 # Insert a record into table report
63 #
64 # @param ID: ID of an Error
65 # @param ErrorID: ID of an Error TypeModel of a report item
66 # @param OtherMsg: Other error message besides the standard error message
67 # @param BelongsToTable: The error item belongs to which table
68 # @param BelongsToItem: The error belongs to which item
69 # @param Enabled: If this error enabled
70 # @param Corrected: if this error corrected
71 #
72 def Insert(self, ErrorID, OtherMsg='', BelongsToTable='', BelongsToItem= -1, Enabled=0, Corrected= -1):
73 self.ID = self.ID + 1
74 SqlCommand = """insert into %s values(%s, %s, '%s', '%s', %s, %s, %s)""" \
75 % (self.Table, self.ID, ErrorID, ConvertToSqlString2(OtherMsg), BelongsToTable, BelongsToItem, Enabled, Corrected)
76 Table.Insert(self, SqlCommand)
77
78 return self.ID
79
80 ## Query table
81 #
82 # @retval: A recordSet of all found records
83 #
84 def Query(self):
85 SqlCommand = """select ID, ErrorID, OtherMsg, BelongsToTable, BelongsToItem, Corrected from %s
86 where Enabled > -1 order by ErrorID, BelongsToItem""" % (self.Table)
87 return self.Exec(SqlCommand)
88
89 ## Update table
90 #
91 def UpdateBelongsToItemByFile(self, ItemID=-1, File=""):
92 SqlCommand = """update Report set BelongsToItem=%s where BelongsToTable='File' and BelongsToItem=-2
93 and OtherMsg like '%%%s%%'""" % (ItemID, File)
94 return self.Exec(SqlCommand)
95
96 ## Convert to CSV
97 #
98 # Get all enabled records from table report and save them to a .csv file
99 #
100 # @param Filename: To filename to save the report content
101 #
102 def ToCSV(self, Filename='Report.csv'):
103 try:
104 File = open(Filename, 'w+')
105 File.write("""No, Error Code, Error Message, File, LineNo, Other Error Message\n""")
106 RecordSet = self.Query()
107 Index = 0
108 for Record in RecordSet:
109 Index = Index + 1
110 ErrorID = Record[1]
111 OtherMsg = Record[2]
112 BelongsToTable = Record[3]
113 BelongsToItem = Record[4]
114 IsCorrected = Record[5]
115 SqlCommand = ''
116 if BelongsToTable == 'File':
117 SqlCommand = """select 1, FullPath from %s where ID = %s
118 """ % (BelongsToTable, BelongsToItem)
119 else:
120 SqlCommand = """select A.StartLine, B.FullPath from %s as A, File as B
121 where A.ID = %s and B.ID = A.BelongsToFile
122 """ % (BelongsToTable, BelongsToItem)
123 NewRecord = self.Exec(SqlCommand)
124 if NewRecord != []:
125 File.write("""%s,%s,"%s",%s,%s,"%s"\n""" % (Index, ErrorID, EccToolError.gEccErrorMessage[ErrorID], NewRecord[0][1], NewRecord[0][0], OtherMsg))
126 EdkLogger.quiet("%s(%s): [%s]%s %s" % (NewRecord[0][1], NewRecord[0][0], ErrorID, EccToolError.gEccErrorMessage[ErrorID], OtherMsg))
127
128 File.close()
129 except IOError:
130 NewFilename = 'Report_' + time.strftime("%Y%m%d_%H%M%S.csv", time.localtime())
131 EdkLogger.warn("ECC", "The report file %s is locked by other progress, use %s instead!" % (Filename, NewFilename))
132 self.ToCSV(NewFilename)
133