Skip to content

Commit

Permalink
Bug 1740123: Move Python version check earlier r=ahal
Browse files Browse the repository at this point in the history
Move Python version check as early as possible so that more code can
safely depend on modern behaviour while out-of-date Python versions still
get graceful error messages.

Without this change, Python 2 usages fail on importing `importlib.util`
before the nice "out-of-date version" warning is printed.

Differential Revision: https://phabricator.services.mozilla.com/D134185
  • Loading branch information
Mitchell Hentges committed Jan 4, 2022
1 parent 07f875d commit 8fff83d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 41 deletions.
41 changes: 0 additions & 41 deletions build/mach_initialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import math
import os
import platform
import shutil
import sys

Expand Down Expand Up @@ -140,31 +139,6 @@ class MetaPathFinder(object):
},
}

INSTALL_PYTHON_GUIDANCE_LINUX = """
See https://firefox-source-docs.mozilla.org/setup/linux_build.html#installingpython
for guidance on how to install Python on your system.
""".strip()

INSTALL_PYTHON_GUIDANCE_OSX = """
See https://firefox-source-docs.mozilla.org/setup/macos_build.html
for guidance on how to prepare your system to build Firefox. Perhaps
you need to update Xcode, or install Python using brew?
""".strip()

INSTALL_PYTHON_GUIDANCE_MOZILLABUILD = """
Python is provided by MozillaBuild; ensure your MozillaBuild
installation is up to date.
See https://firefox-source-docs.mozilla.org/setup/windows_build.html#install-mozillabuild
for details.
""".strip()

INSTALL_PYTHON_GUIDANCE_OTHER = """
We do not have specific instructions for your platform on how to
install Python. You may find Pyenv (https://github.com/pyenv/pyenv)
helpful, if your system package manager does not provide a way to
install a recent enough Python 3.
""".strip()


def _activate_python_environment(topsrcdir, get_state_dir):
from mach.site import MachSiteManager
Expand All @@ -177,21 +151,6 @@ def _activate_python_environment(topsrcdir, get_state_dir):


def initialize(topsrcdir):
# Ensure we are running Python 3.6+. We run this check as soon as
# possible to avoid a cryptic import/usage error.
if sys.version_info < (3, 6):
print("Python 3.6+ is required to run mach.")
print("You are running Python", platform.python_version())
if sys.platform.startswith("linux"):
print(INSTALL_PYTHON_GUIDANCE_LINUX)
elif sys.platform.startswith("darwin"):
print(INSTALL_PYTHON_GUIDANCE_OSX)
elif "MOZILLABUILD" in os.environ:
print(INSTALL_PYTHON_GUIDANCE_MOZILLABUILD)
else:
print(INSTALL_PYTHON_GUIDANCE_OTHER)
sys.exit(1)

# This directory was deleted in bug 1666345, but there may be some ignored
# files here. We can safely just delete it for the user so they don't have
# to clean the repo themselves.
Expand Down
35 changes: 35 additions & 0 deletions mach
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@
from __future__ import absolute_import, print_function, unicode_literals

import os
import platform
import sys
from textwrap import dedent


def load_mach(dir_path, mach_path):
# Defer import of "importlib.util" until after Python version check has happened
# so that Python 2 usages fail gracefully.
import importlib.util
spec = importlib.util.spec_from_file_location('mach_initialize', mach_path)
mach_initialize = importlib.util.module_from_spec(spec)
Expand All @@ -33,6 +37,37 @@ def check_and_get_mach(dir_path):


def main(args):
# Ensure we are running Python 3.6+. We run this check as soon as
# possible to avoid a cryptic import/usage error.
if sys.version_info < (3, 6):
print("Python 3.6+ is required to run mach.")
print("You are running Python", platform.python_version())
if sys.platform.startswith("linux"):
print(dedent("""
See https://firefox-source-docs.mozilla.org/setup/linux_build.html#installingpython
for guidance on how to install Python on your system.
""").strip())
elif sys.platform.startswith("darwin"):
print(dedent("""
See https://firefox-source-docs.mozilla.org/setup/macos_build.html
for guidance on how to prepare your system to build Firefox. Perhaps
you need to update Xcode, or install Python using brew?
""").strip())
elif "MOZILLABUILD" in os.environ:
print(dedent("""
Python is provided by MozillaBuild; ensure your MozillaBuild installation is
up to date. See https://firefox-source-docs.mozilla.org/setup/windows_build.html#install-mozillabuild
for details.
""").strip())
else:
print(dedent("""
We do not have specific instructions for your platform on how to
install Python. You may find Pyenv (https://github.com/pyenv/pyenv)
helpful, if your system package manager does not provide a way to
install a recent enough Python 3.
""").strip())
sys.exit(1)

# XCode python sets __PYVENV_LAUNCHER__, which overrides the executable
# used when a python subprocess is created. This is an issue when we want
# to run using our virtualenv python executables.
Expand Down

0 comments on commit 8fff83d

Please sign in to comment.