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

Commit

Permalink
Add python specific implementations of GetDeployDirectory/GetOperatin…
Browse files Browse the repository at this point in the history
…gDirectory
  • Loading branch information
virtuald committed Apr 6, 2022
1 parent b9135c5 commit 4b68c8e
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 1 deletion.
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
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

0 comments on commit 4b68c8e

Please sign in to comment.