[edk2-devel] [edk2-staging/EdkRepo] [PATCH] EdkRepo: Python 2.x deprecation warning in pip breaks EdkRepoInstaller

Nate DeSimone nathaniel.l.desimone at intel.com
Wed Oct 30 23:48:15 UTC 2019


Newer versions of pip add the following output in addition to
the JSON output when running on Python 2.7:

DEPRECATION: Python 2.7 will reach the end of its life on
January 1st, 2020. Please upgrade your Python as Python 2.7 won't
be maintained after that date. A future version of pip will
drop support for Python 2.7.

This breaks machine parsing of the JSON output from pip.
Adding logic to strip the deprecation warning to fix this issue.

Signed-off-by: Nate DeSimone <nathaniel.l.desimone at intel.com>
Cc: Ashley E Desimone <ashley.e.desimone at intel.com>
Cc: Puja Pandya <puja.pandya at intel.com>
---
 .../EdkRepoInstaller/PythonOperations.cs      | 37 +++++++++++++++++--
 1 file changed, 34 insertions(+), 3 deletions(-)

diff --git a/edkrepo_installer/EdkRepoInstaller/PythonOperations.cs b/edkrepo_installer/EdkRepoInstaller/PythonOperations.cs
index f96502c..73d5bec 100644
--- a/edkrepo_installer/EdkRepoInstaller/PythonOperations.cs
+++ b/edkrepo_installer/EdkRepoInstaller/PythonOperations.cs
@@ -600,17 +600,48 @@ namespace TianoCore.EdkRepoInstaller
             return PythonPackages;
         }
 
+        private static string SanitizePipOutput(string PipOutput)
+        {
+            StringBuilder Sanitized = new StringBuilder();
+            IEnumerable<string> PipLines = PipOutput.SplitLines();
+            foreach(string line in PipLines)
+            {
+                if(line.StartsWith("DEPRECATION:"))
+                {
+                    continue;
+                }
+                if (string.IsNullOrWhiteSpace(line))
+                {
+                    continue;
+                }
+                Sanitized.Append(line.Trim());
+                Sanitized.Append("\r\n");
+            }
+            return Sanitized.ToString().Trim();
+        }
+
         public static List<PythonPackage> GetInstalledPythonPackages(string PythonPath)
         {
             List<PythonPackage> PythonPackages = new List<PythonPackage>();
             SilentProcess.StdoutDataCapture dataCapture = new SilentProcess.StdoutDataCapture();
             SilentProcess process = SilentProcess.StartConsoleProcessSilently(PythonPath, "-m pip list --format=\"json\" --no-index", dataCapture.DataReceivedHandler);
             process.WaitForExit();
+            bool TryLegacy = true;
             if (process.ExitCode == 0)
             {
-                PythonPackages = ParseJsonPipList(dataCapture.GetData());
+                try
+                {
+                    PythonPackages = ParseJsonPipList(SanitizePipOutput(dataCapture.GetData()));
+                    TryLegacy = false;
+                }
+                catch(Exception e)
+                {
+                    InstallLogger.Log("Error occurred while trying to parse pip JSON:");
+                    InstallLogger.Log(e.ToString());
+                    InstallLogger.Log("Falling back to legacy mode");
+                }
             }
-            else
+            if(TryLegacy)
             {
                 //
                 // Older versions of pip don't support the --format flag, parse the legacy format
@@ -620,7 +651,7 @@ namespace TianoCore.EdkRepoInstaller
                 process.WaitForExit();
                 if (process.ExitCode == 0)
                 {
-                    PythonPackages = ParseLegacyPipList(dataCapture.GetData());
+                    PythonPackages = ParseLegacyPipList(SanitizePipOutput(dataCapture.GetData()));
                 }
             }
             return PythonPackages;
-- 
2.23.0.windows.1


-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.

View/Reply Online (#49682): https://edk2.groups.io/g/devel/message/49682
Mute This Topic: https://groups.io/mt/40038547/1813853
Group Owner: devel+owner at edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub  [edk2-devel-archive at redhat.com]
-=-=-=-=-=-=-=-=-=-=-=-




More information about the edk2-devel-archive mailing list