@@ -9,43 +9,55 @@ class NotAPatchRelease(Exception):
99 pass
1010
1111
12- class DistroVersion :
13- major : Optional [int ] = None
14- minor : Optional [int ] = None
15- patch : Optional [int ] = None
12+ def DistroVersion (maybe_distro_version ):
13+ version_components = maybe_distro_version .split ("." )
14+ if len (version_components ) > 3 or len (version_components ) < 2 :
15+ raise UnsupportedDistroVersion (
16+ f"Unexpected version number format { maybe_distro_version } "
17+ )
18+
19+ if not all (map (lambda x : x .isdigit (), version_components )):
20+ raise UnsupportedDistroVersion (
21+ f"Unexpected version number format { maybe_distro_version } "
22+ )
1623
17- def __init__ (self , maybe_version_str ):
18- version_components = maybe_version_str .split ("." )
19- if len (version_components ) > 3 or len (version_components ) < 2 :
20- raise UnsupportedDistroVersion (
21- f"Unexpected version number format { maybe_version_str } "
22- )
24+ if len (version_components ) == 2 :
25+ return LegacyDistroVersion (* (int (c ) for c in version_components ))
26+ if len (version_components ) == 3 :
27+ return SemverDistroVersion (* (int (c ) for c in version_components ))
2328
24- if not all (map (lambda x : x .isdigit (), version_components )):
25- raise UnsupportedDistroVersion (
26- f"Unexpected version number format { maybe_version_str } "
27- )
2829
29- self .major = int (version_components [0 ])
30+ class BaseDistroVersion :
31+ def is_patch_release (self ):
32+ return self .patch and self .patch > 0
3033
31- if len (version_components ) == 2 :
32- self .patch = int (version_components [1 ])
3334
34- if len (version_components ) == 3 :
35- self .minor = int (version_components [1 ])
36- self .patch = int (version_components [2 ])
35+ class LegacyDistroVersion (BaseDistroVersion ):
36+ def __init__ (self , major , patch ):
37+ self .major = major
38+ self .patch = patch
3739
3840 def __str__ (self ):
39- return (
40- f"{ self .major } .{ self .minor } .{ self .patch - 1 } "
41- if self .minor
42- else f"{ self .major } .{ self .patch - 1 } "
43- )
41+ return f"{ self .major } .{ self .patch } "
4442
45- def is_patch_release (self ):
46- return self .patch > 0
43+ def previous_patch_release (self ):
44+ if not self .is_patch_release ():
45+ raise NotAPatchRelease (f"{ self } is not a patch release" )
46+
47+ return LegacyDistroVersion (self .major , self .patch - 1 )
48+
49+
50+ class SemverDistroVersion (BaseDistroVersion ):
51+ def __init__ (self , major , minor , patch ):
52+ self .major = major
53+ self .minor = minor
54+ self .patch = patch
55+
56+ def __str__ (self ):
57+ return f"{ self .major } .{ self .minor } .{ self .patch } "
4758
4859 def previous_patch_release (self ):
4960 if not self .is_patch_release ():
5061 raise NotAPatchRelease (f"{ self } is not a patch release" )
51- return self .__str__ ()
62+
63+ return SemverDistroVersion (self .major , self .minor , self .patch - 1 )
0 commit comments