-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
build: compile-pyc #1891
build: compile-pyc #1891
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
# coding=utf-8 | ||
""" | ||
@project: MaxKB | ||
@Author:虎 | ||
@file: compile.py | ||
@date:2024/12/23 14:11 | ||
@desc: | ||
""" | ||
import os | ||
import sys | ||
import shutil | ||
from py_compile import compile | ||
|
||
|
||
def clean(path_str: str): | ||
for parent, dir_name, filename in os.walk(path_str): | ||
for dir_str in dir_name: | ||
if dir == '__pycache__': | ||
fullname = os.path.join(parent, dir_str) | ||
try: | ||
shutil.rmtree(fullname) | ||
print("Success clean Folder:%s" % fullname) | ||
except Exception as e: | ||
print("Can't clean Folder:%s, reason:%s" % (fullname, e)) | ||
|
||
|
||
def compile_pyc(path_str: str): | ||
for parent, dir_name, filename in os.walk(path_str): | ||
for cfile in filename: | ||
fullname = os.path.join(parent, cfile) | ||
if cfile[-3:] == '.py': | ||
try: | ||
if compile(fullname): | ||
if cfile != 'settings.py' and cfile != 'wsgi.py': | ||
os.remove(fullname) # 删除原文件,保留settings.py和wsgi.py | ||
print("Success compile and remove file:%s" % fullname) | ||
else: | ||
print("Can't compile file:%s,The original file has been retained" % fullname) | ||
except Exception as e: | ||
print("Can't compile file:%s, reason:%s" % (fullname, e)) | ||
|
||
|
||
def move(path_str: str): | ||
for parent, dir_name, filename in os.walk(path_str): | ||
for c_file in filename: | ||
fullname = os.path.join(parent, c_file) | ||
if c_file[-4:] == '.pyc': | ||
try: | ||
if parent.endswith('__pycache__'): | ||
parent_path = os.path.dirname(parent) | ||
shutil.move(fullname, parent_path) | ||
print('update the dir of file successfully') | ||
except Exception as e: | ||
print("Can't move file:%s, reason:%s" % (fullname, e)) | ||
|
||
|
||
def replace_name(path_str: str): | ||
for parent, dir_name, filename in os.walk(path_str): | ||
for c_file in filename: | ||
fullname = os.path.join(parent, c_file) | ||
if c_file[-4:] == '.pyc': | ||
try: | ||
cfile_name = '' | ||
cfile_list = c_file.split('.') | ||
version = sys.version_info | ||
replace_name_str = 'cpython-' + str(version[0]) + str(version[1]) | ||
for i in range(len(cfile_list)): | ||
if cfile_list[i] == replace_name_str: | ||
continue | ||
cfile_name += cfile_list[i] | ||
if i == len(cfile_list) - 1: | ||
continue | ||
cfile_name += '.' | ||
shutil.move(fullname, os.path.join(parent, cfile_name)) | ||
print('update the name of the file successfully') | ||
except Exception as e: | ||
print("Can't remove file:%s, reason:%s" % (fullname, e)) | ||
|
||
|
||
if __name__ == '__main__': | ||
path = "/opt/maxkb/app/apps" | ||
clean(path) | ||
compile_pyc(path) | ||
move(path) | ||
replace_name(path) | ||
clean(path) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Some potential optimizations:
Additionally, note that |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are several issues with this script:
Directory Path Calculation: The
os.path.join
function is used incorrectly. It expects strings, so you should pass them separately.File Extension Checking: The file extension checking logic needs to be updated to correctly identify Python files ('*.py') instead of just '.py'.
File Removal Logic: Removing
.pcy
files when they are inside__pycache__
directories can lead to errors because the directory itself remains and doesn't affect runtime imports.Name Replacement Logic: The replacement of names based on Python version information might not work correctly due to missing parts of sys.version_info.
Main Entry Point: The main entry point checks for
path
at the top level, which causes an error unless it's defined beforehand.Here’s a revised version of the script addressing these issues:
Key Changes made:
os.path.join
..py
files are processed.__pycache__
directories since Python handles caching internally.update_directories
function to correctly handle path manipulations across systems (Python 2 vs 3 compatibility).