def test_bad_shell_versions(self): with self.assertRaises(models.InvalidShellVersion): models.parse_version_string("3") with self.assertRaises(models.InvalidShellVersion): models.parse_version_string("3.2.2.2.1") with self.assertRaises(models.InvalidShellVersion): models.parse_version_string("a.b") with self.assertRaises(models.InvalidShellVersion): models.parse_version_string("3.2.a") with self.assertRaises(models.InvalidShellVersion): models.parse_version_string("40.teta")
def get_best_shell_version(): visible_versions = extension.visible_versions supported_shell_versions = set( shell_version for version in visible_versions for shell_version in version.shell_versions.all()) if not supported_shell_versions: return None supported_shell_versions = sorted(supported_shell_versions, key=lambda x: (x.major, x.minor, x.point)) requested_shell_version = models.parse_version_string(shell_version) if cmp( (supported_shell_versions[0].major, supported_shell_versions[0].minor, supported_shell_versions[0].point), requested_shell_version) > 0: versions = visible_versions.filter( shell_versions=supported_shell_versions[0]) else: versions = visible_versions.filter( shell_versions=supported_shell_versions[-1]) return versions.order_by('-version')[0]
def get_versions_for_version_strings(version_strings): def get_version(major, minor, point): try: return models.ShellVersion.objects.get(major=major, minor=minor, point=point) except models.ShellVersion.DoesNotExist: return None for version_string in version_strings: try: major, minor, point = models.parse_version_string(version_string) except models.InvalidShellVersion: continue version = get_version(major, minor, point) if version: yield version # If we already have a base version, don't bother querying it again... if point == -1: continue base_version = get_version(major, minor, -1) if base_version: yield base_version
def test_bad_shell_versions(self): with self.assertRaises(models.InvalidShellVersion): models.parse_version_string("3") with self.assertRaises(models.InvalidShellVersion): models.parse_version_string("3.2.2.2.1") with self.assertRaises(models.InvalidShellVersion): models.parse_version_string("a.b") with self.assertRaises(models.InvalidShellVersion): models.parse_version_string("3.2.a")
def get_best_shell_version(): visible_versions = extension.visible_versions supported_shell_versions = set(shell_version for version in visible_versions for shell_version in version.shell_versions.all()) if not supported_shell_versions: return None supported_shell_versions = sorted(supported_shell_versions, key=lambda x: (x.major, x.minor, x.point)) requested_shell_version = models.parse_version_string(shell_version) if (supported_shell_versions[0].major, supported_shell_versions[0].minor, supported_shell_versions[0].point) > requested_shell_version: versions = visible_versions.filter(shell_versions=supported_shell_versions[0]) else: supported_shell_versions = list(shell_version for shell_version in supported_shell_versions if (shell_version.major, shell_version.minor, shell_version.point) <= requested_shell_version) versions = visible_versions.filter(shell_versions=supported_shell_versions[-1]) return versions.order_by('-version')[0]