Skip to content

Commit

Permalink
Create a pants.init package.
Browse files Browse the repository at this point in the history
Provides some sanity to the dependencies of the init sequence:
previously they looped between packages, creating package-level,
and in some cases even target-level cycles.

Moves all code related to pants startup (options initialization,
plugin loading, daemon running etc.) to a single package.
Breaks up dependency cycles by passing a reference to
clean_global_runtime_state() into the PantsDaemon and a
reference to the pantsd launcher into the PantsDaemonKill
task (via the context).
  • Loading branch information
benjyw committed Mar 21, 2017
1 parent b7cd335 commit e773bd3
Show file tree
Hide file tree
Showing 32 changed files with 118 additions and 173 deletions.
62 changes: 1 addition & 61 deletions src/python/pants/bin/BUILD
Expand Up @@ -2,13 +2,9 @@
# Licensed under the Apache License, Version 2.0 (see LICENSE).

python_library(
sources=globs('*.py', exclude=['options_initializer.py',
'extension_loader.py',
'plugin_resolver.py']),
dependencies=[
'3rdparty/python/twitter/commons:twitter.common.collections',
'3rdparty/python:setproctitle',
':options_initializer',
'src/python/pants/backend/jvm/tasks:nailgun_task',
'src/python/pants/base:build_environment',
'src/python/pants/base:build_file',
Expand All @@ -35,8 +31,8 @@ python_library(
'src/python/pants/goal:context',
'src/python/pants/goal:run_tracker',
'src/python/pants/help',
'src/python/pants/init',
'src/python/pants/option',
'src/python/pants/pantsd/subsystem:pants_daemon_launcher',
'src/python/pants/reporting',
'src/python/pants/scm/subsystems:changed',
'src/python/pants/subsystem',
Expand All @@ -48,62 +44,6 @@ python_library(
],
)

python_library(
name='extension_loader',
sources=['extension_loader.py'],
dependencies=[
'3rdparty/python:setuptools',
'3rdparty/python/twitter/commons:twitter.common.collections',
'src/python/pants/base:exceptions',
'src/python/pants/build_graph:build_graph',
':plugins',
]
)

python_library(
name='plugin_resolver',
sources=['plugin_resolver.py'],
dependencies=[
'3rdparty/python:pex',
'3rdparty/python:setuptools',
'src/python/pants/option:option',
'src/python/pants/python',
'src/python/pants/subsystem',
'src/python/pants/util:dirutil',
'src/python/pants/util:memo',
'src/python/pants:version',
]
)

python_library(
name='options_initializer',
sources=['options_initializer.py'],
dependencies=[
'3rdparty/python:setuptools',
':extension_loader',
':plugin_resolver',
'src/python/pants/base:build_environment',
'src/python/pants/base:exceptions',
'src/python/pants/engine/legacy:change_calculator',
'src/python/pants/goal:goal',
'src/python/pants/logging:logging',
'src/python/pants/option:option',
'src/python/pants/subsystem:subsystem',
]
)

target(
name='plugins',
dependencies=[
'src/python/pants/backend/codegen:plugin',
'src/python/pants/backend/docgen:plugin',
'src/python/pants/backend/graph_info:plugin',
'src/python/pants/backend/jvm:plugin',
'src/python/pants/backend/project_info:plugin',
'src/python/pants/backend/python:plugin',
],
)

# This binary's entry_point is used by the pantsbuild.pants sdist to setup a binary for
# pip installers, ie: it is why this works to get `pants` on your PATH:
# $ pip install pantsbuild.pants
Expand Down
2 changes: 1 addition & 1 deletion src/python/pants/bin/daemon_pants_runner.py
Expand Up @@ -15,10 +15,10 @@

from pants.bin.exiter import Exiter
from pants.bin.local_pants_runner import LocalPantsRunner
from pants.init.util import clean_global_runtime_state
from pants.java.nailgun_io import NailgunStreamWriter
from pants.java.nailgun_protocol import ChunkType, NailgunProtocol
from pants.pantsd.process_manager import ProcessManager
from pants.pantsd.util import clean_global_runtime_state
from pants.util.contextutil import HardSystemExit, stdio_as


Expand Down
2 changes: 1 addition & 1 deletion src/python/pants/bin/engine_initializer.py
Expand Up @@ -10,7 +10,6 @@

from pants.base.build_environment import get_buildroot, get_scm
from pants.base.file_system_project_tree import FileSystemProjectTree
from pants.bin.options_initializer import OptionsInitializer
from pants.engine.build_files import create_graph_tasks
from pants.engine.engine import LocalSerialEngine
from pants.engine.fs import create_fs_tasks
Expand All @@ -26,6 +25,7 @@
from pants.engine.parser import SymbolTable
from pants.engine.scheduler import LocalScheduler
from pants.engine.subsystem.native import Native
from pants.init.options_initializer import OptionsInitializer
from pants.option.options_bootstrapper import OptionsBootstrapper
from pants.util.memo import memoized_method

Expand Down
18 changes: 9 additions & 9 deletions src/python/pants/bin/goal_runner.py
Expand Up @@ -13,7 +13,6 @@
from pants.base.workunit import WorkUnit, WorkUnitLabel
from pants.bin.engine_initializer import EngineInitializer
from pants.bin.repro import Reproducer
from pants.bin.target_roots import TargetRoots
from pants.build_graph.build_file_address_mapper import BuildFileAddressMapper
from pants.build_graph.build_file_parser import BuildFileParser
from pants.build_graph.mutable_build_graph import MutableBuildGraph
Expand All @@ -23,8 +22,9 @@
from pants.goal.goal import Goal
from pants.goal.run_tracker import RunTracker
from pants.help.help_printer import HelpPrinter
from pants.init.pants_daemon_launcher import PantsDaemonLauncher
from pants.init.target_roots import TargetRoots
from pants.java.nailgun_executor import NailgunProcessGroup
from pants.pantsd.subsystem.pants_daemon_launcher import PantsDaemonLauncher
from pants.reporting.reporting import Reporting
from pants.scm.subsystems.changed import Changed
from pants.source.source_root import SourceRootConfig
Expand Down Expand Up @@ -140,15 +140,14 @@ def generate_targets(specs):

return list(generate_targets(specs))

def _maybe_launch_pantsd(self):
def _maybe_launch_pantsd(self, pantsd_launcher):
"""Launches pantsd if configured to do so."""
if self._global_options.enable_pantsd:
# Avoid runtracker output if pantsd is disabled. Otherwise, show up to inform the user its on.
with self._run_tracker.new_workunit(name='pantsd', labels=[WorkUnitLabel.SETUP]):
pantsd_launcher = PantsDaemonLauncher.Factory.global_instance().create(EngineInitializer)
pantsd_launcher.maybe_launch()

def _setup_context(self):
def _setup_context(self, pantsd_launcher):
with self._run_tracker.new_workunit(name='setup', labels=[WorkUnitLabel.SETUP]):
self._build_graph, self._address_mapper, spec_roots = self._init_graph(
self._global_options.enable_v2_engine,
Expand Down Expand Up @@ -177,14 +176,15 @@ def _setup_context(self):
build_graph=self._build_graph,
build_file_parser=self._build_file_parser,
address_mapper=self._address_mapper,
invalidation_report=invalidation_report)
invalidation_report=invalidation_report,
pantsd_launcher=pantsd_launcher)
return goals, context

def setup(self):
self._maybe_launch_pantsd()
pantsd_launcher = PantsDaemonLauncher.Factory.global_instance().create(EngineInitializer)
self._maybe_launch_pantsd(pantsd_launcher)
self._handle_help(self._help_request)

goals, context = self._setup_context()
goals, context = self._setup_context(pantsd_launcher)
return GoalRunner(context=context,
goals=goals,
run_tracker=self._run_tracker,
Expand Down
2 changes: 1 addition & 1 deletion src/python/pants/bin/local_pants_runner.py
Expand Up @@ -7,9 +7,9 @@

from pants.base.build_environment import get_buildroot
from pants.bin.goal_runner import GoalRunner
from pants.bin.options_initializer import OptionsInitializer
from pants.bin.reporting_initializer import ReportingInitializer
from pants.bin.repro import Reproducer
from pants.init.options_initializer import OptionsInitializer
from pants.option.options_bootstrapper import OptionsBootstrapper
from pants.util.contextutil import hard_exit_handler, maybe_profiled

Expand Down
1 change: 0 additions & 1 deletion src/python/pants/core_tasks/BUILD
Expand Up @@ -19,7 +19,6 @@ python_library(
'src/python/pants/help',
'src/python/pants/option',
'src/python/pants/pantsd:process_manager',
'src/python/pants/pantsd/subsystem:pants_daemon_launcher',
'src/python/pants/reporting',
'src/python/pants/source',
'src/python/pants/task',
Expand Down
3 changes: 1 addition & 2 deletions src/python/pants/core_tasks/pantsd_kill.py
Expand Up @@ -7,7 +7,6 @@

from pants.base.exceptions import TaskError
from pants.pantsd.process_manager import ProcessManager
from pants.pantsd.subsystem.pants_daemon_launcher import PantsDaemonLauncher
from pants.task.task import Task


Expand All @@ -16,6 +15,6 @@ class PantsDaemonKill(Task):

def execute(self):
try:
PantsDaemonLauncher.Factory.global_instance().create().terminate()
self.context.pantsd_launcher.terminate()
except ProcessManager.NonResponsiveProcess as e:
raise TaskError('failure while terminating pantsd: {}'.format(e))
7 changes: 6 additions & 1 deletion src/python/pants/goal/context.py
Expand Up @@ -62,7 +62,7 @@ def fatal(self, *msg_elements):
def __init__(self, options, run_tracker, target_roots,
requested_goals=None, target_base=None, build_graph=None,
build_file_parser=None, address_mapper=None, console_outstream=None, scm=None,
workspace=None, invalidation_report=None):
workspace=None, invalidation_report=None, pantsd_launcher=None):
self._options = options
self.build_graph = build_graph
self.build_file_parser = build_file_parser
Expand All @@ -81,6 +81,7 @@ def __init__(self, options, run_tracker, target_roots,
self._workspace = workspace or (ScmWorkspace(self._scm) if self._scm else None)
self._replace_targets(target_roots)
self._invalidation_report = invalidation_report
self._pantsd_launcher = pantsd_launcher

@property
def options(self):
Expand Down Expand Up @@ -151,6 +152,10 @@ def workspace(self):
def invalidation_report(self):
return self._invalidation_report

@property
def pantsd_launcher(self):
return self._pantsd_launcher

def __str__(self):
ident = Target.identify(self.targets())
return 'Context(id:{}, targets:{})'.format(ident, self.targets())
Expand Down
45 changes: 45 additions & 0 deletions src/python/pants/init/BUILD
@@ -0,0 +1,45 @@
# Copyright 2017 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).


python_library(
dependencies=[
':plugins',
'3rdparty/python:pex',
'3rdparty/python:setuptools',
'3rdparty/python/twitter/commons:twitter.common.collections',
'src/python/pants:version',
'src/python/pants/base:build_environment',
'src/python/pants/base:exceptions',
'src/python/pants/build_graph',
'src/python/pants/core_tasks',
'src/python/pants/engine/legacy:change_calculator',
'src/python/pants/goal',
'src/python/pants/goal:run_tracker',
'src/python/pants/logging',
'src/python/pants/option',
'src/python/pants/pantsd:pants_daemon',
'src/python/pants/pantsd/service:fs_event_service',
'src/python/pants/pantsd/service:pailgun_service',
'src/python/pants/pantsd/service:scheduler_service',
'src/python/pants/pantsd/subsystem:subprocess',
'src/python/pants/pantsd/subsystem:watchman_launcher',
'src/python/pants/process',
'src/python/pants/python',
'src/python/pants/subsystem',
'src/python/pants/util:dirutil',
'src/python/pants/util:memo',
]
)

target(
name='plugins',
dependencies=[
'src/python/pants/backend/codegen:plugin',
'src/python/pants/backend/docgen:plugin',
'src/python/pants/backend/graph_info:plugin',
'src/python/pants/backend/jvm:plugin',
'src/python/pants/backend/project_info:plugin',
'src/python/pants/backend/python:plugin',
],
)
Empty file.
File renamed without changes.
Expand Up @@ -12,9 +12,9 @@

from pants.base.build_environment import pants_version
from pants.base.exceptions import BuildConfigurationError
from pants.bin.extension_loader import load_backends_and_plugins
from pants.bin.plugin_resolver import PluginResolver
from pants.goal.goal import Goal
from pants.init.extension_loader import load_backends_and_plugins
from pants.init.plugin_resolver import PluginResolver
from pants.logging.setup import setup_logging
from pants.option.global_options import GlobalOptionsRegistrar
from pants.subsystem.subsystem import Subsystem
Expand Down
Expand Up @@ -9,7 +9,8 @@
import os

from pants.base.build_environment import get_buildroot
from pants.bin.target_roots import TargetRoots
from pants.init.target_roots import TargetRoots
from pants.init.util import clean_global_runtime_state
from pants.pantsd.pants_daemon import PantsDaemon
from pants.pantsd.service.fs_event_service import FSEventService
from pants.pantsd.service.pailgun_service import PailgunService
Expand Down Expand Up @@ -106,7 +107,8 @@ def __init__(self,

@testable_memoized_property
def pantsd(self):
return PantsDaemon(self._build_root, self._pants_workdir, self._log_level, self._log_dir)
return PantsDaemon(self._build_root, self._pants_workdir, self._log_level, self._log_dir,
reset_func=clean_global_runtime_state)

@testable_memoized_property
def watchman_launcher(self):
Expand Down
File renamed without changes.
Expand Up @@ -12,7 +12,7 @@
from pants.base.build_environment import get_buildroot
from pants.base.cmd_line_spec_parser import CmdLineSpecParser
from pants.base.specs import SingleAddress
from pants.bin.options_initializer import OptionsInitializer
from pants.init.options_initializer import OptionsInitializer
from pants.option.options_bootstrapper import OptionsBootstrapper
from pants.scm.subsystems.changed import ChangedRequest

Expand Down Expand Up @@ -54,7 +54,8 @@ def create(cls, options=None, args=None, build_root=None, change_calculator=None
# Determine the literal target roots.
spec_roots = cls.parse_specs(options.target_specs, build_root)

# Determine `Changed` arguments directly from options to support pre-`Subsystem` initialization paths.
# Determine `Changed` arguments directly from options to support pre-`Subsystem`
# initialization paths.
changed_options = options.for_scope('changed')
changed_request = ChangedRequest.from_options(changed_options)

Expand Down
Expand Up @@ -5,10 +5,10 @@
from __future__ import (absolute_import, division, generators, nested_scopes, print_function,
unicode_literals, with_statement)

from pants.bin.options_initializer import OptionsInitializer
from pants.build_graph.intermediate_target_factory import IntermediateTargetFactoryBase
from pants.goal.goal import Goal
from pants.goal.run_tracker import RunTracker
from pants.init.options_initializer import OptionsInitializer
from pants.subsystem.subsystem import Subsystem


Expand Down
3 changes: 3 additions & 0 deletions src/python/pants/logging/setup.py
Expand Up @@ -14,6 +14,9 @@
from pants.util.dirutil import safe_mkdir


# TODO: Once pantsd had a separate launcher entry point, and so no longer needs to call this
# function, move this into the pants.init package, and remove the pants.logging package.

def setup_logging(level, console_stream=None, log_dir=None, scope=None, log_name=None):
"""Configures logging for a given scope, by default the global scope.
Expand Down
13 changes: 0 additions & 13 deletions src/python/pants/pantsd/BUILD
Expand Up @@ -49,18 +49,5 @@ python_library(
'3rdparty/python:setproctitle',
'src/python/pants/goal:run_tracker',
':process_manager',
':util'
]
)

python_library(
name = 'util',
sources = ['util.py'],
dependencies = [
'src/python/pants/bin:options_initializer',
'src/python/pants/build_graph',
'src/python/pants/goal:goal',
'src/python/pants/goal:run_tracker',
'src/python/pants/subsystem:subsystem',
]
)

0 comments on commit e773bd3

Please sign in to comment.