module-bank 0.1.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.
- module_bank-0.1.0/.gitignore +14 -0
- module_bank-0.1.0/LICENSE +21 -0
- module_bank-0.1.0/PKG-INFO +259 -0
- module_bank-0.1.0/README.md +251 -0
- module_bank-0.1.0/pyproject.toml +26 -0
- module_bank-0.1.0/scripts/clean_pycache.py +13 -0
- module_bank-0.1.0/scripts/code_content.py +29 -0
- module_bank-0.1.0/src/module_bank/__init__.py +11 -0
- module_bank-0.1.0/src/module_bank/cli.py +83 -0
- module_bank-0.1.0/src/module_bank/python_to_sqlite.py +133 -0
- module_bank-0.1.0/src/module_bank/sqlite_meta_path_finder.py +53 -0
- module_bank-0.1.0/src/module_bank/sqlite_module_importer.py +107 -0
- module_bank-0.1.0/src/module_bank/sqlite_module_loader.py +63 -0
- module_bank-0.1.0/uv.lock +1369 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 chakcy
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: module_bank
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Add your description here
|
|
5
|
+
License-File: LICENSE
|
|
6
|
+
Requires-Python: >=3.7
|
|
7
|
+
Description-Content-Type: text/markdown
|
|
8
|
+
|
|
9
|
+
# Python Module Bank - SQLite 模块打包系统
|
|
10
|
+
|
|
11
|
+
一个将Python模块打包到SQLite数据库,并支持从数据库直接导入模块的工具系统。
|
|
12
|
+
|
|
13
|
+
## 🌟 特性
|
|
14
|
+
|
|
15
|
+
- **单文件分发** - 将所有模块打包到单个SQLite数据库文件
|
|
16
|
+
- **源代码保护** - 模块以编译后的字节码形式存储
|
|
17
|
+
- **动态导入** - 运行时直接从数据库加载模块,无需文件系统
|
|
18
|
+
- **完整包支持** - 支持包结构和子模块导入
|
|
19
|
+
- **CLI工具** - 提供完整的命令行接口
|
|
20
|
+
- **导入钩子** - 无缝集成Python导入系统
|
|
21
|
+
|
|
22
|
+
## 📦 安装
|
|
23
|
+
|
|
24
|
+
### 从源码安装
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
git clone http://124.71.68.6:3000/chakcy/module_bank.git
|
|
28
|
+
cd module-bank
|
|
29
|
+
pip install -e .
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
### 依赖要求
|
|
33
|
+
|
|
34
|
+
- Python 3.7+
|
|
35
|
+
- 无需额外依赖(仅使用标准库)
|
|
36
|
+
|
|
37
|
+
## 🚀 快速开始
|
|
38
|
+
|
|
39
|
+
### 1. 创建示例模块
|
|
40
|
+
|
|
41
|
+
```python
|
|
42
|
+
# my_module.py
|
|
43
|
+
def hello():
|
|
44
|
+
print("Hello from my_module!")
|
|
45
|
+
return "success"
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
### 2. 打包模块到数据库
|
|
49
|
+
|
|
50
|
+
```python
|
|
51
|
+
# pack_example.py
|
|
52
|
+
from module_bank import PythonToSQLite
|
|
53
|
+
|
|
54
|
+
packer = PythonToSQLite("my_modules.db")
|
|
55
|
+
packer.pack_module("my_module.py", "my_module")
|
|
56
|
+
packer.pack_directory("my_package/")
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
### 3. 从数据库导入
|
|
60
|
+
|
|
61
|
+
```python
|
|
62
|
+
from module_bank import PythonToSQLite
|
|
63
|
+
|
|
64
|
+
# 安装导入器
|
|
65
|
+
packer = PythonToSQLite("my_modules.db")
|
|
66
|
+
finder = packer.install_importer()
|
|
67
|
+
|
|
68
|
+
# 现在可以从数据库导入模块了!
|
|
69
|
+
import my_module
|
|
70
|
+
import my_package.package_module
|
|
71
|
+
|
|
72
|
+
my_module.hello()
|
|
73
|
+
my_package.package_module.hello()
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
## 📖 详细使用
|
|
77
|
+
|
|
78
|
+
### 命令行工具
|
|
79
|
+
|
|
80
|
+
```python
|
|
81
|
+
# 打包模块或目录
|
|
82
|
+
mb pack my_package --db modules.db
|
|
83
|
+
|
|
84
|
+
# 列出数据库中的模块
|
|
85
|
+
mb list --db modules.db
|
|
86
|
+
|
|
87
|
+
# 安装导入器并进入交互模式
|
|
88
|
+
mb install --db modules.db
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
### 编程接口
|
|
92
|
+
|
|
93
|
+
#### 打包模块
|
|
94
|
+
|
|
95
|
+
```python
|
|
96
|
+
from module_bank import PythonToSQLite
|
|
97
|
+
|
|
98
|
+
packer = PythonToSQLite("modules.db")
|
|
99
|
+
|
|
100
|
+
# 打包单个模块
|
|
101
|
+
packer.pack_module("module.py", "module_name")
|
|
102
|
+
|
|
103
|
+
# 打包整个目录(自动识别包结构)
|
|
104
|
+
packer.pack_directory("my_package/")
|
|
105
|
+
|
|
106
|
+
# 验证包结构
|
|
107
|
+
packer.verify_package_structure()
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
#### 导入模块
|
|
111
|
+
|
|
112
|
+
```python
|
|
113
|
+
from module_bank import PythonToSQLite
|
|
114
|
+
import sys
|
|
115
|
+
|
|
116
|
+
packer = PythonToSQLite("modules.db")
|
|
117
|
+
|
|
118
|
+
# 安装导入器到sys.meta_path
|
|
119
|
+
finder = packer.install_importer()
|
|
120
|
+
|
|
121
|
+
# 列出所有可用模块
|
|
122
|
+
modules = packer.list_modules()
|
|
123
|
+
for module in modules:
|
|
124
|
+
print(f"{module['module_name']} {'[包]' if module['is_package'] else ''}")
|
|
125
|
+
|
|
126
|
+
# 导入数据库中的模块
|
|
127
|
+
import my_package
|
|
128
|
+
import my_package.submodule
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
## 🏗️ 架构设计
|
|
132
|
+
|
|
133
|
+
### 核心组件
|
|
134
|
+
|
|
135
|
+
```python
|
|
136
|
+
src/module_bank/
|
|
137
|
+
├── python_to_sqlite.py # 主打包类
|
|
138
|
+
├── sqlite_module_importer.py # 数据库存储管理器
|
|
139
|
+
├── sqlite_meta_path_finder.py # 元路径查找器
|
|
140
|
+
├── sqlite_module_loader.py # 模块加载器
|
|
141
|
+
├── cli.py # 命令行接口
|
|
142
|
+
└── __init__.py # 模块导出
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
### 数据流
|
|
146
|
+
|
|
147
|
+
```text
|
|
148
|
+
1. 打包阶段:
|
|
149
|
+
.py文件 → 编译为字节码 → 存储到SQLite数据库
|
|
150
|
+
|
|
151
|
+
2. 导入阶段:
|
|
152
|
+
导入请求 → MetaPathFinder查找 → ModuleLoader加载 → 执行模块
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
### 数据库模式
|
|
156
|
+
|
|
157
|
+
```sql
|
|
158
|
+
CREATE TABLE python_modules (
|
|
159
|
+
module_name TEXT PRIMARY KEY,
|
|
160
|
+
source_code TEXT, -- 源代码(可选)
|
|
161
|
+
bytecode BLOB, -- 编译后的字节码
|
|
162
|
+
is_package BOOLEAN, -- 是否是包
|
|
163
|
+
metadata TEXT, -- 元数据(JSON格式)
|
|
164
|
+
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
165
|
+
)
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
## 🔧 高级功能
|
|
169
|
+
|
|
170
|
+
### 排除模式
|
|
171
|
+
|
|
172
|
+
```python
|
|
173
|
+
# 打包时排除特定文件
|
|
174
|
+
packer.pack_directory(
|
|
175
|
+
"my_project/",
|
|
176
|
+
exclude_patterns=["*_test.py", "*.pyc", "__pycache__"]
|
|
177
|
+
)
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
### 元数据存储
|
|
181
|
+
|
|
182
|
+
```python
|
|
183
|
+
# 为模块添加元数据
|
|
184
|
+
packer.importer.add_module(
|
|
185
|
+
"my_module",
|
|
186
|
+
source_code,
|
|
187
|
+
is_package=False,
|
|
188
|
+
metadata={"version": "1.0", "author": "me"}
|
|
189
|
+
)
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
### 混合导入
|
|
193
|
+
|
|
194
|
+
```python
|
|
195
|
+
# 可以同时使用文件系统和数据库导入
|
|
196
|
+
# 数据库导入器优先级更高
|
|
197
|
+
import sys
|
|
198
|
+
from module_bank import PythonToSQLite
|
|
199
|
+
|
|
200
|
+
packer = PythonToSQLite("modules.db")
|
|
201
|
+
finder = packer.install_importer() # 插入到meta_path开头
|
|
202
|
+
|
|
203
|
+
# 如果需要文件系统优先,可以调整插入位置
|
|
204
|
+
sys.meta_path.append(finder)
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
## ⚠️ 注意事项
|
|
208
|
+
|
|
209
|
+
### 安全性
|
|
210
|
+
|
|
211
|
+
- 模块字节码直接执行,确保数据库来源可信
|
|
212
|
+
- 生产环境建议添加代码签名验证
|
|
213
|
+
|
|
214
|
+
### 兼容性
|
|
215
|
+
|
|
216
|
+
- 字节码不跨Python版本兼容
|
|
217
|
+
- 不支持C扩展模块
|
|
218
|
+
- 不支持需要文件系统资源的模块(如__file__依赖)
|
|
219
|
+
|
|
220
|
+
### 性能
|
|
221
|
+
|
|
222
|
+
- **启动时**:有一次性数据库查询和反序列化开销
|
|
223
|
+
- **运行时**:与传统导入性能相同(使用sys.modules缓存)
|
|
224
|
+
- **最佳适用**:长期运行的服务、桌面应用
|
|
225
|
+
|
|
226
|
+
### 更新模块
|
|
227
|
+
|
|
228
|
+
```python
|
|
229
|
+
# 重新打包会自动更新
|
|
230
|
+
packer.pack_module("updated_module.py", "module_name")
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
### 删除模块
|
|
234
|
+
|
|
235
|
+
```sql
|
|
236
|
+
-- 直接从数据库删除
|
|
237
|
+
DELETE FROM python_modules WHERE module_name = 'module_to_remove';
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
### 备份与恢复
|
|
241
|
+
|
|
242
|
+
```bash
|
|
243
|
+
# 数据库是单个文件,易于备份
|
|
244
|
+
cp modules.db modules.backup.db
|
|
245
|
+
|
|
246
|
+
# 恢复
|
|
247
|
+
cp modules.backup.db modules.db
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
## 📚 应用场景
|
|
251
|
+
|
|
252
|
+
1. 商业软件分发 - 保护源代码知识产权
|
|
253
|
+
2. 插件系统 - 动态加载数据库中的插件模块
|
|
254
|
+
3. 教育平台 - 安全分发练习代码
|
|
255
|
+
4. 微服务 - 打包多个服务模块到单个文件
|
|
256
|
+
5. 嵌入式系统 - 减少文件系统依赖
|
|
257
|
+
|
|
258
|
+
---
|
|
259
|
+
**注意**: 本工具主要用于模块分发和部署场景,不适合开发阶段的频繁修改。
|
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
# Python Module Bank - SQLite 模块打包系统
|
|
2
|
+
|
|
3
|
+
一个将Python模块打包到SQLite数据库,并支持从数据库直接导入模块的工具系统。
|
|
4
|
+
|
|
5
|
+
## 🌟 特性
|
|
6
|
+
|
|
7
|
+
- **单文件分发** - 将所有模块打包到单个SQLite数据库文件
|
|
8
|
+
- **源代码保护** - 模块以编译后的字节码形式存储
|
|
9
|
+
- **动态导入** - 运行时直接从数据库加载模块,无需文件系统
|
|
10
|
+
- **完整包支持** - 支持包结构和子模块导入
|
|
11
|
+
- **CLI工具** - 提供完整的命令行接口
|
|
12
|
+
- **导入钩子** - 无缝集成Python导入系统
|
|
13
|
+
|
|
14
|
+
## 📦 安装
|
|
15
|
+
|
|
16
|
+
### 从源码安装
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
git clone http://124.71.68.6:3000/chakcy/module_bank.git
|
|
20
|
+
cd module-bank
|
|
21
|
+
pip install -e .
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
### 依赖要求
|
|
25
|
+
|
|
26
|
+
- Python 3.7+
|
|
27
|
+
- 无需额外依赖(仅使用标准库)
|
|
28
|
+
|
|
29
|
+
## 🚀 快速开始
|
|
30
|
+
|
|
31
|
+
### 1. 创建示例模块
|
|
32
|
+
|
|
33
|
+
```python
|
|
34
|
+
# my_module.py
|
|
35
|
+
def hello():
|
|
36
|
+
print("Hello from my_module!")
|
|
37
|
+
return "success"
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
### 2. 打包模块到数据库
|
|
41
|
+
|
|
42
|
+
```python
|
|
43
|
+
# pack_example.py
|
|
44
|
+
from module_bank import PythonToSQLite
|
|
45
|
+
|
|
46
|
+
packer = PythonToSQLite("my_modules.db")
|
|
47
|
+
packer.pack_module("my_module.py", "my_module")
|
|
48
|
+
packer.pack_directory("my_package/")
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
### 3. 从数据库导入
|
|
52
|
+
|
|
53
|
+
```python
|
|
54
|
+
from module_bank import PythonToSQLite
|
|
55
|
+
|
|
56
|
+
# 安装导入器
|
|
57
|
+
packer = PythonToSQLite("my_modules.db")
|
|
58
|
+
finder = packer.install_importer()
|
|
59
|
+
|
|
60
|
+
# 现在可以从数据库导入模块了!
|
|
61
|
+
import my_module
|
|
62
|
+
import my_package.package_module
|
|
63
|
+
|
|
64
|
+
my_module.hello()
|
|
65
|
+
my_package.package_module.hello()
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
## 📖 详细使用
|
|
69
|
+
|
|
70
|
+
### 命令行工具
|
|
71
|
+
|
|
72
|
+
```python
|
|
73
|
+
# 打包模块或目录
|
|
74
|
+
mb pack my_package --db modules.db
|
|
75
|
+
|
|
76
|
+
# 列出数据库中的模块
|
|
77
|
+
mb list --db modules.db
|
|
78
|
+
|
|
79
|
+
# 安装导入器并进入交互模式
|
|
80
|
+
mb install --db modules.db
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
### 编程接口
|
|
84
|
+
|
|
85
|
+
#### 打包模块
|
|
86
|
+
|
|
87
|
+
```python
|
|
88
|
+
from module_bank import PythonToSQLite
|
|
89
|
+
|
|
90
|
+
packer = PythonToSQLite("modules.db")
|
|
91
|
+
|
|
92
|
+
# 打包单个模块
|
|
93
|
+
packer.pack_module("module.py", "module_name")
|
|
94
|
+
|
|
95
|
+
# 打包整个目录(自动识别包结构)
|
|
96
|
+
packer.pack_directory("my_package/")
|
|
97
|
+
|
|
98
|
+
# 验证包结构
|
|
99
|
+
packer.verify_package_structure()
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
#### 导入模块
|
|
103
|
+
|
|
104
|
+
```python
|
|
105
|
+
from module_bank import PythonToSQLite
|
|
106
|
+
import sys
|
|
107
|
+
|
|
108
|
+
packer = PythonToSQLite("modules.db")
|
|
109
|
+
|
|
110
|
+
# 安装导入器到sys.meta_path
|
|
111
|
+
finder = packer.install_importer()
|
|
112
|
+
|
|
113
|
+
# 列出所有可用模块
|
|
114
|
+
modules = packer.list_modules()
|
|
115
|
+
for module in modules:
|
|
116
|
+
print(f"{module['module_name']} {'[包]' if module['is_package'] else ''}")
|
|
117
|
+
|
|
118
|
+
# 导入数据库中的模块
|
|
119
|
+
import my_package
|
|
120
|
+
import my_package.submodule
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
## 🏗️ 架构设计
|
|
124
|
+
|
|
125
|
+
### 核心组件
|
|
126
|
+
|
|
127
|
+
```python
|
|
128
|
+
src/module_bank/
|
|
129
|
+
├── python_to_sqlite.py # 主打包类
|
|
130
|
+
├── sqlite_module_importer.py # 数据库存储管理器
|
|
131
|
+
├── sqlite_meta_path_finder.py # 元路径查找器
|
|
132
|
+
├── sqlite_module_loader.py # 模块加载器
|
|
133
|
+
├── cli.py # 命令行接口
|
|
134
|
+
└── __init__.py # 模块导出
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
### 数据流
|
|
138
|
+
|
|
139
|
+
```text
|
|
140
|
+
1. 打包阶段:
|
|
141
|
+
.py文件 → 编译为字节码 → 存储到SQLite数据库
|
|
142
|
+
|
|
143
|
+
2. 导入阶段:
|
|
144
|
+
导入请求 → MetaPathFinder查找 → ModuleLoader加载 → 执行模块
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
### 数据库模式
|
|
148
|
+
|
|
149
|
+
```sql
|
|
150
|
+
CREATE TABLE python_modules (
|
|
151
|
+
module_name TEXT PRIMARY KEY,
|
|
152
|
+
source_code TEXT, -- 源代码(可选)
|
|
153
|
+
bytecode BLOB, -- 编译后的字节码
|
|
154
|
+
is_package BOOLEAN, -- 是否是包
|
|
155
|
+
metadata TEXT, -- 元数据(JSON格式)
|
|
156
|
+
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
157
|
+
)
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
## 🔧 高级功能
|
|
161
|
+
|
|
162
|
+
### 排除模式
|
|
163
|
+
|
|
164
|
+
```python
|
|
165
|
+
# 打包时排除特定文件
|
|
166
|
+
packer.pack_directory(
|
|
167
|
+
"my_project/",
|
|
168
|
+
exclude_patterns=["*_test.py", "*.pyc", "__pycache__"]
|
|
169
|
+
)
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
### 元数据存储
|
|
173
|
+
|
|
174
|
+
```python
|
|
175
|
+
# 为模块添加元数据
|
|
176
|
+
packer.importer.add_module(
|
|
177
|
+
"my_module",
|
|
178
|
+
source_code,
|
|
179
|
+
is_package=False,
|
|
180
|
+
metadata={"version": "1.0", "author": "me"}
|
|
181
|
+
)
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
### 混合导入
|
|
185
|
+
|
|
186
|
+
```python
|
|
187
|
+
# 可以同时使用文件系统和数据库导入
|
|
188
|
+
# 数据库导入器优先级更高
|
|
189
|
+
import sys
|
|
190
|
+
from module_bank import PythonToSQLite
|
|
191
|
+
|
|
192
|
+
packer = PythonToSQLite("modules.db")
|
|
193
|
+
finder = packer.install_importer() # 插入到meta_path开头
|
|
194
|
+
|
|
195
|
+
# 如果需要文件系统优先,可以调整插入位置
|
|
196
|
+
sys.meta_path.append(finder)
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
## ⚠️ 注意事项
|
|
200
|
+
|
|
201
|
+
### 安全性
|
|
202
|
+
|
|
203
|
+
- 模块字节码直接执行,确保数据库来源可信
|
|
204
|
+
- 生产环境建议添加代码签名验证
|
|
205
|
+
|
|
206
|
+
### 兼容性
|
|
207
|
+
|
|
208
|
+
- 字节码不跨Python版本兼容
|
|
209
|
+
- 不支持C扩展模块
|
|
210
|
+
- 不支持需要文件系统资源的模块(如__file__依赖)
|
|
211
|
+
|
|
212
|
+
### 性能
|
|
213
|
+
|
|
214
|
+
- **启动时**:有一次性数据库查询和反序列化开销
|
|
215
|
+
- **运行时**:与传统导入性能相同(使用sys.modules缓存)
|
|
216
|
+
- **最佳适用**:长期运行的服务、桌面应用
|
|
217
|
+
|
|
218
|
+
### 更新模块
|
|
219
|
+
|
|
220
|
+
```python
|
|
221
|
+
# 重新打包会自动更新
|
|
222
|
+
packer.pack_module("updated_module.py", "module_name")
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
### 删除模块
|
|
226
|
+
|
|
227
|
+
```sql
|
|
228
|
+
-- 直接从数据库删除
|
|
229
|
+
DELETE FROM python_modules WHERE module_name = 'module_to_remove';
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
### 备份与恢复
|
|
233
|
+
|
|
234
|
+
```bash
|
|
235
|
+
# 数据库是单个文件,易于备份
|
|
236
|
+
cp modules.db modules.backup.db
|
|
237
|
+
|
|
238
|
+
# 恢复
|
|
239
|
+
cp modules.backup.db modules.db
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
## 📚 应用场景
|
|
243
|
+
|
|
244
|
+
1. 商业软件分发 - 保护源代码知识产权
|
|
245
|
+
2. 插件系统 - 动态加载数据库中的插件模块
|
|
246
|
+
3. 教育平台 - 安全分发练习代码
|
|
247
|
+
4. 微服务 - 打包多个服务模块到单个文件
|
|
248
|
+
5. 嵌入式系统 - 减少文件系统依赖
|
|
249
|
+
|
|
250
|
+
---
|
|
251
|
+
**注意**: 本工具主要用于模块分发和部署场景,不适合开发阶段的频繁修改。
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "module_bank"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "Add your description here"
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
requires-python = ">=3.7"
|
|
7
|
+
dependencies = []
|
|
8
|
+
|
|
9
|
+
[[tool.uv.index]]
|
|
10
|
+
default = true
|
|
11
|
+
url = "https://pypi.tuna.tsinghua.edu.cn/simple"
|
|
12
|
+
|
|
13
|
+
[build-system]
|
|
14
|
+
build-backend = "hatchling.build"
|
|
15
|
+
requires = ["hatchling"]
|
|
16
|
+
|
|
17
|
+
[dependency-groups]
|
|
18
|
+
dev = [
|
|
19
|
+
"twine>=4.0.2",
|
|
20
|
+
]
|
|
21
|
+
|
|
22
|
+
[tool.hatch.build.targets.wheel]
|
|
23
|
+
packages = ["src/module_bank"]
|
|
24
|
+
|
|
25
|
+
[project.scripts]
|
|
26
|
+
mb = "module_bank.cli:main"
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import shutil
|
|
3
|
+
|
|
4
|
+
# 需要遍历的目录
|
|
5
|
+
root_dir = "./"
|
|
6
|
+
# 遍历目录
|
|
7
|
+
for dirpath, dirnames, filenames in os.walk(root_dir):
|
|
8
|
+
if "__pycache__" in dirnames:
|
|
9
|
+
# 获取 __pycache__ 目录的全路径
|
|
10
|
+
pycache_dir = os.path.join(dirpath, "__pycache__")
|
|
11
|
+
# 删除目录
|
|
12
|
+
shutil.rmtree(pycache_dir)
|
|
13
|
+
print(f"Removed: {pycache_dir}")
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import sys
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
def generate_markdown_from_py_files(directory, output_file):
|
|
6
|
+
with open(output_file, "w", encoding="utf-8") as md_file:
|
|
7
|
+
for root, dirs, files in os.walk(directory):
|
|
8
|
+
# 排除 venv 目录
|
|
9
|
+
dirs[:] = [d for d in dirs if d != ".venv"]
|
|
10
|
+
dirs[:] = [d for d in dirs if d != ".vscode"]
|
|
11
|
+
dirs[:] = [d for d in dirs if d != "scripts"]
|
|
12
|
+
dirs[:] = [d for d in dirs if d != "build"]
|
|
13
|
+
for file in files:
|
|
14
|
+
if file.endswith(".py") or file.endswith(".rs"):
|
|
15
|
+
file_path = os.path.join(root, file)
|
|
16
|
+
md_file.write(f"`{file_path}`\n")
|
|
17
|
+
md_file.write("```python\n")
|
|
18
|
+
with open(file_path, "r", encoding="utf-8") as py_file:
|
|
19
|
+
md_file.write(py_file.read())
|
|
20
|
+
md_file.write("\n```\n\n")
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
if __name__ == "__main__":
|
|
24
|
+
# 指定目录和输出文件名
|
|
25
|
+
target_directory = sys.argv[1] # 替换为你的目标目录
|
|
26
|
+
output_markdown_file = "output.md" # 输出的 Markdown 文件名
|
|
27
|
+
|
|
28
|
+
generate_markdown_from_py_files(target_directory, output_markdown_file)
|
|
29
|
+
print(f"Markdown 文件已生成:{output_markdown_file}")
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
from .python_to_sqlite import PythonToSQLite
|
|
2
|
+
from .sqlite_module_importer import SQLiteModuleImporter
|
|
3
|
+
from .sqlite_module_loader import SQLiteModuleLoader
|
|
4
|
+
from .sqlite_meta_path_finder import SQLiteMetaPathFinder
|
|
5
|
+
|
|
6
|
+
__all__ = [
|
|
7
|
+
"PythonToSQLite",
|
|
8
|
+
"SQLiteModuleImporter",
|
|
9
|
+
"SQLiteModuleLoader",
|
|
10
|
+
"SQLiteMetaPathFinder",
|
|
11
|
+
]
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import argparse
|
|
2
|
+
from pathlib import Path
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
def main():
|
|
6
|
+
parser = argparse.ArgumentParser(description="Python模块SQLite打包工具")
|
|
7
|
+
subparsers = parser.add_subparsers(dest="command", help="命令")
|
|
8
|
+
|
|
9
|
+
# 打包命令
|
|
10
|
+
pack_parser = subparsers.add_parser("pack", help="打包模块到SQLite")
|
|
11
|
+
pack_parser.add_argument("source", help="源文件或目录")
|
|
12
|
+
pack_parser.add_argument("--db", default="modules.db", help="数据库文件路径")
|
|
13
|
+
pack_parser.add_argument("--name", help="模块名(默认从文件名推断)")
|
|
14
|
+
|
|
15
|
+
# 列出命令
|
|
16
|
+
list_parser = subparsers.add_parser("list", help="列出数据库中的模块")
|
|
17
|
+
list_parser.add_argument("--db", default="modules.db", help="数据库文件路径")
|
|
18
|
+
|
|
19
|
+
# 删除原代码
|
|
20
|
+
delete_source_parser = subparsers.add_parser(
|
|
21
|
+
"delete_source", help="删除数据库中的源代码"
|
|
22
|
+
)
|
|
23
|
+
delete_source_parser.add_argument(
|
|
24
|
+
"--db", default="modules.db", help="数据库文件路径"
|
|
25
|
+
)
|
|
26
|
+
delete_source_parser.add_argument("--module", help="要删除的模块名")
|
|
27
|
+
|
|
28
|
+
# 安装命令
|
|
29
|
+
install_parser = subparsers.add_parser("install", help="安装SQLite导入器")
|
|
30
|
+
install_parser.add_argument("--db", default="modules.db", help="数据库文件路径")
|
|
31
|
+
|
|
32
|
+
args = parser.parse_args()
|
|
33
|
+
|
|
34
|
+
if args.command == "pack":
|
|
35
|
+
from .python_to_sqlite import PythonToSQLite
|
|
36
|
+
|
|
37
|
+
packer = PythonToSQLite(args.db)
|
|
38
|
+
|
|
39
|
+
if Path(args.source).is_dir():
|
|
40
|
+
packer.pack_directory(args.source)
|
|
41
|
+
print(f"已打包目录: {args.source}")
|
|
42
|
+
else:
|
|
43
|
+
packer.pack_module(args.source, args.name)
|
|
44
|
+
print(f"已打包模块: {args.source}")
|
|
45
|
+
|
|
46
|
+
elif args.command == "list":
|
|
47
|
+
from .python_to_sqlite import PythonToSQLite
|
|
48
|
+
|
|
49
|
+
packer = PythonToSQLite(args.db)
|
|
50
|
+
modules = packer.list_modules()
|
|
51
|
+
|
|
52
|
+
print(f"数据库中的模块 ({args.db}):")
|
|
53
|
+
for module in modules:
|
|
54
|
+
package_flag = " [包]" if module["is_package"] else ""
|
|
55
|
+
print(f" - {module['module_name']}{package_flag}")
|
|
56
|
+
|
|
57
|
+
elif args.command == "delete_source":
|
|
58
|
+
from .python_to_sqlite import PythonToSQLite
|
|
59
|
+
|
|
60
|
+
packer = PythonToSQLite(args.db)
|
|
61
|
+
if args.module:
|
|
62
|
+
packer.delete_source_code(args.module)
|
|
63
|
+
else:
|
|
64
|
+
packer.delete_source_code(None)
|
|
65
|
+
|
|
66
|
+
elif args.command == "install":
|
|
67
|
+
from .python_to_sqlite import PythonToSQLite
|
|
68
|
+
|
|
69
|
+
packer = PythonToSQLite(args.db)
|
|
70
|
+
packer.install_importer()
|
|
71
|
+
print(f"已安装SQLite导入器,可以从数据库导入模块了!")
|
|
72
|
+
|
|
73
|
+
# 保持程序运行以便交互使用
|
|
74
|
+
import code
|
|
75
|
+
|
|
76
|
+
code.interact(local=locals())
|
|
77
|
+
|
|
78
|
+
else:
|
|
79
|
+
parser.print_help()
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
if __name__ == "__main__":
|
|
83
|
+
main()
|