]> git.proxmox.com Git - mirror_edk2.git/commitdiff
BaseTools: Add the support for python 2
authorFeng, Bob C <bob.c.feng@intel.com>
Tue, 30 Jul 2019 10:19:33 +0000 (18:19 +0800)
committerFeng, Bob C <bob.c.feng@intel.com>
Fri, 9 Aug 2019 15:15:55 +0000 (23:15 +0800)
BZ: https://bugzilla.tianocore.org/show_bug.cgi?id=1875

python3 change the module name of Queue to queue.
python3 add a new log handler of QueueHandler.

This patch is to make Multiple process AutoGen
feature work for python2

Cc: Liming Gao <liming.gao@intel.com>
Signed-off-by: Bob Feng <bob.c.feng@intel.com>
Acked-by: Laszlo Ersek <lersek@redhat.com>
Tested-by: Laszlo Ersek <lersek@redhat.com>
Acked-by: Liming Gao <liming.gao@intel.com>
BaseTools/Source/Python/AutoGen/AutoGenWorker.py
BaseTools/Source/Python/Common/EdkLogger.py

index 1296604f688d3844ff3bdb443e01dab7491727fa..0a3c1d8e0ebd02967a4d3f04ad63a5c15fb070a0 100644 (file)
@@ -16,7 +16,10 @@ import os
 from Common.MultipleWorkspace import MultipleWorkspace as mws\r
 from AutoGen.AutoGen import AutoGen\r
 from Workspace.WorkspaceDatabase import BuildDB\r
-from queue import Empty\r
+try:\r
+    from queue import Empty\r
+except:\r
+    from Queue import Empty\r
 import traceback\r
 import sys\r
 from AutoGen.DataPipe import MemoryDataPipe\r
index f6a5e3b4daf9e544ed06082048c6a62f056d8b94..15fd1458a95ac85fd959945d20084e2adb22b675 100644 (file)
@@ -5,12 +5,96 @@
 # SPDX-License-Identifier: BSD-2-Clause-Patent\r
 #\r
 \r
+# Copyright 2001-2016 by Vinay Sajip. All Rights Reserved.\r
+#\r
+# Permission to use, copy, modify, and distribute this software and its\r
+# documentation for any purpose and without fee is hereby granted,\r
+# provided that the above copyright notice appear in all copies and that\r
+# both that copyright notice and this permission notice appear in\r
+# supporting documentation, and that the name of Vinay Sajip\r
+# not be used in advertising or publicity pertaining to distribution\r
+# of the software without specific, written prior permission.\r
+# VINAY SAJIP DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING\r
+# ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL\r
+# VINAY SAJIP BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR\r
+# ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER\r
+# IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT\r
+# OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.\r
+# This copyright is for QueueHandler.\r
+\r
 ## Import modules\r
 from __future__ import absolute_import\r
 import Common.LongFilePathOs as os, sys, logging\r
 import traceback\r
 from  .BuildToolError import *\r
-import logging.handlers\r
+try:\r
+    from logging.handlers import QueueHandler\r
+except:\r
+    class QueueHandler(logging.Handler):\r
+        """\r
+        This handler sends events to a queue. Typically, it would be used together\r
+        with a multiprocessing Queue to centralise logging to file in one process\r
+        (in a multi-process application), so as to avoid file write contention\r
+        between processes.\r
+\r
+        This code is new in Python 3.2, but this class can be copy pasted into\r
+        user code for use with earlier Python versions.\r
+        """\r
+\r
+        def __init__(self, queue):\r
+            """\r
+            Initialise an instance, using the passed queue.\r
+            """\r
+            logging.Handler.__init__(self)\r
+            self.queue = queue\r
+\r
+        def enqueue(self, record):\r
+            """\r
+            Enqueue a record.\r
+\r
+            The base implementation uses put_nowait. You may want to override\r
+            this method if you want to use blocking, timeouts or custom queue\r
+            implementations.\r
+            """\r
+            self.queue.put_nowait(record)\r
+\r
+        def prepare(self, record):\r
+            """\r
+            Prepares a record for queuing. The object returned by this method is\r
+            enqueued.\r
+\r
+            The base implementation formats the record to merge the message\r
+            and arguments, and removes unpickleable items from the record\r
+            in-place.\r
+\r
+            You might want to override this method if you want to convert\r
+            the record to a dict or JSON string, or send a modified copy\r
+            of the record while leaving the original intact.\r
+            """\r
+            # The format operation gets traceback text into record.exc_text\r
+            # (if there's exception data), and also returns the formatted\r
+            # message. We can then use this to replace the original\r
+            # msg + args, as these might be unpickleable. We also zap the\r
+            # exc_info and exc_text attributes, as they are no longer\r
+            # needed and, if not None, will typically not be pickleable.\r
+            msg = self.format(record)\r
+            record.message = msg\r
+            record.msg = msg\r
+            record.args = None\r
+            record.exc_info = None\r
+            record.exc_text = None\r
+            return record\r
+\r
+        def emit(self, record):\r
+            """\r
+            Emit a record.\r
+\r
+            Writes the LogRecord to the queue, preparing it for pickling first.\r
+            """\r
+            try:\r
+                self.enqueue(self.prepare(record))\r
+            except Exception:\r
+                self.handleError(record)\r
 \r
 ## Log level constants\r
 DEBUG_0 = 1\r
@@ -208,19 +292,19 @@ def LogClientInitialize(log_q):
     #\r
     # For DEBUG level (All DEBUG_0~9 are applicable)\r
     _DebugLogger.setLevel(INFO)\r
-    _DebugChannel = logging.handlers.QueueHandler(log_q)\r
+    _DebugChannel = QueueHandler(log_q)\r
     _DebugChannel.setFormatter(_DebugFormatter)\r
     _DebugLogger.addHandler(_DebugChannel)\r
 \r
     # For VERBOSE, INFO, WARN level\r
     _InfoLogger.setLevel(INFO)\r
-    _InfoChannel = logging.handlers.QueueHandler(log_q)\r
+    _InfoChannel = QueueHandler(log_q)\r
     _InfoChannel.setFormatter(_InfoFormatter)\r
     _InfoLogger.addHandler(_InfoChannel)\r
 \r
     # For ERROR level\r
     _ErrorLogger.setLevel(INFO)\r
-    _ErrorCh = logging.handlers.QueueHandler(log_q)\r
+    _ErrorCh = QueueHandler(log_q)\r
     _ErrorCh.setFormatter(_ErrorFormatter)\r
     _ErrorLogger.addHandler(_ErrorCh)\r
 \r