Skip to content
This repository was archived by the owner on Nov 13, 2023. It is now read-only.

Misc #710

Merged
merged 2 commits into from
Apr 6, 2022
Merged

Misc #710

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion gen/Filesystem.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
---

functions:
GetLaunchDirectory:
GetOperatingDirectory:
GetDeployDirectory:
GetOperatingDirectoryFs:
ignore: true
GetDeployDirectoryFs:
ignore: true
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ extension = "_wpilib"
sources = [
"wpilib/src/main.cpp",
"wpilib/src/rpy/ControlWord.cpp",
"wpilib/src/rpy/Filesystem.cpp",
"wpilib/src/rpy/Notifier.cpp",
"wpilib/src/rpy/SmartDashboardData.cpp",
"wpilib/src/rpy/MotorControllerGroup.cpp",
Expand Down Expand Up @@ -91,6 +92,7 @@ DutyCycleEncoder = "frc/DutyCycleEncoder.h"
Encoder = "frc/Encoder.h"
Errors = "frc/Errors.h"
# Filesystem = "frc/Filesystem.h"
Filesystem = "rpy/Filesystem.h"
# GenericHID = "frc/GenericHID.h" # interfaces
I2C = "frc/I2C.h"
IterativeRobotBase = "frc/IterativeRobotBase.h"
Expand Down
4 changes: 4 additions & 0 deletions wpilib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,9 @@
Watchdog,
XboxController,
getCurrentThreadPriority,
getDeployDirectory,
getErrorMessage,
getOperatingDirectory,
getTime,
setCurrentThreadPriority,
wait,
Expand Down Expand Up @@ -200,7 +202,9 @@
"Watchdog",
"XboxController",
"getCurrentThreadPriority",
"getDeployDirectory",
"getErrorMessage",
"getOperatingDirectory",
"getTime",
"setCurrentThreadPriority",
"wait",
Expand Down
2 changes: 1 addition & 1 deletion wpilib/deployinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def getDeployData() -> typing.Optional[typing.Dict[str, str]]:

:returns: None in simulation, or a dictionary
"""
if RobotBase.isReal():
if not RobotBase.isReal():
return None

try:
Expand Down
33 changes: 33 additions & 0 deletions wpilib/src/rpy/Filesystem.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

#pragma once

#include <wpi/fs.h>

namespace robotpy::filesystem {

/**
* Obtains the operating directory of the program. On the roboRIO, this
* is /home/lvuser/py. In simulation, it is the location of robot.py
*
* @return The result of the operating directory lookup.
*/
std::string GetOperatingDirectory();

/**
* Obtains the deploy directory of the program, which is the remote location
* the deploy directory is deployed to by default. On the roboRIO, this is
* /home/lvuser/py/deploy. In simulation, it is where the simulation was launched
* from, in the subdirectory "deploy" (`dirname(robot.py)`/deploy).
*
* @return The result of the operating directory lookup
*/
std::string GetDeployDirectory();

// intended to be used by C++ bindings, returns same as GetOperatingDirectory
fs::path GetOperatingDirectoryFs();
// intended to be used by C++ bindings, returns same as GetDeployDirectory
fs::path GetDeployDirectoryFs();

} // namespace robotpy::filesystem

#include "Filesystem.inc"
45 changes: 45 additions & 0 deletions wpilib/src/rpy/Filesystem.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@

// TODO: this should be in a shared library, but robotpy-build does not support that

#include <pybind11/eval.h>
#include <robotpy_build.h>

namespace robotpy::filesystem {

static fs::path getMainPath() {
py::gil_scoped_acquire gil;
py::dict locals;
py::exec(R"(
import sys, os.path
main = sys.modules['__main__'];
if hasattr(main, '__file__'):
main_path = os.path.abspath(os.path.dirname(main.__file__))

)",
py::globals(), locals);

if (locals.contains("main_path")) {
return fs::path(py::cast<std::string>(locals["main_path"]));
} else {
#ifdef __FRC_ROBORIO__
return fs::path("/home/lvuser/py");
#else
return fs::current_path();
#endif
}
}

inline std::string GetOperatingDirectory() {
return GetOperatingDirectoryFs().string();
}

inline std::string GetDeployDirectory() { return GetDeployDirectoryFs().string(); }

inline fs::path GetOperatingDirectoryFs() {
static fs::path operatingPath = getMainPath();
return operatingPath;
}

inline fs::path GetDeployDirectoryFs() { return GetOperatingDirectoryFs() / "deploy"; }

} // namespace robotpy::filesystem