Bug 1239296 - Add telemetry_handler function to mach context r?gps
This adds a telemetry_handler function to the mach context that can be
used to store telemetry data in the state directory for later reporting.
--- a/build/mach_bootstrap.py
+++ b/build/mach_bootstrap.py
@@ -1,19 +1,21 @@
# 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/.
from __future__ import print_function, unicode_literals
import errno
+import json
import os
import platform
import sys
import time
+import uuid
import __builtin__
from types import ModuleType
STATE_DIR_FIRST_RUN = '''
mach and the build system store shared state in a common directory on the
filesystem. The following directory will be created:
@@ -214,16 +216,41 @@ def bootstrap(topsrcdir, mozilla_dir=Non
# to react. We always exit after creating the directory because users don't
# like surprises.
try:
import mach.main
except ImportError:
sys.path[0:0] = [os.path.join(mozilla_dir, path) for path in SEARCH_PATHS]
import mach.main
+ def telemetry_handler(context, data):
+ # We have not opted-in to telemetry
+ if 'BUILD_SYSTEM_TELEMETRY' not in os.environ:
+ return
+
+ telemetry_dir = os.path.join(get_state_dir()[0], 'telemetry')
+ try:
+ os.mkdir(telemetry_dir)
+ except OSError as e:
+ if e.errno != errno.EEXIST:
+ raise
+ outgoing_dir = os.path.join(telemetry_dir, 'outgoing')
+ try:
+ os.mkdir(outgoing_dir)
+ except OSError as e:
+ if e.errno != errno.EEXIST:
+ raise
+
+ # Add common metadata to help submit sorted data later on.
+ # For now, we'll just record the mach command that was invoked.
+ data['argv'] = sys.argv
+
+ with open(os.path.join(outgoing_dir, str(uuid.uuid4())), 'w') as f:
+ json.dump(data, f, sort_keys=True)
+
def pre_dispatch_handler(context, handler, args):
"""Perform global checks before command dispatch.
Currently, our goal is to ensure developers periodically run
`mach mercurial-setup` (when applicable) to ensure their Mercurial
tools are up to date.
"""
# Don't do anything when...
@@ -295,16 +322,19 @@ def bootstrap(topsrcdir, mozilla_dir=Non
return state_dir
if key == 'topdir':
return topsrcdir
if key == 'pre_dispatch_handler':
return pre_dispatch_handler
+ if key == 'telemetry_handler':
+ return telemetry_handler
+
raise AttributeError(key)
mach = mach.main.Mach(os.getcwd())
mach.populate_context_handler = populate_context
for category, meta in CATEGORIES.items():
mach.define_category(category, meta['short'], meta['long'],
meta['priority'])