sapling/eden/fs/cli/logfile.py
Shannon Zhu 6ec7546cab Update pyre version for eden
Summary: Automatic upgrade to remove `version` override and silence errors.

Reviewed By: grievejia

Differential Revision: D21207287

fbshipit-source-id: ab82cf81690cb847bd130ff3448345f5a0ea19af
2020-04-23 12:09:39 -07:00

59 lines
1.5 KiB
Python

#!/usr/bin/env python3
# Copyright (c) Facebook, Inc. and its affiliates.
#
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2.
import asyncio
import contextlib
import io
import pathlib
import typing
_BinaryIO = typing.Union[typing.IO[bytes], io.BufferedIOBase]
@contextlib.contextmanager
def forward_log_file(
file_path: pathlib.Path, output_file: _BinaryIO
) -> typing.Iterator["LogForwarder"]:
with follow_log_file(file_path) as follower:
yield LogForwarder(follower=follower, output_file=output_file)
class LogForwarder:
__follower: "LogFollower"
__output_file: _BinaryIO
def __init__(self, follower: "LogFollower", output_file: _BinaryIO) -> None:
super().__init__()
self.__follower = follower
self.__output_file = output_file
def poll(self) -> None:
self.__output_file.write(self.__follower.poll())
self.__output_file.flush()
async def poll_forever_async(self) -> typing.NoReturn:
while True:
self.poll()
await asyncio.sleep(0.1)
@contextlib.contextmanager
def follow_log_file(file_path: pathlib.Path) -> typing.Iterator["LogFollower"]:
with open(file_path, "rb") as file:
yield LogFollower(file)
class LogFollower:
__file: _BinaryIO
def __init__(self, file: _BinaryIO) -> None:
super().__init__()
self.__file = file
def poll(self) -> bytes:
return self.__file.read()