]> git.mxchange.org Git - fba.git/blob - fba/helpers/version.py
Continued:
[fba.git] / fba / helpers / version.py
1 # Copyright (C) 2023 Free Software Foundation
2 #
3 # This program is free software: you can redistribute it and/or modify
4 # it under the terms of the GNU Affero General Public License as published
5 # by the Free Software Foundation, either version 3 of the License, or
6 # (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 # GNU Affero General Public License for more details.
12 #
13 # You should have received a copy of the GNU Affero General Public License
14 # along with this program.  If not, see <https://www.gnu.org/licenses/>.
15
16 import logging
17 import re
18
19 from fba.helpers import software as software_helper
20
21 logging.basicConfig(level=logging.INFO)
22 logger = logging.getLogger(__name__)
23
24 # Pattern instance for version numbers
25 patterns = [
26     # semantic version number (with v|V) prefix)
27     re.compile(r"^(?P<version>v|V{0,1})(\.{0,1})(?P<major>0|[1-9]\d*)\.(?P<minor>0+|[1-9]\d*)(\.(?P<patch>0+|[1-9]\d*)(?:-(?P<prerelease>(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+(?P<buildmetadata>[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?)?$"),
28     # non-sematic, e.g. 1.2.3.4
29     re.compile(r"^(?P<version>v|V{0,1})(\.{0,1})(?P<major>0|[1-9]\d*)\.(?P<minor>0+|[1-9]\d*)(\.(?P<patch>0+|[1-9]\d*)(\.(?P<subpatch>0|[1-9]\d*))?)$"),
30     # non-sematic, e.g. 2023-05[-dev]
31     re.compile(r"^(?P<year>[1-9]{1}[0-9]{3})\.(?P<month>[0-9]{2})(-dev){0,1}$"),
32     # non-semantic, e.g. abcdef0
33     re.compile("^[a-f0-9]{7}$"),
34 ]
35
36 def remove(software: str) -> str:
37     logger.debug("software='%s' - CALLED!", software)
38     if "." not in software and " " not in software:
39         logger.warning("software='%s' does not contain a version number.", software)
40         return software
41
42     temp = software
43     if ";" in software:
44         temp = software.split(";")[0]
45     elif "," in software:
46         temp = software.split(",")[0]
47     elif " - " in software:
48         temp = software.split(" - ")[0]
49
50     logger.debug("software='%s'", software)
51     version = None
52     if " " in software:
53         version = temp.split(" ")[-1]
54     elif "/" in software:
55         version = temp.split("/")[-1]
56     elif "-" in software:
57         version = temp.split("-")[-1]
58     else:
59         logger.debug("Was not able to find common seperator, returning untouched software='%s' - EXIT!", software)
60         return software
61
62     match = None
63     logger.debug("Checking %d patterns ...", len(patterns))
64     for pattern in patterns:
65         # Run match()
66         match = pattern.match(version)
67
68         logger.debug("match[]='%s'", type(match))
69         if isinstance(match, re.Match):
70             logger.debug("version='%s' is matching pattern='%s'", version, pattern)
71             break
72
73     logger.debug("version[%s]='%s',match='%s'", type(version), version, match)
74     if not isinstance(match, re.Match):
75         logger.warning("version='%s' does not match regex, leaving software='%s' untouched.", version, software)
76         return software
77
78     logger.debug("Found valid version number: '%s', removing it ...", version)
79     end = len(temp) - len(version) - 1
80
81     logger.debug("end[%s]='%s'", type(end), end)
82     software = temp[0:end].strip()
83     if " version" in software:
84         logger.debug("software='%s' contains word ' version'", software)
85         software = software_helper.strip_until(software, " version")
86
87     logger.debug("software='%s' - EXIT!", software)
88     return software