sapling/eden/integration/lib/fake_edenfs.py
Matt Glazar 69515ab060 Improve Pyre workarounds for FindEXE
Summary:
A bug in Pyre causes the properties of FindEXE to have an incorrect type. We currently work around this bug by silencing type errors. Unfortunately, this might silence legitimate errors too.

Instead of silencing type errors, using `typing.cast` to tell Pyre the correct type. This should expose legitimate errors if they exist.

This diff should not change behavior.

Reviewed By: chadaustin

Differential Revision: D13709138

fbshipit-source-id: 55f47f47062a35911c6bbe03ffd7b02a90a5107f
2019-01-18 12:31:08 -08:00

95 lines
2.8 KiB
Python

#!/usr/bin/env python3
#
# Copyright (c) 2016-present, Facebook, Inc.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree. An additional grant
# of patent rights can be found in the PATENTS file in the same directory.
import contextlib
import os
import pathlib
import signal
import subprocess
import typing
import eden.thrift
from .find_executables import FindExe
class FakeEdenFS(typing.ContextManager[int]):
"""A running fake_edenfs process."""
@classmethod
def spawn(
cls,
eden_dir: pathlib.Path,
etc_eden_dir: pathlib.Path,
home_dir: pathlib.Path,
extra_arguments: typing.Optional[typing.Sequence[str]] = None,
) -> "FakeEdenFS":
command = [
typing.cast(str, FindExe.FAKE_EDENFS), # T38947910
"--configPath",
str(home_dir / ".edenrc"),
"--edenDir",
str(eden_dir),
"--etcEdenDir",
str(etc_eden_dir),
"--edenfs",
]
if extra_arguments:
command.extend(extra_arguments)
subprocess.check_call(command)
return cls.from_existing_process(eden_dir=eden_dir)
@classmethod
def spawn_via_cli(
cls,
eden_dir: pathlib.Path,
etc_eden_dir: pathlib.Path,
home_dir: pathlib.Path,
extra_arguments: typing.Optional[typing.Sequence[str]] = None,
) -> "FakeEdenFS":
command = [
typing.cast(str, FindExe.EDEN_CLI), # T38947910
"--config-dir",
str(eden_dir),
"--etc-eden-dir",
str(etc_eden_dir),
"--home-dir",
str(home_dir),
"start",
"--daemon-binary",
typing.cast(str, FindExe.FAKE_EDENFS), # T38947910
]
if extra_arguments:
command.append("--")
command.extend(extra_arguments)
subprocess.check_call(command)
return cls.from_existing_process(eden_dir=eden_dir)
@staticmethod
def from_existing_process(eden_dir: pathlib.Path) -> "FakeEdenFS":
edenfs_pid = int((eden_dir / "lock").read_text())
return FakeEdenFS(process_id=edenfs_pid)
def __init__(self, process_id: int) -> None:
super().__init__()
self.process_id = process_id
def __enter__(self) -> int:
return self.process_id
def __exit__(self, exc_type, exc_val, exc_tb):
with contextlib.suppress(ProcessLookupError):
os.kill(self.process_id, signal.SIGTERM)
return None
def get_fake_edenfs_argv(eden_dir: pathlib.Path) -> typing.List[str]:
with eden.thrift.create_thrift_client(str(eden_dir)) as client:
return client.getCommandLine().split("\0")