]> git.proxmox.com Git - mirror_edk2.git/blobdiff - BaseTools/Source/Python/UPT/GenMetaFile/GenInfFile.py
BaseTools/UPT: Porting UPT Tool from Python2 to Python3
[mirror_edk2.git] / BaseTools / Source / Python / UPT / GenMetaFile / GenInfFile.py
index c1362e6fb3d953b9e9950949fb92c843e05cc8a9..82df4c62cfaa5b27c42fefd4c25adac54dc75bd1 100644 (file)
@@ -2,11 +2,11 @@
 #\r
 # This file contained the logical of transfer package object to INF files.\r
 #\r
 #\r
 # This file contained the logical of transfer package object to INF files.\r
 #\r
-# Copyright (c) 2011 - 2016, Intel Corporation. All rights reserved.<BR>\r
+# Copyright (c) 2011 - 2018, Intel Corporation. All rights reserved.<BR>\r
 #\r
 #\r
-# This program and the accompanying materials are licensed and made available \r
-# under the terms and conditions of the BSD License which accompanies this \r
-# distribution. The full text of the license may be found at \r
+# This program and the accompanying materials are licensed and made available\r
+# under the terms and conditions of the BSD License which accompanies this\r
+# distribution. The full text of the license may be found at\r
 # http://opensource.org/licenses/bsd-license.php\r
 #\r
 # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
 # http://opensource.org/licenses/bsd-license.php\r
 #\r
 # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
@@ -18,9 +18,9 @@ GenInf
 import os\r
 import stat\r
 import codecs\r
 import os\r
 import stat\r
 import codecs\r
-import md5\r
+from hashlib import md5\r
 from Core.FileHook import __FileHookOpen__\r
 from Core.FileHook import __FileHookOpen__\r
-from Library.String import GetSplitValueList\r
+from Library.StringUtils import GetSplitValueList\r
 from Library.Parsing import GenSection\r
 from Library.Parsing import GetWorkspacePackage\r
 from Library.Parsing import ConvertArchForInstall\r
 from Library.Parsing import GenSection\r
 from Library.Parsing import GetWorkspacePackage\r
 from Library.Parsing import ConvertArchForInstall\r
@@ -41,13 +41,13 @@ import Logger.Log as Logger
 from Library import DataType as DT\r
 from GenMetaFile import GenMetaFileMisc\r
 from Library.UniClassObject import FormatUniEntry\r
 from Library import DataType as DT\r
 from GenMetaFile import GenMetaFileMisc\r
 from Library.UniClassObject import FormatUniEntry\r
-from Library.String import GetUniFileName\r
+from Library.StringUtils import GetUniFileName\r
 \r
 \r
 ## Transfer Module Object to Inf files\r
 #\r
 \r
 \r
 ## Transfer Module Object to Inf files\r
 #\r
-# Transfer all contents of a standard Module Object to an Inf file \r
-# @param ModuleObject: A Module Object  \r
+# Transfer all contents of a standard Module Object to an Inf file\r
+# @param ModuleObject: A Module Object\r
 #\r
 def ModuleToInf(ModuleObject, PackageObject=None, DistHeader=None):\r
     if not GlobalData.gWSPKG_LIST:\r
 #\r
 def ModuleToInf(ModuleObject, PackageObject=None, DistHeader=None):\r
     if not GlobalData.gWSPKG_LIST:\r
@@ -59,9 +59,9 @@ def ModuleToInf(ModuleObject, PackageObject=None, DistHeader=None):
 \r
     Content = ''\r
     #\r
 \r
     Content = ''\r
     #\r
-    # Generate file header, If any Abstract, Description, Copyright or License XML elements are missing, \r
-    # should 1) use the Abstract, Description, Copyright or License from the PackageSurfaceArea.Header elements \r
-    # that the module belongs to, or 2) if this is a stand-alone module that is not included in a PackageSurfaceArea, \r
+    # Generate file header, If any Abstract, Description, Copyright or License XML elements are missing,\r
+    # should 1) use the Abstract, Description, Copyright or License from the PackageSurfaceArea.Header elements\r
+    # that the module belongs to, or 2) if this is a stand-alone module that is not included in a PackageSurfaceArea,\r
     # use the abstract, description, copyright or license from the DistributionPackage.Header elements.\r
     #\r
     ModuleAbstract = GetLocalValue(ModuleObject.GetAbstract())\r
     # use the abstract, description, copyright or license from the DistributionPackage.Header elements.\r
     #\r
     ModuleAbstract = GetLocalValue(ModuleObject.GetAbstract())\r
@@ -107,15 +107,15 @@ def ModuleToInf(ModuleObject, PackageObject=None, DistHeader=None):
 \r
     #\r
     # Generate header comment section of INF file\r
 \r
     #\r
     # Generate header comment section of INF file\r
-    #        \r
+    #\r
     Content += GenHeaderCommentSection(ModuleAbstract,\r
                                        ModuleDescription,\r
                                        ModuleCopyright,\r
                                        ModuleLicense).replace('\r\n', '\n')\r
 \r
     #\r
     Content += GenHeaderCommentSection(ModuleAbstract,\r
                                        ModuleDescription,\r
                                        ModuleCopyright,\r
                                        ModuleLicense).replace('\r\n', '\n')\r
 \r
     #\r
-    # Generate Binary Header \r
-    # \r
+    # Generate Binary Header\r
+    #\r
     for UserExtension in ModuleObject.GetUserExtensionList():\r
         if UserExtension.GetUserID() == DT.TAB_BINARY_HEADER_USERID \\r
         and UserExtension.GetIdentifier() == DT.TAB_BINARY_HEADER_IDENTIFIER:\r
     for UserExtension in ModuleObject.GetUserExtensionList():\r
         if UserExtension.GetUserID() == DT.TAB_BINARY_HEADER_USERID \\r
         and UserExtension.GetIdentifier() == DT.TAB_BINARY_HEADER_IDENTIFIER:\r
@@ -140,7 +140,9 @@ def ModuleToInf(ModuleObject, PackageObject=None, DistHeader=None):
     #\r
     FileHeader = GenHeaderCommentSection(ModuleAbstract, ModuleDescription, ModuleCopyright, ModuleLicense, False, \\r
                                          DT.TAB_COMMENT_EDK1_SPLIT)\r
     #\r
     FileHeader = GenHeaderCommentSection(ModuleAbstract, ModuleDescription, ModuleCopyright, ModuleLicense, False, \\r
                                          DT.TAB_COMMENT_EDK1_SPLIT)\r
-    GenModuleUNIEncodeFile(ModuleObject, FileHeader)\r
+    ModuleUniFile = GenModuleUNIEncodeFile(ModuleObject, FileHeader)\r
+    if ModuleUniFile:\r
+        ModuleObject.SetModuleUniFile(os.path.basename(ModuleUniFile))\r
 \r
     #\r
     # Judge whether the INF file is an AsBuild INF.\r
 \r
     #\r
     # Judge whether the INF file is an AsBuild INF.\r
@@ -150,10 +152,10 @@ def ModuleToInf(ModuleObject, PackageObject=None, DistHeader=None):
     else:\r
         GlobalData.gIS_BINARY_INF = False\r
     #\r
     else:\r
         GlobalData.gIS_BINARY_INF = False\r
     #\r
-    # for each section, maintain a dict, sorted arch will be its key, \r
+    # for each section, maintain a dict, sorted arch will be its key,\r
     # statement list will be its data\r
     # { 'Arch1 Arch2 Arch3': [statement1, statement2],\r
     # statement list will be its data\r
     # { 'Arch1 Arch2 Arch3': [statement1, statement2],\r
-    #   'Arch1' : [statement1, statement3] \r
+    #   'Arch1' : [statement1, statement3]\r
     #  }\r
     #\r
     # Gen section contents\r
     #  }\r
     #\r
     # Gen section contents\r
@@ -169,15 +171,16 @@ def ModuleToInf(ModuleObject, PackageObject=None, DistHeader=None):
     Content += GenGuidSections(ModuleObject.GetGuidList())\r
     Content += GenBinaries(ModuleObject)\r
     Content += GenDepex(ModuleObject)\r
     Content += GenGuidSections(ModuleObject.GetGuidList())\r
     Content += GenBinaries(ModuleObject)\r
     Content += GenDepex(ModuleObject)\r
-    Content += GenUserExtensions(ModuleObject)\r
+    __UserExtensionsContent = GenUserExtensions(ModuleObject)\r
+    Content += __UserExtensionsContent\r
     if ModuleObject.GetEventList() or ModuleObject.GetBootModeList() or ModuleObject.GetHobList():\r
         Content += '\n'\r
     #\r
     # generate [Event], [BootMode], [Hob] section\r
     #\r
     if ModuleObject.GetEventList() or ModuleObject.GetBootModeList() or ModuleObject.GetHobList():\r
         Content += '\n'\r
     #\r
     # generate [Event], [BootMode], [Hob] section\r
     #\r
-    Content += GenSpecialSections(ModuleObject.GetEventList(), 'Event')\r
-    Content += GenSpecialSections(ModuleObject.GetBootModeList(), 'BootMode')\r
-    Content += GenSpecialSections(ModuleObject.GetHobList(), 'Hob')\r
+    Content += GenSpecialSections(ModuleObject.GetEventList(), 'Event', __UserExtensionsContent)\r
+    Content += GenSpecialSections(ModuleObject.GetBootModeList(), 'BootMode', __UserExtensionsContent)\r
+    Content += GenSpecialSections(ModuleObject.GetHobList(), 'Hob', __UserExtensionsContent)\r
     SaveFileOnChange(ContainerFile, Content, False)\r
     if DistHeader.ReadOnly:\r
         os.chmod(ContainerFile, stat.S_IRUSR|stat.S_IRGRP|stat.S_IROTH)\r
     SaveFileOnChange(ContainerFile, Content, False)\r
     if DistHeader.ReadOnly:\r
         os.chmod(ContainerFile, stat.S_IRUSR|stat.S_IRGRP|stat.S_IROTH)\r
@@ -194,7 +197,7 @@ def GenModuleUNIEncodeFile(ModuleObject, UniFileHeader='', Encoding=DT.TAB_ENCOD
     BinaryAbstract = []\r
     BinaryDescription = []\r
     #\r
     BinaryAbstract = []\r
     BinaryDescription = []\r
     #\r
-    # If more than one language code is used for any element that would be present in the MODULE_UNI_FILE, \r
+    # If more than one language code is used for any element that would be present in the MODULE_UNI_FILE,\r
     # then the MODULE_UNI_FILE must be created.\r
     #\r
     for (Key, Value) in ModuleObject.GetAbstract() + ModuleObject.GetDescription():\r
     # then the MODULE_UNI_FILE must be created.\r
     #\r
     for (Key, Value) in ModuleObject.GetAbstract() + ModuleObject.GetDescription():\r
@@ -231,28 +234,28 @@ def GenModuleUNIEncodeFile(ModuleObject, UniFileHeader='', Encoding=DT.TAB_ENCOD
     if not os.path.exists(os.path.dirname(ModuleObject.GetFullPath())):\r
         os.makedirs(os.path.dirname(ModuleObject.GetFullPath()))\r
 \r
     if not os.path.exists(os.path.dirname(ModuleObject.GetFullPath())):\r
         os.makedirs(os.path.dirname(ModuleObject.GetFullPath()))\r
 \r
-    Content = UniFileHeader + '\r\n'\r
-    Content += '\r\n'\r
+    Content = UniFileHeader + '\n'\r
+    Content += '\n'\r
 \r
 \r
-    Content += FormatUniEntry('#string ' + DT.TAB_INF_ABSTRACT, ModuleObject.GetAbstract(), ContainerFile) + '\r\n'\r
+    Content += FormatUniEntry('#string ' + DT.TAB_INF_ABSTRACT, ModuleObject.GetAbstract(), ContainerFile) + '\n'\r
 \r
     Content += FormatUniEntry('#string ' + DT.TAB_INF_DESCRIPTION, ModuleObject.GetDescription(), ContainerFile) \\r
 \r
     Content += FormatUniEntry('#string ' + DT.TAB_INF_DESCRIPTION, ModuleObject.GetDescription(), ContainerFile) \\r
-            + '\r\n'\r
+            + '\n'\r
 \r
     BinaryAbstractString = FormatUniEntry('#string ' + DT.TAB_INF_BINARY_ABSTRACT, BinaryAbstract, ContainerFile)\r
     if BinaryAbstractString:\r
 \r
     BinaryAbstractString = FormatUniEntry('#string ' + DT.TAB_INF_BINARY_ABSTRACT, BinaryAbstract, ContainerFile)\r
     if BinaryAbstractString:\r
-        Content += BinaryAbstractString + '\r\n'\r
+        Content += BinaryAbstractString + '\n'\r
 \r
     BinaryDescriptionString = FormatUniEntry('#string ' + DT.TAB_INF_BINARY_DESCRIPTION, BinaryDescription, \\r
                                              ContainerFile)\r
     if BinaryDescriptionString:\r
 \r
     BinaryDescriptionString = FormatUniEntry('#string ' + DT.TAB_INF_BINARY_DESCRIPTION, BinaryDescription, \\r
                                              ContainerFile)\r
     if BinaryDescriptionString:\r
-        Content += BinaryDescriptionString + '\r\n'\r
+        Content += BinaryDescriptionString + '\n'\r
 \r
     if not os.path.exists(ContainerFile):\r
         File = codecs.open(ContainerFile, 'wb', Encoding)\r
         File.write(u'\uFEFF' + Content)\r
         File.stream.close()\r
 \r
     if not os.path.exists(ContainerFile):\r
         File = codecs.open(ContainerFile, 'wb', Encoding)\r
         File.write(u'\uFEFF' + Content)\r
         File.stream.close()\r
-    Md5Sigature = md5.new(__FileHookOpen__(str(ContainerFile), 'rb').read())\r
+    Md5Sigature = md5(__FileHookOpen__(str(ContainerFile), 'rb').read())\r
     Md5Sum = Md5Sigature.hexdigest()\r
     if (ContainerFile, Md5Sum) not in ModuleObject.FileList:\r
         ModuleObject.FileList.append((ContainerFile, Md5Sum))\r
     Md5Sum = Md5Sigature.hexdigest()\r
     if (ContainerFile, Md5Sum) not in ModuleObject.FileList:\r
         ModuleObject.FileList.append((ContainerFile, Md5Sum))\r
@@ -271,7 +274,7 @@ def GenDefines(ModuleObject):
         if not DefinesDict:\r
             continue\r
         for Statement in DefinesDict:\r
         if not DefinesDict:\r
             continue\r
         for Statement in DefinesDict:\r
-            if Statement.split(DT.TAB_EQUAL_SPLIT) > 1:\r
+            if len(Statement.split(DT.TAB_EQUAL_SPLIT)) > 1:\r
                 Statement = (u'%s ' % Statement.split(DT.TAB_EQUAL_SPLIT, 1)[0]).ljust(LeftOffset) \\r
                              + u'= %s' % Statement.split(DT.TAB_EQUAL_SPLIT, 1)[1].lstrip()\r
             SortedArch = DT.TAB_ARCH_COMMON\r
                 Statement = (u'%s ' % Statement.split(DT.TAB_EQUAL_SPLIT, 1)[0]).ljust(LeftOffset) \\r
                              + u'= %s' % Statement.split(DT.TAB_EQUAL_SPLIT, 1)[1].lstrip()\r
             SortedArch = DT.TAB_ARCH_COMMON\r
@@ -297,11 +300,11 @@ def GenDefines(ModuleObject):
         BaseName = '_' + BaseName\r
     Statement = (u'%s ' % DT.TAB_INF_DEFINES_BASE_NAME).ljust(LeftOffset) + u'= %s' % BaseName\r
     SpecialStatementList.append(Statement)\r
         BaseName = '_' + BaseName\r
     Statement = (u'%s ' % DT.TAB_INF_DEFINES_BASE_NAME).ljust(LeftOffset) + u'= %s' % BaseName\r
     SpecialStatementList.append(Statement)\r
-    \r
+\r
     # TAB_INF_DEFINES_FILE_GUID\r
     Statement = (u'%s ' % DT.TAB_INF_DEFINES_FILE_GUID).ljust(LeftOffset) + u'= %s' % ModuleObject.GetGuid()\r
     SpecialStatementList.append(Statement)\r
     # TAB_INF_DEFINES_FILE_GUID\r
     Statement = (u'%s ' % DT.TAB_INF_DEFINES_FILE_GUID).ljust(LeftOffset) + u'= %s' % ModuleObject.GetGuid()\r
     SpecialStatementList.append(Statement)\r
-    \r
+\r
     # TAB_INF_DEFINES_VERSION_STRING\r
     Statement = (u'%s ' % DT.TAB_INF_DEFINES_VERSION_STRING).ljust(LeftOffset) + u'= %s' % ModuleObject.GetVersion()\r
     SpecialStatementList.append(Statement)\r
     # TAB_INF_DEFINES_VERSION_STRING\r
     Statement = (u'%s ' % DT.TAB_INF_DEFINES_VERSION_STRING).ljust(LeftOffset) + u'= %s' % ModuleObject.GetVersion()\r
     SpecialStatementList.append(Statement)\r
@@ -309,7 +312,7 @@ def GenDefines(ModuleObject):
     # TAB_INF_DEFINES_VERSION_STRING\r
     if ModuleObject.UNIFlag:\r
         Statement = (u'%s ' % DT.TAB_INF_DEFINES_MODULE_UNI_FILE).ljust(LeftOffset) + \\r
     # TAB_INF_DEFINES_VERSION_STRING\r
     if ModuleObject.UNIFlag:\r
         Statement = (u'%s ' % DT.TAB_INF_DEFINES_MODULE_UNI_FILE).ljust(LeftOffset) + \\r
-                    u'= %s' % ModuleObject.GetBaseName() + '.uni'\r
+                    u'= %s' % ModuleObject.GetModuleUniFile()\r
         SpecialStatementList.append(Statement)\r
 \r
     # TAB_INF_DEFINES_MODULE_TYPE\r
         SpecialStatementList.append(Statement)\r
 \r
     # TAB_INF_DEFINES_MODULE_TYPE\r
@@ -406,7 +409,7 @@ def GenLibraryClasses(ModuleObject):
                 Statement += '|' + FFE\r
             ModuleList = LibraryClass.GetSupModuleList()\r
             ArchList = LibraryClass.GetSupArchList()\r
                 Statement += '|' + FFE\r
             ModuleList = LibraryClass.GetSupModuleList()\r
             ArchList = LibraryClass.GetSupArchList()\r
-            for Index in xrange(0, len(ArchList)):\r
+            for Index in range(0, len(ArchList)):\r
                 ArchList[Index] = ConvertArchForInstall(ArchList[Index])\r
             ArchList.sort()\r
             SortedArch = ' '.join(ArchList)\r
                 ArchList[Index] = ConvertArchForInstall(ArchList[Index])\r
             ArchList.sort()\r
             SortedArch = ' '.join(ArchList)\r
@@ -435,14 +438,14 @@ def GenLibraryClasses(ModuleObject):
                 Statement = '# Guid: ' + LibraryItem.Guid + ' Version: ' + LibraryItem.Version\r
 \r
                 if len(BinaryFile.SupArchList) == 0:\r
                 Statement = '# Guid: ' + LibraryItem.Guid + ' Version: ' + LibraryItem.Version\r
 \r
                 if len(BinaryFile.SupArchList) == 0:\r
-                    if LibraryClassDict.has_key('COMMON') and Statement not in LibraryClassDict['COMMON']:\r
+                    if 'COMMON' in LibraryClassDict and Statement not in LibraryClassDict['COMMON']:\r
                         LibraryClassDict['COMMON'].append(Statement)\r
                     else:\r
                         LibraryClassDict['COMMON'] = ['## @LIB_INSTANCES']\r
                         LibraryClassDict['COMMON'].append(Statement)\r
                 else:\r
                     for Arch in BinaryFile.SupArchList:\r
                         LibraryClassDict['COMMON'].append(Statement)\r
                     else:\r
                         LibraryClassDict['COMMON'] = ['## @LIB_INSTANCES']\r
                         LibraryClassDict['COMMON'].append(Statement)\r
                 else:\r
                     for Arch in BinaryFile.SupArchList:\r
-                        if LibraryClassDict.has_key(Arch):\r
+                        if Arch in LibraryClassDict:\r
                             if Statement not in LibraryClassDict[Arch]:\r
                                 LibraryClassDict[Arch].append(Statement)\r
                             else:\r
                             if Statement not in LibraryClassDict[Arch]:\r
                                 LibraryClassDict[Arch].append(Statement)\r
                             else:\r
@@ -477,7 +480,7 @@ def GenPackages(ModuleObject):
         Path = ''\r
         #\r
         # find package path/name\r
         Path = ''\r
         #\r
         # find package path/name\r
-        # \r
+        #\r
         for PkgInfo in GlobalData.gWSPKG_LIST:\r
             if Guid == PkgInfo[1]:\r
                 if (not Version) or (Version == PkgInfo[2]):\r
         for PkgInfo in GlobalData.gWSPKG_LIST:\r
             if Guid == PkgInfo[1]:\r
                 if (not Version) or (Version == PkgInfo[2]):\r
@@ -490,8 +493,7 @@ def GenPackages(ModuleObject):
         Statement += RelaPath.replace('\\', '/')\r
         if FFE:\r
             Statement += '|' + FFE\r
         Statement += RelaPath.replace('\\', '/')\r
         if FFE:\r
             Statement += '|' + FFE\r
-        ArchList = PackageDependency.GetSupArchList()\r
-        ArchList.sort()\r
+        ArchList = sorted(PackageDependency.GetSupArchList())\r
         SortedArch = ' '.join(ArchList)\r
         if SortedArch in NewSectionDict:\r
             NewSectionDict[SortedArch] = NewSectionDict[SortedArch] + [Statement]\r
         SortedArch = ' '.join(ArchList)\r
         if SortedArch in NewSectionDict:\r
             NewSectionDict[SortedArch] = NewSectionDict[SortedArch] + [Statement]\r
@@ -510,8 +512,7 @@ def GenSources(ModuleObject):
         SourceFile = Source.GetSourceFile()\r
         Family = Source.GetFamily()\r
         FeatureFlag = Source.GetFeatureFlag()\r
         SourceFile = Source.GetSourceFile()\r
         Family = Source.GetFamily()\r
         FeatureFlag = Source.GetFeatureFlag()\r
-        SupArchList = Source.GetSupArchList()\r
-        SupArchList.sort()\r
+        SupArchList = sorted(Source.GetSupArchList())\r
         SortedArch = ' '.join(SupArchList)\r
         Statement = GenSourceStatement(ConvertPath(SourceFile), Family, FeatureFlag)\r
         if SortedArch in NewSectionDict:\r
         SortedArch = ' '.join(SupArchList)\r
         Statement = GenSourceStatement(ConvertPath(SourceFile), Family, FeatureFlag)\r
         if SortedArch in NewSectionDict:\r
@@ -552,7 +553,7 @@ def GenDepex(ModuleObject):
             else:\r
                 NewSectionDict[Key] = [Statement]\r
     Content += GenSection('Depex', NewSectionDict, False)\r
             else:\r
                 NewSectionDict[Key] = [Statement]\r
     Content += GenSection('Depex', NewSectionDict, False)\r
-    \r
+\r
     return Content\r
 ## GenUserExtensions\r
 #\r
     return Content\r
 ## GenUserExtensions\r
 #\r
@@ -567,10 +568,11 @@ def GenUserExtensions(ModuleObject):
         if UserExtension.GetIdentifier() == 'Depex':\r
             continue\r
         Statement = UserExtension.GetStatement()\r
         if UserExtension.GetIdentifier() == 'Depex':\r
             continue\r
         Statement = UserExtension.GetStatement()\r
-        if not Statement:\r
-            continue\r
+# Comment the code to support user extension without any statement just the section header in []\r
+#         if not Statement:\r
+#             continue\r
         ArchList = UserExtension.GetSupArchList()\r
         ArchList = UserExtension.GetSupArchList()\r
-        for Index in xrange(0, len(ArchList)):\r
+        for Index in range(0, len(ArchList)):\r
             ArchList[Index] = ConvertArchForInstall(ArchList[Index])\r
         ArchList.sort()\r
         KeyList = []\r
             ArchList[Index] = ConvertArchForInstall(ArchList[Index])\r
         ArchList.sort()\r
         KeyList = []\r
@@ -614,11 +616,11 @@ def GenSourceStatement(SourceFile, Family, FeatureFlag, TagName=None,
     # format of SourceFile|Family|TagName|ToolCode|FeatureFlag\r
     #\r
     Statement += SourceFile\r
     # format of SourceFile|Family|TagName|ToolCode|FeatureFlag\r
     #\r
     Statement += SourceFile\r
-    if TagName == None:\r
+    if TagName is None:\r
         TagName = ''\r
         TagName = ''\r
-    if ToolCode == None:\r
+    if ToolCode is None:\r
         ToolCode = ''\r
         ToolCode = ''\r
-    if HelpStr == None:\r
+    if HelpStr is None:\r
         HelpStr = ''\r
     if FeatureFlag:\r
         Statement += '|' + Family + '|' + TagName + '|' + ToolCode + '|' + FeatureFlag\r
         HelpStr = ''\r
     if FeatureFlag:\r
         Statement += '|' + Family + '|' + TagName + '|' + ToolCode + '|' + FeatureFlag\r
@@ -671,7 +673,7 @@ def GenBinaryStatement(Key, Value, SubTypeGuidValue=None):
             Statement += '|' + Target\r
     return Statement\r
 ## GenGuidSections\r
             Statement += '|' + Target\r
     return Statement\r
 ## GenGuidSections\r
-# \r
+#\r
 #  @param GuidObjList: List of GuidObject\r
 #  @retVal Content: The generated section contents\r
 #\r
 #  @param GuidObjList: List of GuidObject\r
 #  @retVal Content: The generated section contents\r
 #\r
@@ -718,8 +720,7 @@ def GenGuidSections(GuidObjList):
         #\r
         # merge duplicate items\r
         #\r
         #\r
         # merge duplicate items\r
         #\r
-        ArchList = Guid.GetSupArchList()\r
-        ArchList.sort()\r
+        ArchList = sorted(Guid.GetSupArchList())\r
         SortedArch = ' '.join(ArchList)\r
         if (Statement, SortedArch) in GuidDict:\r
             PreviousComment = GuidDict[Statement, SortedArch]\r
         SortedArch = ' '.join(ArchList)\r
         if (Statement, SortedArch) in GuidDict:\r
             PreviousComment = GuidDict[Statement, SortedArch]\r
@@ -735,7 +736,7 @@ def GenGuidSections(GuidObjList):
     return Content\r
 \r
 ## GenProtocolPPiSections\r
     return Content\r
 \r
 ## GenProtocolPPiSections\r
-# \r
+#\r
 #  @param ObjList: List of ProtocolObject or Ppi Object\r
 #  @retVal Content: The generated section contents\r
 #\r
 #  @param ObjList: List of ProtocolObject or Ppi Object\r
 #  @retVal Content: The generated section contents\r
 #\r
@@ -778,8 +779,7 @@ def GenProtocolPPiSections(ObjList, IsProtocol):
         #\r
         # merge duplicate items\r
         #\r
         #\r
         # merge duplicate items\r
         #\r
-        ArchList = Object.GetSupArchList()\r
-        ArchList.sort()\r
+        ArchList = sorted(Object.GetSupArchList())\r
         SortedArch = ' '.join(ArchList)\r
         if (Statement, SortedArch) in Dict:\r
             PreviousComment = Dict[Statement, SortedArch]\r
         SortedArch = ' '.join(ArchList)\r
         if (Statement, SortedArch) in Dict:\r
             PreviousComment = Dict[Statement, SortedArch]\r
@@ -804,7 +804,7 @@ def GenPcdSections(ModuleObject):
     Content = ''\r
     if not GlobalData.gIS_BINARY_INF:\r
         #\r
     Content = ''\r
     if not GlobalData.gIS_BINARY_INF:\r
         #\r
-        # for each Pcd Itemtype, maintain a dict so the same type will be grouped \r
+        # for each Pcd Itemtype, maintain a dict so the same type will be grouped\r
         # together\r
         #\r
         ItemTypeDict = {}\r
         # together\r
         #\r
         ItemTypeDict = {}\r
@@ -853,8 +853,7 @@ def GenPcdSections(ModuleObject):
             #\r
             # Merge duplicate entries\r
             #\r
             #\r
             # Merge duplicate entries\r
             #\r
-            ArchList = Pcd.GetSupArchList()\r
-            ArchList.sort()\r
+            ArchList = sorted(Pcd.GetSupArchList())\r
             SortedArch = ' '.join(ArchList)\r
             if (Statement, SortedArch) in Dict:\r
                 PreviousComment = Dict[Statement, SortedArch]\r
             SortedArch = ' '.join(ArchList)\r
             if (Statement, SortedArch) in Dict:\r
                 PreviousComment = Dict[Statement, SortedArch]\r
@@ -867,7 +866,7 @@ def GenPcdSections(ModuleObject):
             if NewSectionDict:\r
                 Content += GenSection(ItemType, NewSectionDict)\r
     #\r
             if NewSectionDict:\r
                 Content += GenSection(ItemType, NewSectionDict)\r
     #\r
-    # For AsBuild INF files   \r
+    # For AsBuild INF files\r
     #\r
     else:\r
         Content += GenAsBuiltPacthPcdSections(ModuleObject)\r
     #\r
     else:\r
         Content += GenAsBuiltPacthPcdSections(ModuleObject)\r
@@ -906,21 +905,21 @@ def GenAsBuiltPacthPcdSections(ModuleObject):
             Statement = HelpString + TokenSpaceName + '.' + PcdCName + ' | ' + PcdValue + ' | ' + \\r
                          PcdOffset + DT.TAB_SPACE_SPLIT\r
             #\r
             Statement = HelpString + TokenSpaceName + '.' + PcdCName + ' | ' + PcdValue + ' | ' + \\r
                          PcdOffset + DT.TAB_SPACE_SPLIT\r
             #\r
-            # Use binary file's Arch to be Pcd's Arch \r
+            # Use binary file's Arch to be Pcd's Arch\r
             #\r
             ArchList = []\r
             FileNameObjList = BinaryFile.GetFileNameList()\r
             if FileNameObjList:\r
                 ArchList = FileNameObjList[0].GetSupArchList()\r
             if len(ArchList) == 0:\r
             #\r
             ArchList = []\r
             FileNameObjList = BinaryFile.GetFileNameList()\r
             if FileNameObjList:\r
                 ArchList = FileNameObjList[0].GetSupArchList()\r
             if len(ArchList) == 0:\r
-                if PatchPcdDict.has_key(DT.TAB_ARCH_COMMON):\r
+                if DT.TAB_ARCH_COMMON in PatchPcdDict:\r
                     if Statement not in PatchPcdDict[DT.TAB_ARCH_COMMON]:\r
                         PatchPcdDict[DT.TAB_ARCH_COMMON].append(Statement)\r
                 else:\r
                     PatchPcdDict[DT.TAB_ARCH_COMMON] = [Statement]\r
             else:\r
                 for Arch in ArchList:\r
                     if Statement not in PatchPcdDict[DT.TAB_ARCH_COMMON]:\r
                         PatchPcdDict[DT.TAB_ARCH_COMMON].append(Statement)\r
                 else:\r
                     PatchPcdDict[DT.TAB_ARCH_COMMON] = [Statement]\r
             else:\r
                 for Arch in ArchList:\r
-                    if PatchPcdDict.has_key(Arch):\r
+                    if Arch in PatchPcdDict:\r
                         if Statement not in PatchPcdDict[Arch]:\r
                             PatchPcdDict[Arch].append(Statement)\r
                     else:\r
                         if Statement not in PatchPcdDict[Arch]:\r
                             PatchPcdDict[Arch].append(Statement)\r
                     else:\r
@@ -955,7 +954,7 @@ def GenAsBuiltPcdExSections(ModuleObject):
             Statement = HelpString + TokenSpaceName + DT.TAB_SPLIT + PcdCName + DT.TAB_SPACE_SPLIT\r
 \r
             #\r
             Statement = HelpString + TokenSpaceName + DT.TAB_SPLIT + PcdCName + DT.TAB_SPACE_SPLIT\r
 \r
             #\r
-            # Use binary file's Arch to be Pcd's Arch \r
+            # Use binary file's Arch to be Pcd's Arch\r
             #\r
             ArchList = []\r
             FileNameObjList = BinaryFile.GetFileNameList()\r
             #\r
             ArchList = []\r
             FileNameObjList = BinaryFile.GetFileNameList()\r
@@ -963,13 +962,13 @@ def GenAsBuiltPcdExSections(ModuleObject):
                 ArchList = FileNameObjList[0].GetSupArchList()\r
 \r
             if len(ArchList) == 0:\r
                 ArchList = FileNameObjList[0].GetSupArchList()\r
 \r
             if len(ArchList) == 0:\r
-                if PcdExDict.has_key('COMMON'):\r
+                if 'COMMON' in PcdExDict:\r
                     PcdExDict['COMMON'].append(Statement)\r
                 else:\r
                     PcdExDict['COMMON'] = [Statement]\r
             else:\r
                 for Arch in ArchList:\r
                     PcdExDict['COMMON'].append(Statement)\r
                 else:\r
                     PcdExDict['COMMON'] = [Statement]\r
             else:\r
                 for Arch in ArchList:\r
-                    if PcdExDict.has_key(Arch):\r
+                    if Arch in PcdExDict:\r
                         if Statement not in PcdExDict[Arch]:\r
                             PcdExDict[Arch].append(Statement)\r
                     else:\r
                         if Statement not in PcdExDict[Arch]:\r
                             PcdExDict[Arch].append(Statement)\r
                     else:\r
@@ -979,7 +978,7 @@ def GenAsBuiltPcdExSections(ModuleObject):
 ## GenSpecialSections\r
 #  generate special sections for Event/BootMode/Hob\r
 #\r
 ## GenSpecialSections\r
 #  generate special sections for Event/BootMode/Hob\r
 #\r
-def GenSpecialSections(ObjectList, SectionName):\r
+def GenSpecialSections(ObjectList, SectionName, UserExtensionsContent=''):\r
     #\r
     # generate section\r
     #\r
     #\r
     # generate section\r
     #\r
@@ -1002,6 +1001,11 @@ def GenSpecialSections(ObjectList, SectionName):
         else:\r
             assert(SectionName)\r
         Usage = Obj.GetUsage()\r
         else:\r
             assert(SectionName)\r
         Usage = Obj.GetUsage()\r
+\r
+        # If the content already in UserExtensionsContent then ignore\r
+        if '[%s]' % SectionName in UserExtensionsContent and Type in UserExtensionsContent:\r
+            return ''\r
+\r
         Statement = ' ' + Type + ' ## ' + Usage\r
         if CommentStr in ['#\n', '#\n#\n']:\r
             CommentStr = '#\n#\n#\n'\r
         Statement = ' ' + Type + ' ## ' + Usage\r
         if CommentStr in ['#\n', '#\n#\n']:\r
             CommentStr = '#\n#\n#\n'\r
@@ -1016,8 +1020,7 @@ def GenSpecialSections(ObjectList, SectionName):
         if CommentStr and not CommentStr.endswith('\n#\n'):\r
             CommentStr = CommentStr + '#\n'\r
         NewStateMent = CommentStr + Statement\r
         if CommentStr and not CommentStr.endswith('\n#\n'):\r
             CommentStr = CommentStr + '#\n'\r
         NewStateMent = CommentStr + Statement\r
-        SupArch = Obj.GetSupArchList()\r
-        SupArch.sort()\r
+        SupArch = sorted(Obj.GetSupArchList())\r
         SortedArch = ' '.join(SupArch)\r
         if SortedArch in NewSectionDict:\r
             NewSectionDict[SortedArch] = NewSectionDict[SortedArch] + [NewStateMent]\r
         SortedArch = ' '.join(SupArch)\r
         if SortedArch in NewSectionDict:\r
             NewSectionDict[SortedArch] = NewSectionDict[SortedArch] + [NewStateMent]\r
@@ -1030,7 +1033,7 @@ def GenSpecialSections(ObjectList, SectionName):
         Content = Content.lstrip()\r
     #\r
     # add a return to differentiate it between other possible sections\r
         Content = Content.lstrip()\r
     #\r
     # add a return to differentiate it between other possible sections\r
-    # \r
+    #\r
     if Content:\r
         Content += '\n'\r
     return Content\r
     if Content:\r
         Content += '\n'\r
     return Content\r
@@ -1062,7 +1065,7 @@ def GenBuildOptions(ModuleObject):
             for BuilOptionItem in BinaryFile.AsBuiltList[0].BinaryBuildFlagList:\r
                 Statement = '#' + BuilOptionItem.AsBuiltOptionFlags\r
                 if len(BinaryFile.SupArchList) == 0:\r
             for BuilOptionItem in BinaryFile.AsBuiltList[0].BinaryBuildFlagList:\r
                 Statement = '#' + BuilOptionItem.AsBuiltOptionFlags\r
                 if len(BinaryFile.SupArchList) == 0:\r
-                    if BuildOptionDict.has_key('COMMON'):\r
+                    if 'COMMON' in BuildOptionDict:\r
                         if Statement not in BuildOptionDict['COMMON']:\r
                             BuildOptionDict['COMMON'].append(Statement)\r
                     else:\r
                         if Statement not in BuildOptionDict['COMMON']:\r
                             BuildOptionDict['COMMON'].append(Statement)\r
                     else:\r
@@ -1070,7 +1073,7 @@ def GenBuildOptions(ModuleObject):
                         BuildOptionDict['COMMON'].append(Statement)\r
                 else:\r
                     for Arch in BinaryFile.SupArchList:\r
                         BuildOptionDict['COMMON'].append(Statement)\r
                 else:\r
                     for Arch in BinaryFile.SupArchList:\r
-                        if BuildOptionDict.has_key(Arch):\r
+                        if Arch in BuildOptionDict:\r
                             if Statement not in BuildOptionDict[Arch]:\r
                                 BuildOptionDict[Arch].append(Statement)\r
                         else:\r
                             if Statement not in BuildOptionDict[Arch]:\r
                                 BuildOptionDict[Arch].append(Statement)\r
                         else:\r
@@ -1095,8 +1098,7 @@ def GenBinaries(ModuleObject):
             FileName = ConvertPath(FileNameObj.GetFilename())\r
             FileType = FileNameObj.GetFileType()\r
             FFE = FileNameObj.GetFeatureFlag()\r
             FileName = ConvertPath(FileNameObj.GetFilename())\r
             FileType = FileNameObj.GetFileType()\r
             FFE = FileNameObj.GetFeatureFlag()\r
-            ArchList = FileNameObj.GetSupArchList()\r
-            ArchList.sort()\r
+            ArchList = sorted(FileNameObj.GetSupArchList())\r
             SortedArch = ' '.join(ArchList)\r
             Key = (FileName, FileType, FFE, SortedArch)\r
             if Key in BinariesDict:\r
             SortedArch = ' '.join(ArchList)\r
             Key = (FileName, FileType, FFE, SortedArch)\r
             if Key in BinariesDict:\r
@@ -1108,7 +1110,7 @@ def GenBinaries(ModuleObject):
                     else:\r
                         NewSectionDict[SortedArch] = [Statement]\r
                 #\r
                     else:\r
                         NewSectionDict[SortedArch] = [Statement]\r
                 #\r
-                # as we already generated statement for this DictKey here set the Valuelist to be empty \r
+                # as we already generated statement for this DictKey here set the Valuelist to be empty\r
                 # to avoid generate duplicate entries as the DictKey may have multiple entries\r
                 #\r
                 BinariesDict[Key] = []\r
                 # to avoid generate duplicate entries as the DictKey may have multiple entries\r
                 #\r
                 BinariesDict[Key] = []\r