def ssh_USERAUTH_FAILURE(self, packet): """ We received a MSG_USERAUTH_FAILURE. Payload:: string methods byte partial success If partial success is True, then the previous method succeeded but is not sufficent for authentication. methods is a comma-separated list of accepted authentication methods. We sort the list of methods by their position in self.preferredOrder, removing methods that have already succeeded. We then call self.tryAuth with the most preferred method, """ canContinue, partial = getNS(packet) partial = ord(partial) if partial: self.authenticatedWith.append(self.lastAuth) def orderByPreference(meth): if meth in self.preferredOrder: return self.preferredOrder.index(meth) else: return -1 canContinue = util.dsu([meth for meth in canContinue.split(',') if meth not in self.authenticatedWith], orderByPreference) log.msg('can continue with: %s' % canContinue) return self._cbUserauthFailure(None, iter(canContinue))
def sort(self, xs): """ Sort the given things using L{sorter}. @param xs: A list of test cases, class or modules. """ return dsu(xs, self.sorter)
def _trialSortAlgorithm(self, sorter): """ Right now, halfway by accident, trial sorts like this: 1. all modules are grouped together in one list and sorted. 2. within each module, the classes are grouped together in one list and sorted. 3. finally within each class, each test method is grouped together in a list and sorted. This attempts to return a sorted list of testable thingies following those rules, so that we can compare the behavior of loadPackage. The things that show as 'cases' are errors from modules which failed to import, and test methods. Let's gather all those together. """ pkg = getModule('uberpackage') testModules = [] for testModule in pkg.walkModules(): if testModule.name.split(".")[-1].startswith("test_"): testModules.append(testModule) sortedModules = util.dsu(testModules, sorter) # ONE for modinfo in sortedModules: # Now let's find all the classes. module = modinfo.load(None) if module is None: yield modinfo else: testClasses = [] for attrib in modinfo.iterAttributes(): if runner.isTestCase(attrib.load()): testClasses.append(attrib) sortedClasses = util.dsu(testClasses, sorter) # TWO for clsinfo in sortedClasses: testMethods = [] for attr in clsinfo.iterAttributes(): if attr.name.split(".")[-1].startswith('test'): testMethods.append(attr) sortedMethods = util.dsu(testMethods, sorter) # THREE for methinfo in sortedMethods: yield methinfo
def _cbMX(self, answers, domain): answers = util.dsu(answers, lambda e: e.preference) for answer in answers: host = str(answer.name) if host not in self.badMXs: return answer t = time.time() - self.badMXs[host] if t > 0: del self.badMXs[host] return answer return answers[0]
def _cbMX(self, answers, domain): if not answers: raise IOError("No MX found for %r" % (domain, )) answers = util.dsu(answers, lambda e: e.preference) for answer in answers: host = str(answer.name) if host not in self.badMXs: return answer t = time.time() - self.badMXs[host] if t > 0: del self.badMXs[host] return answer return answers[0]
def _cbMX(self, answers, domain): if not answers: raise IOError("No MX found for %r" % (domain,)) answers = util.dsu(answers, lambda e: e.preference) for answer in answers: host = str(answer.name) if host not in self.badMXs: return answer t = time.time() - self.badMXs[host] if t > 0: del self.badMXs[host] return answer return answers[0]
def ssh_USERAUTH_FAILURE(self, packet): """ We received a MSG_USERAUTH_FAILURE. Payload:: string methods byte partial success If partial success is C{True}, then the previous method succeeded but is not sufficent for authentication. C{methods} is a comma-separated list of accepted authentication methods. We sort the list of methods by their position in C{self.preferredOrder}, removing methods that have already succeeded. We then call C{self.tryAuth} with the most preferred method. @param packet: the L{MSG_USERAUTH_FAILURE} payload. @type packet: C{str} @return: a L{defer.Deferred} that will be callbacked with C{None} as soon as all authentication methods have been tried, or C{None} if no more authentication methods are available. @rtype: C{defer.Deferred} or C{None} """ canContinue, partial = getNS(packet) partial = ord(partial) if partial: self.authenticatedWith.append(self.lastAuth) def orderByPreference(meth): """ Invoked once per authentication method in order to extract a comparison key which is then used for sorting. @param meth: the authentication method. @type meth: C{str} @return: the comparison key for C{meth}. @rtype: C{int} """ if meth in self.preferredOrder: return self.preferredOrder.index(meth) else: # put the element at the end of the list. return len(self.preferredOrder) canContinue = util.dsu([ meth for meth in canContinue.split(',') if meth not in self.authenticatedWith ], orderByPreference) log.msg('can continue with: %s' % canContinue) return self._cbUserauthFailure(None, iter(canContinue))
def ssh_USERAUTH_FAILURE(self, packet): """ We received a MSG_USERAUTH_FAILURE. Payload:: string methods byte partial success If partial success is C{True}, then the previous method succeeded but is not sufficent for authentication. C{methods} is a comma-separated list of accepted authentication methods. We sort the list of methods by their position in C{self.preferredOrder}, removing methods that have already succeeded. We then call C{self.tryAuth} with the most preferred method. @param packet: the L{MSG_USERAUTH_FAILURE} payload. @type packet: C{str} @return: a L{defer.Deferred} that will be callbacked with C{None} as soon as all authentication methods have been tried, or C{None} if no more authentication methods are available. @rtype: C{defer.Deferred} or C{None} """ canContinue, partial = getNS(packet) partial = ord(partial) if partial: self.authenticatedWith.append(self.lastAuth) def orderByPreference(meth): """ Invoked once per authentication method in order to extract a comparison key which is then used for sorting. @param meth: the authentication method. @type meth: C{str} @return: the comparison key for C{meth}. @rtype: C{int} """ if meth in self.preferredOrder: return self.preferredOrder.index(meth) else: # put the element at the end of the list. return len(self.preferredOrder) canContinue = util.dsu([meth for meth in canContinue.split(',') if meth not in self.authenticatedWith], orderByPreference) log.msg('can continue with: %s' % canContinue) return self._cbUserauthFailure(None, iter(canContinue))
def test_failUnless_matches_assert(self): asserts = self._getAsserts() failUnlesses = reflect.prefixedMethods(self, 'failUnless') self.failUnlessEqual(dsu(asserts, self._name), dsu(failUnlesses, self._name))
def testDSU(self): L = [Foo(x) for x in range(20, 9, -1)] L2 = util.dsu(L, lambda o: o.x) self.assertEquals(range(10, 21), [o.x for o in L2])
def test_failIf_matches_assertNot(self): asserts = reflect.prefixedMethods(unittest.TestCase, 'assertNot') failIfs = reflect.prefixedMethods(unittest.TestCase, 'failIf') self.failUnlessEqual(dsu(asserts, self._name), dsu(failIfs, self._name))
def test_deprecation(self): self.assertWarns(DeprecationWarning, ("dsu is deprecated since Twisted 10.1. " "Use the built-in sorted() instead."), __file__, lambda: util.dsu([], lambda: 0))
def sort(self, xs): return dsu(xs, self.sorter)