common env variables

This commit is contained in:
Devaev Maxim 2020-10-03 03:34:55 +03:00
parent 872145590a
commit cccf44655a
14 changed files with 53 additions and 45 deletions

View File

@ -91,6 +91,8 @@ run: testenv $(TESTENV_GPIO)
--device $(TESTENV_VIDEO):$(TESTENV_VIDEO) \
--device $(TESTENV_GPIO):$(TESTENV_GPIO) \
--env KVMD_GPIO_DEVICE_PATH=$(TESTENV_GPIO) \
--env KVMD_SYSFS_PREFIX=/fake_sysfs \
--env KVMD_PROCFS_PREFIX=/fake_procfs \
$(if $(TESTENV_RELAY),--device $(TESTENV_RELAY):$(TESTENV_RELAY),) \
--publish 8080:80/tcp \
-it $(TESTENV_IMAGE) /bin/bash -c " \

View File

@ -20,7 +20,6 @@
# ========================================================================== #
import os
import asyncio
import asyncio.queues
import threading
@ -35,12 +34,6 @@ import gpiod
from . import aiotools
# =====
# XXX: Do not use this variable for any purpose other than testing.
# It can be removed at any time.
DEVICE_PATH = os.getenv("KVMD_GPIO_DEVICE_PATH", "/dev/gpiochip0")
# =====
async def pulse(line: gpiod.Line, delay: float, final: float) -> None:
try:

View File

@ -302,8 +302,6 @@ def _get_config_scheme() -> Dict:
"extras": Option("/usr/share/kvmd/extras", type=valid_abs_dir),
"hw": {
"vcgencmd_cmd": Option(["/opt/vc/bin/vcgencmd"], type=valid_command),
"procfs_prefix": Option("", type=tools.str_strip),
"sysfs_prefix": Option("", type=tools.str_strip),
"state_poll": Option(10.0, type=valid_float_f01),
},
},

View File

@ -33,6 +33,7 @@ import aiofiles
from ....logging import get_logger
from .... import env
from .... import aioproc
from .base import BaseInfoSubmanager
@ -47,14 +48,10 @@ class HwInfoSubmanager(BaseInfoSubmanager):
def __init__(
self,
vcgencmd_cmd: List[str],
procfs_prefix: str,
sysfs_prefix: str,
state_poll: float,
) -> None:
self.__vcgencmd_cmd = vcgencmd_cmd
self.__sysfs_prefix = sysfs_prefix
self.__procfs_prefix = procfs_prefix
self.__state_poll = state_poll
async def get_state(self) -> Dict:
@ -90,7 +87,7 @@ class HwInfoSubmanager(BaseInfoSubmanager):
# =====
async def __get_dt_model(self) -> Optional[str]:
model_path = f"{self.__procfs_prefix}/proc/device-tree/model"
model_path = f"{env.PROCFS_PREFIX}/proc/device-tree/model"
try:
async with aiofiles.open(model_path) as model_file:
return (await model_file.read()).strip(" \t\r\n\0")
@ -99,7 +96,7 @@ class HwInfoSubmanager(BaseInfoSubmanager):
return None
async def __get_cpu_temp(self) -> Optional[float]:
temp_path = f"{self.__sysfs_prefix}/sys/class/thermal/thermal_zone0/temp"
temp_path = f"{env.SYSFS_PREFIX}/sys/class/thermal/thermal_zone0/temp"
try:
async with aiofiles.open(temp_path) as temp_file:
return int((await temp_file.read()).strip()) / 1000

33
kvmd/env.py Normal file
View File

@ -0,0 +1,33 @@
# ========================================================================== #
# #
# KVMD - The main Pi-KVM daemon. #
# #
# Copyright (C) 2018 Maxim Devaev <mdevaev@gmail.com> #
# #
# This program is free software: you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation, either version 3 of the License, or #
# (at your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program. If not, see <https://www.gnu.org/licenses/>. #
# #
# ========================================================================== #
import os
# =====
# XXX: Don't use these variables for any purpose other than testing.
# It can be removed at any time.
GPIO_DEVICE_PATH = str(os.getenv("KVMD_GPIO_DEVICE_PATH", "/dev/gpiochip0")).strip()
SYSFS_PREFIX = str(os.getenv("KVMD_SYSFS_PREFIX", "")).strip()
PROCFS_PREFIX = str(os.getenv("KVMD_PROCFS_PREFIX", "")).strip()

View File

@ -28,6 +28,7 @@ import gpiod
from ...logging import get_logger
from ... import env
from ... import aiotools
from ... import aiogp
@ -76,7 +77,7 @@ class Plugin(BaseAtx): # pylint: disable=too-many-instance-attributes
self.__reset_switch_line: Optional[gpiod.Line] = None
self.__reader = aiogp.AioReader(
path=aiogp.DEVICE_PATH,
path=env.GPIO_DEVICE_PATH,
consumer="kvmd::atx-gpio::leds",
pins={
power_led_pin: aiogp.AioReaderPinParams(power_led_inverted, power_led_debounce),
@ -107,7 +108,7 @@ class Plugin(BaseAtx): # pylint: disable=too-many-instance-attributes
assert self.__power_switch_line is None
assert self.__reset_switch_line is None
self.__chip = gpiod.Chip(aiogp.DEVICE_PATH)
self.__chip = gpiod.Chip(env.GPIO_DEVICE_PATH)
self.__power_switch_line = self.__chip.get_line(self.__power_switch_pin)
self.__power_switch_line.request("kvmd::atx-gpio::power_switch", gpiod.LINE_REQ_DIR_OUT, default_vals=[0])

View File

@ -43,6 +43,7 @@ from ...logging import get_logger
from ...keyboard.mappings import KEYMAP
from ... import env
from ... import aiotools
from ... import aiomulti
from ... import aioproc
@ -168,7 +169,7 @@ class _Gpio:
if self.__reset_pin >= 0:
assert self.__chip is None
assert self.__reset_line is None
self.__chip = gpiod.Chip(aiogp.DEVICE_PATH)
self.__chip = gpiod.Chip(env.GPIO_DEVICE_PATH)
self.__reset_line = self.__chip.get_line(self.__reset_pin)
self.__reset_line.request("kvmd::hid-serial::reset", gpiod.LINE_REQ_DIR_OUT, default_vals=[0])

View File

@ -44,7 +44,6 @@ from ....yamlconf import Option
from ....validators.os import valid_abs_dir
from ....validators.os import valid_command
from .... import tools
from .... import aiotools
from .... import aiofs
@ -139,7 +138,6 @@ class Plugin(BaseMsd): # pylint: disable=too-many-instance-attributes
remount_cmd: List[str],
unlock_cmd: List[str],
sysfs_prefix: str,
gadget: str, # XXX: Not from options, see /kvmd/apps/kvmd/__init__.py for details
) -> None:
@ -150,7 +148,7 @@ class Plugin(BaseMsd): # pylint: disable=too-many-instance-attributes
self.__remount_cmd = remount_cmd
self.__unlock_cmd = unlock_cmd
self.__drive = Drive(sysfs_prefix, gadget, instance=0, lun=0)
self.__drive = Drive(gadget, instance=0, lun=0)
self.__new_file: Optional[aiofiles.base.AiofilesContextManager] = None
self.__new_file_written = 0
@ -170,7 +168,6 @@ class Plugin(BaseMsd): # pylint: disable=too-many-instance-attributes
"storage": Option("/var/lib/kvmd/msd", type=valid_abs_dir, unpack_as="storage_path"),
"remount_cmd": Option([*sudo, "/usr/bin/kvmd-helper-otgmsd-remount", "{mode}"], type=valid_command),
"unlock_cmd": Option([*sudo, "/usr/bin/kvmd-helper-otgmsd-unlock", "unlock"], type=valid_command),
"sysfs_prefix": Option("", type=tools.str_strip),
}
async def get_state(self) -> Dict:

View File

@ -23,6 +23,8 @@
import os
import errno
from .... import env
from .. import MsdOperationError
@ -34,9 +36,9 @@ class MsdDriveLockedError(MsdOperationError):
# =====
class Drive:
def __init__(self, prefix: str, gadget: str, instance: int, lun: int) -> None:
def __init__(self, gadget: str, instance: int, lun: int) -> None:
self.__path = os.path.join(
f"{prefix}/sys/kernel/config/usb_gadget",
f"{env.SYSFS_PREFIX}/sys/kernel/config/usb_gadget",
gadget,
f"functions/mass_storage.usb{instance}/lun.{lun}",
)

View File

@ -39,6 +39,7 @@ import gpiod
from ...logging import get_logger
from ... import env
from ... import aiotools
from ... import aiofs
from ... import aiogp
@ -172,7 +173,7 @@ class _Gpio:
assert self.__target_line is None
assert self.__reset_line is None
self.__chip = gpiod.Chip(aiogp.DEVICE_PATH)
self.__chip = gpiod.Chip(env.GPIO_DEVICE_PATH)
self.__target_line = self.__chip.get_line(self.__target_pin)
self.__target_line.request("kvmd::msd-relay::target", gpiod.LINE_REQ_DIR_OUT, default_vals=[0])

View File

@ -25,6 +25,7 @@ from typing import Optional
import gpiod
from ... import env
from ... import aiotools
from ... import aiogp
@ -58,13 +59,13 @@ class Plugin(BaseUserGpioDriver):
def prepare(self) -> None:
assert self.__reader is None
self.__reader = aiogp.AioReader(
path=aiogp.DEVICE_PATH,
path=env.GPIO_DEVICE_PATH,
consumer="kvmd::ugpio-gpio::inputs",
pins=self.__input_pins,
notifier=self._notifier,
)
self.__chip = gpiod.Chip(aiogp.DEVICE_PATH)
self.__chip = gpiod.Chip(env.GPIO_DEVICE_PATH)
for (pin, initial) in self.__output_pins.items():
line = self.__chip.get_line(pin)
line.request("kvmd::ugpio-gpio::outputs", gpiod.LINE_REQ_DIR_OUT, default_vals=[int(initial or False)])

View File

@ -28,12 +28,6 @@ from typing import List
from typing import Dict
from typing import Hashable
from typing import TypeVar
from typing import Any
# =====
def str_strip(arg: Any) -> str:
return str(arg).strip()
# =====

View File

@ -2,11 +2,6 @@ kvmd:
server:
unix_mode: 0666
info:
hw:
procfs_prefix: /fake_procfs
sysfs_prefix: /fake_sysfs
hid:
keyboard:
device: /dev/null
@ -17,7 +12,6 @@ kvmd:
msd:
remount_cmd: /bin/true
unlock_cmd: /bin/true
sysfs_prefix: /fake_sysfs
streamer:
desired_fps: 30

View File

@ -2,11 +2,6 @@ kvmd:
server:
unix_mode: 0666
info:
hw:
procfs_prefix: /fake_procfs
sysfs_prefix: /fake_sysfs
hid:
keyboard:
device: /dev/null
@ -17,7 +12,6 @@ kvmd:
msd:
remount_cmd: /bin/true
unlock_cmd: /bin/true
sysfs_prefix: /fake_sysfs
streamer:
cmd: