def _detect_process_changes(self): changes = {} processes = self._get_processes() creates, updates, deletes = diff(self._persist_processes, processes) if creates: changes["add-processes"] = list(itervalues(creates)) if updates: changes["update-processes"] = list(itervalues(updates)) if deletes: changes["kill-processes"] = list(deletes) # Update cached values for use on the next run. self._previous_processes = processes return changes
def _detect_user_changes(self): """ Compare the current user snapshot to the old one and return a C{dict} with C{create-users}, C{update-users} and C{delete-users} fields. Fields without data aren't included in the result. """ changes = {} creates, updates, deletes = diff(self._old_users, self._new_users) if creates: changes["create-users"] = list(itervalues(creates)) if updates: changes["update-users"] = list(itervalues(updates)) if deletes: changes["delete-users"] = list(deletes) return changes
def _disconnected(self, reason): """ Called when the stream has been closed. From this point on, the manager doesn't interact with the L{XmlStream} anymore and notifies each handler that the connection was lost by calling its C{connectionLost} method. """ self.xmlstream = None self._initialized = False # Twisted versions before 11.0 passed an XmlStream here. if not hasattr(reason, 'trap'): reason = failure.Failure(ConnectionDone()) # Notify all child services which implement # the IService interface for e in list(self): e.connectionLost(reason) # This errbacks all deferreds of iq's for which no response has # been received with a L{ConnectionLost} failure. Otherwise, the # deferreds will never be fired. iqDeferreds = self._iqDeferreds self._iqDeferreds = {} for d in itervalues(iqDeferreds): d.errback(reason)
def add_user(self, username, name, password, require_password_reset, primary_group_name, location, work_phone, home_phone): try: uid = 1000 if self._users: uid = max([x["uid"] for x in itervalues(self._users)]) + 1 if self._groups: primary_gid = self.get_gid(primary_group_name) else: primary_gid = uid self._users[uid] = { "username": username, "name": name, "uid": uid, "enabled": True, "location": location, "work-phone": work_phone, "home-phone": home_phone, "primary-gid": primary_gid } gecos_string = "%s,%s,%s,%s" % (name, location or "", work_phone or "", home_phone or "") userdata = (username, "x", uid, primary_gid, gecos_string, "/bin/sh", "/home/user") self.provider.users.append(userdata) except KeyError: raise UserManagementError("add_user failed") return "add_user succeeded"
def add_group(self, name): gid = 1000 if self._groups: gid = max([x["gid"] for x in itervalues(self._groups)]) + 1 self._groups[name] = {"name": name, "gid": gid, "members": []} self.update_provider_from_groups() return "add_group succeeded"
def __get_status(self): if None in self.statuses: return self.statuses[None] elif self.statuses: for status in itervalues(self.status): return status else: return None
def _detect_group_changes(self): """ Compare the current group snapshot to the old one and create a C{dict} with C{create-groups}, C{delete-groups}, C{create-group-members} and {delete-group-members} fields. Fields without data aren't included in the result. """ changes = {} creates, updates, deletes = diff(self._old_groups, self._new_groups) if creates: groups = [] create_members = {} for value in itervalues(creates): # Use a copy to avoid removing the 'members' element # from stored data. value = value.copy() members = value.pop("members") if members: create_members[value["name"]] = members groups.append(value) changes["create-groups"] = groups if create_members: changes["create-group-members"] = create_members if updates: remove_members = {} create_members = {} update_groups = [] for groupname, new_data in iteritems(updates): old_data = self._old_groups[groupname] old_members = set(old_data["members"]) new_members = set(new_data["members"]) created = new_members - old_members if created: create_members[groupname] = sorted(created) removed = old_members - new_members if removed: remove_members[groupname] = sorted(removed) if old_data["gid"] != new_data["gid"]: update_groups.append({ "name": groupname, "gid": new_data["gid"] }) if create_members: members = changes.setdefault("create-group-members", {}) members.update(create_members) if remove_members: members = changes.setdefault("delete-group-members", {}) members.update(remove_members) if update_groups: members = changes.setdefault("update-groups", []) members.extend(update_groups) if deletes: changes["delete-groups"] = sorted(deletes.keys()) return changes
def setMetadata(self, meta): self.meta = meta sets = [] for p in itervalues(self.users): d = defer.maybeDeferred(p.groupMetaUpdate, self, meta) d.addErrback(self._ebUserCall, p=p) sets.append(d) defer.DeferredList(sets).addCallback(self._cbUserCall) return defer.succeed(None)
def receive(self, sender, recipient, message): assert recipient is self receives = [] for p in itervalues(self.users): if p is not sender: d = defer.maybeDeferred(p.receive, sender, self, message) d.addErrback(self._ebUserCall, p=p) receives.append(d) defer.DeferredList(receives).addCallback(self._cbUserCall) return defer.succeed(None)
def disconnected(_): """ Make sure deferreds do not linger on after disconnect. This errbacks all deferreds of iq's for which no response has been received with a L{ConnectionLost} failure. Otherwise, the deferreds will never be fired. """ iqDeferreds = xs.iqDeferreds xs.iqDeferreds = {} for d in itervalues(iqDeferreds): d.errback(ConnectionLost())
def add(self, user): assert iwords.IChatClient.providedBy(user), "%r is not a chat client" % (user,) if user.name not in self.users: additions = [] self.users[user.name] = user for p in itervalues(self.users): if p is not user: d = defer.maybeDeferred(p.userJoined, self, user) d.addErrback(self._ebUserCall, p=p) additions.append(d) defer.DeferredList(additions).addCallback(self._cbUserCall) return defer.succeed(None)
def agentc_REQUEST_IDENTITIES(self, data): """ Return all of the identities that have been added to the server """ assert data == b'' numKeys = len(self.factory.keys) resp = [] resp.append(struct.pack('!L', numKeys)) for key, comment in itervalues(self.factory.keys): resp.append(NS(key.blob())) # yes, wrapped in an NS resp.append(NS(comment)) self.sendResponse(AGENT_IDENTITIES_ANSWER, b''.join(resp))
def remove(self, user, reason=None): try: del self.users[user.name] except KeyError: pass else: removals = [] for p in itervalues(self.users): if p is not user: d = defer.maybeDeferred(p.userLeft, self, user, reason) d.addErrback(self._ebUserCall, p=p) removals.append(d) defer.DeferredList(removals).addCallback(self._cbUserCall) return defer.succeed(None)
def maybeCallProcessEnded(): """A less strict version of Process.maybeCallProcessEnded. This behaves exactly like the original method, but in case the process has ended already and sent us a SIGCHLD, it doesn't wait for the stdin/stdout pipes to close, because the child process itself might have passed them to its own child processes. @note: Twisted 8.2 now has a processExited hook that could be used in place of this workaround. """ if process.pipes and not process.pid: for pipe in itervalues(process.pipes): if isinstance(pipe, ProcessReader): # Read whatever is left pipe.doRead() pipe.stopReading() process.pipes = {} Process.maybeCallProcessEnded(process)
def __init__(self, qname, defaultUri=None, attribs=None, localPrefixes=None): """ @param qname: Tuple of (uri, name) @param defaultUri: The default URI of the element; defaults to the URI specified in C{qname} @param attribs: Dictionary of attributes @param localPrefixes: Dictionary of namespace declarations on this element. The key is the prefix to bind the namespace uri to. """ self.localPrefixes = localPrefixes or {} self.uri, self.name = qname if defaultUri is None and \ self.uri not in itervalues(self.localPrefixes): self.defaultUri = self.uri else: self.defaultUri = defaultUri self.attributes = attribs or {} self.children = [] self.parent = None
def connectionMade(self): data = struct.pack('!L', max(self.versions)) for (k, v) in itervalues(self.extData): data += NS(k) + NS(v) self.sendPacket(FXP_INIT, data)
def get_packages(self): """Get all the packages available in the channels.""" return itervalues(self._hash2pkg)
def connectionMade(self): data = struct.pack('!L', max(self.versions)) for k,v in itervalues(self.extData): data += NS(k) + NS(v) self.sendPacket(FXP_INIT, data)
def itergroups(self): return defer.succeed(itervalues(self.groups))