# This Source Code Form is subject to the terms of the Mozilla Public # License, v. 2.0. If a copy of the MPL was not distributed with this # file, You can obtain one at http://mozilla.org/MPL/2.0/.
import configparser import json import os import subprocess import sys import urllib.parse as urllib_parse from pathlib import Path from textwrap import dedent
import requests from mozbuild.base import BuildEnvironmentNotFoundException, MozbuildObject from mozbuild.telemetry import filter_args from mozversioncontrol import InvalidRepoPath, get_repository_object
from mach.config import ConfigSettings from mach.settings import MachSettings from mach.site import MozSiteMetadata from mach.telemetry_interface import GleanTelemetry, NoopTelemetry from mach.util import get_state_dir
def create_telemetry_from_environment(settings): """Creates and a Telemetry instance based on system details.
If telemetry isn't enabled or Glean can't be imported, then a "mock" telemetry
instance is returned that doesn't set or record any data. This allows consumers
to optimistically set telemetry data without needing to specifically handle the
case where the current system doesn't support it. """
active_metadata = MozSiteMetadata.from_runtime()
mach_sites = [site_path.stem for site_path in SITE_DIR.glob("*.txt")]
is_a_mach_virtualenv = active_metadata and active_metadata.site_name in mach_sites
ifnot (
is_applicable_telemetry_environment() # If not using a mach virtualenv (e.g.: bootstrap uses native python) # then we can't guarantee that the glean package that we import is a # compatible version. Therefore, don't use glean. and is_a_mach_virtualenv
): return NoopTelemetry(False)
is_enabled = is_telemetry_enabled(settings)
try: from glean import Glean except ImportError: return NoopTelemetry(is_enabled)
try:
instance = MozbuildObject.from_environment() except BuildEnvironmentNotFoundException: # Mach may be invoked with the state dir as the current working # directory, in which case we're not able to find the topsrcdir (so # we can't create a MozbuildObject instance). # Without this information, we're unable to filter argv paths, so # we skip submitting them to telemetry. return
metrics.mach.argv.set(
filter_args(command, sys.argv, instance.topsrcdir, instance.topobjdir)
)
def is_applicable_telemetry_environment(): if os.environ.get("MACH_MAIN_PID") != str(os.getpid()): # This is a child mach process. Since we're collecting telemetry for the parent, # we don't want to collect telemetry again down here. returnFalse
returnTrue
def is_telemetry_enabled(settings): if os.environ.get("DISABLE_TELEMETRY") == "1": returnFalse
return settings.mach_telemetry.is_enabled
def arcrc_path(): if sys.platform.startswith("win32") or sys.platform.startswith("msys"): return Path(os.environ.get("APPDATA", "")) / ".arcrc" else: return Path("~/.arcrc").expanduser()
def resolve_is_employee(topsrcdir: Path): """Detect whether or not the current user is a Mozilla employee.
Checks using Bugzilla authentication, if possible. Otherwise falls back to checking if email configured in VCS is"@mozilla.com".
Returns Trueif the user could be identified as an employee, Falseif the user is confirmed asnot being an employee, orNoneif the user couldn't be
identified. """
is_employee = resolve_is_employee_by_credentials(topsrcdir) if is_employee isnotNone: return is_employee
def record_telemetry_settings(
main_settings,
state_dir: Path,
is_enabled,
): # We want to update the user's machrc file. However, the main settings object # contains config from "$topsrcdir/machrc" (if it exists) which we don't want # to accidentally include. So, we have to create a brand new mozbuild-specific # settings, update it, then write to it.
settings_path = state_dir / "machrc"
file_settings = ConfigSettings()
file_settings.register_provider(MachSettings) try:
file_settings.load_file(settings_path) except configparser.Error as error:
print(
f"Your mach configuration file at `{settings_path}` cannot be parsed:\n{error}"
) return
with open(settings_path, "w") as f:
file_settings.write(f)
# Telemetry will want this elsewhere in the mach process, so we'll slap the # new values on the main settings object.
main_settings.mach_telemetry.is_enabled = is_enabled
main_settings.mach_telemetry.is_set_up = True
If you'd like to opt out of data collection, select (N) at the prompt.
Would you like to enable build system telemetry? (Yn): """
)
% TELEMETRY_DESCRIPTION_PREAMBLE
).strip()
choice = input(prompt)
choice = choice.strip().lower() if choice == "": returnTrue if choice notin ("y", "n"):
print("ERROR! Please enter y or n!") else: return choice == "y"
def initialize_telemetry_setting(settings, topsrcdir: str, state_dir: str): """Enables telemetry for employees or prompts the user.""" # If the user doesn't care about telemetry for this invocation, then # don't make requests to Bugzilla and/or prompt for whether the # user wants to opt-in.
if topsrcdir isnotNone:
topsrcdir = Path(topsrcdir)
if state_dir isnotNone:
state_dir = Path(state_dir)
if os.environ.get("DISABLE_TELEMETRY") == "1": return
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.