Skip to content

Commit

Permalink
- Display warning if not default directory name...
Browse files Browse the repository at this point in the history
and contains files not related to sticker_convert
- Only move files that seem to be related to sticker_convert
- Code cleanup
- Fix typing
  • Loading branch information
laggykiller committed Feb 6, 2024
1 parent d4e3edf commit ec3d93e
Show file tree
Hide file tree
Showing 17 changed files with 182 additions and 112 deletions.
34 changes: 18 additions & 16 deletions compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ def osx_run_in_venv(cmd, get_stdout=False):
else:
return subprocess.run(sh_cmd + [venv_cmd + cmd])

def search_wheel_in_dir(package: str, dir: str):
for i in os.listdir(dir):
def search_wheel_in_dir(package: str, dir: Path):
for i in dir.iterdir():
if i.startswith(package):
return i

def copy_if_universal(wheel_name: str, in_dir: str, out_dir: str):
def copy_if_universal(wheel_name: str, in_dir: Path, out_dir: Path):
if wheel_name.endswith('universal2.whl') or wheel_name.endswith('any.whl'):
src_path = Path(in_dir, wheel_name)
dst_path = Path(
Expand All @@ -42,8 +42,8 @@ def copy_if_universal(wheel_name: str, in_dir: str, out_dir: str):
else:
return False

def create_universal_wheels(in_dir1, in_dir2, out_dir):
for wheel_name_1 in os.listdir(in_dir1):
def create_universal_wheels(in_dir1: Path, in_dir2: Path, out_dir: Path):
for wheel_name_1 in in_dir1.iterdir():
package = wheel_name_1.split('-')[0]
wheel_name_2 = search_wheel_in_dir(package, in_dir2)
if copy_if_universal(wheel_name_1, in_dir1, out_dir):
Expand All @@ -53,31 +53,31 @@ def create_universal_wheels(in_dir1, in_dir2, out_dir):

wheel_path_1 = Path(in_dir1, wheel_name_1)
wheel_path_2 = Path(in_dir2, wheel_name_2)
subprocess.run(['delocate-fuse', wheel_path_1, wheel_path_2, '-w', out_dir])
subprocess.run(['delocate-fuse', wheel_path_1, wheel_path_2, '-w', str(out_dir)])
print(f'Created universal wheel {wheel_path_1} {wheel_path_2}')

for wheel_name in os.listdir(out_dir):
for wheel_name in out_dir.iterdir():
wheel_name_new = wheel_name.replace('x86_64', 'universal2').replace('arm64', 'universal2')

src_path = Path(out_dir, wheel_name)
dst_path = Path(out_dir, wheel_name_new)

os.rename(src_path, dst_path)
src_path.rename(dst_path)
print(f'Renamed universal wheel {dst_path}')

def osx_install_universal2_dep():
shutil.rmtree('wheel_arm', ignore_errors=True)
shutil.rmtree('wheel_x64', ignore_errors=True)
shutil.rmtree('wheel_universal2', ignore_errors=True)

os.mkdir('wheel_arm')
os.mkdir('wheel_x64')
os.mkdir('wheel_universal2')
Path('wheel_arm').mkdir()
Path('wheel_x64').mkdir()
Path('wheel_universal2').mkdir()

osx_run_in_venv('python -m pip download --require-virtualenv -r requirements.txt --platform macosx_11_0_arm64 --only-binary=:all: -d wheel_arm')
osx_run_in_venv('python -m pip download --require-virtualenv -r requirements.txt --platform macosx_11_0_x86_64 --only-binary=:all: -d wheel_x64')

create_universal_wheels('./wheel_arm', './wheel_x64', 'wheel_universal2')
create_universal_wheels(Path('./wheel_arm'), Path('./wheel_x64'), Path('wheel_universal2'))
osx_run_in_venv('python -m pip install --require-virtualenv ./wheel_universal2/*')

def nuitka(python_bin, arch):
Expand Down Expand Up @@ -115,19 +115,21 @@ def nuitka(python_bin, arch):
subprocess.run(cmd_list, shell=True)

def win_patch():
for i in os.listdir('sticker-convert.dist/av.libs'):
for i in Path('sticker-convert.dist/av.libs').iterdir():
file_path = Path('sticker-convert.dist', i)
if file_path.is_file():
os.remove(file_path)

def osx_patch():
# https://github.com/pyinstaller/pyinstaller/issues/5154#issuecomment-1567603461
os.rename('sticker-convert.app/Contents/MacOS/sticker-convert', 'sticker-convert.app/Contents/MacOS/sticker-convert-cli')
with open('sticker-convert.app/Contents/MacOS/sticker-convert', 'w+') as f:
sticker_bin = Path('sticker-convert.app/Contents/MacOS/sticker-convert')
sticker_bin_cli = Path('sticker-convert.app/Contents/MacOS/sticker-convert-cli')
sticker_bin.rename(sticker_bin_cli)
with open(sticker_bin, 'w+') as f:
f.write('#!/bin/bash\n')
f.write('cd "$(dirname "$0")"\n')
f.write('open ./sticker-convert-cli')
os.chmod('sticker-convert.app/Contents/MacOS/sticker-convert', 0o744)
os.chmod(sticker_bin, 0o744)

osx_run_in_venv('codesign --force --deep -s - sticker-convert.app')

Expand Down
1 change: 1 addition & 0 deletions src/sticker_convert/definitions.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/env python3
import os
import platform
import sys
Expand Down
13 changes: 7 additions & 6 deletions src/sticker_convert/downloaders/download_base.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env python3
from __future__ import annotations
from pathlib import Path
from multiprocessing.managers import BaseProxy

from typing import Optional, Union
Expand All @@ -13,7 +14,7 @@ class DownloadBase:
def __init__(
self,
url: str,
out_dir: str,
out_dir: Path,
opt_cred: Optional[CredOption] = None,
cb: Union[BaseProxy, Callback, None] = None,
cb_return: Optional[CallbackReturn] = None,
Expand All @@ -23,11 +24,11 @@ def __init__(
cb = Callback(silent=True)
cb_return = CallbackReturn()

self.url = url
self.out_dir = out_dir
self.opt_cred = opt_cred
self.cb = cb
self.cb_return = cb_return
self.url: str = url
self.out_dir: Path = out_dir
self.opt_cred: Optional[CredOption] = opt_cred
self.cb: Union[BaseProxy, Callback, None] = cb
self.cb_return: Optional[CallbackReturn] = cb_return

def download_multiple_files(
self, targets: list[tuple[str, str]], retries: int = 3, **kwargs
Expand Down
2 changes: 1 addition & 1 deletion src/sticker_convert/downloaders/download_kakao.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ def download_animated(self, item_code: str) -> bool:
@staticmethod
def start(
url: str,
out_dir: str,
out_dir: Path,
opt_cred: Optional[CredOption] = None,
cb: Union[BaseProxy, Callback, None] = None,
cb_return: Optional[CallbackReturn] = None,
Expand Down
4 changes: 2 additions & 2 deletions src/sticker_convert/downloaders/download_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ def get_name_text_key(self, sticker_text: str) -> Optional[str]:
return name_text_key

def combine_custom_text(self):
for i in sorted(os.listdir(self.out_dir)):
for i in sorted(self.out_dir.iterdir()):
if i.endswith('-text.png'):
base_path = Path(self.out_dir, i.replace('-text.png', '.png'))
text_path = Path(self.out_dir, i)
Expand Down Expand Up @@ -394,7 +394,7 @@ def download_stickers_line(self) -> bool:
@staticmethod
def start(
url: str,
out_dir: str,
out_dir: Path,
opt_cred: Optional[CredOption] = None,
cb: Union[BaseProxy, Callback, None] = None,
cb_return: Optional[CallbackReturn] = None,
Expand Down
7 changes: 3 additions & 4 deletions src/sticker_convert/downloaders/download_signal.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python3
import os
from pathlib import Path
from typing import Optional, Union
from multiprocessing.managers import BaseProxy
Expand Down Expand Up @@ -47,8 +46,8 @@ def save_stickers(self, pack: StickerPack):
msg = f"Warning: Downloaded {f_path} but cannot get file codec"
self.cb.put(msg)
else:
f_path_new = f"{f_path}.{codec}"
os.rename(f_path, f_path_new)
f_path_new = Path(f"{f_path}.{codec}")
f_path.rename(f_path_new)
msg = f"Downloaded {f_id}.{codec}"
self.cb.put(msg)

Expand Down Expand Up @@ -77,7 +76,7 @@ def download_stickers_signal(self) -> bool:
@staticmethod
def start(
url: str,
out_dir: str,
out_dir: Path,
opt_cred: Optional[CredOption] = None,
cb: Union[BaseProxy, Callback, None] = None,
cb_return: Optional[CallbackReturn] = None,
Expand Down
2 changes: 1 addition & 1 deletion src/sticker_convert/downloaders/download_telegram.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ async def save_stickers(self) -> bool:
@staticmethod
def start(
url: str,
out_dir: str,
out_dir: Path,
opt_cred: Optional[CredOption] = None,
cb: Union[BaseProxy, Callback, None] = None,
cb_return: Optional[CallbackReturn] = None,
Expand Down
4 changes: 2 additions & 2 deletions src/sticker_convert/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -575,15 +575,15 @@ def highlight_fields(self) -> bool:
self.output_frame.output_setdir_entry.config(bootstyle='default')

if (MetadataHandler.check_metadata_required(output_option, 'title') and
not MetadataHandler.check_metadata_provided(self.input_setdir_var.get(), input_option, 'title') and
not MetadataHandler.check_metadata_provided(Path(self.input_setdir_var.get()), input_option, 'title') and
not self.title_var.get()):

self.output_frame.title_entry.config(bootstyle='warning')
else:
self.output_frame.title_entry.config(bootstyle='default')

if (MetadataHandler.check_metadata_required(output_option, 'author') and
not MetadataHandler.check_metadata_provided(self.input_setdir_var.get(), input_option, 'author') and
not MetadataHandler.check_metadata_provided(Path(self.input_setdir_var.get()), input_option, 'author') and
not self.author_var.get()):

self.output_frame.author_entry.config(bootstyle='warning')
Expand Down
Loading

0 comments on commit ec3d93e

Please sign in to comment.