Skip to content

Commit

Permalink
Merge pull request #99 from robotpy/misc
Browse files Browse the repository at this point in the history
Misc
  • Loading branch information
virtuald authored Jan 15, 2024
2 parents 25277e3 + 42935cc commit 55f74e8
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 85 deletions.
166 changes: 85 additions & 81 deletions robotpy_installer/cli_installer.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,34 @@ def _add_ssh_options(parser: argparse.ArgumentParser):
)


class _BasicInstallerCmd:
log_usage = True

def __init__(self, parser: argparse.ArgumentParser) -> None:
_add_ssh_options(parser)

@handle_cli_error
def run(
self,
project_path: pathlib.Path,
main_file: pathlib.Path,
ignore_image_version: bool,
robot: typing.Optional[str],
):
installer = RobotpyInstaller()
with installer.connect_to_robot(
project_path=project_path,
main_file=main_file,
robot_or_team=robot,
ignore_image_version=ignore_image_version,
log_usage=self.log_usage,
):
self.on_run(installer)

def on_run(self, installer: RobotpyInstaller):
raise NotImplementedError()


#
# installer cache
#
Expand Down Expand Up @@ -100,56 +128,22 @@ def run(self, use_certifi: bool):
installer.download_python(use_certifi)


class InstallerInstallPython:
class InstallerInstallPython(_BasicInstallerCmd):
"""
Installs Python on a RoboRIO
"""

def __init__(self, parser: argparse.ArgumentParser) -> None:
_add_ssh_options(parser)

@handle_cli_error
def run(
self,
project_path: pathlib.Path,
main_file: pathlib.Path,
ignore_image_version: bool,
robot: typing.Optional[str],
):
installer = RobotpyInstaller()
with installer.connect_to_robot(
project_path=project_path,
main_file=main_file,
robot_or_team=robot,
ignore_image_version=ignore_image_version,
):
installer.install_python()
def on_run(self, installer: RobotpyInstaller):
installer.install_python()


class InstallerUninstallPython:
class InstallerUninstallPython(_BasicInstallerCmd):
"""
Uninstall Python from a RoboRIO
"""

def __init__(self, parser: argparse.ArgumentParser) -> None:
_add_ssh_options(parser)

@handle_cli_error
def run(
self,
project_path: pathlib.Path,
main_file: pathlib.Path,
ignore_image_version: bool,
robot: typing.Optional[str],
):
installer = RobotpyInstaller()
with installer.connect_to_robot(
project_path=project_path,
main_file=main_file,
robot_or_team=robot,
ignore_image_version=ignore_image_version,
):
installer.uninstall_python()
def on_run(self, installer: RobotpyInstaller):
installer.uninstall_python()


class InstallerUninstallRobotPy:
Expand Down Expand Up @@ -185,31 +179,14 @@ def run(
installer.uninstall_robotpy()


class InstallerUninstallJavaCpp:
class InstallerUninstallJavaCpp(_BasicInstallerCmd):
"""
Uninstall FRC Java/C++ programs from a RoboRIO
"""

def __init__(self, parser: argparse.ArgumentParser) -> None:
_add_ssh_options(parser)

@handle_cli_error
def run(
self,
project_path: pathlib.Path,
main_file: pathlib.Path,
ignore_image_version: bool,
robot: typing.Optional[str],
):
installer = RobotpyInstaller()
with installer.connect_to_robot(
project_path=project_path,
main_file=main_file,
robot_or_team=robot,
ignore_image_version=ignore_image_version,
):
if not roborio_utils.uninstall_cpp_java_lvuser(installer.ssh):
roborio_utils.uninstall_cpp_java_admin(installer.ssh)
def on_run(self, installer: RobotpyInstaller):
if not roborio_utils.uninstall_cpp_java_lvuser(installer.ssh):
roborio_utils.uninstall_cpp_java_admin(installer.ssh)


#
Expand Down Expand Up @@ -329,31 +306,57 @@ def run(
)


class InstallerList:
class InstallerNiWebEnable(_BasicInstallerCmd):
"""
Enables the NI web server and starts it
"""

def on_run(self, installer: RobotpyInstaller):
installer.ssh.exec_bash(
"update-rc.d -f systemWebServer defaults",
"/etc/init.d/systemWebServer start",
check=True,
print_output=True,
)


class InstallerNiWebDisable(_BasicInstallerCmd):
"""
Stops the NI web server and disables it from starting
"""

def on_run(self, installer: RobotpyInstaller):
installer.ssh.exec_bash(
"/etc/init.d/systemWebServer stop",
"update-rc.d -f systemWebServer remove",
check=True,
print_output=True,
)


class InstallerNiWeb:
"""
Manipulates the NI web server
The NI web server on the RoboRIO takes up a lot of memory, and isn't
used for very much. Use these commands to enable or disable it.
"""

subcommands = [
("enable", InstallerNiWebEnable),
("disable", InstallerNiWebDisable),
]


class InstallerList(_BasicInstallerCmd):
"""
Lists Python packages present on RoboRIO
"""

def __init__(self, parser: argparse.ArgumentParser) -> None:
_add_ssh_options(parser)
log_usage = False

@handle_cli_error
def run(
self,
project_path: pathlib.Path,
main_file: pathlib.Path,
ignore_image_version: bool,
robot: typing.Optional[str],
):
installer = RobotpyInstaller()
with installer.connect_to_robot(
project_path=project_path,
main_file=main_file,
robot_or_team=robot,
ignore_image_version=ignore_image_version,
log_usage=False,
):
installer.pip_list()
def on_run(self, installer: RobotpyInstaller):
installer.pip_list()


class InstallerUninstall:
Expand Down Expand Up @@ -406,6 +409,7 @@ class Installer:
("install", InstallerInstall),
("install-python", InstallerInstallPython),
("list", InstallerList),
("niweb", InstallerNiWeb),
("uninstall", InstallerUninstall),
("uninstall-python", InstallerUninstallPython),
("uninstall-robotpy", InstallerUninstallRobotPy),
Expand Down
15 changes: 11 additions & 4 deletions robotpy_installer/installer.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
import logging
import pathlib
import re
import shlex
import subprocess
import sys
from urllib.parse import urlparse
import typing

from os.path import basename
from os.path import basename, exists

from .version import version as __version__
from . import roborio_utils
Expand Down Expand Up @@ -572,10 +573,16 @@ def pip_install(
requirements,
)

pip_args.extend(packages)
for package in packages:
if package.endswith(".whl") and exists(package):
fname = basename(package)
cache_server.add_mapping(f"/extra/{fname}", package)
pip_args.append(f"http://localhost:{cache_server.port}/extra/{fname}")
else:
pip_args.append(package)

try:
self.ssh.exec_cmd(" ".join(pip_args), check=True, print_output=True)
self.ssh.exec_cmd(shlex.join(pip_args), check=True, print_output=True)
except SshExecError as e:
raise PipInstallError(f"installing packages: {e}") from e

Expand Down Expand Up @@ -612,7 +619,7 @@ def pip_uninstall(
pip_args.extend(packages)

with catch_ssh_error("uninstalling packages"):
self.ssh.exec_cmd(" ".join(pip_args), check=True, print_output=True)
self.ssh.exec_cmd(shlex.join(pip_args), check=True, print_output=True)


def _make_ssl_context(use_certifi: bool):
Expand Down

0 comments on commit 55f74e8

Please sign in to comment.