|
| 1 | +# Copyright (c) Microsoft Corporation. |
| 2 | +# Licensed under the MIT License. |
| 3 | + |
| 4 | +"""Best-effort check for Function App dependencies that allow a new major.""" |
| 5 | + |
| 6 | +import os |
| 7 | +import re |
| 8 | +import shlex |
| 9 | +from importlib.metadata import PackageNotFoundError, version |
| 10 | +from logging import Logger |
| 11 | +from pathlib import Path |
| 12 | + |
| 13 | +from packaging.markers import UndefinedComparison, UndefinedEnvironmentName |
| 14 | +from packaging.requirements import InvalidRequirement, Requirement |
| 15 | +from packaging.specifiers import SpecifierSet |
| 16 | +from packaging.utils import canonicalize_name |
| 17 | +from packaging.version import InvalidVersion, Version |
| 18 | + |
| 19 | +_PACKAGE = "azure-functions-durable" |
| 20 | +_INDEX_OPTIONS_WITH_VALUE = { |
| 21 | + "-i", "-f", "--index-url", "--extra-index-url", "--find-links", |
| 22 | + "--trusted-host", "--only-binary", "--no-binary", |
| 23 | +} |
| 24 | +_INDEX_FLAGS = {"--no-index", "--pre", "--prefer-binary"} |
| 25 | + |
| 26 | + |
| 27 | +def _read_specifiers(root: Path, logger: Logger) -> tuple[SpecifierSet, bool] | None: |
| 28 | + specifiers: list[str] = [] |
| 29 | + required = False |
| 30 | + allow_prereleases = False |
| 31 | + active: set[tuple[Path, bool]] = set() |
| 32 | + visited: set[tuple[Path, bool]] = set() |
| 33 | + |
| 34 | + def read_file(path: Path, constraint: bool) -> bool: |
| 35 | + nonlocal allow_prereleases, required |
| 36 | + try: |
| 37 | + path = path.resolve() |
| 38 | + key = (path, constraint) |
| 39 | + if key in active: |
| 40 | + logger.debug("Cannot check Durable Functions dependencies: cyclic requirements in %s.", path) |
| 41 | + return False |
| 42 | + if key in visited: |
| 43 | + return True |
| 44 | + content = path.read_text(encoding="utf-8-sig") |
| 45 | + except (OSError, UnicodeError) as exc: |
| 46 | + logger.debug("Cannot read Function App requirements from %s: %s", path, exc) |
| 47 | + return False |
| 48 | + |
| 49 | + active.add(key) |
| 50 | + try: |
| 51 | + for raw_line in content.splitlines(): |
| 52 | + line = re.sub(r"\s+#.*$", "", raw_line).strip() |
| 53 | + if not line or line.startswith("#"): |
| 54 | + continue |
| 55 | + if line.endswith("\\"): |
| 56 | + logger.debug("Cannot check multiline requirements in %s.", path) |
| 57 | + return False |
| 58 | + if line.startswith("-"): |
| 59 | + try: |
| 60 | + parts = shlex.split(line) |
| 61 | + except ValueError: |
| 62 | + logger.debug("Cannot parse a requirements option in %s.", path) |
| 63 | + return False |
| 64 | + option = parts[0] |
| 65 | + argument = None |
| 66 | + if option in ("-r", "--requirement", "-c", "--constraint") and len(parts) == 2: |
| 67 | + argument = parts[1] |
| 68 | + elif option.startswith(("--requirement=", "--constraint=")) and len(parts) == 1: |
| 69 | + option, argument = option.split("=", 1) |
| 70 | + elif option.startswith(("-r", "-c")) and len(option) > 2 and len(parts) == 1: |
| 71 | + option, argument = option[:2], option[2:] |
| 72 | + elif option in _INDEX_FLAGS and len(parts) == 1: |
| 73 | + allow_prereleases = allow_prereleases or option == "--pre" |
| 74 | + continue |
| 75 | + elif ( |
| 76 | + option in _INDEX_OPTIONS_WITH_VALUE and len(parts) == 2) or ( |
| 77 | + option.split("=", 1)[0] in _INDEX_OPTIONS_WITH_VALUE |
| 78 | + and "=" in option and len(parts) == 1): |
| 79 | + continue |
| 80 | + if not argument or "://" in argument or "${" in argument: |
| 81 | + logger.debug("Cannot inspect a requirements option in %s.", path) |
| 82 | + return False |
| 83 | + nested_constraint = option in ("-c", "--constraint") |
| 84 | + if not read_file(path.parent / argument, nested_constraint): |
| 85 | + return False |
| 86 | + continue |
| 87 | + try: |
| 88 | + requirement = Requirement(line) |
| 89 | + except InvalidRequirement: |
| 90 | + logger.debug("Cannot parse a requirement in %s.", path) |
| 91 | + return False |
| 92 | + if canonicalize_name(requirement.name) != _PACKAGE: |
| 93 | + continue |
| 94 | + try: |
| 95 | + if requirement.marker is not None and not requirement.marker.evaluate(): |
| 96 | + continue |
| 97 | + except (UndefinedComparison, UndefinedEnvironmentName, InvalidVersion) as exc: |
| 98 | + logger.debug("Cannot evaluate a requirement marker in %s: %s", path, exc) |
| 99 | + return False |
| 100 | + if requirement.url is not None: |
| 101 | + logger.debug("Cannot determine the Durable Functions version from a URL in %s.", path) |
| 102 | + return False |
| 103 | + required = required or not constraint |
| 104 | + specifiers.append(str(requirement.specifier)) |
| 105 | + finally: |
| 106 | + active.remove(key) |
| 107 | + visited.add(key) |
| 108 | + return True |
| 109 | + |
| 110 | + if not read_file(root / "requirements.txt", False) or not required: |
| 111 | + return None |
| 112 | + package_specifiers = SpecifierSet(",".join(specifiers)) |
| 113 | + allow_prereleases = allow_prereleases or any(specifier.prereleases for specifier in package_specifiers) |
| 114 | + return package_specifiers, allow_prereleases |
| 115 | + |
| 116 | + |
| 117 | +def _allows_later_major( |
| 118 | + specifiers: SpecifierSet, |
| 119 | + installed: Version, |
| 120 | + allow_prereleases: bool = False) -> bool: |
| 121 | + candidates: set[Version] = set() |
| 122 | + |
| 123 | + def add_release(epoch: int, major: int, minor: int, patch: int) -> None: |
| 124 | + prefix = f"{epoch}!" if epoch else "" |
| 125 | + candidates.add(Version(f"{prefix}{major}.{minor}.{patch}")) |
| 126 | + |
| 127 | + for major in (installed.major, installed.major + 1, installed.major + 2): |
| 128 | + add_release(installed.epoch, major, 0, 0) |
| 129 | + add_release(installed.epoch, major, 0, 1) |
| 130 | + add_release(installed.epoch, major, 1, 0) |
| 131 | + |
| 132 | + for specifier in specifiers: |
| 133 | + try: |
| 134 | + bound = Version(specifier.version.removesuffix(".*")) |
| 135 | + except InvalidVersion: |
| 136 | + continue |
| 137 | + candidates.add(bound) |
| 138 | + add_release(bound.epoch, bound.major, bound.minor, bound.micro + 1) |
| 139 | + add_release(bound.epoch, bound.major, bound.minor + 1, 0) |
| 140 | + add_release(bound.epoch, bound.major + 1, 0, 0) |
| 141 | + |
| 142 | + for candidate in tuple(candidates): |
| 143 | + base = candidate.base_version |
| 144 | + candidates.update(Version(f"{base}{suffix}") for suffix in (".dev0", "a0", "b0", "rc0")) |
| 145 | + if candidate.pre and candidate.pre[1] > 0: |
| 146 | + label, number = candidate.pre |
| 147 | + candidates.add(Version(f"{base}{label}{number - 1}")) |
| 148 | + |
| 149 | + allowed = [ |
| 150 | + candidate for candidate in candidates |
| 151 | + if specifiers.contains(candidate, prereleases=allow_prereleases) |
| 152 | + ] |
| 153 | + return ( |
| 154 | + any(candidate.major == installed.major for candidate in allowed) |
| 155 | + and any(candidate.major > installed.major and candidate > installed for candidate in allowed) |
| 156 | + ) |
| 157 | + |
| 158 | + |
| 159 | +def warn_for_function_directory(function_directory: str, logger: Logger) -> None: |
| 160 | + """Find the Function App root before checking its dependencies.""" |
| 161 | + roots: list[Path] = [] |
| 162 | + configured_root = os.environ.get("AzureWebJobsScriptRoot") |
| 163 | + if configured_root: |
| 164 | + roots.append(Path(configured_root)) |
| 165 | + directory = Path(function_directory) |
| 166 | + roots.extend((directory, *directory.parents)) |
| 167 | + try: |
| 168 | + roots.append(Path.cwd()) |
| 169 | + except OSError as exc: |
| 170 | + logger.debug("Cannot determine the Function App working directory: %s", exc) |
| 171 | + for root in roots: |
| 172 | + if (root / "host.json").is_file(): |
| 173 | + warn_if_unbounded(str(root), logger) |
| 174 | + return |
| 175 | + logger.debug("Cannot find the Function App root for %s.", function_directory) |
| 176 | + |
| 177 | + |
| 178 | +def warn_if_unbounded(function_directory: str, logger: Logger) -> None: |
| 179 | + """Warn when the app's requirements demonstrably permit a later major.""" |
| 180 | + root = Path(function_directory) |
| 181 | + try: |
| 182 | + requirements = _read_specifiers(root, logger) |
| 183 | + except RecursionError as exc: |
| 184 | + logger.debug("Cannot check deeply nested Function App requirements: %s", exc) |
| 185 | + return |
| 186 | + if requirements is None: |
| 187 | + return |
| 188 | + specifiers, allow_prereleases = requirements |
| 189 | + try: |
| 190 | + installed = Version(version(_PACKAGE)) |
| 191 | + allows_later_major = _allows_later_major(specifiers, installed, allow_prereleases) |
| 192 | + except (PackageNotFoundError, InvalidVersion) as exc: |
| 193 | + logger.debug("Cannot determine the installed Durable Functions version: %s", exc) |
| 194 | + return |
| 195 | + if allows_later_major: |
| 196 | + next_major = f"{installed.epoch}!" if installed.epoch else "" |
| 197 | + next_major += str(installed.major + 1) |
| 198 | + logger.warning( |
| 199 | + "The Function App requirements allow azure-functions-durable to upgrade beyond major version %d. " |
| 200 | + "Future major releases may include broad, replay-breaking changes. If a deployment installs these " |
| 201 | + "requirements without a lock, an automatic upgrade could cause existing orchestrations to fail " |
| 202 | + "unexpectedly. Pin the major version, for example azure-functions-durable>=%s,<%s, " |
| 203 | + "or deploy using a complete dependency lock.", |
| 204 | + installed.major, installed.public, next_major, |
| 205 | + ) |
0 commit comments