[sabayon: 8/19] Add appply code for group profiles.



commit 48b3e508a58c7aed173ca95a1c4021b13411c43c
Author: Scott Balneaves <sbalneav ltsp org>
Date:   Thu Sep 3 21:13:10 2009 -0500

    Add appply code for group profiles.

 admin-tool/sabayon-apply |   68 +++++++++++++++++++++++++++++++++------------
 lib/util.py              |   31 +++++++++++++++++++++
 2 files changed, 81 insertions(+), 18 deletions(-)
---
diff --git a/admin-tool/sabayon-apply b/admin-tool/sabayon-apply
index 62e86d0..78e8407 100755
--- a/admin-tool/sabayon-apply
+++ b/admin-tool/sabayon-apply
@@ -20,7 +20,7 @@
 # Exit codes from this program:
 #
 # 0 - Success
-# 
+#
 # 1 - A fatal error occurred, and the user profile may not have been applied completely.
 #
 # 2 - One or more recoverable errors ocurred while applying the profile.
@@ -36,7 +36,7 @@ if __name__ == '__main__':
     import sys
     import optparse
     import shutil
-    
+
     from sabayon import userprofile
     from sabayon import config
     from sabayon import debuglog
@@ -81,29 +81,61 @@ if __name__ == '__main__':
         util.set_readable_log_config_filename (readable_log_config_filename)
 
         user_name = util.get_user_name ()
-
-        if systemdb.get_user_database().is_sabayon_controlled (user_name):
-            try:
-                shutil.rmtree (os.path.join (util.get_home_dir (), ".gconf.xml.defaults"), True)
-                shutil.rmtree (os.path.join (util.get_home_dir (), ".gconf.xml.mandatory"), True)
-                os.mkdir (os.path.join (util.get_home_dir (), ".gconf.xml.defaults"))
-                os.mkdir (os.path.join (util.get_home_dir (), ".gconf.xml.mandatory"))
-            except:
-                pass
+        group_membership = util.get_group_membership ()
+
+        #
+        # Begin the process of determining WHICH profile to apply.
+        # We have 4 possibilities, rated in order of priority (highest first)
+        # 1) We've been passed a profile on the command line.  We'll apply
+        #    this above all others, as it allows sysadmins to build their own
+        #    apply scripts that override the "autodetect" mechanism.
+        # 2) We've detected that a profile applies to this user, via the
+        #    users.xml database.  User profiles should override group profiles.
+        # 3) The user is a member of a group for which there's a profile.
+        #    Apply the profile for the group.
+        # 4) The user belongs to multiple groups to which there are profiles.
+        #    We'll pick the first one we come across. FIXME: is this good
+        #    behavior?
+        #
+
+        profile_name = None
 
         num_args = len (args)
-        if num_args == 0:
-            profile_name = systemdb.get_user_database().get_profile (user_name)
-            if not profile_name:
-                mprint ("No profile for user '%s' found", user_name)
-                sys.stderr.write (_("No profile for user '%s' found\n") % user_name)
-                sys.exit (util.EXIT_CODE_NO_USER_PROFILE)
-        elif num_args == 1:
+        if num_args == 1:
+            # We've been passed a profile name
             profile_name = args[0]
+        elif num_args == 0:
+            # lookup profile name for user
+            profile_name = systemdb.get_user_database().get_profile (user_name)
         else:
             sys.stderr.write (_("Please use -h for usage options"))
             sys.exit (util.EXIT_CODE_FATAL)
 
+        # Test for group lookup
+        if not profile_name:
+            for group_name in group_membership:
+                if systemdb.get_group_database().is_sabayon_controlled (group_name):
+                    profile_name = systemdb.get_group_database().get_profile (group_name)
+                    break
+
+        if not profile_name:
+            mprint ("No profile for user '%s' found", user_name)
+            sys.stderr.write (_("No profile for user '%s' found\n") % user_name)
+            sys.exit (util.EXIT_CODE_NO_USER_PROFILE)
+
+        #
+        # We've determined a profile applies to us.  Clean up existing
+        # defaults.
+        #
+
+        try:
+            shutil.rmtree (os.path.join (util.get_home_dir (), ".gconf.xml.defaults"), True)
+            shutil.rmtree (os.path.join (util.get_home_dir (), ".gconf.xml.mandatory"), True)
+            os.mkdir (os.path.join (util.get_home_dir (), ".gconf.xml.defaults"))
+            os.mkdir (os.path.join (util.get_home_dir (), ".gconf.xml.mandatory"))
+        except:
+            pass
+
         mprint ("Applying profile '%s' for user '%s'",
                 profile_name, util.get_user_name ())
 
diff --git a/lib/util.py b/lib/util.py
index 5aa8bff..0e8ecb4 100755
--- a/lib/util.py
+++ b/lib/util.py
@@ -21,6 +21,7 @@ import os.path
 import sys
 import fnmatch
 import pwd
+import grp
 import gettext
 import locale
 import errno
@@ -92,6 +93,36 @@ def get_home_dir ():
     else:
         raise GeneralError (_("Cannot find home directory: not set in /etc/passwd and no value for $HOME in environment"))
 
+def get_group_membership ():
+    """Returns a list of non-primary, non-system groups that the user belongs
+    to.  Raises a GeneralError if this fails.  May return an empty list.
+    """
+
+    groups = grp.getgrall()
+
+    try:
+        pw = pwd.getpwuid (os.getuid ())
+        user = pw[0]
+    except KeyError:
+        if os.environ.has_key("USER"):
+            user = os.environ["USER"]
+        else:
+            raise GeneralError (_("Cannot find username: not set in /etc/passwd and no value for $USER in environment"))
+
+    members = []
+
+    for group in groups:
+        if group[0] in members:
+            continue
+        if group[2] < 500:
+            continue
+        if group[0] == user:
+            continue
+        if user in group[3]:
+            members.append(group[0])
+
+    return members
+
 def get_user_name ():
     try:
         pw = pwd.getpwuid (os.getuid ())



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]