]> 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})(-[a-z]+){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
39     if "." not in software and " " not in software:
40         logger.warning("software='%s' does not contain a version number.", software)
41         return software
42
43     temp = software
44     if ";" in software:
45         temp = software.split(";")[0]
46     elif "," in software:
47         temp = software.split(",")[0]
48     elif " - " in software:
49         temp = software.split(" - ")[0]
50
51     logger.debug("software='%s'", software)
52     version = None
53     if " " in software:
54         version = temp.split(" ")[-1]
55     elif "/" in software:
56         version = temp.split("/")[-1]
57     elif "-" in software:
58         version = temp.split("-")[-1]
59     else:
60         logger.debug("Was not able to find common seperator, returning untouched software='%s' - EXIT!", software)
61         return software
62
63     match = None
64     logger.debug("Checking %d patterns ...", len(patterns))
65     for pattern in patterns:
66         # Run match()
67         match = pattern.match(version)
68
69         logger.debug("match[]='%s'", type(match))
70         if isinstance(match, re.Match):
71             logger.debug("version='%s' is matching pattern='%s'", version, pattern)
72             break
73
74     logger.debug("version[%s]='%s',match='%s'", type(version), version, match)
75     if not isinstance(match, re.Match):
76         logger.warning("version='%s' does not match regex, leaving software='%s' untouched.", version, software)
77         return software
78
79     logger.debug("Found valid version number: '%s', removing it ...", version)
80     end = len(temp) - len(version) - 1
81
82     logger.debug("end[%s]='%s'", type(end), end)
83     software = temp[0:end].strip()
84     if " version" in software:
85         logger.debug("software='%s' contains word ' version'", software)
86         software = software_helper.strip_until(software, " version")
87
88     logger.debug("software='%s' - EXIT!", software)
89     return software