Bug 1855360 - Fix the skip-if syntax. a=bustage-fix
[gecko.git] / tools / phabricator / mach_commands.py
blob657267a5109445dacf75b4cd9180f5a6f8a6740a
1 # This Source Code Form is subject to the terms of the Mozilla Public
2 # License, v. 2.0. If a copy of the MPL was not distributed with this
3 # file, # You can obtain one at http://mozilla.org/MPL/2.0/.
5 import mozfile
6 from mach.decorators import Command, CommandArgument
7 from mach.site import MozSiteMetadata
10 @Command(
11 "install-moz-phab",
12 category="misc",
13 description="Install patch submission tool.",
15 @CommandArgument(
16 "--force",
17 "-f",
18 action="store_true",
19 help="Force installation even if already installed.",
21 def install_moz_phab(command_context, force=False):
22 import logging
23 import os
24 import re
25 import subprocess
26 import sys
28 existing = mozfile.which("moz-phab")
29 if existing and not force:
30 command_context.log(
31 logging.ERROR,
32 "already_installed",
33 {},
34 "moz-phab is already installed in %s." % existing,
36 sys.exit(1)
38 active_metadata = MozSiteMetadata.from_runtime()
39 original_python = active_metadata.original_python.python_path
40 is_external_python_virtualenv = (
41 subprocess.check_output(
43 original_python,
44 "-c",
45 "import sys; print(sys.prefix != sys.base_prefix)",
47 ).strip()
48 == b"True"
51 # pip3 is part of Python since 3.4, however some distros choose to
52 # remove core components from languages, so show a useful error message
53 # if pip3 is missing.
54 has_pip = subprocess.run([original_python, "-c", "import pip"]).returncode == 0
55 if not has_pip:
56 command_context.log(
57 logging.ERROR,
58 "pip_not_installed",
59 {},
60 "Python 3's `pip` is not installed. Try installing it with your system "
61 "package manager.",
63 sys.exit(1)
65 command = [original_python, "-m", "pip", "install", "--upgrade", "MozPhab"]
67 if (
68 sys.platform.startswith("linux")
69 or sys.platform.startswith("openbsd")
70 or sys.platform.startswith("dragonfly")
71 or sys.platform.startswith("freebsd")
72 or sys.platform.startswith("netbsd")
74 # On all Linux and BSD distros we consider doing a user installation.
75 platform_prefers_user_install = True
77 elif sys.platform.startswith("darwin"):
78 # On MacOS we require brew or ports, which work better without --user.
79 platform_prefers_user_install = False
81 elif sys.platform.startswith("win32") or sys.platform.startswith("msys"):
82 # Likewise for Windows we assume a system level install is preferred.
83 platform_prefers_user_install = False
85 else:
86 # Unsupported, default to --user.
87 command_context.log(
88 logging.WARNING,
89 "unsupported_platform",
90 {},
91 "Unsupported platform (%s), assuming per-user installation is "
92 "preferred." % sys.platform,
94 platform_prefers_user_install = True
96 command_env = os.environ.copy()
98 if platform_prefers_user_install and not is_external_python_virtualenv:
99 # Virtual environments don't see user packages, so only perform a user
100 # installation if we're not within one.
101 command.append("--user")
102 # This is needed to work around a problem on Ubuntu 23.04 and Debian 12
103 # See bug 1831442 for more details
104 command_env["PIP_BREAK_SYSTEM_PACKAGES"] = "1"
106 command_context.log(logging.INFO, "run", {}, "Installing moz-phab")
107 subprocess.run(command, env=command_env)
109 # There isn't an elegant way of determining the CLI location of a pip-installed package.
110 # The viable mechanism used here is to:
111 # 1. Get the list of info about the installed package via pip
112 # 2. Parse out the install location. This gives us the python environment in which the
113 # package is installed
114 # 3. Parse out the relative location of the cli script
115 # 4. Join the two paths, and execute the script at that location
117 info = subprocess.check_output(
118 [original_python, "-m", "pip", "show", "-f", "MozPhab"],
119 universal_newlines=True,
121 mozphab_package_location = re.compile(r"Location: (.*)").search(info).group(1)
122 # This needs to match "moz-phab" (*nix) and "moz-phab.exe" (Windows) while missing
123 # "moz-phab-script.py" (Windows).
124 potential_cli_paths = re.compile(
125 r"([^\s]*moz-phab(?:\.exe)?)$", re.MULTILINE
126 ).findall(info)
128 if len(potential_cli_paths) != 1:
129 command_context.log(
130 logging.WARNING,
131 "no_mozphab_console_script",
133 "Could not find the CLI script for moz-phab. Skipping install-certificate step.",
135 sys.exit(1)
137 console_script = os.path.realpath(
138 os.path.join(mozphab_package_location, potential_cli_paths[0])
140 subprocess.run([console_script, "install-certificate"])