Commit ea8cefed by 余海波

Delete embed_resources.py

parent dd368057
"""
Pythonexe
SDK_internal
"""
import os
import base64
from pathlib import Path
def embed_file_to_python(file_path, output_path, var_name):
"""
Pythonbase64
Args:
file_path:
output_path: Python
var_name:
"""
with open(file_path, 'rb') as f:
data = f.read()
encoded = base64.b64encode(data).decode('utf-8')
# Python
code = f'''"""
: {file_path}
"""
import base64
import io
# Base64
_{var_name}_data = """{encoded}"""
def get_{var_name}():
""""""
return base64.b64decode(_{var_name}_data)
def get_{var_name}_path():
""""""
import tempfile
import os
data = get_{var_name}()
#
ext = os.path.splitext("{os.path.basename(file_path)}")[1]
#
fd, temp_path = tempfile.mkstemp(suffix=ext, prefix='embedded_resource_')
try:
with os.fdopen(fd, 'wb') as f:
f.write(data)
return temp_path
except:
os.close(fd)
raise
'''
with open(output_path, 'w', encoding='utf-8') as f:
f.write(code)
print(f" : {file_path} -> {output_path} (: {var_name})")
def embed_directory_to_python(dir_path, output_dir, max_size_mb=1):
"""
Python
Args:
dir_path:
output_dir:
max_size_mb: MB
"""
os.makedirs(output_dir, exist_ok=True)
embedded_files = []
skipped_files = []
for root, dirs, files in os.walk(dir_path):
for file in files:
file_path = os.path.join(root, file)
rel_path = os.path.relpath(file_path, dir_path)
#
size_mb = os.path.getsize(file_path) / (1024 * 1024)
if size_mb > max_size_mb:
skipped_files.append((rel_path, size_mb))
continue
#
var_name = rel_path.replace(os.sep, '_').replace('.', '_').replace('-', '_')
#
output_path = os.path.join(output_dir, f"embedded_{var_name}.py")
try:
embed_file_to_python(file_path, output_path, var_name)
embedded_files.append((rel_path, file_path))
except Exception as e:
print(f" : {file_path} - {e}")
skipped_files.append((rel_path, size_mb))
print(f"\n:")
print(f" : {len(embedded_files)} ")
print(f" : {len(skipped_files)} {max_size_mb}MB")
return embedded_files, skipped_files
if __name__ == '__main__':
# icons
project_root = os.path.abspath('.')
icons_dir = os.path.join(project_root, 'icons')
output_dir = os.path.join(project_root, 'hooks', 'embedded_resources')
if os.path.exists(icons_dir):
print("icons...")
embed_directory_to_python(icons_dir, output_dir, max_size_mb=0.5) # 0.5MB
else:
print(f": {icons_dir}")
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment