def cs_mod_user(self, username, group=None, extragroups=[], password=None, name=None, homedir=None, policy={}): """ This function returns a ChangeSet which performs user modification. It prepares any given arguments and checks also if changing the primary group was requested. In case the primary group is to be changed, the function also includes the differences from the old to the new group policy, keeping the account up to date (eg. quota). @param username: The username of the account to be modified @param group: The new primary group name @param password: The new password for the account in crypt hash format @param extragroups: Extra groups that the new account should belong to @param name: Real name of the accont owner @param homedir: Custom home directory for this account @param policy: Additional parameters that override the group policy @return: A ChangeSet """ # retrieve information about the users current group oldgid = get_user_by_name(username).pw_gid oldgroup = get_group_by_id(oldgid).gr_name # merge the given arguments into dict args args = {'username': username} args = merge_into(args, policy) # if changing the primary group if group is not None: args['group'] = group args['oldgroup'] = oldgroup args['extragroups'] = extragroups # find out the differences between the old and the new policy npol = self.pt.policy['groups'].get([group]) opol = self.pt.policy['groups'].get([oldgroup]) diff = compare_trees(npol, opol) # merge in the attributes that have new values for attr in diff: args[attr] = diff[attr] if password is not None: args['password'] = password if name is not None: args['name'] = name if homedir is not None: args['homedir'] = homedir # prepare the ChangeSet cs = ChangeSet(Change(self.name, "mod_user", args)) # notify the PolicyTool with a USER_MODIFIED event self.pt.emit_event(syspolicy.event.USER_MODIFIED, cs) return cs
#!/usr/bin/python from syspolicy.config import compare_trees import yaml a = {"aaa": "hehehe", "bbb": "asdf", "tree": {"asd": 1, "kekekk": 42}, "list": ["a"], "empty": None} b = {"bbb": "asd", "tree": {"asd": 1}, "list": ["a"]} r = compare_trees(a, b) print yaml.dump(r, default_flow_style=False)