Skip to content

Commit 4ea1495

Browse files
committed
1.中英双语文档 2.对路径不存在等情况进行处理
1 parent 65ab439 commit 4ea1495

File tree

4 files changed

+163
-107
lines changed

4 files changed

+163
-107
lines changed

AutoCython/AutoCython.py

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,36 @@
1+
import os
12
from .run_tasks import run_tasks
23
from .compile import compile_to_binary
3-
from .tools import parse_arguments, find_python_files, show_not_find_file
4+
from .tools import parse_arguments, find_python_files
5+
from .tools import show_path_not_find_file, show_file_find_file, show_path_find_file
46

57
def compile():
68
args = parse_arguments()
79

810
if args.file:
9-
compile_file = args.file
10-
del_source = args.del_source
11-
tasks = [
12-
# 函数, 位置参数, 关键字参数
13-
(compile_to_binary, compile_file, (compile_file, del_source), {}),
14-
]
15-
run_tasks(tasks, max_workers=1) # 执行编译
11+
if os.path.isfile(args.file):
12+
compile_file = args.file
13+
del_source = args.del_source
14+
tasks = [
15+
# 函数, 位置参数, 关键字参数
16+
(compile_to_binary, compile_file, (compile_file, del_source), {}),
17+
]
18+
run_tasks(tasks, max_workers=1) # 执行编译
19+
else:
20+
show_file_find_file(args.file)
1621
elif args.path:
17-
compile_file_list = find_python_files(args.path)
18-
del_source = args.del_source
19-
if compile_file_list:
20-
tasks = []
21-
for compile_file in compile_file_list:
22-
tasks.append(
23-
# 函数, 位置参数, 关键字参数
24-
(compile_to_binary, compile_file, (compile_file, del_source), {}),
25-
)
26-
run_tasks(tasks, max_workers=args.conc) # 执行编译
22+
if os.path.exists(args.path) and not os.path.isfile(args.path):
23+
compile_file_list = find_python_files(args.path)
24+
if compile_file_list:
25+
del_source = args.del_source
26+
tasks = []
27+
for compile_file in compile_file_list:
28+
tasks.append(
29+
# 函数, 位置参数, 关键字参数
30+
(compile_to_binary, compile_file, (compile_file, del_source), {}),
31+
)
32+
run_tasks(tasks, max_workers=args.conc) # 执行编译
33+
else:
34+
show_path_not_find_file(args.path)
2735
else:
28-
show_not_find_file()
36+
show_path_find_file(args.path)

AutoCython/tools.py

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,18 +127,41 @@ def error(self, message):
127127

128128
return args
129129

130-
def show_not_find_file():
131-
# 获取系统语言
130+
def show_path_not_find_file(path):
131+
try:
132+
sys_language, _ = locale.getdefaultlocale()
133+
lang = 'zh' if sys_language and sys_language.startswith('zh') else 'en'
134+
except Exception:
135+
lang = 'en' # 异常时默认英文
136+
137+
if lang == 'zh':
138+
print(f"{path} 目录下没有任何需要编译的文件!")
139+
else:
140+
print(f"The {path} directory does not contain any files that need to be compiled!")
141+
142+
def show_file_find_file(file):
143+
try:
144+
sys_language, _ = locale.getdefaultlocale()
145+
lang = 'zh' if sys_language and sys_language.startswith('zh') else 'en'
146+
except Exception:
147+
lang = 'en' # 异常时默认英文
148+
149+
if lang == 'zh':
150+
print(f"文件 {file} 不存在!")
151+
else:
152+
print(f"File {file} does not exist!")
153+
154+
def show_path_find_file(path):
132155
try:
133156
sys_language, _ = locale.getdefaultlocale()
134157
lang = 'zh' if sys_language and sys_language.startswith('zh') else 'en'
135158
except Exception:
136159
lang = 'en' # 异常时默认英文
137160

138161
if lang == 'zh':
139-
print("没有找到任何需要编译的文件!")
162+
print(f"路径 {path} 不存在!")
140163
else:
141-
print("No files found that need to be compiled!")
164+
print(f"Path {path} does not exist!")
142165

143166
if __name__ == "__main__":
144167
args = parse_arguments()

README.en.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# AutoCython
2+
[中文](https://github.com/EVA-JianJun/AutoCython/blob/master/README.md) | English
3+
4+
**AutoCython: Batch Compile Python Files to PYD Files with One Click**
5+
![py_pyd](https://raw.githubusercontent.com/EVA-JianJun/GitPigBed/master/blog_files/img/AutoCython_20210824.png)
6+
7+
## ✨ Key Features
8+
- Single-file/Multi-file batch compilation
9+
- Cross-platform support (Windows/Linux/MacOS)
10+
- Clean command-line interface
11+
12+
## 📦 Installation
13+
```bash
14+
pip install AutoCython-jianjun
15+
```
16+
17+
## ⚙️ Dependencies
18+
### C/C++ Compiler Requirements
19+
- **Windows**: Visual Studio
20+
- **Linux**: gcc & g++
21+
22+
**Important**: Compiler architecture must match Python interpreter (64-bit Python requires 64-bit compiler)
23+
24+
> For other compiler configurations, refer to [Cython](https://github.com/cython/cython) project
25+
26+
## 🚀 Usage Guide
27+
### Command Line Operations
28+
![CLI Demo](https://raw.githubusercontent.com/EVA-JianJun/GitPigBed/master/blog_files/img/AutoCython_20250609_3.png)
29+
30+
```bash
31+
# Compile single file
32+
AutoCython -f test.py
33+
34+
# Compile entire directory
35+
AutoCython -p D:/python_code/ProjectPath
36+
37+
# Delete source after compilation (disabled by default)
38+
AutoCython -d True -f test.py
39+
AutoCython -d True -p D:/python_code/ProjectPath
40+
```
41+
42+
### Compilation Interface
43+
![AutoCython GUI](https://raw.githubusercontent.com/EVA-JianJun/GitPigBed/master/blog_files/img/AutoCython_20250609_4.png)
44+
45+
### Manually Exclude Files from Compilation
46+
Add declaration in the **first two lines** of file header:
47+
```python
48+
# AutoCython No Compile
49+
# This file will be skipped during compilation
50+
```
51+
52+
## ⚠️ Troubleshooting
53+
54+
Typically caused by Cython-unsupported statements or invalid filenames.
55+
Consult [Cython Wiki](https://github.com/cython/cython/wiki) official documentation or submit an Issue.
56+
57+
## 📅 Changelog
58+
### V2 Releases
59+
1. 20250609 release V2.0.0: Codebase refactored with new UI
60+
61+
### V1 Releases
62+
1. 20220613: Added Linux support (requires gcc & g++ configuration)
63+
2. 20221123: Added manual file exclusion via file header comments
64+
3. 20230306: Added CLI header specification (e.g., `Python310`) for non-Windows compilation
65+
4. 20230324: Documentation updates
66+
5. 20240506: Fixed issue with `__init__.py` restoration after failed compilation

README.md

Lines changed: 43 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,107 +1,66 @@
11
# AutoCython
2+
中文 | [English](https://github.com/EVA-JianJun/AutoCython/blob/master/README.en.md)
23

3-
**自动 Cython, 使用 Cython 批量编译 `.py` 文件为 `.pyd` 文件!**
4-
![py_pyd][1]
4+
**自动 Cython:一键将 Python 文件批量编译为 PYD 文件**
5+
![py_pyd](https://raw.githubusercontent.com/EVA-JianJun/GitPigBed/master/blog_files/img/AutoCython_20210824.png)
56

6-
## 安装
7+
## ✨ 特性亮点
8+
- 单文件/多文件批量编译
9+
- 跨平台支持 (Windows/Linux/MacOS)
10+
- 简洁命令行界面
711

8-
pip install AutoCython-jianjun
9-
10-
## 依赖
11-
12-
[Cython](https://github.com/cython/cython) 和 C/C++ 编译器
13-
14-
**如果你都配置完毕了你可以跳过本节.**
15-
16-
### Cython
17-
18-
```
19-
pip install Cython
12+
## 📦 安装
13+
```bash
14+
pip install AutoCython-jianjun
2015
```
2116

17+
## ⚙️ 依赖环境
2218
### C/C++ 编译器
19+
- **Windows**: Visual Studio
20+
- **Linux**: gcc & g++
2321

24-
推荐 `gcc``visual studio` 选一个, 其他编译器请查阅 Cython 文档.
25-
26-
* `VS` 安装简便, 占用空间大, 配置简单.
27-
* `gcc` 安装简便, 占用空间小, 配置比较复杂.
28-
29-
不想折腾或者你是 Windows 的话推荐安装 VS, Linux 等系统可以直接使用自带的 gcc。
30-
31-
大部分的编译环境都可以使用, 我在 Win, Linux, Mac, Python3.6 到 Python3.10 都可以使用.
32-
33-
**需要注意的是, 如果你使用的 Python 是64位的, 那么对应的 C、C++ 编译器也必须为64位;**
34-
35-
**win下gcc推荐安装MinGW**
36-
37-
* 64位:<http://mingw-w64.org/>
38-
* 32位:<http://www.mingw.org/>
22+
**重要提示**:编译器架构必须与Python解释器一致(64位Python需64位编译器)
3923

40-
## 使用
24+
> 其他编译器配置请参考 [Cython](https://github.com/cython/cython) 项目
4125
42-
![命令行][11]
26+
## 🚀 使用指南
27+
### 命令行操作
28+
![命令行演示](https://raw.githubusercontent.com/EVA-JianJun/GitPigBed/master/blog_files/img/AutoCython_20250609_1.png)
4329

44-
### 命令行
30+
```bash
31+
# 编译单个文件
32+
AutoCython -f test.py
4533

46-
使用命令行进行编译:
34+
# 编译整个目录
35+
AutoCython -p D:/python_code/ProjectPath
4736

48-
`AutoCython -h` 或者 `AutoCython --ch` 查看英文和中文帮助.
49-
50-
# 编译一个文件
51-
AutoCython -f test.py
52-
53-
# 编译一个目录
54-
AutoCython -C D:/python_code/ProjectPath
55-
56-
![AutoCython][2]
57-
58-
**例子**
59-
60-
编译目录 `D:/python_code/ProjectPath`\
61-
排除所有名为 `tmp.py` 的文件\
62-
排除 `./ProjectPath/print_cy.py` 文件\
63-
排除 `./ProjectPath/data/tmp` 目录下的文件不编译\
64-
使用2个CPU核心\
65-
只删除编译后产生的 `build` 文件夹和中间文件 `setup_file`, 保留自动生成的 `C` 代码.\
66-
67-
AutoCython -C D:/python_code/ProjectPath -E tmp.py;./ProjectPath/print_cy.py;./ProjectPath/data/tmp -M 2 -D bp
68-
69-
## 特性
70-
71-
* **全自动**:自动编译当前目录下所有.py文件, 支持指定目录编译或单文件编译;
72-
* **个性化**:支持指定排除目录或排除文件跳过编译;
73-
* **高效率**:默认启动进程数为cpu核心数四分之一, 大多数情况下可以把cpu跑满;
74-
* **易纠错**:快速纠错, 在编译失败时能极快的获取错误信息;
75-
76-
### 常见错误
37+
# 编译后删除源代码 (默认不删除)
38+
AutoCython -d True -f test.py
39+
AutoCython -d True -p D:/python_code/ProjectPath
40+
```
7741

78-
* `print(end='')`\
79-
Cython 不支持 print 函数的 end 参数, 可以在外部定义 `def my_print(*args, **kwargs): print(*args, end='', **kwargs)`, 然后在需要编译的文件中导入这个外部函数.
42+
### 编译界面
43+
![AutoCython GUI](https://raw.githubusercontent.com/EVA-JianJun/GitPigBed/master/blog_files/img/AutoCython_20250609_2.png)
8044

81-
* `新建文本文档.py`\
82-
文件名需要符合 `C` 命名规范
45+
### 手动排除文件不编译
46+
在文件头部 **前两行** 添加声明:
47+
```python
48+
# AutoCython No Compile
49+
# 此文件将跳过编译处理
50+
```
8351

84-
其他问题请查阅 `Cython` 官方文档.
52+
## ⚠️ 常见问题解决
8553

86-
### 手动指定不编译
54+
一般是源代码中有 Cython 不支持的语句, 或者文件名不支持等.
55+
可以查阅 [Cython Wiki](https://github.com/cython/cython/wiki) 项目 官方文档, 或者提 Issue.
8756

88-
在不需要编译的文件 **头两行** 任意一行写上 `# AucoCython No Compile` 则该文件会跳过编译.
57+
## 📅 更新记录
58+
### V2 版本
59+
1. 20250609 release V2.0.0 重构了代码, 使用新的界面
8960

90-
## 更新记录
61+
### V1 版本
9162
1. 20220613 更新对Linux的支持, Linux下需要配置gcc&g++
9263
2. 20221123 可以通过文件头手动指定不编译的文件
9364
3. 20230306 更新可以指定命令行头如 `Python310` 以此支持非Widnows系统下编译
9465
4. 20230324 更新文档
95-
5. 20240506 修复编译失败时遗漏复原 \_\_init\_\_.py 的问题
96-
97-
[1]: https://raw.githubusercontent.com/EVA-JianJun/GitPigBed/master/blog_files/img/AutoCython_20210824.png
98-
[2]: https://raw.githubusercontent.com/EVA-JianJun/GitPigBed/master/blog_files/img/AutoCython_20200316_2.jpg
99-
[3]: https://raw.githubusercontent.com/EVA-JianJun/GitPigBed/master/blog_files/img/AutoCython_20200316_3.jpg
100-
[4]: https://raw.githubusercontent.com/EVA-JianJun/GitPigBed/master/blog_files/img/AutoCython_20200316_4.jpg
101-
[5]: https://raw.githubusercontent.com/EVA-JianJun/GitPigBed/master/blog_files/img/AutoCython_20200316_5.jpg
102-
[6]: https://raw.githubusercontent.com/EVA-JianJun/GitPigBed/master/blog_files/img/AutoCython_20200316_6.jpg
103-
[7]: https://raw.githubusercontent.com/EVA-JianJun/GitPigBed/master/blog_files/img/AutoCython_20200316_7.jpg
104-
[8]: https://raw.githubusercontent.com/EVA-JianJun/GitPigBed/master/blog_files/img/AutoCython_20200316_8.jpg
105-
[9]: https://github.com/EVA-JianJun/AutoCython/releases
106-
[10]: https://raw.githubusercontent.com/EVA-JianJun/GitPigBed/master/blog_files/img/AutoCython_20200316_10.jpg
107-
[11]: https://raw.githubusercontent.com/EVA-JianJun/GitPigBed/master/blog_files/img/AutoCython_20200316_11.jpg
66+
5. 20240506 修复编译失败时遗漏复原 \_\_init\_\_.py 的问题

0 commit comments

Comments
 (0)