sapling/eden/fs/cli/daemon_util.py
Adam Simpkins 17a3c9124d update Python platform checks to use sys.platform
Summary:
Update out platform checks to use `sys.platform` instead of `os.name`

Python type checkers (mypy and Pyre) currently understand checks against
`sys.platform`, and know that code guarded by these checks is platform
specific.  They don't understand the `os.name` checks.

This code should behave the same, but allows type checkers to do a better job
checking this code.

Note that there is also `platform.system()`, but this has drawbacks that it
apparently does runtime-checks to determine the platform, whereas `sys.platform`
is baked-in at compile time.  Additionally, the typecheckers do not appear to
support checking based on `platform.system()` for now.  There is an open
feature request for this for mypy, but it is not implemented yet.

One caveat for `sys.platform` is that the results on Linux are not consistent
across Python versions: older versions of Python used to report `linux2` while
new versions report simply `linux`.

Reviewed By: chadaustin

Differential Revision: D20830149

fbshipit-source-id: d173e41f1ae84951a84b87e2dc05787fe8b01407
2020-04-03 16:52:32 -07:00

60 lines
2.0 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.
# pyre-strict
import os
import sys
from typing import Optional
class DaemonBinaryNotFound(Exception):
def __init__(self) -> None:
super().__init__("unable to find edenfs executable")
def find_daemon_binary(explicit_daemon_binary: Optional[str]) -> str:
if explicit_daemon_binary is not None:
return explicit_daemon_binary
daemon_binary = _find_default_daemon_binary()
if daemon_binary is None:
raise DaemonBinaryNotFound()
return daemon_binary
def _find_default_daemon_binary() -> Optional[str]:
# We search for the daemon executable relative to the edenfsctl CLI tool.
cli_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
# Check the normal release installation location first
if sys.platform != "win32":
# On non-Windows platforms, the edenfs binary is installed under
# <prefix>/libexec/eden/, while edenfsctl is in <prefix>/bin/
suffix = ""
candidate = os.path.normpath(os.path.join(cli_dir, "../libexec/eden/edenfs"))
else:
# On Windows, edenfs.exe is normally installed in the same directory as
# edenfsctl.exe
suffix = ".exe"
candidate = os.path.normpath(os.path.join(cli_dir, "edenfs.exe"))
permissions = os.R_OK | os.X_OK
if os.access(candidate, permissions):
return candidate
# This is where the binary will be found relative to this file when it is
# run out of buck-out in debug mode.
candidate = os.path.normpath(os.path.join(cli_dir, "../service/edenfs"))
if os.access(candidate, permissions):
return candidate
# This is where the binary will be found relative to this file when it is
# run out of a CMake-based build
candidate = os.path.normpath(os.path.join(cli_dir, "../edenfs" + suffix))
if os.access(candidate, permissions):
return candidate
return None