Coverage for cc_modules/cc_version.py: 55%

1"""

2 camcops_server/cc_modules/cc_version.py

3

4===============================================================================

5

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

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

8

9 This file is part of CamCOPS.

10

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

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

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

14 (at your option) any later version.

15

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

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

18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the

19 GNU General Public License for more details.

20

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

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

23

24===============================================================================

25

26**Version helper functions.**

27

28"""

29

30 from typing import Union

31

32 from semantic_version import Version

33 from camcops_server.cc_modules.cc_version_string import (

34 CAMCOPS_SERVER_VERSION_STRING,

35 MINIMUM_TABLET_VERSION_STRING,

36)

37

38# =============================================================================

39# Version constants and configuration variables read by shell scripts

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

41

42 CAMCOPS_SERVER_VERSION = Version(CAMCOPS_SERVER_VERSION_STRING)

43 MINIMUM_TABLET_VERSION = Version(MINIMUM_TABLET_VERSION_STRING)

44

45 FIRST_CPP_TABLET_VER = Version("2.0.0")

46 FIRST_TABLET_VER_WITHOUT_IDDESC_IN_PT_TABLE = FIRST_CPP_TABLET_VER

47 FIRST_TABLET_VER_WITH_SEPARATE_IDNUM_TABLE = Version("2.0.1")

48 FIRST_TABLET_VER_WITH_EXPLICIT_PKNAME_IN_UPLOAD_TABLE = Version("2.0.4")

49

50

51# =============================================================================

52# For converting from older formats

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

54

55

56 def 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

97 TEST_CODE = """

98

99 from camcops_server.cc_modules.cc_version import make_version

100

101 for 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

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