SERVER-98435 Update python codeowners lib and remove workaround (#35533)

GitOrigin-RevId: fb1e897638ceeebe6aa78b067a5a26628c569f76
This commit is contained in:
Mathias Stearn 2025-04-29 21:45:58 +02:00 committed by MongoDB Bot
parent c73104627c
commit 6a827a6c0f
3 changed files with 22 additions and 121 deletions

View File

@ -44,6 +44,22 @@ from cindex import (
)
from cindex import File as ClangFile
def perr(*values):
print(*values, file=sys.stderr)
def perr_exit(*values) -> NoReturn:
perr(*values)
sys.exit(1)
if codeowners.path_to_regex("/**/bar").match("/foobar"):
# Detect an outdated version suffering from https://github.com/sbdchd/codeowners/issues/43.
# We need to update to at least 0.8.0 to get the fix.
perr_exit("please run buildscripts/poetry_sync.sh to update dependencies")
# Monkey patch some features into clang's python binding. Keeping commented out for now in case we decide not to use modified lib.
# clang.functionList.append(("clang_File_isEqual", [ClangFile, ClangFile], ctypes.c_int))
# clang.functionList.append(("clang_Cursor_hasAttrs", [Cursor], ctypes.c_uint))
@ -67,112 +83,6 @@ out_from_env = os.environ.get("MOD_SCANNER_OUTPUT", None)
is_local = out_from_env is None
# Copied from
# https://github.com/sbdchd/codeowners/blob/53a7a9533ab455b0aa3f35f599558a2e1a1e97b7/codeowners/__init__.py#L17-L108
# then modified to correctly handle **, fixing https://github.com/sbdchd/codeowners/issues/43.
def path_to_regex(pattern: str):
"""
ported from https://github.com/hmarr/codeowners/blob/d0452091447bd2a29ee508eebc5a79874fb5d4ff/match.go#L33
MIT License
Copyright (c) 2020 Harry Marr
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
regex = ""
slash_pos = pattern.find("/")
anchored = slash_pos > -1 and slash_pos != len(pattern) - 1
regex += r"\A" if anchored else r"(?:\A|/)"
matches_dir = pattern[-1] == "/"
matches_no_subdirs = pattern[-2:] == "/*"
pattern_trimmed = pattern.strip("/")
in_char_class = False
escaped = False
iterator = enumerate(pattern_trimmed)
for i, ch in iterator:
if escaped:
regex += re.escape(ch)
escaped = False
continue
if ch == "\\":
escaped = True
elif ch == "*":
if i + 1 < len(pattern_trimmed) and pattern_trimmed[i + 1] == "*":
left_anchored = i == 0
leading_slash = i > 0 and pattern_trimmed[i - 1] == "/"
right_anchored = i + 2 == len(pattern_trimmed)
trailing_slash = i + 2 < len(pattern_trimmed) and pattern_trimmed[i + 2] == "/"
if (left_anchored or leading_slash) and (right_anchored or trailing_slash):
# MONGO CHANGE vvvv
if trailing_slash:
# Match behavior of glob.translate() from Python 3.13+
# https://github.com/python/cpython/blob/0879ebc953fa7372a4d99f3f79889093f04cac67/Lib/glob.py#L291
regex += "(?:.*/)?"
else:
regex += ".*"
# ORIG CODE vvvv
# regex += ".*"
# MONGO CHANGE ^^^^
next(iterator, None)
next(iterator, None)
continue
regex += "[^/]*"
elif ch == "?":
regex += "[^/]"
elif ch == "[":
in_char_class = True
regex += ch
elif ch == "]":
if in_char_class:
regex += ch
in_char_class = False
else:
regex += re.escape(ch)
else:
regex += re.escape(ch)
if in_char_class:
raise ValueError(f"unterminated character class in pattern {pattern}")
if matches_dir:
regex += "/"
elif matches_no_subdirs:
regex += r"\Z"
else:
regex += r"(?:\Z|/)"
return re.compile(regex)
# Monkey-patch codeowners lib to work around https://github.com/sbdchd/codeowners/issues/43.
# This must be done prior to the first usage of the library.
codeowners.path_to_regex = path_to_regex
with open(root / ".github/CODEOWNERS") as f:
code_owners = CodeOwners(f.read())
@ -195,15 +105,6 @@ with open(parent / "modules.yaml") as f:
modules = CodeOwners(parseModules())
def perr(*values):
print(*values, file=sys.stderr)
def perr_exit(*values) -> NoReturn:
perr(*values)
sys.exit(1)
class DecoratedCursor(Cursor):
# All USRs start with 'c:'. Local USRs then have a filename+'@' followed by
# an optional number+'@'. Global USRs just start with 'c:@'

10
poetry.lock generated
View File

@ -474,15 +474,15 @@ colorama = {version = "*", markers = "platform_system == \"Windows\""}
[[package]]
name = "codeowners"
version = "0.7.0"
version = "0.8.0"
description = "Codeowners parser for Python"
optional = false
python-versions = ">=3.7,<4.0"
python-versions = "<4.0,>=3.7"
groups = ["modules_poc"]
markers = "platform_machine != \"s390x\" and platform_machine != \"ppc64le\""
files = [
{file = "codeowners-0.7.0-py3-none-any.whl", hash = "sha256:0df5cd47299f984ba2e120dc4a0a7be68b528d53016ff39d06e86f85e33c7fc2"},
{file = "codeowners-0.7.0.tar.gz", hash = "sha256:a842647b20968c14da6066e4de4fffac4fd7c1c30de9cfa8b2fc8f534b3d9f48"},
{file = "codeowners-0.8.0-py3-none-any.whl", hash = "sha256:b92ce6f6c36dd9de5295c4dc285b2a3b14c81cae8b5eb3b05f0cfdbc4d93995a"},
{file = "codeowners-0.8.0.tar.gz", hash = "sha256:6505727574b67db4832a3d0d5e41c0f7eb80f8858ee27996e85b1e046f9266f8"},
]
[package.dependencies]
@ -5377,4 +5377,4 @@ libdeps = ["cxxfilt", "eventlet", "flask", "flask-cors", "gevent", "lxml", "prog
[metadata]
lock-version = "2.1"
python-versions = ">=3.10,<4.0"
content-hash = "6a229234b161f0731b4eb689cc18116538310e4ec0b977f3e816d4bae5a888ef"
content-hash = "30b34500b9366915bce3c0565417ae0d16818b23ea5fc80b93e8941f209d7a54"

View File

@ -119,7 +119,7 @@ evergreen-lint = "^0.1.9"
ruff = "^0.6.7"
[tool.poetry.group.modules_poc.dependencies]
codeowners = { version = "^0.7.0", markers = "platform_machine != 's390x' and platform_machine != 'ppc64le'" }
codeowners = { version = "^0.8.0", markers = "platform_machine != 's390x' and platform_machine != 'ppc64le'" }
progressbar2 = { version = "^4.4.2", markers = "platform_machine != 's390x' and platform_machine != 'ppc64le'" }
textual = {extras = ["syntax"], version = "^1.0.0", markers = "platform_machine != 's390x' and platform_machine != 'ppc64le'" }
tree-sitter = { version = "^0.24.0", markers = "platform_machine != 's390x' and platform_machine != 'ppc64le'" }