New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New-style BinaryTool Subsystems for isort and go distribution. #5523
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,54 +9,28 @@ | |
from collections import OrderedDict, namedtuple | ||
|
||
from pants.base.workunit import WorkUnit, WorkUnitLabel | ||
from pants.binaries.binary_util import BinaryUtil | ||
from pants.binaries.binary_tool import NativeTool | ||
from pants.fs.archive import TGZ | ||
from pants.subsystem.subsystem import Subsystem | ||
from pants.util.contextutil import temporary_dir | ||
from pants.util.memo import memoized_property | ||
from pants.util.process_handler import subprocess | ||
|
||
|
||
class GoDistribution(object): | ||
class GoDistribution(NativeTool): | ||
"""Represents a self-bootstrapping Go distribution.""" | ||
|
||
class Factory(Subsystem): | ||
options_scope = 'go-distribution' | ||
options_scope = 'go-distribution' | ||
name = 'go' | ||
default_version = '1.8.3' | ||
archive_type = 'tgz' | ||
|
||
@classmethod | ||
def subsystem_dependencies(cls): | ||
return (BinaryUtil.Factory,) | ||
|
||
@classmethod | ||
def register_options(cls, register): | ||
register('--supportdir', advanced=True, default='bin/go', | ||
help='Find the go distributions under this dir. Used as part of the path to lookup ' | ||
'the distribution with --binary-util-baseurls and --pants-bootstrapdir') | ||
register('--version', advanced=True, default='1.8.3', fingerprint=True, | ||
help='Go distribution version. Used as part of the path to lookup the distribution ' | ||
'with --binary-util-baseurls and --pants-bootstrapdir') | ||
|
||
def create(self): | ||
# NB: create is an instance method to allow the user to choose global or scoped. | ||
# It's not unreasonable to imagine multiple go versions in play; for example: when | ||
# transitioning from the 1.x series to the 2.x series. | ||
binary_util = BinaryUtil.Factory.create() | ||
options = self.get_options() | ||
return GoDistribution(binary_util, options.supportdir, options.version) | ||
|
||
def __init__(self, binary_util, relpath, version): | ||
self._binary_util = binary_util | ||
self._relpath = relpath | ||
self._version = version | ||
|
||
@property | ||
def version(self): | ||
"""Returns the version of the Go distribution. | ||
|
||
:returns: The Go distribution version number string. | ||
:rtype: string | ||
""" | ||
return self._version | ||
@classmethod | ||
def register_options(cls, register): | ||
super(GoDistribution, cls).register_options(register) | ||
register('--supportdir', advanced=True, default='bin/go', | ||
removal_version='1.7.0.dev0', removal_hint='No longer supported.', | ||
help='Find the go distributions under this dir. Used as part of the path to lookup ' | ||
'the distribution with --binary-util-baseurls and --pants-bootstrapdir') | ||
|
||
@memoized_property | ||
def goroot(self): | ||
|
@@ -65,7 +39,7 @@ def goroot(self): | |
:returns: The Go distribution $GOROOT. | ||
:rtype: string | ||
""" | ||
go_distribution = self._binary_util.select_binary(self._relpath, self.version, 'go.tar.gz') | ||
go_distribution = self.select() | ||
distribution_workdir = os.path.dirname(go_distribution) | ||
outdir = os.path.join(distribution_workdir, 'unpacked') | ||
if not os.path.exists(outdir): | ||
|
@@ -89,14 +63,14 @@ class GoCommand(namedtuple('GoCommand', ['cmdline', 'env'])): | |
"""Encapsulates a go command that can be executed.""" | ||
|
||
@classmethod | ||
def _create(cls, goroot, cmd, go_env, args=None): | ||
def create(cls, goroot, cmd, go_env, args=None): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not a big objection, but it's not used publically fwict - perhaps you have plans? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No plans, I think my IDE didn't like accessing a private member of another class. But I guess it's fine if the class is a member of the same class as the code is in. I changed it back. |
||
return cls([os.path.join(goroot, 'bin', 'go'), cmd] + (args or []), env=go_env) | ||
|
||
def spawn(self, env=None, **kwargs): | ||
""" | ||
:param dict env: A custom environment to launch the Go command in. If `None` the current | ||
environment is used. | ||
:param **kwargs: Keyword arguments to pass through to `subprocess.Popen`. | ||
:param kwargs: Keyword arguments to pass through to `subprocess.Popen`. | ||
:returns: A handle to the spawned go command subprocess. | ||
:rtype: :class:`subprocess.Popen` | ||
""" | ||
|
@@ -109,7 +83,7 @@ def check_output(self, env=None, **kwargs): | |
|
||
:param dict env: A custom environment to launch the Go command in. If `None` the current | ||
environment is used. | ||
:param **kwargs: Keyword arguments to pass through to `subprocess.check_output`. | ||
:param kwargs: Keyword arguments to pass through to `subprocess.check_output`. | ||
:return str: Output of Go command. | ||
:raises subprocess.CalledProcessError: Raises if Go command fails. | ||
""" | ||
|
@@ -132,7 +106,7 @@ def create_go_cmd(self, cmd, gopath=None, args=None): | |
:returns: A go command that can be executed later. | ||
:rtype: :class:`GoDistribution.GoCommand` | ||
""" | ||
return self.GoCommand._create(self.goroot, cmd, go_env=self.go_env(gopath=gopath), args=args) | ||
return self.GoCommand.create(self.goroot, cmd, go_env=self.go_env(gopath=gopath), args=args) | ||
|
||
def execute_go_cmd(self, cmd, gopath=None, args=None, env=None, | ||
workunit_factory=None, workunit_name=None, workunit_labels=None, **kwargs): | ||
|
@@ -149,11 +123,11 @@ def execute_go_cmd(self, cmd, gopath=None, args=None, env=None, | |
:param workunit_factory: An optional callable that can produce a `WorkUnit` context | ||
:param string workunit_name: An optional name for the work unit; defaults to the `cmd` | ||
:param list workunit_labels: An optional sequence of labels for the work unit. | ||
:param **kwargs: Keyword arguments to pass through to `subprocess.Popen`. | ||
:param kwargs: Keyword arguments to pass through to `subprocess.Popen`. | ||
:returns: A tuple of the exit code and the go command that was run. | ||
:rtype: (int, :class:`GoDistribution.GoCommand`) | ||
""" | ||
go_cmd = self.GoCommand._create(self.goroot, cmd, go_env=self.go_env(gopath=gopath), args=args) | ||
go_cmd = self.GoCommand.create(self.goroot, cmd, go_env=self.go_env(gopath=gopath), args=args) | ||
if workunit_factory is None: | ||
return go_cmd.spawn(**kwargs).wait() | ||
else: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# coding=utf-8 | ||
# Copyright 2018 Pants project contributors (see CONTRIBUTORS.md). | ||
# Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
|
||
from __future__ import (absolute_import, division, generators, nested_scopes, print_function, | ||
unicode_literals, with_statement) | ||
|
||
from pants.binaries.binary_tool import Script | ||
|
||
|
||
class Isort(Script): | ||
options_scope = 'isort' | ||
default_version = '4.2.5' | ||
suffix = '.pex' | ||
|
||
replaces_scope = 'fmt.isort' | ||
replaces_name = 'version' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like maybe this bit is naive in the face of
.tgz
vs.tar.gz
: https://github.com/pantsbuild/pants/blob/master/src/python/pants/binaries/binary_util.py#L180There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I should have linked the CI test failure - this was looking like a bug in the underlying extraction process.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, did not read the backtrace closely. The extraction blowup is in GoDistribution code using TGZ directly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, took me a moment to figure it out, but the extension thing doesn't matter - it's just how we generate a name for the dir so it doesn't collide with the name of the downloaded file. The issue was I forgot to remove the unpacking code in GoDistribution, since BinaryTool now does this for us...