gl-py2pyd 0.1.1__tar.gz → 0.2.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.
@@ -1,30 +1,23 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: gl-py2pyd
3
- Version: 0.1.1
3
+ Version: 0.2.0
4
4
  Summary: 将Python文件转换为pyd/so文件的工具
5
5
  Home-page: https://github.com/youjunxiaji/py2pyd-arg
6
6
  Author: gu lei
7
7
  Author-email: youjunxiaji@gmail.com
8
- Classifier: Programming Language :: Python :: 3
9
- Classifier: License :: OSI Approved :: MIT License
10
- Classifier: Operating System :: OS Independent
11
- Requires-Python: >=3.6
8
+ Requires-Python: >=3.9
12
9
  Description-Content-Type: text/markdown
13
10
  Requires-Dist: cython
14
- Requires-Dist: loguru
11
+ Requires-Dist: rich
12
+ Requires-Dist: setuptools
15
13
  Dynamic: author
16
14
  Dynamic: author-email
17
- Dynamic: classifier
18
- Dynamic: description
19
- Dynamic: description-content-type
20
15
  Dynamic: home-page
21
- Dynamic: requires-dist
22
16
  Dynamic: requires-python
23
- Dynamic: summary
24
17
 
25
- # Python转pyd/so工具
18
+ # py2pyd - Python转pyd/so工具
26
19
 
27
- 这个工具用于将Python文件(.py)转换为编译后的Python扩展模块(.pyd或.so),使用Cython进行编译。
20
+ Python文件(.py)转换为编译后的Python扩展模块(.pyd或.so),使用Cython进行编译。
28
21
 
29
22
  ## 功能特点
30
23
 
@@ -34,57 +27,61 @@ Dynamic: summary
34
27
  - 转换后可选择是否删除原始Python文件
35
28
  - 保留`__init__.py`文件内容处理
36
29
  - 自动检测依赖并提供错误处理
37
- - 针对不同操作系统自动选择正确的扩展名(Windows为.pyd,Linux/MacOS为.so)
38
- - 优化编译选项,消除常见警告信息
30
+ - 针对不同操作系统自动选择正确的扩展名(Windows为.pyd,Linux/macOS为.so)
31
+ - 美观的进度条显示
39
32
 
40
- ## 安装依赖
41
-
42
- 在使用此工具前,请确保已安装以下依赖:
33
+ ## 安装
43
34
 
44
35
  ```bash
45
- pip install cython loguru
36
+ pip install gl-py2pyd
46
37
  ```
47
38
 
48
- 此外,你的系统需要有合适的C/C++编译器:
49
- - Windows: 需要安装Visual C++ Build Tools
50
- - Linux: 需要安装GCC
51
- - macOS: 需要安装XCode命令行工具(`xcode-select --install`)
39
+ ### 系统要求
40
+
41
+ 你的系统需要有合适的C/C++编译器:
42
+ - **Windows**: 需要安装 Visual C++ Build Tools
43
+ - **Linux**: 需要安装 GCC
44
+ - **macOS**: 需要安装 XCode 命令行工具(`xcode-select --install`)
52
45
 
53
46
  ## 使用方法
54
47
 
55
- ### 1. 转换单个文件
48
+ ### 命令行使用
56
49
 
57
50
  ```bash
58
- python py2pyd.py path/to/your/file.py
59
- ```
51
+ # 转换单个文件
52
+ py2pyd file.py
60
53
 
61
- ### 2. 转换目录下所有Python文件
54
+ # 转换目录下所有Python文件
55
+ py2pyd folder/
62
56
 
63
- ```bash
64
- python py2pyd.py path/to/your/directory
65
- ```
57
+ # 递归转换目录及其子目录中的所有Python文件
58
+ py2pyd -r folder/
66
59
 
67
- ### 3. 递归转换目录及其子目录中的所有Python文件
60
+ # 转换后删除原始Python文件
61
+ py2pyd --remove file.py
68
62
 
69
- ```bash
70
- python py2pyd.py -r path/to/your/directory
71
- ```
63
+ # 组合使用
64
+ py2pyd -r --remove folder/
72
65
 
73
- ### 4. 转换后删除原始Python文件
66
+ # 查看版本
67
+ py2pyd -v
74
68
 
75
- ```bash
76
- python py2pyd.py --remove path/to/your/file.py
69
+ # 查看帮助
70
+ py2pyd -h
77
71
  ```
78
72
 
79
- ### 5. 全部选项组合
73
+ ### 输出示例
80
74
 
81
- ```bash
82
- python py2pyd.py -r --remove path/to/your/directory
83
75
  ```
76
+ 📄 处理文件: test.py
77
+ 转换进度 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 100%
84
78
 
85
- ## 作为模块导入
79
+ 处理完成!成功: 1 个文件
86
80
 
87
- 你也可以在自己的Python代码中导入该模块:
81
+ 🎉 全部转换成功!
82
+ ```
83
+
84
+ ### 作为模块导入
88
85
 
89
86
  ```python
90
87
  # 转换单个文件
@@ -106,8 +103,8 @@ print(f"成功:{converter.success_count} 个文件,失败:{converter.fail_
106
103
 
107
104
  工具会自动检测必要的依赖并提供错误信息。常见问题包括:
108
105
 
109
- 1. **缺少Cython**: 运行前会检查Cython是否已安装,若未安装则会提示安装。
110
- 2. **编译失败**: 如果编译失败,会提供详细错误信息,可能是缺少编译器或者Python文件内容有问题。
106
+ 1. **缺少Cython**: 运行前会检查Cython是否已安装,若未安装则会提示安装
107
+ 2. **编译失败**: 可能是缺少编译器或者Python文件内容有问题
111
108
 
112
109
  ## 优化的编译选项
113
110
 
@@ -121,5 +118,9 @@ print(f"成功:{converter.success_count} 个文件,失败:{converter.fail_
121
118
 
122
119
  - 确保已安装Cython和适当的编译器
123
120
  - 转换后的so/pyd文件会保留在原始Python文件的相同目录中
124
- - 在Windows系统上生成.pyd文件,在Linux/MacOS上生成.so文件
125
- - 对于复杂的Python项目,可能需要额外的编译选项
121
+ - 在Windows系统上生成.pyd文件,在Linux/macOS上生成.so文件
122
+ - 对于复杂的Python项目,可能需要额外的编译选项
123
+
124
+ ## License
125
+
126
+ MIT License
@@ -1,6 +1,6 @@
1
- # Python转pyd/so工具
1
+ # py2pyd - Python转pyd/so工具
2
2
 
3
- 这个工具用于将Python文件(.py)转换为编译后的Python扩展模块(.pyd或.so),使用Cython进行编译。
3
+ Python文件(.py)转换为编译后的Python扩展模块(.pyd或.so),使用Cython进行编译。
4
4
 
5
5
  ## 功能特点
6
6
 
@@ -10,57 +10,61 @@
10
10
  - 转换后可选择是否删除原始Python文件
11
11
  - 保留`__init__.py`文件内容处理
12
12
  - 自动检测依赖并提供错误处理
13
- - 针对不同操作系统自动选择正确的扩展名(Windows为.pyd,Linux/MacOS为.so)
14
- - 优化编译选项,消除常见警告信息
13
+ - 针对不同操作系统自动选择正确的扩展名(Windows为.pyd,Linux/macOS为.so)
14
+ - 美观的进度条显示
15
15
 
16
- ## 安装依赖
17
-
18
- 在使用此工具前,请确保已安装以下依赖:
16
+ ## 安装
19
17
 
20
18
  ```bash
21
- pip install cython loguru
19
+ pip install gl-py2pyd
22
20
  ```
23
21
 
24
- 此外,你的系统需要有合适的C/C++编译器:
25
- - Windows: 需要安装Visual C++ Build Tools
26
- - Linux: 需要安装GCC
27
- - macOS: 需要安装XCode命令行工具(`xcode-select --install`)
22
+ ### 系统要求
23
+
24
+ 你的系统需要有合适的C/C++编译器:
25
+ - **Windows**: 需要安装 Visual C++ Build Tools
26
+ - **Linux**: 需要安装 GCC
27
+ - **macOS**: 需要安装 XCode 命令行工具(`xcode-select --install`)
28
28
 
29
29
  ## 使用方法
30
30
 
31
- ### 1. 转换单个文件
31
+ ### 命令行使用
32
32
 
33
33
  ```bash
34
- python py2pyd.py path/to/your/file.py
35
- ```
34
+ # 转换单个文件
35
+ py2pyd file.py
36
36
 
37
- ### 2. 转换目录下所有Python文件
37
+ # 转换目录下所有Python文件
38
+ py2pyd folder/
38
39
 
39
- ```bash
40
- python py2pyd.py path/to/your/directory
41
- ```
40
+ # 递归转换目录及其子目录中的所有Python文件
41
+ py2pyd -r folder/
42
42
 
43
- ### 3. 递归转换目录及其子目录中的所有Python文件
43
+ # 转换后删除原始Python文件
44
+ py2pyd --remove file.py
44
45
 
45
- ```bash
46
- python py2pyd.py -r path/to/your/directory
47
- ```
46
+ # 组合使用
47
+ py2pyd -r --remove folder/
48
48
 
49
- ### 4. 转换后删除原始Python文件
49
+ # 查看版本
50
+ py2pyd -v
50
51
 
51
- ```bash
52
- python py2pyd.py --remove path/to/your/file.py
52
+ # 查看帮助
53
+ py2pyd -h
53
54
  ```
54
55
 
55
- ### 5. 全部选项组合
56
+ ### 输出示例
56
57
 
57
- ```bash
58
- python py2pyd.py -r --remove path/to/your/directory
59
58
  ```
59
+ 📄 处理文件: test.py
60
+ 转换进度 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 100%
60
61
 
61
- ## 作为模块导入
62
+ 处理完成!成功: 1 个文件
62
63
 
63
- 你也可以在自己的Python代码中导入该模块:
64
+ 🎉 全部转换成功!
65
+ ```
66
+
67
+ ### 作为模块导入
64
68
 
65
69
  ```python
66
70
  # 转换单个文件
@@ -82,8 +86,8 @@ print(f"成功:{converter.success_count} 个文件,失败:{converter.fail_
82
86
 
83
87
  工具会自动检测必要的依赖并提供错误信息。常见问题包括:
84
88
 
85
- 1. **缺少Cython**: 运行前会检查Cython是否已安装,若未安装则会提示安装。
86
- 2. **编译失败**: 如果编译失败,会提供详细错误信息,可能是缺少编译器或者Python文件内容有问题。
89
+ 1. **缺少Cython**: 运行前会检查Cython是否已安装,若未安装则会提示安装
90
+ 2. **编译失败**: 可能是缺少编译器或者Python文件内容有问题
87
91
 
88
92
  ## 优化的编译选项
89
93
 
@@ -97,5 +101,9 @@ print(f"成功:{converter.success_count} 个文件,失败:{converter.fail_
97
101
 
98
102
  - 确保已安装Cython和适当的编译器
99
103
  - 转换后的so/pyd文件会保留在原始Python文件的相同目录中
100
- - 在Windows系统上生成.pyd文件,在Linux/MacOS上生成.so文件
101
- - 对于复杂的Python项目,可能需要额外的编译选项
104
+ - 在Windows系统上生成.pyd文件,在Linux/macOS上生成.so文件
105
+ - 对于复杂的Python项目,可能需要额外的编译选项
106
+
107
+ ## License
108
+
109
+ MIT License
@@ -1,30 +1,23 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: gl-py2pyd
3
- Version: 0.1.1
3
+ Version: 0.2.0
4
4
  Summary: 将Python文件转换为pyd/so文件的工具
5
5
  Home-page: https://github.com/youjunxiaji/py2pyd-arg
6
6
  Author: gu lei
7
7
  Author-email: youjunxiaji@gmail.com
8
- Classifier: Programming Language :: Python :: 3
9
- Classifier: License :: OSI Approved :: MIT License
10
- Classifier: Operating System :: OS Independent
11
- Requires-Python: >=3.6
8
+ Requires-Python: >=3.9
12
9
  Description-Content-Type: text/markdown
13
10
  Requires-Dist: cython
14
- Requires-Dist: loguru
11
+ Requires-Dist: rich
12
+ Requires-Dist: setuptools
15
13
  Dynamic: author
16
14
  Dynamic: author-email
17
- Dynamic: classifier
18
- Dynamic: description
19
- Dynamic: description-content-type
20
15
  Dynamic: home-page
21
- Dynamic: requires-dist
22
16
  Dynamic: requires-python
23
- Dynamic: summary
24
17
 
25
- # Python转pyd/so工具
18
+ # py2pyd - Python转pyd/so工具
26
19
 
27
- 这个工具用于将Python文件(.py)转换为编译后的Python扩展模块(.pyd或.so),使用Cython进行编译。
20
+ Python文件(.py)转换为编译后的Python扩展模块(.pyd或.so),使用Cython进行编译。
28
21
 
29
22
  ## 功能特点
30
23
 
@@ -34,57 +27,61 @@ Dynamic: summary
34
27
  - 转换后可选择是否删除原始Python文件
35
28
  - 保留`__init__.py`文件内容处理
36
29
  - 自动检测依赖并提供错误处理
37
- - 针对不同操作系统自动选择正确的扩展名(Windows为.pyd,Linux/MacOS为.so)
38
- - 优化编译选项,消除常见警告信息
30
+ - 针对不同操作系统自动选择正确的扩展名(Windows为.pyd,Linux/macOS为.so)
31
+ - 美观的进度条显示
39
32
 
40
- ## 安装依赖
41
-
42
- 在使用此工具前,请确保已安装以下依赖:
33
+ ## 安装
43
34
 
44
35
  ```bash
45
- pip install cython loguru
36
+ pip install gl-py2pyd
46
37
  ```
47
38
 
48
- 此外,你的系统需要有合适的C/C++编译器:
49
- - Windows: 需要安装Visual C++ Build Tools
50
- - Linux: 需要安装GCC
51
- - macOS: 需要安装XCode命令行工具(`xcode-select --install`)
39
+ ### 系统要求
40
+
41
+ 你的系统需要有合适的C/C++编译器:
42
+ - **Windows**: 需要安装 Visual C++ Build Tools
43
+ - **Linux**: 需要安装 GCC
44
+ - **macOS**: 需要安装 XCode 命令行工具(`xcode-select --install`)
52
45
 
53
46
  ## 使用方法
54
47
 
55
- ### 1. 转换单个文件
48
+ ### 命令行使用
56
49
 
57
50
  ```bash
58
- python py2pyd.py path/to/your/file.py
59
- ```
51
+ # 转换单个文件
52
+ py2pyd file.py
60
53
 
61
- ### 2. 转换目录下所有Python文件
54
+ # 转换目录下所有Python文件
55
+ py2pyd folder/
62
56
 
63
- ```bash
64
- python py2pyd.py path/to/your/directory
65
- ```
57
+ # 递归转换目录及其子目录中的所有Python文件
58
+ py2pyd -r folder/
66
59
 
67
- ### 3. 递归转换目录及其子目录中的所有Python文件
60
+ # 转换后删除原始Python文件
61
+ py2pyd --remove file.py
68
62
 
69
- ```bash
70
- python py2pyd.py -r path/to/your/directory
71
- ```
63
+ # 组合使用
64
+ py2pyd -r --remove folder/
72
65
 
73
- ### 4. 转换后删除原始Python文件
66
+ # 查看版本
67
+ py2pyd -v
74
68
 
75
- ```bash
76
- python py2pyd.py --remove path/to/your/file.py
69
+ # 查看帮助
70
+ py2pyd -h
77
71
  ```
78
72
 
79
- ### 5. 全部选项组合
73
+ ### 输出示例
80
74
 
81
- ```bash
82
- python py2pyd.py -r --remove path/to/your/directory
83
75
  ```
76
+ 📄 处理文件: test.py
77
+ 转换进度 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 100%
84
78
 
85
- ## 作为模块导入
79
+ 处理完成!成功: 1 个文件
86
80
 
87
- 你也可以在自己的Python代码中导入该模块:
81
+ 🎉 全部转换成功!
82
+ ```
83
+
84
+ ### 作为模块导入
88
85
 
89
86
  ```python
90
87
  # 转换单个文件
@@ -106,8 +103,8 @@ print(f"成功:{converter.success_count} 个文件,失败:{converter.fail_
106
103
 
107
104
  工具会自动检测必要的依赖并提供错误信息。常见问题包括:
108
105
 
109
- 1. **缺少Cython**: 运行前会检查Cython是否已安装,若未安装则会提示安装。
110
- 2. **编译失败**: 如果编译失败,会提供详细错误信息,可能是缺少编译器或者Python文件内容有问题。
106
+ 1. **缺少Cython**: 运行前会检查Cython是否已安装,若未安装则会提示安装
107
+ 2. **编译失败**: 可能是缺少编译器或者Python文件内容有问题
111
108
 
112
109
  ## 优化的编译选项
113
110
 
@@ -121,5 +118,9 @@ print(f"成功:{converter.success_count} 个文件,失败:{converter.fail_
121
118
 
122
119
  - 确保已安装Cython和适当的编译器
123
120
  - 转换后的so/pyd文件会保留在原始Python文件的相同目录中
124
- - 在Windows系统上生成.pyd文件,在Linux/MacOS上生成.so文件
125
- - 对于复杂的Python项目,可能需要额外的编译选项
121
+ - 在Windows系统上生成.pyd文件,在Linux/macOS上生成.so文件
122
+ - 对于复杂的Python项目,可能需要额外的编译选项
123
+
124
+ ## License
125
+
126
+ MIT License
@@ -0,0 +1,3 @@
1
+ cython
2
+ rich
3
+ setuptools
@@ -0,0 +1,92 @@
1
+ '''
2
+ Author: gu lei
3
+ Date: 2023-01-20 13:52:06
4
+ LastEditTime: 2023-09-26 10:43:03
5
+ LastEditors: gu lei
6
+ '''
7
+ import os
8
+ from rich.console import Console
9
+ from rich.progress import Progress, SpinnerColumn, TextColumn, BarColumn, TaskProgressColumn
10
+ from .single_py2pyd import py2pyd
11
+
12
+ console = Console()
13
+
14
+ class FileConversion:
15
+
16
+ def __init__(self) -> None:
17
+ self.initpy = None
18
+ self.success_count = 0
19
+ self.fail_count = 0
20
+ self.failed_files = [] # 记录失败的文件
21
+
22
+ def get_all_file(self, path, need_remove: bool): # 遍历此目录下的所有py文件,包含子目录里的py
23
+ # 首先收集所有需要处理的文件
24
+ all_files = []
25
+ init_py_dirs = [] # 记录有 __init__.py 的目录
26
+
27
+ for root, dirs, files in os.walk(path):
28
+ if "__init__.py" in files:
29
+ init_py_dirs.append(root)
30
+ files = [f for f in files if f != "__init__.py"]
31
+ for name in files:
32
+ if name.endswith(".py"):
33
+ all_files.append(os.path.join(root, name))
34
+
35
+ if not all_files:
36
+ console.print("⚠️ [yellow]目录中没有找到 .py 文件[/yellow]")
37
+ return True
38
+
39
+ # 使用 rich 进度条
40
+ with Progress(
41
+ SpinnerColumn(),
42
+ TextColumn("[progress.description]{task.description}"),
43
+ BarColumn(),
44
+ TaskProgressColumn(),
45
+ console=console
46
+ ) as progress:
47
+ task = progress.add_task("转换进度", total=len(all_files))
48
+
49
+ for file_path in all_files:
50
+ # 处理 __init__.py
51
+ dir_path = os.path.dirname(file_path)
52
+ if dir_path in init_py_dirs and self.initpy is None:
53
+ self.process_init_py(dir_path)
54
+ init_py_dirs.remove(dir_path)
55
+
56
+ success = py2pyd(file_path)
57
+ if success:
58
+ self.success_count += 1
59
+ if need_remove:
60
+ os.remove(file_path)
61
+ else:
62
+ self.fail_count += 1
63
+ self.failed_files.append(file_path)
64
+
65
+ # 恢复 __init__.py
66
+ if self.initpy and dir_path not in init_py_dirs:
67
+ self.process_init_py(dir_path)
68
+
69
+ progress.update(task, advance=1)
70
+
71
+ # 显示结果
72
+ console.print()
73
+ if self.fail_count == 0:
74
+ console.print(f"✅ [bold green]处理完成!成功: {self.success_count} 个文件[/bold green]")
75
+ else:
76
+ console.print(f"⚠️ [yellow]处理完成!成功: {self.success_count} 个文件,失败: {self.fail_count} 个文件[/yellow]")
77
+ console.print("[red]失败的文件:[/red]")
78
+ for f in self.failed_files:
79
+ console.print(f" - {f}")
80
+
81
+ return self.success_count > 0 and self.fail_count == 0
82
+
83
+ def process_init_py(self, path):
84
+ init_py_path = os.path.join(path, '__init__.py')
85
+ if self.initpy:
86
+ with open(init_py_path, 'w', encoding='utf-8') as file:
87
+ file.write(self.initpy)
88
+ self.initpy = None
89
+ else:
90
+ with open(init_py_path, 'r', encoding='utf-8') as file:
91
+ self.initpy = file.read()
92
+ os.remove(init_py_path)
@@ -9,7 +9,6 @@ import shutil
9
9
  import glob
10
10
  import subprocess
11
11
  import sys
12
- from loguru import logger
13
12
 
14
13
  def py2pyd(path):
15
14
  """
@@ -17,11 +16,10 @@ def py2pyd(path):
17
16
 
18
17
  Args:
19
18
  path: Python文件路径
20
- """
21
- logger.info(f"==========================================")
22
- logger.info(f"开始处理文件: {path}")
23
- logger.info(f"当前工作目录: {os.getcwd()}")
24
19
 
20
+ Returns:
21
+ bool: 转换是否成功
22
+ """
25
23
  # 保存原始工作目录
26
24
  original_dir = os.getcwd()
27
25
  try:
@@ -29,8 +27,6 @@ def py2pyd(path):
29
27
  try:
30
28
  import Cython
31
29
  except ImportError:
32
- logger.error("缺少必要的依赖: Cython")
33
- logger.info("请先安装依赖: pip install cython")
34
30
  return False
35
31
 
36
32
  # 转换为绝对路径
@@ -39,12 +35,8 @@ def py2pyd(path):
39
35
  file_path: str = os.path.basename(abs_path) # 带py的文件名
40
36
  filename = file_path.split('.py')[0] # 不带py的文件名
41
37
 
42
- logger.info(f"目标文件夹路径: {folder_path}")
43
- logger.info(f"文件名: {file_path}")
44
-
45
38
  # 更改工作目录
46
39
  os.chdir(folder_path)
47
- logger.info(f"切换后的工作目录: {os.getcwd()}")
48
40
 
49
41
  # 根据系统配置编译参数和链接参数
50
42
  extra_compile_args = []
@@ -91,12 +83,11 @@ def py2pyd(path):
91
83
  f.write(' )\n')
92
84
  f.write(')\n')
93
85
 
94
- logger.info(f"开始执行 setup.py")
95
86
  try:
96
- subprocess.check_call(['python', 'setup.py', 'build_ext', '--inplace'])
97
- except subprocess.CalledProcessError as e:
98
- logger.error(f"执行setup.py失败: {e}")
99
- logger.error("请确保已正确安装Cython,并且Python环境中有适当的编译器")
87
+ # 隐藏编译输出,保持界面简洁
88
+ subprocess.check_call(['python', 'setup.py', 'build_ext', '--inplace'],
89
+ stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
90
+ except subprocess.CalledProcessError:
100
91
  # 清理临时文件
101
92
  if os.path.exists('setup.py'):
102
93
  os.remove('setup.py')
@@ -106,10 +97,8 @@ def py2pyd(path):
106
97
  is_windows = sys.platform.startswith('win')
107
98
  extension = '.pyd' if is_windows else '.so'
108
99
  output_file = f"{filename}{extension}"
109
- logger.info(f"目标文件名: {output_file}")
110
100
 
111
101
  if os.path.exists(output_file):
112
- logger.info(f"删除已存在的文件: {output_file}")
113
102
  os.remove(output_file) # 删除老的文件
114
103
 
115
104
  # 查找生成的扩展文件
@@ -119,16 +108,11 @@ def py2pyd(path):
119
108
  ext_files = glob.glob(filename + "*.so")
120
109
 
121
110
  if not ext_files:
122
- logger.error(f"未找到编译后的文件")
123
111
  return False
124
-
125
- logger.info(f"找到的文件: {ext_files}")
126
112
 
127
113
  os.rename(ext_files[0], output_file) # 改名字,删除多余的cp38-win_amd64.等
128
- logger.info(f"重命名文件完成")
129
114
 
130
115
  # 清理临时文件
131
- logger.info("开始清理临时文件")
132
116
  if os.path.exists('%s.c' % filename):
133
117
  os.remove('%s.c' % filename) # 删除临时文件
134
118
  if os.path.exists('build'):
@@ -138,18 +122,14 @@ def py2pyd(path):
138
122
  if os.path.exists('__pycache__'):
139
123
  shutil.rmtree('__pycache__') # 删除 __pycache__文件夹
140
124
  [os.remove(i) for i in glob.glob("*.ui")] # 删除ui文件
141
- logger.info("清理临时文件完成")
142
- logger.info(f"==========================================\n")
125
+
143
126
  return True
144
127
 
145
- except Exception as e:
146
- logger.error(f"处理文件时发生错误: {e}")
128
+ except Exception:
147
129
  return False
148
130
  finally:
149
131
  # 恢复原始工作目录
150
132
  os.chdir(original_dir)
151
- logger.info(f"恢复原始工作目录: {original_dir}")
152
- logger.info(f"==========================================\n")
153
133
 
154
134
  if __name__ == "__main__":
155
135
  import argparse
@@ -0,0 +1,153 @@
1
+ #!/usr/bin/env python
2
+ # -*- coding: utf-8 -*-
3
+ '''
4
+ Author: gu lei
5
+ Date: 2023-01-20 13:52:06
6
+ LastEditTime: 2023-09-26 10:43:03
7
+ LastEditors: gu lei
8
+ '''
9
+ import os
10
+ import sys
11
+ import argparse
12
+ from rich.console import Console
13
+ from rich.progress import Progress, SpinnerColumn, TextColumn, BarColumn, TaskProgressColumn
14
+
15
+ __version__ = '0.2.0'
16
+
17
+ console = Console()
18
+
19
+ def check_dependencies():
20
+ """检查必要的依赖是否已安装"""
21
+ try:
22
+ import Cython
23
+ return True
24
+ except ImportError:
25
+ console.print("❌ [bold red]缺少必要的依赖: Cython[/bold red]")
26
+ console.print(" 请先安装依赖: pip install cython")
27
+ return False
28
+
29
+ from module.single_py2pyd import py2pyd
30
+ from module.fileConversion import FileConversion
31
+
32
+ def process_files(files, need_remove=False, desc="转换进度"):
33
+ """处理文件列表并显示进度条"""
34
+ success_count = 0
35
+ fail_count = 0
36
+ failed_files = []
37
+
38
+ with Progress(
39
+ SpinnerColumn(),
40
+ TextColumn("[progress.description]{task.description}"),
41
+ BarColumn(),
42
+ TaskProgressColumn(),
43
+ console=console
44
+ ) as progress:
45
+ task = progress.add_task(desc, total=len(files))
46
+
47
+ for file_path in files:
48
+ success = py2pyd(file_path)
49
+ if success:
50
+ success_count += 1
51
+ if need_remove:
52
+ os.remove(file_path)
53
+ else:
54
+ fail_count += 1
55
+ failed_files.append(file_path)
56
+ progress.update(task, advance=1)
57
+
58
+ return success_count, fail_count, failed_files
59
+
60
+ def main():
61
+ parser = argparse.ArgumentParser(
62
+ description="将Python文件转换为pyd/so文件",
63
+ epilog="示例:\n"
64
+ " py2pyd file.py 转换单个文件\n"
65
+ " py2pyd folder/ 转换目录下的文件\n"
66
+ " py2pyd -r folder/ 递归转换目录\n"
67
+ " py2pyd --remove file.py 转换后删除原文件",
68
+ formatter_class=argparse.RawDescriptionHelpFormatter
69
+ )
70
+ parser.add_argument("path", nargs='?', help="要转换的Python文件或目录路径")
71
+ parser.add_argument("-r", "--recursive", action="store_true", help="递归处理目录")
72
+ parser.add_argument("--remove", action="store_true", help="转换后删除原始.py文件")
73
+ parser.add_argument("-v", "--version", action="version", version=f"py2pyd {__version__}")
74
+
75
+ args = parser.parse_args()
76
+
77
+ # 如果没有提供路径参数,显示帮助信息
78
+ if not args.path:
79
+ parser.print_help()
80
+ sys.exit(0)
81
+
82
+ # 检查依赖
83
+ if not check_dependencies():
84
+ sys.exit(1)
85
+
86
+ # 删除确认
87
+ if args.remove:
88
+ confirm = input("⚠️ 警告: --remove 选项将会删除所有py源文件,是否继续? (y/n): ")
89
+ if confirm.lower() != 'y':
90
+ console.print("操作已取消")
91
+ sys.exit(0)
92
+
93
+ path = args.path
94
+
95
+ if not os.path.exists(path):
96
+ console.print(f"❌ [bold red]路径不存在: {path}[/bold red]")
97
+ sys.exit(1)
98
+
99
+ success_count = 0
100
+ fail_count = 0
101
+ failed_files = []
102
+
103
+ if os.path.isfile(path):
104
+ # 处理单个文件
105
+ if not path.endswith(".py"):
106
+ console.print(f"❌ [bold red]不是Python文件: {path}[/bold red]")
107
+ sys.exit(1)
108
+ console.print(f"📄 处理文件: [cyan]{path}[/cyan]")
109
+ success_count, fail_count, failed_files = process_files([path], args.remove)
110
+
111
+ elif os.path.isdir(path):
112
+ # 处理目录
113
+ if args.recursive:
114
+ console.print(f"📁 递归处理目录: [cyan]{path}[/cyan]")
115
+ converter = FileConversion()
116
+ success = converter.get_all_file(path, args.remove)
117
+ if success:
118
+ console.print("\n🎉 [bold green]全部转换成功![/bold green]")
119
+ else:
120
+ console.print("\n❌ [bold red]处理完成,但有部分文件转换失败![/bold red]")
121
+ sys.exit(0 if success else 1)
122
+ else:
123
+ # 仅处理当前目录下的.py文件
124
+ console.print(f"📁 处理目录: [cyan]{path}[/cyan]")
125
+
126
+ # 收集所有 .py 文件
127
+ py_files = [os.path.join(path, f) for f in os.listdir(path) if f.endswith(".py")]
128
+
129
+ if not py_files:
130
+ console.print("⚠️ [yellow]目录中没有找到 .py 文件[/yellow]")
131
+ sys.exit(0)
132
+
133
+ success_count, fail_count, failed_files = process_files(py_files, args.remove)
134
+
135
+ # 显示结果
136
+ console.print()
137
+ if fail_count == 0:
138
+ console.print(f"✅ [bold green]处理完成!成功: {success_count} 个文件[/bold green]")
139
+ else:
140
+ console.print(f"⚠️ [yellow]处理完成!成功: {success_count} 个文件,失败: {fail_count} 个文件[/yellow]")
141
+ console.print("[red]失败的文件:[/red]")
142
+ for f in failed_files:
143
+ console.print(f" - {f}")
144
+
145
+ if fail_count == 0:
146
+ console.print("\n🎉 [bold green]全部转换成功![/bold green]")
147
+ sys.exit(0)
148
+ else:
149
+ console.print("\n❌ [bold red]处理完成,但有部分文件转换失败![/bold red]")
150
+ sys.exit(1)
151
+
152
+ if __name__ == "__main__":
153
+ main()
@@ -0,0 +1,30 @@
1
+ [project]
2
+ name = "gl-py2pyd"
3
+ version = "0.2.0"
4
+ description = "将Python文件转换为pyd/so文件的工具"
5
+ readme = "README.md"
6
+ requires-python = ">=3.9"
7
+ dependencies = [
8
+ "cython",
9
+ "rich",
10
+ "setuptools",
11
+ ]
12
+
13
+ [project.scripts]
14
+ py2pyd = "py2pyd:main"
15
+
16
+ [dependency-groups]
17
+ dev = [
18
+ "build>=1.4.0",
19
+ "twine>=6.2.0",
20
+ ]
21
+
22
+ [build-system]
23
+ requires = ["setuptools>=61.0"]
24
+ build-backend = "setuptools.build_meta"
25
+
26
+ [tool.setuptools.packages.find]
27
+ include = ["module*"]
28
+
29
+ [tool.setuptools]
30
+ py-modules = ["py2pyd"]
@@ -5,7 +5,7 @@ with open("README.md", "r", encoding="utf-8") as fh:
5
5
 
6
6
  setup(
7
7
  name="gl-py2pyd",
8
- version="0.1.1",
8
+ version="0.2.0",
9
9
  author="gu lei",
10
10
  author_email="youjunxiaji@gmail.com",
11
11
  description="将Python文件转换为pyd/so文件的工具",
@@ -20,10 +20,11 @@ setup(
20
20
  "License :: OSI Approved :: MIT License",
21
21
  "Operating System :: OS Independent",
22
22
  ],
23
- python_requires=">=3.6",
23
+ python_requires=">=3.9",
24
24
  install_requires=[
25
25
  "cython",
26
- "loguru",
26
+ "rich",
27
+ "setuptools",
27
28
  ],
28
29
  entry_points={
29
30
  "console_scripts": [
@@ -1,2 +0,0 @@
1
- cython
2
- loguru
@@ -1,49 +0,0 @@
1
- '''
2
- Author: gu lei
3
- Date: 2023-01-20 13:52:06
4
- LastEditTime: 2023-09-26 10:43:03
5
- LastEditors: gu lei
6
- '''
7
- import os
8
- import time
9
- from loguru import logger
10
- from .single_py2pyd import py2pyd
11
-
12
- class FileConversion:
13
-
14
- def __init__(self) -> None:
15
- self.initpy = None
16
- self.success_count = 0
17
- self.fail_count = 0
18
-
19
- def get_all_file(self, path, need_remove: bool): # 遍历此目录下的所有py文件,包含子目录里的py
20
- for root, dirs, files in os.walk(path):
21
- if "__init__.py" in files:
22
- self.process_init_py(root)
23
- files.remove("__init__.py")
24
- for name in files:
25
- if name.endswith(".py"):
26
- file_path = os.path.join(root, name)
27
- success = py2pyd(file_path)
28
- if success:
29
- self.success_count += 1
30
- if need_remove:
31
- os.remove(file_path)
32
- else:
33
- self.fail_count += 1
34
- if self.initpy:
35
- self.process_init_py(root)
36
-
37
- logger.info(f"处理完成!成功: {self.success_count} 个文件,失败: {self.fail_count} 个文件")
38
- return self.success_count > 0 and self.fail_count == 0
39
-
40
- def process_init_py(self, path):
41
- init_py_path = os.path.join(path, '__init__.py')
42
- if self.initpy:
43
- with open(init_py_path, 'w', encoding='utf-8') as file:
44
- file.write(self.initpy)
45
- self.initpy = None
46
- else:
47
- with open(init_py_path, 'r', encoding='utf-8') as file:
48
- self.initpy = file.read()
49
- os.remove(init_py_path)
gl_py2pyd-0.1.1/py2pyd.py DELETED
@@ -1,91 +0,0 @@
1
- #!/usr/bin/env python
2
- # -*- coding: utf-8 -*-
3
- '''
4
- Author: gu lei
5
- Date: 2023-01-20 13:52:06
6
- LastEditTime: 2023-09-26 10:43:03
7
- LastEditors: gu lei
8
- '''
9
- import os
10
- import sys
11
- import argparse
12
- from loguru import logger
13
-
14
- def check_dependencies():
15
- """检查必要的依赖是否已安装"""
16
- try:
17
- import Cython
18
- logger.info(f"检测到Cython版本: {Cython.__version__}")
19
- return True
20
- except ImportError:
21
- logger.error("缺少必要的依赖: Cython \t 请先安装依赖: pip install cython")
22
- return False
23
-
24
- from module.single_py2pyd import py2pyd
25
- from module.fileConversion import FileConversion
26
-
27
- def main():
28
- # 检查依赖
29
- if not check_dependencies():
30
- sys.exit(1)
31
-
32
- parser = argparse.ArgumentParser(description="将Python文件转换为pyd/so文件")
33
- parser.add_argument("path", help="要转换的Python文件或目录路径")
34
- parser.add_argument("-r", "--recursive", action="store_true", help="递归处理目录")
35
- parser.add_argument("--remove", action="store_true", help="转换后删除原始.py文件")
36
-
37
- args = parser.parse_args()
38
-
39
- path = args.path
40
-
41
- if not os.path.exists(path):
42
- logger.error(f"路径不存在: {path}")
43
- sys.exit(1)
44
-
45
- success = False
46
-
47
- if os.path.isfile(path):
48
- # 处理单个文件
49
- if not path.endswith(".py"):
50
- logger.error(f"不是Python文件: {path}")
51
- sys.exit(1)
52
- logger.info(f"处理单个文件: {path}")
53
- success = py2pyd(path)
54
- if success and args.remove:
55
- logger.info(f"删除原始文件: {path}")
56
- os.remove(path)
57
- elif os.path.isdir(path):
58
- # 处理目录
59
- if args.recursive:
60
- logger.info(f"递归处理目录: {path}")
61
- converter = FileConversion()
62
- success = converter.get_all_file(path, args.remove)
63
- else:
64
- # 仅处理当前目录下的.py文件
65
- logger.info(f"处理目录: {path}")
66
- success_count = 0
67
- fail_count = 0
68
- for filename in os.listdir(path):
69
- if filename.endswith(".py"):
70
- file_path = os.path.join(path, filename)
71
- file_success = py2pyd(file_path)
72
- if file_success:
73
- success_count += 1
74
- if args.remove:
75
- logger.info(f"删除原始文件: {file_path}")
76
- os.remove(file_path)
77
- else:
78
- fail_count += 1
79
-
80
- logger.info(f"处理完成!成功: {success_count} 个文件,失败: {fail_count} 个文件")
81
- success = success_count > 0 and fail_count == 0
82
-
83
- if success:
84
- logger.info("处理完成!全部转换成功!")
85
- sys.exit(0)
86
- else:
87
- logger.error("处理完成,但有部分文件转换失败!")
88
- sys.exit(1)
89
-
90
- if __name__ == "__main__":
91
- main()
@@ -1,3 +0,0 @@
1
- [build-system]
2
- requires = ["setuptools>=42", "wheel"]
3
- build-backend = "setuptools.build_meta"
File without changes
File without changes
File without changes