1. Add -m and -s to support only scanning meta-data files or source code files
[people/mcb30/basetools.git] / Source / Python / Common / DecClassObject.py
index 27e0d67..b95ff62 100644 (file)
@@ -23,7 +23,7 @@ from CommonDataClass.PackageClass import *
 from CommonDataClass.CommonClass import PcdClass\r
 from BuildToolError import *\r
 from Table.TableDec import TableDec\r
-import Database as Database\r
+import Database\r
 from Parsing import *\r
 import GlobalData\r
 \r
@@ -82,16 +82,18 @@ class DecObject(object):
 # @var KeyList:             To store value for KeyList, a list for all Keys used in Dec\r
 #\r
 class Dec(DecObject):\r
-    def __init__(self, Filename = None, IsMergeAllArches = False, IsToPackage = False, WorkspaceDir = None, Database = None, SupArchList = DataType.ARCH_LIST):\r
+    def __init__(self, Filename = None, IsToDatabase = False, IsToPackage = False, WorkspaceDir = None, Database = None, SupArchList = DataType.ARCH_LIST):\r
         self.Identification = Identification()\r
         self.Package = PackageClass()\r
         self.UserExtensions = ''\r
         self.WorkspaceDir = WorkspaceDir\r
         self.SupArchList = SupArchList\r
+        self.IsToDatabase = IsToDatabase\r
         \r
         self.Cur = Database.Cur\r
         self.TblFile = Database.TblFile\r
-        self.TblDec = TableDec(Database.Cur)\r
+        self.TblDec = Database.TblDec\r
+        self.FileID = -1\r
 \r
         self.KeyList = [\r
             TAB_INCLUDES, TAB_GUIDS, TAB_PROTOCOLS, TAB_PPIS, TAB_LIBRARY_CLASSES, \\r
@@ -135,13 +137,13 @@ class Dec(DecObject):
         Filename = NormPath(Filename)\r
         self.Identification.FileFullPath = Filename\r
         (self.Identification.FileRelativePath, self.Identification.FileName) = os.path.split(Filename)\r
-        FileID = self.TblFile.InsertFile(Filename, MODEL_FILE_DSC)\r
+        self.FileID = self.TblFile.InsertFile(Filename, MODEL_FILE_DEC)\r
         \r
         #\r
         # Init DecTable\r
         #\r
-        self.TblDec.Table = "Dec%s" % FileID\r
-        self.TblDec.Create()\r
+        #self.TblDec.Table = "Dec%s" % self.FileID\r
+        #self.TblDec.Create()\r
         \r
         #\r
         # Init common datas\r
@@ -153,20 +155,23 @@ class Dec(DecObject):
         #\r
         # Parse file content\r
         #\r
-        FileContent = open(Filename, 'r').read()\r
-        for Line in FileContent.splitlines():\r
+        IsFindBlockComment = False\r
+        ReservedLine = ''\r
+        for Line in open(Filename, 'r'):\r
             LineNo = LineNo + 1\r
             #\r
-            # Reomve spaces in head and tail\r
+            # Remove comment block\r
             #\r
-            Line = Line.strip()\r
-            \r
-            #\r
-            # Ignore comments\r
-            #\r
-            if Line.startswith(TAB_COMMENT_SPLIT):\r
+            if Line.find(TAB_COMMENT_R8_START) > -1:\r
+                ReservedLine = GetSplitValueList(Line, TAB_COMMENT_R8_START, 1)[0]\r
+                IsFindBlockComment = True\r
+            if Line.find(TAB_COMMENT_R8_END) > -1:\r
+                Line = ReservedLine + GetSplitValueList(Line, TAB_COMMENT_R8_END, 1)[1]\r
+                ReservedLine = ''\r
+                IsFindBlockComment = False\r
+            if IsFindBlockComment:\r
                 continue\r
-            \r
+\r
             #\r
             # Remove comments at tail and remove spaces again\r
             #\r
@@ -184,7 +189,7 @@ class Dec(DecObject):
                 # Insert items data of previous section\r
                 #\r
                 Model = Section[CurrentSection.upper()]\r
-                InsertSectionItemsIntoDatabase(self.TblDec, FileID, Filename, Model, CurrentSection, SectionItemList, ArchList, ThirdList, IfDefList, self.RecordSet)\r
+                InsertSectionItemsIntoDatabase(self.TblDec, self.FileID, Filename, Model, CurrentSection, SectionItemList, ArchList, ThirdList, IfDefList, self.RecordSet)\r
 \r
                 #\r
                 # Parse the new section\r
@@ -193,10 +198,15 @@ class Dec(DecObject):
                 ArchList = []\r
                 ThirdList = []\r
                 \r
+                CurrentSection = ''\r
                 LineList = GetSplitValueList(Line[len(TAB_SECTION_START):len(Line) - len(TAB_SECTION_END)], TAB_COMMA_SPLIT)\r
                 for Item in LineList:\r
                     ItemList = GetSplitValueList(Item, TAB_SPLIT)\r
-                    CurrentSection = ItemList[0]\r
+                    if CurrentSection == '':\r
+                        CurrentSection = ItemList[0]\r
+                    else:\r
+                        if CurrentSection != ItemList[0]:\r
+                            EdkLogger.error("Parser", PARSER_ERROR, "Different section names '%s' and '%s' are found in one section definition, this is not allowed." % (CurrentSection, ItemList[0]), File=Filename, Line=LineNo, RaiseError = EdkLogger.IsRaiseError)\r
                     if CurrentSection.upper() not in self.KeyList:\r
                         RaiseParserError(Line, CurrentSection, Filename, '', LineNo)\r
                     ItemList.append('')\r
@@ -205,7 +215,7 @@ class Dec(DecObject):
                         RaiseParserError(Line, CurrentSection, Filename, '', LineNo)\r
                     else:\r
                         if ItemList[1] != '' and ItemList[1].upper() not in ARCH_LIST_FULL:\r
-                            EdkLogger.error("Parser", PARSER_ERROR, "Invalid Arch definition '%s' found" % ItemList[1], File=Filename, Line=LineNo)\r
+                            EdkLogger.error("Parser", PARSER_ERROR, "Invalid Arch definition '%s' found" % ItemList[1], File=Filename, Line=LineNo, RaiseError = EdkLogger.IsRaiseError)\r
                         ArchList.append(ItemList[1].upper())\r
                         ThirdList.append(ItemList[2])\r
 \r
@@ -216,7 +226,7 @@ class Dec(DecObject):
             #\r
             if CurrentSection == TAB_UNKNOWN:\r
                 ErrorMsg = "%s is not in any defined section" % Line\r
-                EdkLogger.error("Parser", PARSER_ERROR, ErrorMsg, File=Filename, Line=LineNo)\r
+                EdkLogger.error("Parser", PARSER_ERROR, ErrorMsg, File=Filename, Line=LineNo, RaiseError = EdkLogger.IsRaiseError)\r
 \r
             #\r
             # Add a section item\r
@@ -229,7 +239,7 @@ class Dec(DecObject):
         # Insert items data of last section\r
         #\r
         Model = Section[CurrentSection.upper()]\r
-        InsertSectionItemsIntoDatabase(self.TblDec, FileID, Filename, Model, CurrentSection, SectionItemList, ArchList, ThirdList, IfDefList, self.RecordSet)\r
+        InsertSectionItemsIntoDatabase(self.TblDec, self.FileID, Filename, Model, CurrentSection, SectionItemList, ArchList, ThirdList, IfDefList, self.RecordSet)\r
         \r
         #\r
         # Replace all DEFINE macros with its actual values\r
@@ -308,12 +318,12 @@ class Dec(DecObject):
         for Arch in self.SupArchList:\r
             PackageHeader = PackageHeaderClass()\r
             \r
-            PackageHeader.Name = QueryDefinesItem(self.TblDec, TAB_DEC_DEFINES_PACKAGE_NAME, Arch)[0]\r
-            PackageHeader.Guid = QueryDefinesItem(self.TblDec, TAB_DEC_DEFINES_PACKAGE_GUID, Arch)[0]\r
-            PackageHeader.Version = QueryDefinesItem(self.TblDec, TAB_DEC_DEFINES_PACKAGE_VERSION, Arch)[0]\r
+            PackageHeader.Name = QueryDefinesItem(self.TblDec, TAB_DEC_DEFINES_PACKAGE_NAME, Arch, self.FileID)[0]\r
+            PackageHeader.Guid = QueryDefinesItem(self.TblDec, TAB_DEC_DEFINES_PACKAGE_GUID, Arch, self.FileID)[0]\r
+            PackageHeader.Version = QueryDefinesItem(self.TblDec, TAB_DEC_DEFINES_PACKAGE_VERSION, Arch, self.FileID)[0]\r
             PackageHeader.FileName = self.Identification.FileName\r
             PackageHeader.FullPath = self.Identification.FileFullPath\r
-            PackageHeader.DecSpecification = QueryDefinesItem(self.TblDec, TAB_DEC_DEFINES_DEC_SPECIFICATION, Arch)[0]\r
+            PackageHeader.DecSpecification = QueryDefinesItem(self.TblDec, TAB_DEC_DEFINES_DEC_SPECIFICATION, Arch, self.FileID)[0]\r
             \r
             self.Package.Header[Arch] = PackageHeader\r
     \r
@@ -367,7 +377,12 @@ class Dec(DecObject):
         for Arch in self.SupArchList:\r
             for Record in RecordSet:\r
                 if Record[1] == Arch or Record[1] == TAB_ARCH_COMMON:\r
-                    MergeArches(Lists, GetGuidsProtocolsPpisOfDec(Record[0], Type, ContainerFile, Record[2]), Arch)\r
+                    (Name, Value) = GetGuidsProtocolsPpisOfDec(Record[0], Type, ContainerFile, Record[2])\r
+                    MergeArches(Lists, (Name, Value), Arch)\r
+                    if self.IsToDatabase:\r
+                        SqlCommand = """update %s set Value1 = '%s', Value2 = '%s'\r
+                                        where ID = %s""" % (self.TblDec.Table, ConvertToSqlString2(Name), ConvertToSqlString2(Value), Record[3])\r
+                        self.TblDec.Exec(SqlCommand)\r
         \r
         ListMember = None\r
         if Type == TAB_GUIDS:\r
@@ -412,6 +427,11 @@ class Dec(DecObject):
                     else:\r
                         CheckFileExist(self.Identification.FileRelativePath, List[1], ContainerFile, 'LibraryClasses', Record[0])\r
                     MergeArches(LibraryClasses, (List[0], List[1]), Arch)\r
+                    if self.IsToDatabase:\r
+                        SqlCommand = """update %s set Value1 = '%s', Value2 = '%s', Value3 = '%s'\r
+                                        where ID = %s""" % (self.TblDec.Table, ConvertToSqlString2(List[0]), ConvertToSqlString2(List[1]), SUP_MODULE_LIST_STRING, Record[3])\r
+                        self.TblDec.Exec(SqlCommand)\r
+\r
         \r
         for Key in LibraryClasses.keys():\r
             LibraryClass = LibraryClassClass()\r
@@ -431,6 +451,7 @@ class Dec(DecObject):
     def GenPcds(self, ContainerFile):\r
         EdkLogger.debug(2, "Generate %s ..." % TAB_PCDS)\r
         Pcds = {}\r
+        PcdToken = {}\r
         #\r
         # Get all Guids\r
         #\r
@@ -446,19 +467,36 @@ class Dec(DecObject):
         for Arch in self.SupArchList:\r
             for Record in RecordSet1:\r
                 if Record[1] == Arch or Record[1] == TAB_ARCH_COMMON:\r
-                    MergeArches(Pcds, GetPcdOfDec(Record[0], TAB_PCDS_FIXED_AT_BUILD, ContainerFile, Record[2]), Arch)\r
+                    (TokenGuidCName, TokenName, Value, DatumType, Token, Type) = GetPcdOfDec(Record[0], TAB_PCDS_FIXED_AT_BUILD, ContainerFile, Record[2])\r
+                    MergeArches(Pcds, (TokenGuidCName, TokenName, Value, DatumType, Token, Type), Arch)\r
+                    PcdToken[Record[3]] = (TokenGuidCName, TokenName)\r
             for Record in RecordSet2:\r
                 if Record[1] == Arch or Record[1] == TAB_ARCH_COMMON:\r
-                    MergeArches(Pcds, GetPcdOfDec(Record[0], TAB_PCDS_PATCHABLE_IN_MODULE, ContainerFile, Record[2]), Arch)\r
+                    (TokenGuidCName, TokenName, Value, DatumType, Token, Type) = GetPcdOfDec(Record[0], TAB_PCDS_PATCHABLE_IN_MODULE, ContainerFile, Record[2])\r
+                    MergeArches(Pcds, (TokenGuidCName, TokenName, Value, DatumType, Token, Type), Arch)\r
+                    PcdToken[Record[3]] = (TokenGuidCName, TokenName)\r
             for Record in RecordSet3:\r
                 if Record[1] == Arch or Record[1] == TAB_ARCH_COMMON:\r
-                    MergeArches(Pcds, GetPcdOfDec(Record[0], TAB_PCDS_FEATURE_FLAG, ContainerFile, Record[2]), Arch)\r
+                    (TokenGuidCName, TokenName, Value, DatumType, Token, Type) = GetPcdOfDec(Record[0], TAB_PCDS_FEATURE_FLAG, ContainerFile, Record[2])\r
+                    MergeArches(Pcds, (TokenGuidCName, TokenName, Value, DatumType, Token, Type), Arch)\r
+                    PcdToken[Record[3]] = (TokenGuidCName, TokenName)\r
             for Record in RecordSet4:\r
                 if Record[1] == Arch or Record[1] == TAB_ARCH_COMMON:\r
-                    MergeArches(Pcds, GetPcdOfDec(Record[0], TAB_PCDS_DYNAMIC_EX, ContainerFile, Record[2]), Arch)\r
+                    (TokenGuidCName, TokenName, Value, DatumType, Token, Type) = GetPcdOfDec(Record[0], TAB_PCDS_DYNAMIC_EX, ContainerFile, Record[2])\r
+                    MergeArches(Pcds, (TokenGuidCName, TokenName, Value, DatumType, Token, Type), Arch)\r
+                    PcdToken[Record[3]] = (TokenGuidCName, TokenName)\r
             for Record in RecordSet5:\r
                 if Record[1] == Arch or Record[1] == TAB_ARCH_COMMON:\r
-                    MergeArches(Pcds, GetPcdOfDec(Record[0], TAB_PCDS_DYNAMIC, ContainerFile, Record[2]), Arch)                    \r
+                    (TokenGuidCName, TokenName, Value, DatumType, Token, Type) = GetPcdOfDec(Record[0], TAB_PCDS_DYNAMIC, ContainerFile, Record[2])\r
+                    MergeArches(Pcds, (TokenGuidCName, TokenName, Value, DatumType, Token, Type), Arch)\r
+                    PcdToken[Record[3]] = (TokenGuidCName, TokenName)\r
+        #\r
+        # Update to database\r
+        #\r
+        if self.IsToDatabase:\r
+            for Key in PcdToken.keys():\r
+                SqlCommand = """update %s set Value2 = '%s' where ID = %s""" % (self.TblDec.Table, ".".join((PcdToken[Key][0], PcdToken[Key][1])), Key)\r
+                self.TblDec.Exec(SqlCommand)\r
 \r
         for Key in Pcds.keys():\r
             Pcd = PcdClass()\r
@@ -516,10 +554,10 @@ if __name__ == '__main__':
     W = os.getenv('WORKSPACE')\r
     F = os.path.join(W, 'Nt32Pkg/Nt32Pkg.dec')\r
 \r
-    Db = Database.Database(DATABASE_PATH)\r
+    Db = Database.Database('Dec.db')\r
     Db.InitDatabase()\r
     \r
-    P = Dec(os.path.normpath(F), False, True, W, Db)\r
+    P = Dec(os.path.normpath(F), True, True, W, Db)\r
     P.ShowPackage()\r
     \r
     Db.Close()\r