1 # Copyright (C) 2023 Free Software Foundation
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.
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.
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/>.
19 from fba.helpers import software as software_helper
21 logging.basicConfig(level=logging.INFO)
22 logger = logging.getLogger(__name__)
24 # Pattern instance for version numbers
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}$"),
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)
44 temp = software.split(";")[0]
46 temp = software.split(",")[0]
47 elif " - " in software:
48 temp = software.split(" - ")[0]
50 logger.debug("software='%s'", software)
53 version = temp.split(" ")[-1]
55 version = temp.split("/")[-1]
57 version = temp.split("-")[-1]
59 logger.debug("Was not able to find common seperator, returning untouched software='%s' - EXIT!", software)
63 logger.debug("Checking %d patterns ...", len(patterns))
64 for pattern in patterns:
66 match = pattern.match(version)
68 logger.debug("match[]='%s'", type(match))
69 if isinstance(match, re.Match):
70 logger.debug("version='%s' is matching pattern='%s'", version, pattern)
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)
78 logger.debug("Found valid version number: '%s', removing it ...", version)
79 end = len(temp) - len(version) - 1
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")
87 logger.debug("software='%s' - EXIT!", software)