File: version.py

package info (click to toggle)
gtk-layer-shell 0.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,444 kB
  • sloc: ansic: 13,285; python: 1,400; xml: 1,281; makefile: 20
file content (105 lines) | stat: -rw-r--r-- 3,818 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
'''
MIT License

Copyright 2020 Sophie Winter

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
'''

import logging
import functools
import re

from config import MIN_SUPPORTED_GTK, MAX_SUPPORTED_GTK

logger = logging.getLogger(__name__)

COMBO_FACTOR = 1000

@functools.total_ordering # implement all comparisons with just eq and lt
class Version:
    def __init__(self, git_name, minor, micro, released):
        self.git_name = git_name
        self.minor = minor
        self.micro = micro
        self.released = released

    def __eq__(self, other):
        return self.minor == other.minor and self.micro == other.micro

    def __lt__(self, other):
        if self.minor == other.minor:
            return self.micro < other.micro
        else:
            return self.minor < other.minor

    def get_checkout_name(self):
        return self.git_name

    def is_supported(self):
        '''Returns if the version is one we support'''
        return (
            self >= min_supported_version and
            self <= max_supported_version and
            self != bad_release_3_24_19)

    def is_released(self):
        return self.released

    def get_combo(self):
        return self.minor * 1000 + self.micro

    def __str__(self):
        result = 'v3.' + str(self.minor) + '.' + str(self.micro)
        if not self.is_released():
            result += ' (unreleased)'
        return result

    def c_id(self):
        '''a string suitable for a C identifier'''
        return 'v3_' + str(self.minor) + '_' + str(self.micro)

def parse_tag(tag):
    match = re.search(r'^3\.(\d+)\.(\d+)$', tag)
    if match:
        minor = int(match.group(1))
        micro = int(match.group(2))
        return Version(tag, minor, micro, True)
    else:
        return None

def parse_branch(branch):
    match = re.search(r'^gtk\-3\-(\d+)$', branch)
    if match:
        minor = int(match.group(1))
        return minor
    else:
        return None

min_supported_version = parse_tag(MIN_SUPPORTED_GTK)
max_supported_version = parse_tag(MAX_SUPPORTED_GTK)
bad_release_3_24_19 = parse_tag('3.24.19') # this is not a good release

def parse_tags_and_branches(tags, branches):
    result = []
    for tag in tags:
        version = parse_tag(tag)
        if version and version.is_supported():
            result.append(version)
    highest_micro = {}
    for v in result:
        highest_micro[v.minor] = max(v.micro, highest_micro.get(v.minor, -1))
    for branch in branches:
        minor = parse_branch(branch)
        if minor is not None:
            micro = highest_micro.get(minor, -1) + 1
            version = Version(branch, minor, micro, False)
            if version.is_supported():
                result.append(version)
    result.sort()
    logger.info('Found ' + str(len(result)) + ' supported versions')
    return result