Skip to content

Commit 87f4eb8

Browse files
tmediccicederom
authored andcommitted
interpreters/python: add wrapper to initialize Python
This wrapper application checks if the Python's modules are already mounted (and mounts them, if not), sets the necessary environment variables and, then, runs the Python interpreter.
1 parent 4c6a6c7 commit 87f4eb8

File tree

4 files changed

+116
-38
lines changed

4 files changed

+116
-38
lines changed

interpreters/python/Kconfig

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ config INTERPRETER_CPYTHON_STACKSIZE
2727

2828
config INTERPRETER_CPYTHON_PRIORITY
2929
int "CPython task priority"
30-
default 150
30+
default 100
3131
---help---
3232
This is the priority of the CPython task.
3333

@@ -37,22 +37,4 @@ config INTERPRETER_CPYTHON_PROGNAME
3737
---help---
3838
This is the name of the program that will be used from the nsh.
3939

40-
config INTERPRETER_CPYTHON_MOUNT_MODULES_STACKSIZE
41-
int "CPython's Modules Mount stack size"
42-
default 4096
43-
---help---
44-
This is the stack size allocated when the CPython's Modules Mount task runs.
45-
46-
config INTERPRETER_CPYTHON_MOUNT_MODULES_PRIORITY
47-
int "CPython's Modules Mount task priority"
48-
default 150
49-
---help---
50-
This is the priority of the CPython's Modules Mount task.
51-
52-
config INTERPRETER_CPYTHON_MOUNT_MODULES_PROGNAME
53-
string "CPython's Modules Mount app name"
54-
default "python_mount_modules"
55-
---help---
56-
This is the name of the program that will be used from the nsh.
57-
5840
endif

interpreters/python/Makefile

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ $(CPYTHON_UNPACKNAME): $(CPYTHON_ZIP)
8282
$(Q) patch -p1 -d $(CPYTHON_UNPACKNAME) < patch$(DELIM)0010-check-for-the-d_ino-member-of-the-structure-dirent.patch
8383
$(Q) patch -p1 -d $(CPYTHON_UNPACKNAME) < patch$(DELIM)0011-avoid-redefinition-warning-if-UNUSED-is-already-defi.patch
8484
$(Q) patch -p1 -d $(CPYTHON_UNPACKNAME) < patch$(DELIM)0012-hack-place-_PyRuntime-structure-into-PSRAM-bss-regio.patch
85+
$(Q) patch -p1 -d $(CPYTHON_UNPACKNAME) < patch$(DELIM)0013-transform-functions-used-by-NuttX-to-lowercase.patch
8586

8687
$(HOSTPYTHON):
8788
mkdir -p $(HOSTBUILD)
@@ -175,13 +176,7 @@ PROGNAME += $(CONFIG_INTERPRETER_CPYTHON_PROGNAME)
175176
PRIORITY += $(CONFIG_INTERPRETER_CPYTHON_PRIORITY)
176177
STACKSIZE += $(CONFIG_INTERPRETER_CPYTHON_STACKSIZE)
177178

178-
MAINSRC += python.c
179-
180-
PROGNAME += $(CONFIG_INTERPRETER_CPYTHON_MOUNT_MODULES_PROGNAME)
181-
PRIORITY += $(CONFIG_INTERPRETER_CPYTHON_MOUNT_MODULES_PRIORITY)
182-
STACKSIZE += $(CONFIG_INTERPRETER_CPYTHON_MOUNT_MODULES_STACKSIZE)
183-
184-
MAINSRC += mount_modules.c
179+
MAINSRC += python_wrapper.c
185180

186181
checkgenromfs:
187182
@genromfs -h 1>/dev/null 2>&1 || { \
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
From 914c80b7969d73840bc1b573b478d9148999b7d0 Mon Sep 17 00:00:00 2001
2+
From: Tiago Medicci <[email protected]>
3+
Date: Fri, 31 Jan 2025 14:06:21 -0300
4+
Subject: [PATCH 13/13] transform functions used by NuttX to lowercase
5+
6+
---
7+
Include/pylifecycle.h | 10 ++++++++++
8+
1 file changed, 10 insertions(+)
9+
10+
diff --git a/Include/pylifecycle.h b/Include/pylifecycle.h
11+
index de1bcb1d2cb..044780ee188 100644
12+
--- a/Include/pylifecycle.h
13+
+++ b/Include/pylifecycle.h
14+
@@ -33,6 +33,16 @@ PyAPI_FUNC(void) _Py_NO_RETURN Py_Exit(int);
15+
PyAPI_FUNC(int) Py_Main(int argc, wchar_t **argv);
16+
PyAPI_FUNC(int) Py_BytesMain(int argc, char **argv);
17+
18+
+#if defined(__NuttX__)
19+
+#define py_bytesmain Py_BytesMain
20+
+#endif
21+
+
22+
+void _PyRuntime_Early_Init(void);
23+
+
24+
+#if defined(__NuttX__)
25+
+#define _pyruntime_early_init _PyRuntime_Early_Init
26+
+#endif
27+
+
28+
/* In pathconfig.c */
29+
Py_DEPRECATED(3.11) PyAPI_FUNC(void) Py_SetProgramName(const wchar_t *);
30+
Py_DEPRECATED(3.13) PyAPI_FUNC(wchar_t *) Py_GetProgramName(void);
31+
--
32+
2.47.1
33+

interpreters/python/mount_modules.c renamed to interpreters/python/python_wrapper.c

Lines changed: 80 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/****************************************************************************
2-
* apps/interpreters/python/mount_modules.c
2+
* apps/interpreters/python/python_wrapper.c
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*
@@ -41,11 +41,14 @@
4141
#include <fcntl.h>
4242
#include <dirent.h>
4343
#include <errno.h>
44+
#include <debug.h>
4445

4546
#include <nuttx/drivers/ramdisk.h>
4647

4748
#include "romfs_cpython_modules.h"
4849

50+
#include "Python.h"
51+
4952
/****************************************************************************
5053
* Pre-processor Definitions
5154
****************************************************************************/
@@ -61,7 +64,7 @@
6164
#endif
6265

6366
#ifndef CONFIG_CPYTHON_ROMFS_MOUNTPOINT
64-
# define CONFIG_CPYTHON_ROMFS_MOUNTPOINT "/usr/local/lib/"
67+
# define CONFIG_CPYTHON_ROMFS_MOUNTPOINT "/usr/local/lib"
6568
#endif
6669

6770
#ifdef CONFIG_DISABLE_MOUNTPOINT
@@ -90,19 +93,57 @@
9093
****************************************************************************/
9194

9295
/****************************************************************************
93-
* Public Functions
94-
****************************************************************************/
95-
96-
/****************************************************************************
97-
* Name: mount_modules
96+
* Name: check_and_mount_romfs
97+
*
98+
* Description:
99+
* Check if the ROMFS is already mounted, and if not, mount it.
100+
*
101+
* Input Parameters:
102+
* None
103+
*
104+
* Returned Value:
105+
* 0 on success, 1 on failure
106+
*
98107
****************************************************************************/
99108

100-
int main(int argc, FAR char *argv[])
109+
static int check_and_mount_romfs(void)
101110
{
102-
int ret;
111+
int ret = OK;
103112
struct boardioc_romdisk_s desc;
113+
FILE *fp;
114+
char line[256];
115+
int is_mounted = 0;
104116

105-
/* Create a RAM disk for the test */
117+
/* Check if the device is already mounted */
118+
119+
fp = fopen("/proc/fs/mount", "r");
120+
if (fp == NULL)
121+
{
122+
printf("ERROR: Failed to open /proc/fs/mount\n");
123+
UNUSED(desc);
124+
return ret = ERROR;
125+
}
126+
127+
while (fgets(line, sizeof(line), fp))
128+
{
129+
if (strstr(line, CONFIG_CPYTHON_ROMFS_MOUNTPOINT) != NULL)
130+
{
131+
is_mounted = 1;
132+
break;
133+
}
134+
}
135+
136+
fclose(fp);
137+
138+
if (is_mounted)
139+
{
140+
_info("Device is already mounted at %s\n",
141+
CONFIG_CPYTHON_ROMFS_MOUNTPOINT);
142+
UNUSED(desc);
143+
return ret;
144+
}
145+
146+
/* Create a RAM disk */
106147

107148
desc.minor = CONFIG_CPYTHON_ROMFS_RAMDEVNO; /* Minor device number of the ROM disk. */
108149
desc.nsectors = NSECTORS(romfs_cpython_modules_img_len); /* The number of sectors in the ROM disk */
@@ -119,8 +160,8 @@ int main(int argc, FAR char *argv[])
119160

120161
/* Mount the test file system */
121162

122-
printf("Mounting ROMFS filesystem at target=%s with source=%s\n",
123-
CONFIG_CPYTHON_ROMFS_MOUNTPOINT, MOUNT_DEVNAME);
163+
_info("Mounting ROMFS filesystem at target=%s with source=%s\n",
164+
CONFIG_CPYTHON_ROMFS_MOUNTPOINT, MOUNT_DEVNAME);
124165

125166
ret = mount(MOUNT_DEVNAME, CONFIG_CPYTHON_ROMFS_MOUNTPOINT, "romfs",
126167
MS_RDONLY, NULL);
@@ -132,3 +173,30 @@ int main(int argc, FAR char *argv[])
132173

133174
return 0;
134175
}
176+
177+
/****************************************************************************
178+
* Public Functions
179+
****************************************************************************/
180+
181+
/****************************************************************************
182+
* Name: python_wrapper_main
183+
****************************************************************************/
184+
185+
int main(int argc, FAR char *argv[])
186+
{
187+
int ret;
188+
189+
ret = check_and_mount_romfs();
190+
if (ret != 0)
191+
{
192+
return ret;
193+
}
194+
195+
_pyruntime_early_init();
196+
197+
setenv("PYTHONHOME", "/usr/local", 1);
198+
199+
setenv("PYTHON_BASIC_REPL", "1", 1);
200+
201+
return py_bytesmain(argc, argv);
202+
}

0 commit comments

Comments
 (0)