sapling/eden/test_support/environment_variable.py
Matt Glazar c91b552ef5 Allow systemd opt-in via config
Summary:
Currently, users can opt into systemd for edenfs by setting EDEN_EXPERIMENTAL_SYSTEMD=1 every time they use Eden's CLI. Make it easier to opt in: use systemd if service.experimental_systemd is set in ~/.edenrc. Also allow opting out for a single command via EDEN_EXPERIMENTAL_SYSTEMD=0.

For users who don't set service.experimental_systemd or use EDEN_EXPERIMENTAL_SYSTEMD, this diff should not change behavior. (The EDEN_EXPERIMENTAL_SYSTEMD environment variable is still respected.)

Reviewed By: simpkins

Differential Revision: D13371186

fbshipit-source-id: d7b533ea3583f4b6c8a8f63c6a74eace2be7d783
2018-12-10 16:30:06 -08:00

52 lines
1.6 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 abc
import os
import typing
class EnvironmentVariableMixin(metaclass=abc.ABCMeta):
def set_environment_variable(self, name: str, value: str) -> None:
self.__add_cleanup_for_environment_variable(name)
os.environ[name] = value
def set_environment_variables(self, variables: typing.Mapping[str, str]) -> None:
for name, value in variables.items():
self.set_environment_variable(name, value)
def unset_environment_variable(self, name: str) -> None:
self.__add_cleanup_for_environment_variable(name)
self.__unset_environment_variable_with_cleanup(name)
def __add_cleanup_for_environment_variable(self, name: str) -> None:
old_value = os.getenv(name)
def restore() -> None:
if old_value is None:
self.__unset_environment_variable_with_cleanup(name)
else:
os.environ[name] = old_value
self.addCleanup(restore)
def __unset_environment_variable_with_cleanup(self, name: str) -> None:
try:
del os.environ[name]
except KeyError:
pass
def addCleanup(
self,
function: typing.Callable[..., typing.Any],
*args: typing.Any,
**kwargs: typing.Any
) -> None:
raise NotImplementedError()