libtextworker 0.1.4
Cross-platform, free and open library for Python projects
versioning.py
1 """
2 @package libtextworker.versioning
3 @brief Utilities for version control management.
4 """
5 
6 # A cross-platform library for Python apps.
7 # Copyright (C) 2023-2024 Le Bao Nguyen and contributors.
8 # This is a part of the libtextworker project.
9 # Licensed under the GNU General Public License version 3.0 or later.
10 
11 import importlib
12 import packaging.version
13 
14 from . import general
15 
16 """
17 Projects called by functions under libtextworker.versioning.
18 """
19 Requested = {}
20 
21 
22 def parse_version(project: str) -> packaging.version.Version:
23  """
24  Try to import a project then parse its version with the ```packaging.version``` module.
25  Parsed projects are stored in Requested (list).
26  @see is_development_version
27  @see is_development_from_project
28  @see require
29  @see require_exact
30  @see require_lower
31  @see Requested
32  """
33  global Requested
34  module = importlib.import_module(project)
35 
36  if project not in Requested:
37  if not module.__version__:
39  "%(project)s does not have __version__ attribute!"
40  )
41  Requested[project] = packaging.version.parse(module.__version__)
42 
43  return Requested[project]
44 
45 
46 def is_development_version(version: str):
47  """
48  Parse and check if the specified version is a prerelease.
49  @see is_development_version_from_project
50  """
51  return packaging.version.parse(version).is_prerelease
52 
53 
55  """
56  Like is_development_version(), but read the project's current version instead.
57  @see is_development_version
58  """
59  return parse_version(project).is_prerelease
60 
61 
62 def require(project: str, target_version: str):
63  """
64  Ensures the correct versions of a project are available.
65  @param project (str): Target project name
66  @param target_version (str): Target project version
67  """
68  currver = parse_version(project)
69  target = packaging.version.parse(target_version)
70 
71  if currver < target:
73  "Project %(project)s must be version >=%(target_version)s"
74  )
75 
76 
77 def require_exact(project: str, target_version: str):
78  """
79  Ensures the correct version of a project IS available.
80  @param project (str): Target project name
81  @param target_version (str): Target project version
82  """
83  currver = parse_version(project)
84  target = packaging.version.parse(target_version)
85 
86  if currver != target:
88  "Project %(project)s not available for version %(target_version)"
89  )
90 
91 
92 def require_lower(project: str, target_version: str):
93  """
94  Ensures the project version is LOWER than the requested one.
95  @param project (str): Target project name
96  @param target_version (str): Target project version
97  """
98  currver = parse_version(project)
99  target = packaging.version.parse(target_version)
100 
101  if currver >= target:
103  "Project %(project)s not available for version %(target_version)"
104  )
packaging.version.Version parse_version(str project)
Definition: versioning.py:22
def is_development_version(str version)
Definition: versioning.py:46
def is_development_version_from_project(str project)
Definition: versioning.py:54
def require_exact(str project, str target_version)
Definition: versioning.py:77
def require(str project, str target_version)
Definition: versioning.py:62
def require_lower(str project, str target_version)
Definition: versioning.py:92