Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1#!/usr/bin/env python 

2 

3""" 

4camcops_server/cc_modules/cc_version.py 

5 

6=============================================================================== 

7 

8 Copyright (C) 2012-2020 Rudolf Cardinal (rudolf@pobox.com). 

9 

10 This file is part of CamCOPS. 

11 

12 CamCOPS is free software: you can redistribute it and/or modify 

13 it under the terms of the GNU General Public License as published by 

14 the Free Software Foundation, either version 3 of the License, or 

15 (at your option) any later version. 

16 

17 CamCOPS is distributed in the hope that it will be useful, 

18 but WITHOUT ANY WARRANTY; without even the implied warranty of 

19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

20 GNU General Public License for more details. 

21 

22 You should have received a copy of the GNU General Public License 

23 along with CamCOPS. If not, see <https://www.gnu.org/licenses/>. 

24 

25=============================================================================== 

26 

27**Version helper functions.** 

28 

29""" 

30 

31from typing import Union 

32 

33from semantic_version import Version 

34from camcops_server.cc_modules.cc_version_string import ( 

35 CAMCOPS_SERVER_VERSION_STRING, 

36 MINIMUM_TABLET_VERSION_STRING, 

37) 

38 

39# ============================================================================= 

40# Version constants and configuration variables read by shell scripts 

41# ============================================================================= 

42 

43CAMCOPS_SERVER_VERSION = Version(CAMCOPS_SERVER_VERSION_STRING) 

44MINIMUM_TABLET_VERSION = Version(MINIMUM_TABLET_VERSION_STRING) 

45 

46FIRST_CPP_TABLET_VER = Version("2.0.0") 

47FIRST_TABLET_VER_WITHOUT_IDDESC_IN_PT_TABLE = FIRST_CPP_TABLET_VER 

48FIRST_TABLET_VER_WITH_SEPARATE_IDNUM_TABLE = Version("2.0.1") 

49FIRST_TABLET_VER_WITH_EXPLICIT_PKNAME_IN_UPLOAD_TABLE = Version("2.0.4") 

50 

51 

52# ============================================================================= 

53# For converting from older formats 

54# ============================================================================= 

55 

56def make_version(v: Union[str, float, None]) -> Version: 

57 """ 

58 Returns a :class:`semantic_version.Version` from its input or raises 

59 :exc:`ValueError`. 

60 """ 

61 if v is None: 

62 return Version("0.0.0") 

63 vstr = str(v) 

64 # - Note that Version.coerce(vstr) will handle "1.1.1" and "1.1", but not 

65 # e.g. "1.06" (it will complain about leading zeroes). 

66 # - Furthermore, "1.5" -> (1, 5, 0) whilst "1.14" -> (1, 14, 0), which 

67 # doesn't fit float ordering. 

68 # - So: 

69 try: 

70 # Deal with something that's already in semantic numbering format. 

71 return Version(vstr) 

72 except ValueError: 

73 parts = vstr.split(".") 

74 # Easy: 

75 major = int(parts[0]) if len(parts) > 0 else 0 

76 # Defaults: 

77 patch = 0 

78 if len(parts) == 1: # e.g. "1" 

79 minor = 0 

80 elif len(parts) == 2: # e.g. "1.06" 

81 # More tricky: older versions followed float rules, so 1.14 < 1.5. 

82 # The only way of dealing with this is to enforce a number 

83 # of digits/decimal places, so either: 

84 # (a) 1.14 -> "1.14.0" and 1.5 -> "1.50.0", or 

85 # (b) 1.14 -> "1.1.4" and 1.5 -> "1.5.0" 

86 # The decision is arbitrary as long as we right-pad everything. 

87 # ... Option (a) used. 

88 after_dp = parts[1] 

89 max_minor_digits = 2 # the most we used 

90 minor = int(after_dp.ljust(max_minor_digits, "0")) 

91 # "x".ljust(3, "0") -> "x00" 

92 else: 

93 raise 

94 return Version(f"{major}.{minor}.{patch}") 

95 

96 

97TEST_CODE = """ 

98 

99from camcops_server.cc_modules.cc_version import make_version 

100 

101for v in ["1.0", "1.01", "1.14", "1.5", "1"]: 

102 print(make_version(v)) 

103 

104""" 

105 

106# ============================================================================= 

107# Notable previous versions 

108# ============================================================================= 

109 

110TABLET_VERSION_2_0_0 = Version("2.0.0") # move to C++ version, 2016-2017