]> git.proxmox.com Git - mirror_edk2.git/blobdiff - BaseTools/Source/Python/UPT/Library/UniClassObject.py
BaseTools: UPT: remove unused variable and inaccessible code.
[mirror_edk2.git] / BaseTools / Source / Python / UPT / Library / UniClassObject.py
index d542dc5dc86c4c116bb7077842a3358106f909c0..0014a7561ba88b1b02b735ecc04ba2b334571f0c 100644 (file)
@@ -1,7 +1,7 @@
 ## @file\r
 # Collect all defined strings in multiple uni files.\r
 #\r
-# Copyright (c) 2014, Intel Corporation. All rights reserved.<BR>\r
+# Copyright (c) 2014 - 2018, Intel Corporation. All rights reserved.<BR>\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
@@ -27,6 +27,7 @@ from Library.String import GetLineNo
 from Library.Misc import PathClass\r
 from Library.Misc import GetCharIndexOutStr\r
 from Library import DataType as DT\r
+from Library.ParserValidate import CheckUTF16FileHeader\r
 \r
 ##\r
 # Static definitions\r
@@ -46,8 +47,6 @@ NULL = u'\u0000'
 TAB = u'\t'\r
 BACK_SPLASH = u'\\'\r
 \r
-gINCLUDE_PATTERN = re.compile("^!include[\s]+([\S]+)[\s]*$", re.MULTILINE | re.UNICODE)\r
-\r
 gLANG_CONV_TABLE = {'eng':'en', 'fra':'fr', \\r
                  'aar':'aa', 'abk':'ab', 'ave':'ae', 'afr':'af', 'aka':'ak', 'amh':'am', \\r
                  'arg':'an', 'ara':'ar', 'asm':'as', 'ava':'av', 'aym':'ay', 'aze':'az', \\r
@@ -136,6 +135,8 @@ def ConvertSpecialUnicodes(Uni):
 # @retval LangName:  Valid lanugage code in RFC 1766 format or None\r
 #\r
 def GetLanguageCode1766(LangName, File=None):\r
+    return LangName\r
+\r
     length = len(LangName)\r
     if length == 2:\r
         if LangName.isalpha():\r
@@ -245,9 +246,9 @@ def FormatUniEntry(StrTokenName, TokenValueList, ContainerFile):
         for SubValue in ValueList:\r
             if SubValue.strip():\r
                 SubValueContent += \\r
-                ' ' * (PreFormatLength + len('#language en-US    ')) + '\"%s\\n\"' % SubValue.strip() + os.linesep\r
-        SubValueContent = SubValueContent[(PreFormatLength + len('#language en-US    ')):SubValueContent.rfind('\\n')] \\r
-        + '\"' + os.linesep\r
+                ' ' * (PreFormatLength + len('#language en-US ')) + '\"%s\\n\"' % SubValue.strip() + '\r\n'\r
+        SubValueContent = SubValueContent[(PreFormatLength + len('#language en-US ')):SubValueContent.rfind('\\n')] \\r
+        + '\"' + '\r\n'\r
         SubContent += ' '*PreFormatLength + '#language %-5s ' % Lang + SubValueContent\r
     if SubContent:\r
         SubContent = StrTokenName + ' '*(PreFormatLength - len(StrTokenName)) + SubContent[PreFormatLength:]\r
@@ -289,7 +290,7 @@ class StringDefClassObject(object):
     def UpdateValue(self, Value = None):\r
         if Value != None:\r
             if self.StringValue:\r
-                self.StringValue = self.StringValue  + os.linesep + Value\r
+                self.StringValue = self.StringValue + '\r\n' + Value\r
             else:\r
                 self.StringValue = Value\r
             self.StringValueByteList = UniToHexList(self.StringValue)\r
@@ -325,9 +326,11 @@ class UniFileClassObject(object):
         Lang = distutils.util.split_quoted((Line.split(u"//")[0]))\r
         if len(Lang) != 3:\r
             try:\r
-                FileIn = codecs.open(File.Path, mode='rb', encoding='utf_16').read()\r
+                FileIn = codecs.open(File.Path, mode='rb', encoding='utf_8').readlines()\r
+            except UnicodeError, Xstr:\r
+                FileIn = codecs.open(File.Path, mode='rb', encoding='utf_16').readlines()\r
             except UnicodeError, Xstr:\r
-                FileIn = codecs.open(File.Path, mode='rb', encoding='utf_16_le').read()\r
+                FileIn = codecs.open(File.Path, mode='rb', encoding='utf_16_le').readlines()\r
             except:\r
                 EdkLogger.Error("Unicode File Parser", \r
                                 ToolError.FILE_OPEN_FAILURE, \r
@@ -424,7 +427,16 @@ class UniFileClassObject(object):
                              ToolError.FILE_NOT_FOUND,\r
                              ExtraData=File.Path)\r
 \r
+        #\r
+        # Check file header of the Uni file\r
+        #\r
+#         if not CheckUTF16FileHeader(File.Path):\r
+#             EdkLogger.Error("Unicode File Parser", ToolError.FORMAT_INVALID,\r
+#                             ExtraData='The file %s is either invalid UTF-16LE or it is missing the BOM.' % File.Path)\r
+\r
         try:\r
+            FileIn = codecs.open(File.Path, mode='rb', encoding='utf_8').readlines()\r
+        except UnicodeError, Xstr:\r
             FileIn = codecs.open(File.Path, mode='rb', encoding='utf_16').readlines()\r
         except UnicodeError:\r
             FileIn = codecs.open(File.Path, mode='rb', encoding='utf_16_le').readlines()\r
@@ -452,7 +464,7 @@ class UniFileClassObject(object):
             if not Line.startswith(DT.TAB_COMMENT_EDK1_SPLIT) and HeaderStart and not HeaderEnd:\r
                 HeaderEnd = True\r
             if Line.startswith(DT.TAB_COMMENT_EDK1_SPLIT) and HeaderStart and not HeaderEnd and FirstGenHeader:\r
-                self.UniFileHeader += Line + os.linesep\r
+                self.UniFileHeader += Line + '\r\n'\r
                 continue\r
         \r
         #\r
@@ -498,11 +510,11 @@ class UniFileClassObject(object):
                 if FileIn[LineCount].strip().startswith('#language'):\r
                     Line = Line + FileIn[LineCount]\r
                     FileIn[LineCount-1] = Line\r
-                    FileIn[LineCount] = os.linesep\r
+                    FileIn[LineCount] = '\r\n'\r
                     LineCount -= 1\r
                     for Index in xrange (LineCount + 1, len (FileIn) - 1):\r
                         if (Index == len(FileIn) -1):\r
-                            FileIn[Index] = os.linesep\r
+                            FileIn[Index] = '\r\n'\r
                         else:\r
                             FileIn[Index] = FileIn[Index + 1]\r
                     continue\r
@@ -529,21 +541,15 @@ class UniFileClassObject(object):
             Line = Line.replace(u'\t', u' ')\r
             Line = Line.replace(u'\u0006', u'\\')\r
 \r
-            IncList = gINCLUDE_PATTERN.findall(Line)\r
-            if len(IncList) == 1:\r
-                for Dir in [File.Dir] + self.IncludePathList:\r
-                    IncFile = PathClass(str(IncList[0]), Dir)\r
-                    self.IncFileList.append(IncFile)\r
-                    if os.path.isfile(IncFile.Path):\r
-                        Lines.extend(self.PreProcess(IncFile, True))\r
-                        break\r
-                else:\r
-                    EdkLogger.Error("Unicode File Parser", \r
-                                    ToolError.FILE_NOT_FOUND, \r
-                                    Message="Cannot find include file", \r
-                                    ExtraData=str(IncList[0]))\r
-                continue\r
-            \r
+            #\r
+            # Check if single line has correct '"'\r
+            #\r
+            if Line.startswith(u'#string') and Line.find(u'#language') > -1 and Line.find('"') > Line.find(u'#language'):\r
+                if not Line.endswith('"'):\r
+                    EdkLogger.Error("Unicode File Parser", ToolError.FORMAT_INVALID,\r
+                                    ExtraData='''The line %s misses '"' at the end of it in file %s'''\r
+                                                 % (LineCount, File.Path))\r
+\r
             #\r
             # Between Name entry and Language entry can not contain line feed\r
             #\r
@@ -564,16 +570,18 @@ class UniFileClassObject(object):
             #\r
             if Line.startswith(u'"'):\r
                 if StringEntryExistsFlag == 2:\r
-                    EdkLogger.Error("Unicode File Parser", ToolError.FORMAT_INVALID, \r
+                    EdkLogger.Error("Unicode File Parser", ToolError.FORMAT_INVALID,\r
                                     Message=ST.ERR_UNIPARSE_LINEFEED_UP_EXIST % Line, ExtraData=File.Path)\r
-                     \r
+\r
                 StringEntryExistsFlag = 1\r
                 if not Line.endswith('"'):\r
-                    EdkLogger.Error("Unicode File Parser", ToolError.FORMAT_INVALID, ExtraData=File.Path)\r
+                    EdkLogger.Error("Unicode File Parser", ToolError.FORMAT_INVALID,\r
+                                    ExtraData='''The line %s misses '"' at the end of it in file %s'''\r
+                                              % (LineCount, File.Path))\r
             elif Line.startswith(u'#language'):\r
                 if StringEntryExistsFlag == 2:\r
-                    EdkLogger.Error("Unicode File Parser", ToolError.FORMAT_INVALID, \r
-                                    Message=ST.ERR_UNIPARSE_LINEFEED_UP_EXIST % Line, ExtraData=File.Path)\r
+                    EdkLogger.Error("Unicode File Parser", ToolError.FORMAT_INVALID,\r
+                                    Message=ST.ERR_UNI_MISS_STRING_ENTRY % Line, ExtraData=File.Path)\r
                 StringEntryExistsFlag = 0\r
             else:\r
                 StringEntryExistsFlag = 0\r
@@ -858,12 +866,12 @@ class UniFileClassObject(object):
                                 Value = Value + Lines[IndexJ].strip()[1:-1]\r
                             CombineToken = False\r
                         else:\r
-                            Value = Value + Lines[IndexJ].strip()[1:-1] + os.linesep\r
+                            Value = Value + Lines[IndexJ].strip()[1:-1] + '\r\n'\r
                     else:\r
                         IndexI = IndexJ\r
                         break\r
-                if Value.endswith(os.linesep):\r
-                    Value = Value[: Value.rfind(os.linesep)]\r
+                if Value.endswith('\r\n'):\r
+                    Value = Value[: Value.rfind('\r\n')]\r
                 Language = GetLanguageCode(Language, self.IsCompatibleMode, self.File)\r
                 self.AddStringToList(Name, Language, Value)\r
                 continue\r
@@ -1033,6 +1041,8 @@ class UniFileClassObject(object):
                              ToolError.FILE_NOT_FOUND,\r
                              ExtraData=FilaPath)\r
         try:\r
+            FileIn = codecs.open(FilaPath, mode='rb', encoding='utf_8').readlines()\r
+        except UnicodeError, Xstr:\r
             FileIn = codecs.open(FilaPath, mode='rb', encoding='utf_16').readlines()\r
         except UnicodeError:\r
             FileIn = codecs.open(FilaPath, mode='rb', encoding='utf_16_le').readlines()\r