1
1
/****************************************************************************
2
- * apps/interpreters/python/mount_modules .c
2
+ * apps/interpreters/python/python_wrapper .c
3
3
*
4
4
* SPDX-License-Identifier: Apache-2.0
5
5
*
41
41
#include <fcntl.h>
42
42
#include <dirent.h>
43
43
#include <errno.h>
44
+ #include <debug.h>
44
45
45
46
#include <nuttx/drivers/ramdisk.h>
46
47
47
48
#include "romfs_cpython_modules.h"
48
49
50
+ #include "Python.h"
51
+
49
52
/****************************************************************************
50
53
* Pre-processor Definitions
51
54
****************************************************************************/
61
64
#endif
62
65
63
66
#ifndef CONFIG_CPYTHON_ROMFS_MOUNTPOINT
64
- # define CONFIG_CPYTHON_ROMFS_MOUNTPOINT "/usr/local/lib/ "
67
+ # define CONFIG_CPYTHON_ROMFS_MOUNTPOINT "/usr/local/lib"
65
68
#endif
66
69
67
70
#ifdef CONFIG_DISABLE_MOUNTPOINT
90
93
****************************************************************************/
91
94
92
95
/****************************************************************************
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
+ *
98
107
****************************************************************************/
99
108
100
- int main ( int argc , FAR char * argv [] )
109
+ static int check_and_mount_romfs ( void )
101
110
{
102
- int ret ;
111
+ int ret = OK ;
103
112
struct boardioc_romdisk_s desc ;
113
+ FILE * fp ;
114
+ char line [256 ];
115
+ int is_mounted = 0 ;
104
116
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 */
106
147
107
148
desc .minor = CONFIG_CPYTHON_ROMFS_RAMDEVNO ; /* Minor device number of the ROM disk. */
108
149
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[])
119
160
120
161
/* Mount the test file system */
121
162
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 );
124
165
125
166
ret = mount (MOUNT_DEVNAME , CONFIG_CPYTHON_ROMFS_MOUNTPOINT , "romfs" ,
126
167
MS_RDONLY , NULL );
@@ -132,3 +173,30 @@ int main(int argc, FAR char *argv[])
132
173
133
174
return 0 ;
134
175
}
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