Coverage for cc_modules/cc_version.py: 43%

30 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2022-11-08 23:14 +0000

1#!/usr/bin/env python 

2 

3""" 

4camcops_server/cc_modules/cc_version.py 

5 

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

7 

8 Copyright (C) 2012, University of Cambridge, Department of Psychiatry. 

9 Created by Rudolf Cardinal (rnc1001@cam.ac.uk). 

10 

11 This file is part of CamCOPS. 

12 

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

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

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

16 (at your option) any later version. 

17 

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

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

20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

21 GNU General Public License for more details. 

22 

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

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

25 

26=============================================================================== 

27 

28**Version helper functions.** 

29 

30""" 

31 

32from typing import Union 

33 

34from semantic_version import Version 

35from camcops_server.cc_modules.cc_version_string import ( 

36 CAMCOPS_SERVER_VERSION_STRING, 

37 MINIMUM_TABLET_VERSION_STRING, 

38) 

39 

40# ============================================================================= 

41# Version constants and configuration variables read by shell scripts 

42# ============================================================================= 

43 

44CAMCOPS_SERVER_VERSION = Version(CAMCOPS_SERVER_VERSION_STRING) 

45MINIMUM_TABLET_VERSION = Version(MINIMUM_TABLET_VERSION_STRING) 

46 

47FIRST_CPP_TABLET_VER = Version("2.0.0") 

48FIRST_TABLET_VER_WITHOUT_IDDESC_IN_PT_TABLE = FIRST_CPP_TABLET_VER 

49FIRST_TABLET_VER_WITH_SEPARATE_IDNUM_TABLE = Version("2.0.1") 

50FIRST_TABLET_VER_WITH_EXPLICIT_PKNAME_IN_UPLOAD_TABLE = Version("2.0.4") 

51 

52 

53# ============================================================================= 

54# For converting from older formats 

55# ============================================================================= 

56 

57 

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

59 """ 

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

61 :exc:`ValueError`. 

62 """ 

63 if v is None: 

64 return Version("0.0.0") 

65 vstr = str(v) 

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

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

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

69 # doesn't fit float ordering. 

70 # - So: 

71 try: 

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

73 return Version(vstr) 

74 except ValueError: 

75 parts = vstr.split(".") 

76 # Easy: 

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

78 # Defaults: 

79 patch = 0 

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

81 minor = 0 

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

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

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

85 # of digits/decimal places, so either: 

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

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

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

89 # ... Option (a) used. 

90 after_dp = parts[1] 

91 max_minor_digits = 2 # the most we used 

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

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

94 else: 

95 raise 

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

97 

98 

99TEST_CODE = """ 

100 

101from camcops_server.cc_modules.cc_version import make_version 

102 

103for v in ("1.0", "1.01", "1.14", "1.5", "1"): 

104 print(make_version(v)) 

105 

106""" 

107 

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

109# Notable previous versions 

110# ============================================================================= 

111 

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