myextensions 0.0.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- myextensions-0.0.0/PKG-INFO +3 -0
- myextensions-0.0.0/example.cpp +32 -0
- myextensions-0.0.0/myextensions.egg-info/PKG-INFO +3 -0
- myextensions-0.0.0/myextensions.egg-info/SOURCES.txt +7 -0
- myextensions-0.0.0/myextensions.egg-info/dependency_links.txt +1 -0
- myextensions-0.0.0/myextensions.egg-info/top_level.txt +2 -0
- myextensions-0.0.0/setup.cfg +4 -0
- myextensions-0.0.0/setup.py +21 -0
- myextensions-0.0.0/sysinfo_final.cpp +195 -0
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
#include <Python.h>
|
|
2
|
+
|
|
3
|
+
static PyObject* hello_world(PyObject* self, PyObject* args) {
|
|
4
|
+
printf("Hello from C++!\n");
|
|
5
|
+
Py_RETURN_NONE;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
static PyObject* add_numbers(PyObject* self, PyObject* args) {
|
|
9
|
+
int a, b;
|
|
10
|
+
if (!PyArg_ParseTuple(args, "ii", &a, &b)) {
|
|
11
|
+
return NULL;
|
|
12
|
+
}
|
|
13
|
+
return PyLong_FromLong(a + b);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
static PyMethodDef ExampleMethods[] = {
|
|
17
|
+
{"hello_world", hello_world, METH_NOARGS, "Print hello"},
|
|
18
|
+
{"add_numbers", add_numbers, METH_VARARGS, "Add two numbers"},
|
|
19
|
+
{NULL, NULL, 0, NULL}
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
static struct PyModuleDef examplemodule = {
|
|
23
|
+
PyModuleDef_HEAD_INIT,
|
|
24
|
+
"example",
|
|
25
|
+
"Example C++ extension",
|
|
26
|
+
-1,
|
|
27
|
+
ExampleMethods
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
PyMODINIT_FUNC PyInit_example(void) {
|
|
31
|
+
return PyModule_Create(&examplemodule);
|
|
32
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
from setuptools import setup, Extension
|
|
2
|
+
|
|
3
|
+
modules = [
|
|
4
|
+
Extension(
|
|
5
|
+
'example',
|
|
6
|
+
sources=['example.cpp'],
|
|
7
|
+
language='c++',
|
|
8
|
+
extra_compile_args=['-std=c++17'],
|
|
9
|
+
),
|
|
10
|
+
Extension(
|
|
11
|
+
'sysinfo',
|
|
12
|
+
sources=['sysinfo_final.cpp'],
|
|
13
|
+
language='c++',
|
|
14
|
+
extra_compile_args=['-std=c++17'],
|
|
15
|
+
),
|
|
16
|
+
]
|
|
17
|
+
|
|
18
|
+
setup(
|
|
19
|
+
name='myextensions',
|
|
20
|
+
ext_modules=modules,
|
|
21
|
+
)
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
#include <Python.h>
|
|
2
|
+
#include <fstream>
|
|
3
|
+
#include <string>
|
|
4
|
+
#include <sstream>
|
|
5
|
+
#include <cstdlib>
|
|
6
|
+
#include <cstring>
|
|
7
|
+
#include <sys/statvfs.h>
|
|
8
|
+
|
|
9
|
+
// 获取内存信息
|
|
10
|
+
static PyObject* get_memory_info(PyObject* self, PyObject* args) {
|
|
11
|
+
FILE* fp = popen("free -b | grep Mem", "r");
|
|
12
|
+
if (!fp) Py_RETURN_NONE;
|
|
13
|
+
|
|
14
|
+
char line[256];
|
|
15
|
+
unsigned long total, used, free;
|
|
16
|
+
|
|
17
|
+
if (fgets(line, sizeof(line), fp)) {
|
|
18
|
+
if (sscanf(line, "Mem: %lu %lu %lu", &total, &used, &free) == 3) {
|
|
19
|
+
pclose(fp);
|
|
20
|
+
return Py_BuildValue("{s:L, s:L, s:L}",
|
|
21
|
+
"total", total,
|
|
22
|
+
"used", used,
|
|
23
|
+
"free", free
|
|
24
|
+
);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
pclose(fp);
|
|
28
|
+
Py_RETURN_NONE;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// 获取 CPU 信息
|
|
32
|
+
static PyObject* get_cpu_info(PyObject* self, PyObject* args) {
|
|
33
|
+
FILE* fp = popen("nproc", "r");
|
|
34
|
+
if (!fp) Py_RETURN_NONE;
|
|
35
|
+
|
|
36
|
+
int cores = 0;
|
|
37
|
+
if (fscanf(fp, "%d", &cores) == 1) {
|
|
38
|
+
pclose(fp);
|
|
39
|
+
return Py_BuildValue("{s:i, s:s}",
|
|
40
|
+
"cores", cores,
|
|
41
|
+
"model", "ARM Processor"
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
pclose(fp);
|
|
45
|
+
Py_RETURN_NONE;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// 获取磁盘信息
|
|
49
|
+
static PyObject* get_disk_info(PyObject* self, PyObject* args) {
|
|
50
|
+
const char* path = "/";
|
|
51
|
+
if (!PyArg_ParseTuple(args, "|s", &path)) {
|
|
52
|
+
return NULL;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
struct statvfs stat;
|
|
56
|
+
if (statvfs(path, &stat) == 0) {
|
|
57
|
+
unsigned long total = stat.f_blocks * stat.f_bsize;
|
|
58
|
+
unsigned long free = stat.f_bfree * stat.f_bsize;
|
|
59
|
+
unsigned long used = total - free;
|
|
60
|
+
|
|
61
|
+
return Py_BuildValue("{s:L, s:L, s:L}",
|
|
62
|
+
"total", total,
|
|
63
|
+
"used", used,
|
|
64
|
+
"free", free
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
Py_RETURN_NONE;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// 获取系统负载
|
|
71
|
+
static PyObject* get_load_average(PyObject* self, PyObject* args) {
|
|
72
|
+
FILE* fp = popen("uptime | grep -oE '[0-9]+\\.[0-9]+' | head -3 | tr '\\n' ' '", "r");
|
|
73
|
+
if (!fp) Py_RETURN_NONE;
|
|
74
|
+
|
|
75
|
+
char line[128];
|
|
76
|
+
double load1, load5, load15;
|
|
77
|
+
|
|
78
|
+
if (fgets(line, sizeof(line), fp)) {
|
|
79
|
+
if (sscanf(line, "%lf %lf %lf", &load1, &load5, &load15) == 3) {
|
|
80
|
+
pclose(fp);
|
|
81
|
+
return Py_BuildValue("(ddd)", load1, load5, load15);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
pclose(fp);
|
|
85
|
+
Py_RETURN_NONE;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// 获取网络接口(修复版本)
|
|
89
|
+
static PyObject* get_network_interfaces(PyObject* self, PyObject* args) {
|
|
90
|
+
// 尝试多种方法获取网络接口
|
|
91
|
+
FILE* fp = popen("ls /sys/class/net/ 2>/dev/null || ifconfig -a 2>/dev/null | grep '^[a-z]' | awk '{print $1}'", "r");
|
|
92
|
+
if (!fp) Py_RETURN_NONE;
|
|
93
|
+
|
|
94
|
+
PyObject* interfaces = PyList_New(0);
|
|
95
|
+
char line[64];
|
|
96
|
+
|
|
97
|
+
while (fgets(line, sizeof(line), fp)) {
|
|
98
|
+
// 移除换行符
|
|
99
|
+
line[strcspn(line, "\n")] = 0;
|
|
100
|
+
if (strlen(line) > 0 && strcmp(line, "total") != 0) {
|
|
101
|
+
PyList_Append(interfaces, PyUnicode_FromString(line));
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
pclose(fp);
|
|
105
|
+
|
|
106
|
+
return interfaces;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// 获取进程数
|
|
110
|
+
static PyObject* get_process_count(PyObject* self, PyObject* args) {
|
|
111
|
+
FILE* fp = popen("ps | wc -l", "r");
|
|
112
|
+
if (!fp) Py_RETURN_NONE;
|
|
113
|
+
|
|
114
|
+
int count = 0;
|
|
115
|
+
if (fscanf(fp, "%d", &count) == 1) {
|
|
116
|
+
pclose(fp);
|
|
117
|
+
return PyLong_FromLong(count - 1); // 减去标题行
|
|
118
|
+
}
|
|
119
|
+
pclose(fp);
|
|
120
|
+
Py_RETURN_NONE;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// 获取系统运行时间
|
|
124
|
+
static PyObject* get_uptime(PyObject* self, PyObject* args) {
|
|
125
|
+
FILE* fp = popen("uptime -p 2>/dev/null || uptime", "r");
|
|
126
|
+
if (!fp) Py_RETURN_NONE;
|
|
127
|
+
|
|
128
|
+
char line[256];
|
|
129
|
+
if (fgets(line, sizeof(line), fp)) {
|
|
130
|
+
line[strcspn(line, "\n")] = 0;
|
|
131
|
+
pclose(fp);
|
|
132
|
+
return Py_BuildValue("{s:s}", "uptime_str", line);
|
|
133
|
+
}
|
|
134
|
+
pclose(fp);
|
|
135
|
+
Py_RETURN_NONE;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// 获取运行的服务/应用信息
|
|
139
|
+
static PyObject* get_running_services(PyObject* self, PyObject* args) {
|
|
140
|
+
FILE* fp = popen("ps aux | grep -v grep | awk '{print $11}' | tail -n +2", "r");
|
|
141
|
+
if (!fp) Py_RETURN_NONE;
|
|
142
|
+
|
|
143
|
+
PyObject* services = PyList_New(0);
|
|
144
|
+
char line[256];
|
|
145
|
+
|
|
146
|
+
while (fgets(line, sizeof(line), fp)) {
|
|
147
|
+
line[strcspn(line, "\n")] = 0;
|
|
148
|
+
if (strlen(line) > 0) {
|
|
149
|
+
PyList_Append(services, PyUnicode_FromString(line));
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
pclose(fp);
|
|
153
|
+
|
|
154
|
+
return services;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// 获取电池信息(Android/手机特有)
|
|
158
|
+
static PyObject* get_battery_info(PyObject* self, PyObject* args) {
|
|
159
|
+
FILE* fp = popen("cat /sys/class/power_supply/battery/capacity 2>/dev/null", "r");
|
|
160
|
+
if (!fp) Py_RETURN_NONE;
|
|
161
|
+
|
|
162
|
+
int capacity = -1;
|
|
163
|
+
if (fscanf(fp, "%d", &capacity) == 1) {
|
|
164
|
+
pclose(fp);
|
|
165
|
+
return Py_BuildValue("{s:i}", "capacity", capacity);
|
|
166
|
+
}
|
|
167
|
+
pclose(fp);
|
|
168
|
+
Py_RETURN_NONE;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
// 方法定义
|
|
172
|
+
static PyMethodDef SysMethods[] = {
|
|
173
|
+
{"get_memory_info", get_memory_info, METH_NOARGS, "Get memory info"},
|
|
174
|
+
{"get_cpu_info", get_cpu_info, METH_NOARGS, "Get CPU info"},
|
|
175
|
+
{"get_disk_info", get_disk_info, METH_VARARGS, "Get disk info for path"},
|
|
176
|
+
{"get_load_average", get_load_average, METH_NOARGS, "Get load average"},
|
|
177
|
+
{"get_network_interfaces", get_network_interfaces, METH_NOARGS, "Get network interfaces"},
|
|
178
|
+
{"get_process_count", get_process_count, METH_NOARGS, "Get process count"},
|
|
179
|
+
{"get_uptime", get_uptime, METH_NOARGS, "Get system uptime"},
|
|
180
|
+
{"get_running_services", get_running_services, METH_NOARGS, "Get running services"},
|
|
181
|
+
{"get_battery_info", get_battery_info, METH_NOARGS, "Get battery info (Android)"},
|
|
182
|
+
{NULL, NULL, 0, NULL}
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
static struct PyModuleDef sysmodule = {
|
|
186
|
+
PyModuleDef_HEAD_INIT,
|
|
187
|
+
"sysinfo",
|
|
188
|
+
"System information module for Termux",
|
|
189
|
+
-1,
|
|
190
|
+
SysMethods
|
|
191
|
+
};
|
|
192
|
+
|
|
193
|
+
PyMODINIT_FUNC PyInit_sysinfo(void) {
|
|
194
|
+
return PyModule_Create(&sysmodule);
|
|
195
|
+
}
|