Coverage for cc_modules/cc_version.py: 55%
29 statements
« prev ^ index » next coverage.py v7.9.2, created at 2025-07-15 15:55 +0100
« prev ^ index » next coverage.py v7.9.2, created at 2025-07-15 15:55 +0100
1"""
2camcops_server/cc_modules/cc_version.py
4===============================================================================
6 Copyright (C) 2012, University of Cambridge, Department of Psychiatry.
7 Created by Rudolf Cardinal (rnc1001@cam.ac.uk).
9 This file is part of CamCOPS.
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.
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.
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/>.
24===============================================================================
26**Version helper functions.**
28"""
30from typing import Union
32from semantic_version import Version
33from camcops_server.cc_modules.cc_version_string import (
34 CAMCOPS_SERVER_VERSION_STRING,
35 MINIMUM_TABLET_VERSION_STRING,
36)
38# =============================================================================
39# Version constants and configuration variables read by shell scripts
40# =============================================================================
42CAMCOPS_SERVER_VERSION = Version(CAMCOPS_SERVER_VERSION_STRING)
43MINIMUM_TABLET_VERSION = Version(MINIMUM_TABLET_VERSION_STRING)
45FIRST_CPP_TABLET_VER = Version("2.0.0")
46FIRST_TABLET_VER_WITHOUT_IDDESC_IN_PT_TABLE = FIRST_CPP_TABLET_VER
47FIRST_TABLET_VER_WITH_SEPARATE_IDNUM_TABLE = Version("2.0.1")
48FIRST_TABLET_VER_WITH_EXPLICIT_PKNAME_IN_UPLOAD_TABLE = Version("2.0.4")
51# =============================================================================
52# For converting from older formats
53# =============================================================================
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}")
97TEST_CODE = """
99from camcops_server.cc_modules.cc_version import make_version
101for v in ("1.0", "1.01", "1.14", "1.5", "1"):
102 print(make_version(v))
104"""
106# =============================================================================
107# Notable previous versions
108# =============================================================================
110TABLET_VERSION_2_0_0 = Version("2.0.0") # move to C++ version, 2016-2017