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

Commit 9b803ec

Browse files
authored
Merge pull request #710 from robotpy/misc
Misc
2 parents c51b1fc + 4b68c8e commit 9b803ec

File tree

6 files changed

+89
-2
lines changed

6 files changed

+89
-2
lines changed

gen/Filesystem.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
---
22

33
functions:
4-
GetLaunchDirectory:
54
GetOperatingDirectory:
65
GetDeployDirectory:
6+
GetOperatingDirectoryFs:
7+
ignore: true
8+
GetDeployDirectoryFs:
9+
ignore: true

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ extension = "_wpilib"
2929
sources = [
3030
"wpilib/src/main.cpp",
3131
"wpilib/src/rpy/ControlWord.cpp",
32+
"wpilib/src/rpy/Filesystem.cpp",
3233
"wpilib/src/rpy/Notifier.cpp",
3334
"wpilib/src/rpy/SmartDashboardData.cpp",
3435
"wpilib/src/rpy/MotorControllerGroup.cpp",
@@ -91,6 +92,7 @@ DutyCycleEncoder = "frc/DutyCycleEncoder.h"
9192
Encoder = "frc/Encoder.h"
9293
Errors = "frc/Errors.h"
9394
# Filesystem = "frc/Filesystem.h"
95+
Filesystem = "rpy/Filesystem.h"
9496
# GenericHID = "frc/GenericHID.h" # interfaces
9597
I2C = "frc/I2C.h"
9698
IterativeRobotBase = "frc/IterativeRobotBase.h"

wpilib/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,9 @@
9999
Watchdog,
100100
XboxController,
101101
getCurrentThreadPriority,
102+
getDeployDirectory,
102103
getErrorMessage,
104+
getOperatingDirectory,
103105
getTime,
104106
setCurrentThreadPriority,
105107
wait,
@@ -200,7 +202,9 @@
200202
"Watchdog",
201203
"XboxController",
202204
"getCurrentThreadPriority",
205+
"getDeployDirectory",
203206
"getErrorMessage",
207+
"getOperatingDirectory",
204208
"getTime",
205209
"setCurrentThreadPriority",
206210
"wait",

wpilib/deployinfo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def getDeployData() -> typing.Optional[typing.Dict[str, str]]:
2323
2424
:returns: None in simulation, or a dictionary
2525
"""
26-
if RobotBase.isReal():
26+
if not RobotBase.isReal():
2727
return None
2828

2929
try:

wpilib/src/rpy/Filesystem.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
2+
#pragma once
3+
4+
#include <wpi/fs.h>
5+
6+
namespace robotpy::filesystem {
7+
8+
/**
9+
* Obtains the operating directory of the program. On the roboRIO, this
10+
* is /home/lvuser/py. In simulation, it is the location of robot.py
11+
*
12+
* @return The result of the operating directory lookup.
13+
*/
14+
std::string GetOperatingDirectory();
15+
16+
/**
17+
* Obtains the deploy directory of the program, which is the remote location
18+
* the deploy directory is deployed to by default. On the roboRIO, this is
19+
* /home/lvuser/py/deploy. In simulation, it is where the simulation was launched
20+
* from, in the subdirectory "deploy" (`dirname(robot.py)`/deploy).
21+
*
22+
* @return The result of the operating directory lookup
23+
*/
24+
std::string GetDeployDirectory();
25+
26+
// intended to be used by C++ bindings, returns same as GetOperatingDirectory
27+
fs::path GetOperatingDirectoryFs();
28+
// intended to be used by C++ bindings, returns same as GetDeployDirectory
29+
fs::path GetDeployDirectoryFs();
30+
31+
} // namespace robotpy::filesystem
32+
33+
#include "Filesystem.inc"

wpilib/src/rpy/Filesystem.inc

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
2+
// TODO: this should be in a shared library, but robotpy-build does not support that
3+
4+
#include <pybind11/eval.h>
5+
#include <robotpy_build.h>
6+
7+
namespace robotpy::filesystem {
8+
9+
static fs::path getMainPath() {
10+
py::gil_scoped_acquire gil;
11+
py::dict locals;
12+
py::exec(R"(
13+
import sys, os.path
14+
main = sys.modules['__main__'];
15+
if hasattr(main, '__file__'):
16+
main_path = os.path.abspath(os.path.dirname(main.__file__))
17+
18+
)",
19+
py::globals(), locals);
20+
21+
if (locals.contains("main_path")) {
22+
return fs::path(py::cast<std::string>(locals["main_path"]));
23+
} else {
24+
#ifdef __FRC_ROBORIO__
25+
return fs::path("/home/lvuser/py");
26+
#else
27+
return fs::current_path();
28+
#endif
29+
}
30+
}
31+
32+
inline std::string GetOperatingDirectory() {
33+
return GetOperatingDirectoryFs().string();
34+
}
35+
36+
inline std::string GetDeployDirectory() { return GetDeployDirectoryFs().string(); }
37+
38+
inline fs::path GetOperatingDirectoryFs() {
39+
static fs::path operatingPath = getMainPath();
40+
return operatingPath;
41+
}
42+
43+
inline fs::path GetDeployDirectoryFs() { return GetOperatingDirectoryFs() / "deploy"; }
44+
45+
} // namespace robotpy::filesystem

0 commit comments

Comments
 (0)