diff --git a/sdks/apigw-manager/src/apigw_manager/apigw/utils.py b/sdks/apigw-manager/src/apigw_manager/apigw/utils.py index d293bc3..a63dfd1 100644 --- a/sdks/apigw-manager/src/apigw_manager/apigw/utils.py +++ b/sdks/apigw-manager/src/apigw_manager/apigw/utils.py @@ -9,11 +9,12 @@ * specific language governing permissions and limitations under the License. """ import os +import re import zipfile import yaml from bkapi_client_core.config import SettingKeys, settings -from packaging.version import Version as _Version +from packaging.version import Version as _Version, InvalidVersion from apigw_manager.core import configuration @@ -106,12 +107,54 @@ def _get_archived_files(cls, path): for root, _, files in os.walk(path): for name in files: file_path = os.path.join(root, name) - path_to_name[file_path] = file_path[len(path) :] + path_to_name[file_path] = file_path[len(path):] return path_to_name +# 自定义VERSION_PATTERN +VERSION_PATTERN = r""" + v? + (?: + (?:(?P[0-9]+)!)? # epoch + (?P[0-9]+(?:\.[0-9]+)*) # release segment + (?P
                                          # pre-release
+            [-_\.]?
+            (?Palpha|a|beta|b|preview|pre|c|rc)
+            [-_\.]?
+            (?P[0-9]+)?
+        )?
+        (?P                                         # post release
+            (?:-(?P[0-9]+))
+            |
+            (?:
+                [-_\.]?
+                (?Ppost|rev|r)
+                [-_\.]?
+                (?P[0-9]+)?
+            )
+        )?
+        (?P                                          # dev release
+            [-_\.]?
+            (?Pdev)
+            [-_\.]?
+            (?P[0-9]+)?
+        )?
+    )
+    (?:\+(?P[a-z0-9]+(?:[-_\.][a-z0-9]+)*))?       # local version
+    (?:-(?P[a-zA-Z0-9]+(?:[-_\.][a-zA-Z0-9]+)*))?  # extra pre-release tags
+"""
+
+
 class SemVersion(_Version):
+    _regex = re.compile(r"^\s*" + VERSION_PATTERN + r"\s*$", re.VERBOSE | re.IGNORECASE)
+
+    def __init__(self, version):
+        match = self._regex.match(version)
+        if not match:
+            raise InvalidVersion(f"Invalid version: '{version}'")
+        super().__init__(version)
+
     @property
     def pre(self):
         pre = self._version.pre