From 54e89a2118f835bfa3f2db26d0fa54256a5606d4 Mon Sep 17 00:00:00 2001
From: Sergei Trofimov <sergei.trofimov@arm.com>
Date: Fri, 24 Apr 2015 10:05:13 +0100
Subject: [PATCH] Fix to initial ~/.workload_automation deployment when sudoing

Only attempt to chown ~/.workload_automation to SUDO_USER if the
effective user is root.
---
 wlauto/core/bootstrap.py | 27 ++++++++++++++-------------
 1 file changed, 14 insertions(+), 13 deletions(-)

diff --git a/wlauto/core/bootstrap.py b/wlauto/core/bootstrap.py
index 540522a4..c299ebae 100644
--- a/wlauto/core/bootstrap.py
+++ b/wlauto/core/bootstrap.py
@@ -151,19 +151,20 @@ def init_environment(env_root, dep_dir, extension_paths, overwrite_existing=Fals
     for path in extension_paths:
         os.makedirs(path)
 
-    # If running with sudo on POSIX, change the ownership to the real user.
-    real_user = os.getenv('SUDO_USER')
-    if real_user:
-        import pwd  # done here as module won't import on win32
-        user_entry = pwd.getpwnam(real_user)
-        uid, gid = user_entry.pw_uid, user_entry.pw_gid
-        os.chown(env_root, uid, gid)
-        # why, oh why isn't there a recusive=True option for os.chown?
-        for root, dirs, files in os.walk(env_root):
-            for d in dirs:
-                os.chown(os.path.join(root, d), uid, gid)
-            for f in files:  # pylint: disable=W0621
-                os.chown(os.path.join(root, f), uid, gid)
+    if os.getenv('USER') == 'root':
+        # If running with sudo on POSIX, change the ownership to the real user.
+        real_user = os.getenv('SUDO_USER')
+        if real_user:
+            import pwd  # done here as module won't import on win32
+            user_entry = pwd.getpwnam(real_user)
+            uid, gid = user_entry.pw_uid, user_entry.pw_gid
+            os.chown(env_root, uid, gid)
+            # why, oh why isn't there a recusive=True option for os.chown?
+            for root, dirs, files in os.walk(env_root):
+                for d in dirs:
+                    os.chown(os.path.join(root, d), uid, gid)
+                for f in files:  # pylint: disable=W0621
+                    os.chown(os.path.join(root, f), uid, gid)
 
 
 _env_root = os.getenv('WA_USER_DIRECTORY', os.path.join(_user_home, '.workload_automation'))