Skip to content

Commit

Permalink
Merge 83434c7 into 96f4af5
Browse files Browse the repository at this point in the history
  • Loading branch information
cosmicexplorer committed Jan 22, 2019
2 parents 96f4af5 + 83434c7 commit 2eac28c
Show file tree
Hide file tree
Showing 26 changed files with 121 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
from pants.reporting.reporting_utils import items_to_report_element
from pants.util.contextutil import Timer
from pants.util.dirutil import (fast_relpath, fast_relpath_optional, maybe_read_file,
safe_file_dump, safe_mkdir)
safe_file_write, safe_mkdir)
from pants.util.memo import memoized_property


Expand All @@ -60,9 +60,11 @@ def stdout_contents(wu):
return f.read().rstrip()


def dump_digest(output_dir, digest):
safe_file_dump('{}.digest'.format(output_dir),
'{}:{}'.format(digest.fingerprint, digest.serialized_bytes_length), mode='w')
def write_digest(output_dir, digest):
safe_file_write(
'{}.digest'.format(output_dir),
mode='w',
payload='{}:{}'.format(digest.fingerprint, digest.serialized_bytes_length))


def load_digest(output_dir):
Expand Down Expand Up @@ -823,7 +825,7 @@ def _runtool_hermetic(self, main, tool_name, args, distribution, tgt=None, input
raise TaskError(res.stderr)

if output_dir:
dump_digest(output_dir, res.output_directory_digest)
write_digest(output_dir, res.output_directory_digest)
self.context._scheduler.materialize_directories((
DirectoryToMaterialize(
# NB the first element here is the root to materialize into, not the dir to snapshot
Expand Down
4 changes: 2 additions & 2 deletions src/python/pants/backend/native/tasks/conan_fetch.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from pants.base.exceptions import TaskError
from pants.base.workunit import WorkUnitLabel
from pants.task.simple_codegen_task import SimpleCodegenTask
from pants.util.dirutil import mergetree, safe_concurrent_creation, safe_file_dump
from pants.util.dirutil import mergetree, safe_concurrent_creation, safe_file_write
from pants.util.memo import memoized_property


Expand Down Expand Up @@ -73,7 +73,7 @@ def _conan_user_home(self):
url=url,
is_ssl=re.match(r'^https://', url) is not None)
for name, url in self.get_options().conan_remotes.items()))
safe_file_dump(remotes_path, remotes_description, binary_mode=False)
safe_file_write(remotes_path, remotes_description)
return user_home

@memoized_property
Expand Down
4 changes: 2 additions & 2 deletions src/python/pants/goal/run_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
from pants.reporting.report import Report
from pants.stats.statsdb import StatsDBFactory
from pants.subsystem.subsystem import Subsystem
from pants.util.dirutil import relative_symlink, safe_file_dump
from pants.util.dirutil import relative_symlink, safe_file_write


class RunTracker(Subsystem):
Expand Down Expand Up @@ -401,7 +401,7 @@ def store_stats(self):
stats_file = os.path.join(get_pants_cachedir(), 'stats',
'{}.json'.format(self.run_info.get_info('id')))
mode = 'w' if PY3 else 'wb'
safe_file_dump(stats_file, json.dumps(stats), mode=mode)
safe_file_write(stats_file, json.dumps(stats), mode=mode)

# Add to local stats db.
StatsDBFactory.global_instance().get_db().insert_stats(stats)
Expand Down
6 changes: 3 additions & 3 deletions src/python/pants/java/nailgun_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from pants.java.executor import Executor, SubprocessExecutor
from pants.java.nailgun_client import NailgunClient
from pants.pantsd.process_manager import FingerprintedProcessManager, ProcessGroup
from pants.util.dirutil import read_file, safe_file_dump, safe_open
from pants.util.dirutil import read_file, safe_file_write, safe_open


logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -228,8 +228,8 @@ def ensure_connectable(self, nailgun):
def _spawn_nailgun_server(self, fingerprint, jvm_options, classpath, stdout, stderr, stdin):
"""Synchronously spawn a new nailgun server."""
# Truncate the nailguns stdout & stderr.
safe_file_dump(self._ng_stdout, b'')
safe_file_dump(self._ng_stderr, b'')
safe_file_write(self._ng_stdout, b'', mode='wb')
safe_file_write(self._ng_stderr, b'', mode='wb')

jvm_options = jvm_options + [self._PANTS_NG_BUILDROOT_ARG,
self._create_owner_arg(self._workdir),
Expand Down
2 changes: 1 addition & 1 deletion src/python/pants/option/options_bootstrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def create(cls, env=None, args=None):
short_flags = set()

def filecontent_for(path):
return FileContent(ensure_text(path), read_file(path))
return FileContent(ensure_text(path), read_file(path, binary_mode=True))

def capture_the_flags(*args, **kwargs):
for arg in args:
Expand Down
4 changes: 2 additions & 2 deletions src/python/pants/pantsd/process_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from pants.base.build_environment import get_buildroot
from pants.process.lock import OwnerPrintingInterProcessFileLock
from pants.process.subprocess import Subprocess
from pants.util.dirutil import read_file, rm_rf, safe_file_dump, safe_mkdir
from pants.util.dirutil import read_file, rm_rf, safe_file_write, safe_mkdir
from pants.util.memo import memoized_property
from pants.util.process_handler import subprocess

Expand Down Expand Up @@ -191,7 +191,7 @@ def write_metadata_by_name(self, name, metadata_key, metadata_value):
"""
self._maybe_init_metadata_dir_by_name(name)
file_path = self._metadata_file_path(name, metadata_key)
safe_file_dump(file_path, metadata_value, binary_mode=False)
safe_file_write(file_path, metadata_value)

def await_metadata_by_name(self, name, metadata_key, timeout, caster=None):
"""Block up to a timeout for process metadata to arrive on disk.
Expand Down
4 changes: 2 additions & 2 deletions src/python/pants/pantsd/watchman.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

from pants.pantsd.process_manager import ProcessManager
from pants.pantsd.watchman_client import StreamableWatchmanClient
from pants.util.dirutil import safe_file_dump, safe_mkdir
from pants.util.dirutil import safe_file_write, safe_mkdir
from pants.util.retry import retry_on_exception


Expand Down Expand Up @@ -82,7 +82,7 @@ def _normalize_watchman_path(self, watchman_path):
def _maybe_init_metadata(self):
safe_mkdir(self._watchman_work_dir)
# Initialize watchman with an empty, but valid statefile so it doesn't complain on startup.
safe_file_dump(self._state_file, b'{}')
safe_file_write(self._state_file, b'{}', mode='wb')

def _construct_cmd(self, cmd_parts, state_file, sock_file, pid_file, log_file, log_level):
return [part for part in cmd_parts] + ['--no-save-state',
Expand Down
6 changes: 3 additions & 3 deletions src/python/pants/releases/reversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from builtins import open, str

from pants.util.contextutil import open_zip, temporary_dir
from pants.util.dirutil import read_file, safe_file_dump
from pants.util.dirutil import read_file, safe_file_write


def replace_in_file(workspace, src_file_path, from_str, to_str):
Expand All @@ -30,7 +30,7 @@ def replace_in_file(workspace, src_file_path, from_str, to_str):
return None

dst_file_path = src_file_path.replace(from_str, to_str)
safe_file_dump(os.path.join(workspace, dst_file_path), data.replace(from_bytes, to_bytes))
safe_file_write(os.path.join(workspace, dst_file_path), data.replace(from_bytes, to_bytes), mode='wb')
if src_file_path != dst_file_path:
os.unlink(os.path.join(workspace, src_file_path))
return dst_file_path
Expand Down Expand Up @@ -88,7 +88,7 @@ def rewrite_record_file(workspace, src_record_file, mutated_file_tuples):
output_line = line
output_records.append(output_line)

safe_file_dump(file_name, '\r\n'.join(output_records) + '\r\n', binary_mode=False)
safe_file_write(file_name, '\r\n'.join(output_records) + '\r\n')


# The wheel METADATA file will contain a line like: `Version: 1.11.0.dev3+7951ec01`.
Expand Down
1 change: 1 addition & 0 deletions src/python/pants/util/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ python_library(
dependencies = [
':strutil',
'3rdparty/python:future',
'src/python/pants/base:deprecated',
],
)

Expand Down
45 changes: 43 additions & 2 deletions src/python/pants/util/dirutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from collections import defaultdict
from contextlib import contextmanager

from pants.base.deprecated import deprecated, deprecated_conditional
from pants.util.strutil import ensure_text


Expand Down Expand Up @@ -100,6 +101,9 @@ def safe_mkdir_for_all(paths):
created_dirs.add(dir_to_make)


@deprecated(
'1.16.0.dev2',
hint_message='Use safe_file_write() instead. It removes the deprecated `binary_mode` argument to instead only allow `mode`. It also defaults to writing unicode, rather than defaulting to bytes.')
def safe_file_dump(filename, payload, binary_mode=None, mode=None):
"""Write a string to a file.
Expand All @@ -125,32 +129,69 @@ def safe_file_dump(filename, payload, binary_mode=None, mode=None):
else:
mode = 'wb'

safe_file_write(filename, payload=payload, mode=mode)


# TODO(#6742): payload should be Union[str, bytes] in type hint syntax, but from
# https://pythonhosted.org/an_example_pypi_project/sphinx.html#full-code-example it doesn't appear
# that is possible to represent in docstring type syntax.
def safe_file_write(filename, payload='', mode='w'):
"""Write a string to a file.
This method is "safe" to the extent that `safe_open` is "safe". See the explanation on the method
doc there.
When `payload` is an empty string (the default), this method can be used as a concise way to
create an empty file along with its containing directory (or truncate it if it already exists).
:param string filename: The filename of the file to write to.
:param string payload: The string to write to the file.
:param string mode: A mode argument for the python `open` builtin.
"""
with safe_open(filename, mode=mode) as f:
f.write(payload)


def maybe_read_file(filename, binary_mode=True):
def maybe_read_file(filename, binary_mode=None):
"""Read and return the contents of a file in a single file.read().
:param string filename: The filename of the file to read.
:param bool binary_mode: Read from file as bytes or unicode.
:returns: The contents of the file, or opening the file fails for any reason
:rtype: string
"""
# TODO(#7121): Default binary_mode=False after the python 3 switchover!
deprecated_conditional(
lambda: binary_mode is None,
removal_version='1.16.0.dev2',
entity_description='Not specifying binary_mode explicitly in maybe_read_file()',
hint_message='Function will default to unicode when pants migrates to python 3!')
if binary_mode is None:
binary_mode = True

try:
return read_file(filename, binary_mode=binary_mode)
except IOError:
return None


def read_file(filename, binary_mode=True):
def read_file(filename, binary_mode=None):
"""Read and return the contents of a file in a single file.read().
:param string filename: The filename of the file to read.
:param bool binary_mode: Read from file as bytes or unicode.
:returns: The contents of the file.
:rtype: string
"""
# TODO(#7121): Default binary_mode=False after the python 3 switchover!
deprecated_conditional(
lambda: binary_mode is None,
removal_version='1.16.0.dev2',
entity_description='Not specifying binary_mode explicitly in read_file()',
hint_message='Function will default to unicode when pants migrates to python 3!')
if binary_mode is None:
binary_mode = True

mode = 'rb' if binary_mode else 'r'
with open(filename, mode) as f:
return f.read()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

from pants.base.build_environment import get_buildroot
from pants.util.contextutil import temporary_dir
from pants.util.dirutil import safe_file_dump
from pants.util.dirutil import safe_file_write
from pants_test.pants_run_integration_test import PantsRunIntegrationTest


Expand All @@ -37,7 +37,7 @@ def tmp_custom_scala(self, path_suffix):
def tmp_scalastyle_config(self):
with temporary_dir(root_dir=get_buildroot()) as scalastyle_dir:
path = os.path.join(scalastyle_dir, 'config.xml')
safe_file_dump(path, '''<scalastyle/>''', binary_mode=False)
safe_file_write(path, '''<scalastyle/>''')
yield '--lint-scalastyle-config={}'.format(path)

def pants_run(self, options=None):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from pants.build_graph.resources import Resources
from pants.java.jar.jar_dependency import JarDependency
from pants.util.contextutil import open_zip
from pants.util.dirutil import safe_file_dump, safe_mkdir, safe_mkdtemp
from pants.util.dirutil import safe_file_write, safe_mkdir, safe_mkdtemp
from pants_test.backend.jvm.tasks.jvm_binary_task_test_base import JvmBinaryTaskTestBase


Expand All @@ -38,7 +38,7 @@ def add_consolidated_bundle(self, context, tgt, files_dict):
entry_path = safe_mkdtemp(dir=target_dir)
classpath_dir = safe_mkdtemp(dir=target_dir)
for rel_path, content in files_dict.items():
safe_file_dump(os.path.join(entry_path, rel_path), content, binary_mode=False)
safe_file_write(os.path.join(entry_path, rel_path), content)

# Create Jar to mimic consolidate classpath behavior.
jarpath = os.path.join(classpath_dir, 'output-0.jar')
Expand Down Expand Up @@ -71,12 +71,12 @@ def setUp(self):
JarDependency(org='org.gnu', name='gary', rev='4.0.0',
ext='tar.gz')])

safe_file_dump(os.path.join(self.build_root, 'resources/foo/file'), '// dummy content', binary_mode=False)
safe_file_write(os.path.join(self.build_root, 'resources/foo/file'), '// dummy content')
self.resources_target = self.make_target('//resources:foo-resources', Resources,
sources=['foo/file'])

# This is so that payload fingerprint can be computed.
safe_file_dump(os.path.join(self.build_root, 'foo/Foo.java'), '// dummy content', binary_mode=False)
safe_file_write(os.path.join(self.build_root, 'foo/Foo.java'), '// dummy content')
self.java_lib_target = self.make_target('//foo:foo-library', JavaLibrary, sources=['Foo.java'])

self.binary_target = self.make_target(spec='//foo:foo-binary',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from pants.backend.jvm.tasks.consolidate_classpath import ConsolidateClasspath
from pants.build_graph.resources import Resources
from pants.java.jar.jar_dependency import JarDependency
from pants.util.dirutil import safe_file_dump
from pants.util.dirutil import safe_file_write
from pants_test.backend.jvm.tasks.jvm_binary_task_test_base import JvmBinaryTaskTestBase


Expand Down Expand Up @@ -48,12 +48,12 @@ def setUp(self):
JarDependency(org='org.gnu', name='gary', rev='4.0.0',
ext='tar.gz')])

safe_file_dump(os.path.join(self.build_root, 'resources/foo/file'), '// dummy content', binary_mode=False)
safe_file_write(os.path.join(self.build_root, 'resources/foo/file'), '// dummy content')
self.resources_target = self.make_target('//resources:foo-resources', Resources,
sources=['foo/file'])

# This is so that payload fingerprint can be computed.
safe_file_dump(os.path.join(self.build_root, 'foo/Foo.java'), '// dummy content', binary_mode=False)
safe_file_write(os.path.join(self.build_root, 'foo/Foo.java'), '// dummy content')
self.java_lib_target = self.make_target('//foo:foo-library', JavaLibrary, sources=['Foo.java'])

self.binary_target = self.make_target(spec='//foo:foo-binary',
Expand Down
4 changes: 2 additions & 2 deletions tests/python/pants_test/backend/jvm/tasks/test_junit_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from pants.java.distribution.distribution import DistributionLocator
from pants.java.executor import SubprocessExecutor
from pants.util.contextutil import environment_as, temporary_dir
from pants.util.dirutil import safe_file_dump, touch
from pants.util.dirutil import safe_file_write, touch
from pants.util.process_handler import subprocess
from pants_test.jvm.jvm_tool_task_test_base import JvmToolTaskTestBase
from pants_test.subsystem.subsystem_util import global_subsystem_instance, init_subsystem
Expand Down Expand Up @@ -218,7 +218,7 @@ def test_request_classes_by_source(self):

# Existing files (with and without the method name) should trigger.
srcfile = os.path.join(self.test_workdir, 'this.is.a.source.file.scala')
safe_file_dump(srcfile, 'content!', binary_mode=False)
safe_file_write(srcfile, 'content!')
self.assertTrue(JUnitRun.request_classes_by_source([srcfile]))
self.assertTrue(JUnitRun.request_classes_by_source(['{}#method'.format(srcfile)]))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from pants.option.scope import GLOBAL_SCOPE_CONFIG_SECTION
from pants.util.collections import assert_single_element
from pants.util.contextutil import temporary_dir
from pants.util.dirutil import is_executable, read_file, safe_file_dump
from pants.util.dirutil import is_executable, read_file, safe_file_write
from pants.util.process_handler import subprocess
from pants_test.backend.python.tasks.python_task_test_base import name_and_platform
from pants_test.pants_run_integration_test import PantsRunIntegrationTest
Expand Down Expand Up @@ -186,7 +186,7 @@ def _assert_ctypes_interop_with_mock_buildroot(self, toolchain_variant):
orig_wrapped_math_build = read_file(self._wrapped_math_build_file, binary_mode=False)
without_strict_deps_wrapped_math_build = re.sub(
'strict_deps=False,', '', orig_wrapped_math_build)
safe_file_dump(self._wrapped_math_build_file, without_strict_deps_wrapped_math_build, mode='w')
safe_file_write(self._wrapped_math_build_file, without_strict_deps_wrapped_math_build)

# This should fail because it does not turn on strict_deps for a target which requires it.
pants_binary_strict_deps_failure = self.run_pants_with_workdir(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from pants.base.build_environment import get_buildroot
from pants.base.exception_sink import ExceptionSink
from pants.util.contextutil import temporary_dir
from pants.util.dirutil import read_file, safe_file_dump, safe_mkdir, touch
from pants.util.dirutil import read_file, safe_file_write, safe_mkdir, touch
from pants_test.pants_run_integration_test import PantsRunIntegrationTest
from pants_test.testutils.py2_compat import assertRegex

Expand Down Expand Up @@ -197,7 +197,7 @@ def test_reset_interactive_output_stream(self):

with temporary_dir() as tmpdir:
some_file = os.path.join(tmpdir, 'some_file')
safe_file_dump(some_file, b'', binary_mode=True)
safe_file_write(some_file, b'', mode='wb')
redirected_pants_run = self.run_pants([
"--lifecycle-stubs-new-interactive-stream-output-file={}".format(some_file),
] + lifecycle_stub_cmdline)
Expand Down

0 comments on commit 2eac28c

Please sign in to comment.