From 1e1e36df6402276e7766fe88a341c236fb01d9b4 Mon Sep 17 00:00:00 2001
From: Dmitry Tantsur <dtantsur@protonmail.com>
Date: Fri, 29 May 2020 12:22:01 +0200
Subject: [PATCH] Do not fail in a venv when activate_this.py is not found

The standard Python venv module does not have this script, so currently
DIB unconditionally fails. While a real fix will be provided in
https://review.opendev.org/#/c/704478/ this change at least allows
users to try work around the problem.

Change-Id: I45b79d4d283f2b3ea909612e652672dcb6092488
---
 diskimage_builder/disk_image_create.py | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)

diff --git a/diskimage_builder/disk_image_create.py b/diskimage_builder/disk_image_create.py
index 67da2045a..cd164d5c1 100644
--- a/diskimage_builder/disk_image_create.py
+++ b/diskimage_builder/disk_image_create.py
@@ -12,6 +12,8 @@
 # License for the specific language governing permissions and limitations
 # under the License.
 
+from __future__ import print_function
+
 import os
 import os.path
 import runpy
@@ -33,9 +35,16 @@ def running_under_virtualenv():
 def activate_venv():
     if running_under_virtualenv():
         activate_this = os.path.join(sys.prefix, "bin", "activate_this.py")
-        globs = runpy.run_path(activate_this, globals())
-        globals().update(globs)
-        del globs
+        try:
+            globs = runpy.run_path(activate_this, globals())
+            globals().update(globs)
+            del globs
+        # TODO(dtantsur): replace with FileNotFoundError when Python 2 is no
+        # longer supported.
+        except OSError:
+            print("WARNING: A virtual environment was detected, but the "
+                  "activate_this.py script was not found. You may need to set "
+                  "PATH manually", file=sys.stderr)
 
 
 def main():