[PATCH 3/9] Add a method to set environment variables for child processes

David Shea dshea at redhat.com
Tue Feb 10 09:13:11 UTC 2015


iutil.setenv will manipulate a dictionary that is added to a copy of
os.environ that is passed to Popen. This way environment variables can
be made available to child processes without modifying the environment
of the anaconda process, which is not thread safe.

Use iutil.setenv in place of os.environment for a variety of environment
variables not needed by the anaconda process.
---
 anaconda                      | 12 ++++++------
 pyanaconda/iutil.py           | 16 ++++++++++++++++
 pyanaconda/rescue.py          |  6 +++---
 pyanaconda/ui/gui/__init__.py |  2 +-
 4 files changed, 26 insertions(+), 10 deletions(-)

diff --git a/anaconda b/anaconda
index d2cb178..82af7e7 100755
--- a/anaconda
+++ b/anaconda
@@ -1146,14 +1146,14 @@ if __name__ == "__main__":
             log.info("Failed to parse proxy \"%s\": %s", anaconda.proxy, e)
         else:
             # Set environmental variables to be used by pre/post scripts
-            os.environ["PROXY"] = proxy.noauth_url
-            os.environ["PROXY_USER"] = proxy.username or ""
-            os.environ["PROXY_PASSWORD"] = proxy.password or ""
+            iutil.setenv("PROXY", proxy.noauth_url)
+            iutil.setenv("PROXY_USER", proxy.username or "")
+            iutil.setenv("PROXY_PASSWORD", proxy.password or "")
 
             # Variables used by curl, libreport, etc.
-            os.environ["http_proxy"] = proxy.url
-            os.environ["ftp_proxy"] = proxy.url
-            os.environ["HTTPS_PROXY"] = proxy.url
+            iutil.setenv("http_proxy", proxy.url)
+            iutil.setenv("ftp_proxy", proxy.url)
+            iutil.setenv("HTTPS_PROXY", proxy.url)
 
     if flags.noverifyssl:
         ksdata.method.noverifyssl = flags.noverifyssl
diff --git a/pyanaconda/iutil.py b/pyanaconda/iutil.py
index 87378f6..d82ef4f 100644
--- a/pyanaconda/iutil.py
+++ b/pyanaconda/iutil.py
@@ -50,11 +50,27 @@ program_log = logging.getLogger("program")
 
 from pyanaconda.anaconda_log import program_log_lock
 
+_child_env = {}
+
+def setenv(name, value):
+    """ Set an environment variable to be used by child processes.
+
+        This method does not modify os.environ for the running process, which
+        is not thread-safe. If setenv has already been called for a particular
+        variable name, the old value is overwritten.
+
+        :param str name: The name of the environment variable
+        :param str value: The value of the environment variable
+    """
+
+    _child_env[name] = value
+
 def augmentEnv():
     env = os.environ.copy()
     env.update({"LC_ALL": "C",
                 "ANA_INSTALL_PATH": getSysroot()
                })
+    env.update(_child_env)
     return env
 
 _root_path = "/mnt/sysimage"
diff --git a/pyanaconda/rescue.py b/pyanaconda/rescue.py
index 6702e2b..c808558 100644
--- a/pyanaconda/rescue.py
+++ b/pyanaconda/rescue.py
@@ -383,7 +383,7 @@ def doRescue(intf, rescue_mount, ksdata):
             # set a library path to use mounted fs
             libdirs = os.environ.get("LD_LIBRARY_PATH", "").split(":")
             mounted = map(lambda dir: "/mnt/sysimage%s" % dir, libdirs)
-            os.environ["LD_LIBRARY_PATH"] = ":".join(libdirs + mounted)
+            iutil.setenv("LD_LIBRARY_PATH", ":".join(libdirs + mounted))
 
             # find groff data dir
             gversion = None
@@ -401,8 +401,8 @@ def doRescue(intf, rescue_mount, ksdata):
 
             if gversion is not None:
                 gpath = "/mnt/sysimage/usr/share/groff/"+gversion
-                os.environ["GROFF_FONT_PATH"] = gpath + '/font'
-                os.environ["GROFF_TMAC_PATH"] = "%s:/mnt/sysimage/usr/share/groff/site-tmac" % (gpath + '/tmac',)
+                iutil.setenv("GROFF_FONT_PATH", gpath + '/font')
+                iutil.setenv("GROFF_TMAC_PATH", "%s:/mnt/sysimage/usr/share/groff/site-tmac" % (gpath + '/tmac',))
 
             # do we have bash?
             try:
diff --git a/pyanaconda/ui/gui/__init__.py b/pyanaconda/ui/gui/__init__.py
index b775a54..35d2c78 100644
--- a/pyanaconda/ui/gui/__init__.py
+++ b/pyanaconda/ui/gui/__init__.py
@@ -536,7 +536,7 @@ class GraphicalUserInterface(UserInterface):
         if monitor_height_px >= 1200 and monitor_dpi_x > 192 and monitor_dpi_y > 192:
             display.set_window_scale(2)
             # Export the scale so that Gtk programs launched by anaconda are also scaled
-            os.environ["GDK_SCALE"] = "2"
+            iutil.setenv("GDK_SCALE", 2)
 
     def _convertSignals(self):
         # What tends to happen when we receive a signal is that the signal will
-- 
2.1.0



More information about the anaconda-patches mailing list