[Checkins] SVN: cipher.googlepam/trunk/ Make add-google-users skip users that already exist.

Marius Gedminas cvs-admin at zope.org
Thu Oct 11 14:43:22 UTC 2012


Log message for revision 127971:
  Make add-google-users skip users that already exist.
  
  Print a total of users added at the end, to indicate the script did
  something in addition to printing all those 'skipped' and 'not adding'
  messages when run a second time, after group membership changes on the
  Google side.

Changed:
  U   cipher.googlepam/trunk/CHANGES.txt
  U   cipher.googlepam/trunk/src/cipher/googlepam/addusers.py

-=-
Modified: cipher.googlepam/trunk/CHANGES.txt
===================================================================
--- cipher.googlepam/trunk/CHANGES.txt	2012-10-11 14:42:35 UTC (rev 127970)
+++ cipher.googlepam/trunk/CHANGES.txt	2012-10-11 14:43:19 UTC (rev 127971)
@@ -30,9 +30,12 @@
     we check for its existence but before we open it for reading.
 
 - Add missing test file for multi-group support.  It was accidentally left
-  out of the last release.
+  out of the last release causing a test failure.
 
+- Make add-google-users skip users that already exist without printing
+  scary error messages that make it seem the script aborted early.
 
+
 1.5.0 (2012-10-09)
 ------------------
 

Modified: cipher.googlepam/trunk/src/cipher/googlepam/addusers.py
===================================================================
--- cipher.googlepam/trunk/src/cipher/googlepam/addusers.py	2012-10-11 14:42:35 UTC (rev 127970)
+++ cipher.googlepam/trunk/src/cipher/googlepam/addusers.py	2012-10-11 14:43:19 UTC (rev 127971)
@@ -18,6 +18,8 @@
 import subprocess
 import sys
 import ConfigParser
+import pwd
+import grp
 
 from gdata.apps.groups.service import GroupsService
 from gdata.apps.service import AppsService
@@ -104,16 +106,35 @@
             })
         log.debug('Found user data: %r', users[-1])
     # 3. Create a new user account for each account.
+    added = 0
+    added_to_group = 0
     for user in users:
+        # We do not want to fail if the user already exists.
         try:
+            pwd.getpwnam(user['user_name'])
+        except KeyError:
             do(options.command % user, dry_run=options.dry_run)
-        except CMDError, err:
-            # We do not want to fail, if the user already exists.
-            if err.args[0] != 1:
-                raise
-        if options.admin_group:
-            do(options.group_command % user, dry_run=options.dry_run)
+            added += 1
+        else:
+            log.info('Not adding %s: user account already exists',
+                     user['user_name'])
+        if user['admin-group']:
+            if is_member(user['user_name'], user['admin-group']):
+                log.debug('Not adding %s to group %s: already a member',
+                          user['user_name'], user['admin-group'])
+            else:
+                do(options.group_command % user, dry_run=options.dry_run)
+                added_to_group += 1
+    if added:
+        log.info('Added %d users.', added)
+    if added_to_group:
+        log.info('Added %d users to group %s.', added_to_group,
+                 options.admin_group)
 
+def is_member(user, group):
+    return user in grp.getgrnam(group).gr_mem
+
+
 parser.add_option(
     '-C', '--config-file', action='store',
     dest='config_file', default=DEFAULT_CONFIG,



More information about the checkins mailing list