Skip to content

Commit

Permalink
Bug 808346 - Don't scan sys.path to discover mach commands; r=jhammel
Browse files Browse the repository at this point in the history
All mach modules are now explicitly listed in the mach driver.

--HG--
rename : python/mozbuild/mach/commands/build.py => python/mozbuild/mozbuild/mach_commands.py
  • Loading branch information
indygreg committed Nov 7, 2012
1 parent fff0f2c commit 57be670
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 87 deletions.
2 changes: 1 addition & 1 deletion mach
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ MACH_MODULES = [
'layout/tools/reftest/mach_commands.py',
'python/mozboot/mozboot/mach_commands.py',
'python/mozbuild/mozbuild/config.py',
'python/mozbuild/mozbuild/mach_commands.py',
'testing/mochitest/mach_commands.py',
'testing/xpcshell/mach_commands.py',
]
Expand All @@ -55,7 +56,6 @@ except ImportError:

# All of the code is in a module because EVERYTHING IS A LIBRARY.
mach = mach.main.Mach(our_dir)
mach.load_commands_from_sys_path()

for path in MACH_MODULES:
mach.load_commands_from_file(os.path.join(our_dir, path))
Expand Down
6 changes: 1 addition & 5 deletions python/mach/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,7 @@ to the decorators are being used as arguments to
*mach.base* module for more.

The Python modules defining mach commands do not need to live inside the
main mach source tree. If a path on *sys.path* contains a *mach/commands*
directory, modules will be loaded automatically by mach and any classes
containing the decorators described above will be detected and loaded
automatically by mach. So, to add a new subcommand to mach, you just need
to ensure your Python module is present on *sys.path*.
main mach source tree.

Minimizing Code in Mach
-----------------------
Expand Down
28 changes: 6 additions & 22 deletions python/mach/mach/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,28 +145,6 @@ def __init__(self, cwd):

self.log_manager.register_structured_logger(self.logger)

def load_commands_from_sys_path(self):
"""Discover and load mach command modules from sys.path.
This iterates over all paths on sys.path. If the path contains a
"mach/commands" subdirectory, all .py files in that directory will be
loaded and examined for mach commands.
"""
# Create parent module otherwise Python complains when the parent is
# missing.
if b'mach.commands' not in sys.modules:
mod = imp.new_module(b'mach.commands')
sys.modules[b'mach.commands'] = mod

for path in sys.path:
# We only support importing .py files from directories.
commands_path = os.path.join(path, 'mach', 'commands')

if not os.path.isdir(commands_path):
continue

self.load_commands_from_directory(commands_path)

def load_commands_from_directory(self, path):
"""Scan for mach commands from modules in a directory.
Expand All @@ -190,6 +168,12 @@ def load_commands_from_file(self, path, module_name=None):
chosen.
"""
if module_name is None:
# Ensure parent module is present otherwise we'll (likely) get
# an error due to unknown parent.
if b'mach.commands' not in sys.modules:
mod = imp.new_module(b'mach.commands')
sys.modules[b'mach.commands'] = mod

module_name = 'mach.commands.%s' % uuid.uuid1().get_hex()

imp.load_source(module_name, path)
Expand Down
57 changes: 0 additions & 57 deletions python/mozbuild/mach/commands/build.py

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# 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/.
# 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 logging
import operator
import os

Expand All @@ -16,6 +17,48 @@
from mozbuild.base import MachCommandBase


@CommandProvider
class Build(MachCommandBase):
"""Interface to build the tree."""

@Command('build', help='Build the tree.')
def build(self):
# This code is only meant to be temporary until the more robust tree
# building code in bug 780329 lands.
from mozbuild.compilation.warnings import WarningsCollector
from mozbuild.compilation.warnings import WarningsDatabase

warnings_path = self._get_state_filename('warnings.json')
warnings_database = WarningsDatabase()

if os.path.exists(warnings_path):
warnings_database.load_from_file(warnings_path)

warnings_collector = WarningsCollector(database=warnings_database,
objdir=self.topobjdir)

def on_line(line):
try:
warning = warnings_collector.process_line(line)
if warning:
self.log(logging.INFO, 'compiler_warning', warning,
'Warning: {flag} in {filename}: {message}')
except:
# This will get logged in the more robust implementation.
pass

self.log(logging.INFO, 'build_output', {'line': line}, '{line}')

self._run_make(srcdir=True, filename='client.mk', line_handler=on_line,
log=False, print_directory=False)

self.log(logging.WARNING, 'warning_summary',
{'count': len(warnings_collector.database)},
'{count} compiler warnings present.')

warnings_database.save_to_file(warnings_path)


@CommandProvider
class Warnings(MachCommandBase):
"""Provide commands for inspecting warnings."""
Expand Down

0 comments on commit 57be670

Please sign in to comment.