You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by po...@apache.org on 2022/07/22 19:03:29 UTC

[airflow] branch main updated: Protect against the case when emulated Python is used on M1s (#25229)

This is an automated email from the ASF dual-hosted git repository.

potiuk pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/airflow.git


The following commit(s) were added to refs/heads/main by this push:
     new f53bd5df2a Protect against the case when emulated Python is used on M1s (#25229)
f53bd5df2a is described below

commit f53bd5df2a0b370a14f811b353229ad3e9c66662
Author: Jarek Potiuk <ja...@polidea.com>
AuthorDate: Fri Jul 22 21:03:22 2022 +0200

    Protect against the case when emulated Python is used on M1s (#25229)
    
    People might have an intel Python installed on M1s. This happened
    already. The result of it that they (unknowingly) suffer from
    10x slower speed of any Python code they use (even locally).
    
    This can happen in two cases:
    
    * you can run in "arm" terminal and your python might be
      Intel based (for example when your environment is old or
      managed before it was ready for ARM architecture) or when
      you use some scientific package managers like anaconda that
      still have not switched to ARM. In this case we
      detect different architecture reported by Python and system.
    
    * you can run it in emulated terminal when your IDE has been
      installed using different architecture. In this case, we detect
      if rosetta emulation is running via sysctl command.
    
    This also impact Breeze because we are using Python architecture
    in order to determine which platform image should be used by
    Breeze.
    
    This change adds big, fat warning and 20 seconds of delay asking
    the user if they REALLY want to run Breeze command using emulated
    architecture. If they answer y - it will continue. If they do not answer
    or answer anything else, the command will error out.
---
 .../src/airflow_breeze/commands/main_command.py    | 71 ++++++++++++++++++++++
 1 file changed, 71 insertions(+)

diff --git a/dev/breeze/src/airflow_breeze/commands/main_command.py b/dev/breeze/src/airflow_breeze/commands/main_command.py
index c62101a44f..13ea840ce0 100644
--- a/dev/breeze/src/airflow_breeze/commands/main_command.py
+++ b/dev/breeze/src/airflow_breeze/commands/main_command.py
@@ -14,6 +14,9 @@
 # KIND, either express or implied.  See the License for the
 # specific language governing permissions and limitations
 # under the License.
+import platform
+import subprocess
+import sys
 
 from airflow_breeze.configure_rich_click import click
 from airflow_breeze.utils.common_options import (
@@ -63,5 +66,73 @@ from airflow_breeze.utils.common_options import (
 def main(ctx: click.Context, **kwargs):
     from airflow_breeze.commands.developer_commands import shell
 
+    check_for_rosetta_environment()
+    check_for_python_emulation()
+
     if not ctx.invoked_subcommand:
         ctx.forward(shell, extra_args={})
+
+
+def check_for_python_emulation():
+    try:
+        system_machine = subprocess.check_output(["uname", "-m"], text=True).strip()
+        python_machine = platform.uname().machine
+        if system_machine != python_machine:
+            from airflow_breeze.utils.console import get_console
+
+            get_console().print(
+                f'\n\n[error]Your Python architecture is {python_machine} and '
+                f'system architecture is {system_machine}[/]'
+            )
+            get_console().print(
+                '[warning]This is very bad and your Python is 10x slower as it is emulated[/]'
+            )
+            get_console().print(
+                '[warning]You likely installed your Python wrongly and you should '
+                'remove it and reinstall from scratch[/]\n'
+            )
+            from inputimeout import inputimeout
+
+            user_status = inputimeout(
+                prompt="Are you REALLY sure you want to continue? (press y otherwise we exit in 20s) ",
+                timeout=20,
+            )
+            if not user_status.upper() in ['Y', 'YES']:
+                sys.exit(1)
+    except subprocess.CalledProcessError:
+        pass
+    except PermissionError:
+        pass
+
+
+def check_for_rosetta_environment():
+    try:
+        runs_in_rosetta = subprocess.check_output(
+            ["sysctl", "-n", "sysctl.proc_translated"], text=True
+        ).strip()
+        if runs_in_rosetta == '1':
+            from airflow_breeze.utils.console import get_console
+
+            get_console().print(
+                '\n\n[error]You are starting breeze in `rosetta 2` emulated environment on Mac[/]'
+            )
+            get_console().print(
+                '[warning]This is very bad and your Python is 10x slower as it is emulated[/]'
+            )
+            get_console().print(
+                '[warning]You likely have wrong architecture-based IDE (PyCharm/VSCode/Intellij) that '
+                'you run it on\n'
+                'You should download the right architecture for your Mac (Apple Silicon or Intel)[/]\n'
+            )
+            from inputimeout import inputimeout
+
+            user_status = inputimeout(
+                prompt="Are you REALLY sure you want to continue? (press y otherwise we exit in 20s) ",
+                timeout=20,
+            )
+            if not user_status.upper() in ['Y', 'YES']:
+                sys.exit(1)
+    except subprocess.CalledProcessError:
+        pass
+    except PermissionError:
+        pass