Skip to content

Commit

Permalink
Get rid of the Command class completely.
Browse files Browse the repository at this point in the history
GoalRunner now just extends object.

The functionality previously in Command is now in GoalRunner.

Most of the functionality in pants_exe.py is now in GoalRunner too.

RIP old pants...

Testing Done:
All integration and unit tests pass.

Reviewed at https://rbcommons.com/s/twitter/r/1465/
  • Loading branch information
benjyw authored and Benjy committed Dec 11, 2014
1 parent af07ba6 commit 6a30e22
Show file tree
Hide file tree
Showing 10 changed files with 109 additions and 350 deletions.
1 change: 0 additions & 1 deletion src/python/pants/backend/core/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ python_library(
'src/python/pants/backend/core/targets:all',
'src/python/pants/backend/core/tasks:all',
'src/python/pants/base:build_file_aliases',
'src/python/pants/commands:goal_runner',
'src/python/pants/goal:task_registrar',
':core',
]
Expand Down
5 changes: 0 additions & 5 deletions src/python/pants/backend/core/register.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
from pants.base.build_environment import get_buildroot, pants_version
from pants.base.build_file_aliases import BuildFileAliases
from pants.base.source_root import SourceRoot
from pants.commands.goal_runner import GoalRunner
from pants.goal.task_registrar import TaskRegistrar as task


Expand Down Expand Up @@ -92,10 +91,6 @@ def build_file_aliases():
)


def register_commands():
GoalRunner._register()


def register_goals():
# Getting help.
task(name='goals', action=ListGoals
Expand Down
2 changes: 0 additions & 2 deletions src/python/pants/base/extension_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ def load_backend(build_configuration, backend_package):
{}, # globals
{}, # locals
['build_file_aliases',
'register_commands',
'register_goals'])
except ImportError as e:
raise BackendConfigurationError('Failed to load the {backend} backend: {error}'
Expand All @@ -137,5 +136,4 @@ def invoke_entrypoint(name):
if build_file_aliases:
build_configuration.register_aliases(build_file_aliases)

invoke_entrypoint('register_commands')
invoke_entrypoint('register_goals')
19 changes: 2 additions & 17 deletions src/python/pants/bin/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ python_library(
name = 'pants_exe',
sources = ['pants_exe.py'],
dependencies = [
'3rdparty/python:psutil',
'3rdparty/python/twitter/commons:twitter.common.dirutil',

'src/python/internal_backend/repositories:plugin',
'src/python/pants/backend/android:plugin',
'src/python/pants/backend/authentication:authentication',
Expand All @@ -17,12 +16,6 @@ python_library(
'src/python/pants/backend/python:plugin',
'src/python/pants/base:address',
'src/python/pants/base:build_environment',
'src/python/pants/base:build_file_address_mapper',
'src/python/pants/base:build_file_parser',
'src/python/pants/base:build_graph',
'src/python/pants/base:config',
'src/python/pants/base:extension_loader',
'src/python/pants/commands:command',

# XXX these are necessary to parse BUILD.commons. Should instead be
# added as plugins to pants.ini
Expand All @@ -33,22 +26,14 @@ python_library(
'src/python/pants/backend/jvm/tasks:checkstyle',
'src/python/pants/backend/python:python_chroot',
'src/python/pants/scm:git',

# XXX The remaining are clear abstraction leaks.
# The goal target should be goal:run_tracker but it is inseparable from goal; also
# an abstraction leak.
'src/python/pants/backend/jvm/tasks:nailgun_task',
'src/python/pants/goal:initialize_reporting',
'src/python/pants/goal:run_tracker',
'src/python/pants/reporting',
],
)

target(
name='pants_binary_deps',
dependencies=[
':pants_exe',
'src/python/pants/commands:pants_new',
'src/python/pants/commands:goal_runner',
]
)

Expand Down
126 changes: 5 additions & 121 deletions src/python/pants/bin/pants_exe.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,12 @@
print_function, unicode_literals)

import logging
import optparse
import os
import sys
import traceback

from pants.backend.jvm.tasks.nailgun_task import NailgunTask
from pants.base.build_environment import get_buildroot, pants_version
from pants.base.build_file_address_mapper import BuildFileAddressMapper
from pants.base.build_file_parser import BuildFileParser
from pants.base.build_graph import BuildGraph
from pants.base.config import Config
from pants.base.extension_loader import load_plugins_and_backends
from pants.base.workunit import WorkUnit
from pants.commands.command import Command
from pants.goal.initialize_reporting import initial_reporting
from pants.goal.run_tracker import RunTracker
from pants.reporting.report import Report
from pants.commands.goal_runner import GoalRunner


_LOG_EXIT_OPTION = '--log-exit'
Expand Down Expand Up @@ -54,40 +43,10 @@ def _unhandled_exception_hook(exception_class, exception, tb):
_exit_and_fail(msg)


def _find_all_commands():
for cmd in Command.all_commands():
cls = Command.get_command(cmd)
yield '%s\t%s' % (cmd, cls.__doc__)


def _synthesize_command(root_dir, args):
command = args[0]

if command in Command.all_commands():
subcommand_args = args[1:] if len(args) > 1 else []
return command, subcommand_args

if command.startswith('-'):
_exit_and_fail('Invalid command: %s' % command)

_exit_and_fail('Pants build missing a command: args={args} tb=\n{tb}'
.format(args=args, tb=traceback.format_exc()))


def _parse_command(root_dir, args):
command, args = _synthesize_command(root_dir, args)
return Command.get_command(command), args

def _run():
# Place the registration of the unhandled exception hook as early as possible in the code.
sys.excepthook = _unhandled_exception_hook

"""
To add additional paths to sys.path, add a block to the config similar to the following:
[main]
roots: ['src/python/pants_internal/test/',]
"""

logging.basicConfig()
version = pants_version()
if len(sys.argv) == 2 and sys.argv[1] == _VERSION_OPTION:
Expand All @@ -97,85 +56,10 @@ def _run():
if not os.path.exists(root_dir):
_exit_and_fail('PANTS_BUILD_ROOT does not point to a valid path: %s' % root_dir)

if len(sys.argv) < 2:
argv = ['goal']
else:
argv = sys.argv[1:]
# Hack to force ./pants -h etc. to redirect to goal.
if argv[0] != 'goal' and set(['-h', '--help', 'help']).intersection(argv):
argv = ['goal'] + argv

parser = optparse.OptionParser(add_help_option=False, version=version)
parser.add_option(_LOG_EXIT_OPTION,
action='store_true',
default=False,
dest='log_exit',
help='Log an exit message on success or failure.')

# Temporary hack, so that backend-loading code can access config via the cache.
# Later, in GoalRunner, we bootstrap the config properly and cache it, overwriting this one.
# We need this hacked-in support for PANTS_CONFIG_OVERRIDE because ci.sh uses it in the
# self-distribution tests. In normal code PANTS_CONFIG_OVERRIDE is supported via the
# --config-override option.
# This will go away completely once Command is gone.
override = os.environ.get('PANTS_CONFIG_OVERRIDE', None)
configpaths = [os.path.join(get_buildroot(), 'pants.ini')]
if override:
configpaths.append(override)
config = Config.load(configpaths)
Config.cache(config)

# XXX(wickman) This should be in the command goal, not in pants_exe.py!
run_tracker = RunTracker.from_config(config)
report = initial_reporting(config, run_tracker)
run_tracker.start(report)

url = run_tracker.run_info.get_info('report_url')
if url:
run_tracker.log(Report.INFO, 'See a report at: %s' % url)
else:
run_tracker.log(Report.INFO, '(To run a reporting server: ./pants goal server)')

backend_packages = config.getlist('backends', 'packages', [])
plugins = config.getlist('backends', 'plugins', [])

build_configuration = load_plugins_and_backends(plugins, backend_packages)
build_file_parser = BuildFileParser(build_configuration=build_configuration,
root_dir=root_dir,
run_tracker=run_tracker)
address_mapper = BuildFileAddressMapper(build_file_parser)
build_graph = BuildGraph(run_tracker=run_tracker, address_mapper=address_mapper)

command_class, command_args = _parse_command(root_dir, argv)
command = command_class(run_tracker,
root_dir,
parser,
command_args,
build_file_parser,
address_mapper,
build_graph)
try:
try:
result = command.run()
if result:
run_tracker.set_root_outcome(WorkUnit.FAILURE)
_do_exit(result)
except KeyboardInterrupt:
command.cleanup()
raise
except Exception:
run_tracker.set_root_outcome(WorkUnit.FAILURE)
raise
finally:
run_tracker.end()
# Must kill nailguns only after run_tracker.end() is called, because there may still
# be pending background work that needs a nailgun.
# TODO(Eric Ayers) simplify after setup_py goes away.
if (hasattr(command.old_options, 'cleanup_nailguns') and command.old_options.cleanup_nailguns) \
or (hasattr(command, 'new_options')
and command.new_options.for_global_scope().kill_nailguns) \
or config.get('nailgun', 'autokill', default=False):
NailgunTask.killall(None)
goal_runner = GoalRunner(root_dir)
goal_runner.setup()
result = goal_runner.run()
_do_exit(result)

def main():
try:
Expand Down
28 changes: 7 additions & 21 deletions src/python/pants/commands/BUILD
Original file line number Diff line number Diff line change
@@ -1,45 +1,31 @@
# Copyright 2014 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).

python_library(
name = 'command',
sources = ['command.py'],
dependencies = [
'3rdparty/python/twitter/commons:twitter.common.collections',
'src/python/pants/base:build_file',
'src/python/pants/base:build_file_parser',
'src/python/pants/base:target',
'src/python/pants/base:build_graph',
]
)

python_library(
name = 'pants_new',
dependencies = [
':goal_runner',
]
)

python_library(
name = 'goal_runner',
sources = ['goal_runner.py'],
dependencies = [
':command',
'3rdparty/python/twitter/commons:twitter.common.lang',
'3rdparty/python/twitter/commons:twitter.common.log',
'src/python/pants/backend/core/tasks:common',
'src/python/pants/backend/core/tasks:task',
'src/python/pants/backend/jvm/tasks:nailgun_task',
'src/python/pants/base:address',
'src/python/pants/base:build_environment',
'src/python/pants/base:build_file_address_mapper',
'src/python/pants/base:build_file_parser',
'src/python/pants/base:build_graph',
'src/python/pants/base:config',
'src/python/pants/base:cmd_line_spec_parser',
'src/python/pants/base:extension_loader',
'src/python/pants/base:target',
'src/python/pants/base:workunit',
'src/python/pants/engine',
'src/python/pants/goal',
'src/python/pants/goal:context',
'src/python/pants/goal:error',
'src/python/pants/goal:initialize_reporting',
'src/python/pants/goal',
'src/python/pants/goal:run_tracker',
'src/python/pants/option',
'src/python/pants/reporting',
'src/python/pants/util:dirutil',
Expand Down
Loading

0 comments on commit 6a30e22

Please sign in to comment.