On some Linux environment, there may be no distutils.util
library for python3 that will cause UPT crash.
This patch implement distutils.util.split_quoted
in BaseTools so that the UPT tool will be independent with
distutils.util library.

Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Bob Feng <bob.c.f...@intel.com>
Cc: Liming Gao <liming....@intel.com>
---
 BaseTools/Source/Python/UPT/Library/UniClassObject.py | 47 
++++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 44 insertions(+), 3 deletions(-)

diff --git a/BaseTools/Source/Python/UPT/Library/UniClassObject.py 
b/BaseTools/Source/Python/UPT/Library/UniClassObject.py
index bd7804b753..79aa252507 100644
--- a/BaseTools/Source/Python/UPT/Library/UniClassObject.py
+++ b/BaseTools/Source/Python/UPT/Library/UniClassObject.py
@@ -18,11 +18,10 @@ from __future__ import print_function
 
 ##
 # Import Modules
 #
 import os, codecs, re
-import distutils.util
 from Logger import ToolError
 from Logger import Log as EdkLogger
 from Logger import StringTable as ST
 from Library.StringUtils import GetLineNo
 from Library.Misc import PathClass
@@ -36,11 +35,11 @@ from Library.ParserValidate import CheckUTF16FileHeader
 UNICODE_WIDE_CHAR = u'\\wide'
 UNICODE_NARROW_CHAR = u'\\narrow'
 UNICODE_NON_BREAKING_CHAR = u'\\nbr'
 UNICODE_UNICODE_CR = '\r'
 UNICODE_UNICODE_LF = '\n'
-
+WHITESPACE = ' \t\n\r\v\f'
 NARROW_CHAR = u'\uFFF0'
 WIDE_CHAR = u'\uFFF1'
 NON_BREAKING_CHAR = u'\uFFF2'
 CR = u'\u000D'
 LF = u'\u000A'
@@ -296,11 +295,53 @@ class StringDefClassObject(object):
                 self.StringValue = self.StringValue + '\r\n' + Value
             else:
                 self.StringValue = Value
             self.StringValueByteList = UniToHexList(self.StringValue)
             self.Length = len(self.StringValueByteList)
+#
+# This function implement distutils.util.split_quoted in another way.
+#
+def splitquoted(s):
+    words = []
+    word = []
+    InQuoted = False
+    escaped = False
+    CurrentQ = ""
+    s = s.strip()
+    for ch in s:
+        if escaped:
+            # preserve whatever is escaped;
+            # This char will become part of the current word
+            word.append(ch)
+            escaped = False
+            continue
 
+        if ch in WHITESPACE and not InQuoted:
+            # unescaped, unquoted whitespace: now
+            # we definitely have a word delimiter
+            if "".join(word):
+                words.append("".join(word))
+            word = []
+            continue
+        elif ch == "\\":
+            escaped = True
+            if not InQuoted:
+                continue
+        else:
+            # handle singly-quoted string or doubly-quoted string
+            if ch =='"' or ch == "'":
+                if not InQuoted:
+                    InQuoted = True
+                    CurrentQ = ch
+                    continue
+                elif ch == CurrentQ:
+                    InQuoted = False
+                    continue
+        word.append(ch)
+    if word:
+        words.append("".join(word))
+    return words
 ## UniFileClassObject
 #
 # A structure for .uni file definition
 #
 class UniFileClassObject(object):
@@ -324,11 +365,11 @@ class UniFileClassObject(object):
 
     #
     # Get Language definition
     #
     def GetLangDef(self, File, Line):
-        Lang = distutils.util.split_quoted((Line.split(u"//")[0]))
+        Lang = splitquoted((Line.split(u"//")[0]))
         if len(Lang) != 3:
             try:
                 FileIn = codecs.open(File.Path, mode='rb', 
encoding='utf_8').readlines()
             except UnicodeError as Xstr:
                 FileIn = codecs.open(File.Path, mode='rb', 
encoding='utf_16').readlines()
-- 
2.20.1.windows.1

_______________________________________________
edk2-devel mailing list
edk2-devel@lists.01.org
https://lists.01.org/mailman/listinfo/edk2-devel

Reply via email to