Skip to content

Commit 5ade89b

Browse files
github-actions[bot]sanju1198sunank200
authored andcommitted
[v3-1-test] Fix: Prevent AttributeError crash in prek when run inside container (#57497) (#57649)
* Revert virtualenv connections/variables access and logging as test are failing Revert virtualenv connections/variables access and logging as tests are failing in https://github.com/apache/airflow/actions/runs/18896380254. The issue here is that it's a socket initialisation error that occurs when CommsDecoder tries to create a socket from stdin (file descriptor 0) in subprocess environments where stdin is not a socket. This reverts commit fb8b590 (PR #57213) which allowed virtualenv code to access connections/variables and send logs via supervisor comms reinitialization. * Address review: Use built-in print as fallback --------- (cherry picked from commit 5002660) Co-authored-by: Gurram Sai Ganesh <[email protected]> Co-authored-by: Ankit Chaurasia <[email protected]>
1 parent 7630158 commit 5ade89b

File tree

2 files changed

+71
-29
lines changed

2 files changed

+71
-29
lines changed

scripts/ci/prek/common_prek_utils.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -221,9 +221,12 @@ def run_command_via_breeze_shell(
221221
subprocess_cmd.extend(["--project-name", project_name])
222222
subprocess_cmd.append(" ".join([shlex.quote(arg) for arg in cmd]))
223223
if os.environ.get("VERBOSE_COMMANDS"):
224-
console.print(
225-
f"[magenta]Running command: {' '.join([shlex.quote(item) for item in subprocess_cmd])}[/]"
226-
)
224+
if console:
225+
console.print(
226+
f"[magenta]Running command: {' '.join([shlex.quote(item) for item in subprocess_cmd])}[/]"
227+
)
228+
else:
229+
print(f"Running command: {' '.join([shlex.quote(item) for item in subprocess_cmd])}")
227230
result = subprocess.run(
228231
subprocess_cmd,
229232
check=False,

scripts/ci/prek/mypy_folder.py

Lines changed: 65 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,10 @@ def get_all_files(folder: str) -> list[str]:
116116
) and not file.as_posix().endswith("src/airflow/providers/__init__.py"):
117117
files_to_check.append(file.relative_to(AIRFLOW_ROOT_PATH).as_posix())
118118
file_spec = "@/files/mypy_files.txt"
119-
console.print(f"[info]Running mypy with {file_spec}")
119+
if console:
120+
console.print(f"[info]Running mypy with {file_spec}")
121+
else:
122+
print(f"info: Running mypy with {file_spec}")
120123
return files_to_check
121124

122125

@@ -126,26 +129,42 @@ def get_all_files(folder: str) -> list[str]:
126129
all_files_to_check.extend(get_all_files(mypy_folder))
127130
MYPY_FILE_LIST.write_text("\n".join(all_files_to_check))
128131

129-
if os.environ.get("CI"):
130-
console.print("[info]The content of the file is:")
131-
console.print(all_files_to_check)
132+
if console:
133+
if os.environ.get("CI"):
134+
console.print("[info]The content of the file is:")
135+
console.print(all_files_to_check)
136+
else:
137+
console.print(f"[info]You cand check the list of files in:[/] {MYPY_FILE_LIST}")
132138
else:
133-
console.print(f"[info]You cand check the list of files in:[/] {MYPY_FILE_LIST}")
139+
if os.environ.get("CI"):
140+
print("info: The content of the file is:")
141+
print(all_files_to_check)
142+
else:
143+
print(f"info: You cand check the list of files in: {MYPY_FILE_LIST}")
134144

135145
print(f"Running mypy with {FILE_ARGUMENT}")
136146

137147
mypy_cmd_parts = [f"TERM=ansi mypy {shlex.quote(FILE_ARGUMENT)}"]
138148

139149
if show_unused_warnings == "true":
140-
console.print(
141-
"[info]Running mypy with --warn-unused-ignores to display unused ignores, unset environment variable: SHOW_UNUSED_MYPY_WARNINGS to runoff this behaviour"
142-
)
150+
if console:
151+
console.print(
152+
"[info]Running mypy with --warn-unused-ignores to display unused ignores, unset environment variable: SHOW_UNUSED_MYPY_WARNINGS to runoff this behaviour"
153+
)
154+
else:
155+
print(
156+
"info: Running mypy with --warn-unused-ignores to display unused ignores, unset environment variable: SHOW_UNUSED_MYPY_WARNINGS to runoff this behaviour"
157+
)
143158
mypy_cmd_parts.append("--warn-unused-ignores")
144-
145159
if show_unreachable_warnings == "true":
146-
console.print(
147-
"[info]Running mypy with --warn-unreachable to display unreachable code, unset environment variable: SHOW_UNREACHABLE_MYPY_WARNINGS to runoff this behaviour"
148-
)
160+
if console:
161+
console.print(
162+
"[info]Running mypy with --warn-unreachable to display unreachable code, unset environment variable: SHOW_UNREACHABLE_MYPY_WARNINGS to runoff this behaviour"
163+
)
164+
else:
165+
print(
166+
"info: Running mypy with --warn-unreachable to display unreachable code, unset environment variable: SHOW_UNREACHABLE_MYPY_WARNINGS to runoff this behaviour"
167+
)
149168
mypy_cmd_parts.append("--warn-unreachable")
150169

151170
mypy_cmd = " ".join(mypy_cmd_parts)
@@ -165,22 +184,42 @@ def get_all_files(folder: str) -> list[str]:
165184
)
166185
ci_environment = os.environ.get("CI")
167186
if res.returncode != 0:
168-
if ci_environment:
187+
if console:
188+
if ci_environment:
189+
console.print(
190+
"[yellow]You are running mypy with the folders selected. If you want to "
191+
"reproduce it locally, you need to run the following command:\n"
192+
)
193+
console.print("prek --hook-stage manual mypy-<folder> --all-files\n")
194+
upgrading = os.environ.get("UPGRADE_TO_NEWER_DEPENDENCIES", "false") != "false"
195+
if upgrading:
196+
console.print(
197+
"[yellow]You are running mypy with the image that has dependencies upgraded automatically.\n"
198+
)
199+
flag = " --upgrade-to-newer-dependencies" if upgrading else ""
169200
console.print(
170-
"[yellow]You are running mypy with the folders selected. If you want to "
171-
"reproduce it locally, you need to run the following command:\n"
201+
"[yellow]If you see strange stacktraces above, and can't reproduce it, please run"
202+
" this command and try again:\n"
172203
)
173-
console.print("prek --hook-stage manual mypy-<folder> --all-files\n")
174-
upgrading = os.environ.get("UPGRADE_TO_NEWER_DEPENDENCIES", "false") != "false"
175-
if upgrading:
204+
console.print(f"breeze ci-image build --python 3.10{flag}\n")
176205
console.print(
177-
"[yellow]You are running mypy with the image that has dependencies upgraded automatically.\n"
206+
"[yellow]You can also run `breeze down --cleanup-mypy-cache` to clean up the cache used.\n"
178207
)
179-
flag = " --upgrade-to-newer-dependencies" if upgrading else ""
180-
console.print(
181-
"[yellow]If you see strange stacktraces above, and can't reproduce it, please run"
182-
" this command and try again:\n"
183-
)
184-
console.print(f"breeze ci-image build --python 3.10{flag}\n")
185-
console.print("[yellow]You can also run `breeze down --cleanup-mypy-cache` to clean up the cache used.\n")
208+
else:
209+
if ci_environment:
210+
print(
211+
"You are running mypy with the folders selected. If you want to "
212+
"reproduce it locally, you need to run the following command:\n"
213+
)
214+
print("prek --hook-stage manual mypy-<folder> --all-files\n")
215+
upgrading = os.environ.get("UPGRADE_TO_NEWER_DEPENDENCIES", "false") != "false"
216+
if upgrading:
217+
print("You are running mypy with the image that has dependencies upgraded automatically.\n")
218+
flag = " --upgrade-to-newer-dependencies" if upgrading else ""
219+
print(
220+
"If you see strange stacktraces above, and can't reproduce it, please run"
221+
" this command and try again:\n"
222+
)
223+
print(f"breeze ci-image build --python 3.10{flag}\n")
224+
print("You can also run `breeze down --cleanup-mypy-cache` to clean up the cache used.\n")
186225
sys.exit(res.returncode)

0 commit comments

Comments
 (0)