Skip to content

Commit b88415b

Browse files
authored
refactor(ci): replace shell=True and awk pipes with native Python (#2671)
1 parent cb01da6 commit b88415b

File tree

2 files changed

+38
-15
lines changed

2 files changed

+38
-15
lines changed

.evergreen/scripts/resync-all-specs.py

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import subprocess
77
from argparse import Namespace
88
from subprocess import CalledProcessError
9-
from typing import Optional
109

1110

1211
def resync_specs(directory: pathlib.Path, errored: dict[str, str]) -> None:
@@ -32,14 +31,27 @@ def resync_specs(directory: pathlib.Path, errored: dict[str, str]) -> None:
3231

3332
def apply_patches(errored):
3433
print("Beginning to apply patches")
35-
subprocess.run(["bash", "./.evergreen/remove-unimplemented-tests.sh"], check=True) # noqa: S603, S607
34+
subprocess.run(
35+
["bash", "./.evergreen/remove-unimplemented-tests.sh"], # noqa: S603, S607
36+
check=True,
37+
)
3638
try:
37-
subprocess.run(
38-
["git apply -R --allow-empty --whitespace=fix ./.evergreen/spec-patch/*"], # noqa: S607
39-
shell=True, # noqa: S602
40-
check=True,
41-
stderr=subprocess.PIPE,
42-
)
39+
# Avoid shell=True by passing arguments as a list.
40+
# Note: glob expansion doesn't work in shell=False, so we use a list of files.
41+
patches = [str(p) for p in pathlib.Path("./.evergreen/spec-patch/").glob("*")]
42+
if patches:
43+
subprocess.run(
44+
[ # noqa: S603, S607
45+
"git",
46+
"apply",
47+
"-R",
48+
"--allow-empty",
49+
"--whitespace=fix",
50+
*patches,
51+
],
52+
check=True,
53+
stderr=subprocess.PIPE,
54+
)
4355
except CalledProcessError as exc:
4456
errored["applying patches"] = exc.stderr
4557

@@ -73,17 +85,24 @@ def check_new_spec_directories(directory: pathlib.Path) -> list[str]:
7385
return list(spec_set - test_set)
7486

7587

76-
def write_summary(errored: dict[str, str], new: list[str], filename: Optional[str]) -> None:
88+
def write_summary(errored: dict[str, str], new: list[str], filename: str | None) -> None:
7789
"""Generate the PR description"""
7890
pr_body = ""
91+
# Avoid shell=True and complex pipes by using Python to process git output
7992
process = subprocess.run(
80-
["git diff --name-only | awk -F'/' '{print $2}' | sort | uniq"], # noqa: S607
81-
shell=True, # noqa: S602
93+
["git", "diff", "--name-only"], # noqa: S603, S607
8294
capture_output=True,
8395
text=True,
8496
check=True,
8597
)
86-
succeeded = process.stdout.strip().split()
98+
changed_files = process.stdout.strip().splitlines()
99+
succeeded_set = set()
100+
for f in changed_files:
101+
parts = f.split("/")
102+
if len(parts) > 1:
103+
succeeded_set.add(parts[1])
104+
succeeded = sorted(succeeded_set)
105+
87106
if len(succeeded) > 0:
88107
pr_body += "The following specs were changed:\n -"
89108
pr_body += "\n -".join(succeeded)
@@ -120,7 +139,9 @@ def main(args: Namespace):
120139
description="Python Script to resync all specs and generate summary for PR."
121140
)
122141
parser.add_argument(
123-
"--filename", help="Name of file for the summary to be written into.", default=None
142+
"--filename",
143+
help="Name of file for the summary to be written into.",
144+
default=None,
124145
)
125146
args = parser.parse_args()
126147
main(args)

doc/conf.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@
8888
linkcheck_ignore = [
8989
"https://github.com/mongodb/specifications/blob/master/source/server-discovery-and-monitoring/server-monitoring.md#requesting-an-immediate-check",
9090
"https://github.com/mongodb/specifications/blob/master/source/transactions-convenient-api/transactions-convenient-api.md#handling-errors-inside-the-callback",
91+
"https://github.com/mongodb/specifications/blob/master/source/uri-options/uri-options.md",
92+
"https://github.com/mongodb/specifications/blob/master/source/uri-options/uri-options.md",
9193
"https://github.com/mongodb/libmongocrypt/blob/master/bindings/python/README.rst#installing-from-source",
9294
r"https://wiki.centos.org/[\w/]*",
9395
r"https://sourceforge.net/",
@@ -186,8 +188,8 @@
186188
("index", "PyMongo.tex", "PyMongo Documentation", "Michael Dirolf", "manual"),
187189
]
188190

189-
# The name of an image file (relative to this directory) to place at the top of
190-
# the title page.
191+
# The name of an image file (relative to this directory) to place at the top
192+
# of the title page.
191193
# latex_logo = None
192194

193195
# For "manual" documents, if this is true, then toplevel headings are parts,

0 commit comments

Comments
 (0)