Use readlines() over readline() to avoid while

This commit is contained in:
henryruhs 2024-12-21 19:15:40 +01:00
parent 3a254f428c
commit fedd88ce4c
3 changed files with 12 additions and 23 deletions

View File

@ -44,14 +44,13 @@ def conditional_download(download_directory_path : str, urls : List[str]) -> Non
def get_static_download_size(url : str) -> int:
commands = [ '-I', url ]
process = open_curl(commands)
process.wait()
lines = reversed(process.stdout.readlines())
while line := process.stdout.readline().decode().lower():
for line in lines:
line = line.decode().lower()
if 'content-length:' in line:
_, content_length = line.split('content-length:')
if int(content_length) > 0:
return int(content_length)
return int(content_length)
return 0

View File

@ -22,13 +22,13 @@ def run_ffmpeg_with_progress(args: List[str], update_progress : UpdateProgress)
while process_manager.is_processing():
try:
lines = process.stdout.readlines()
while line := process.stdout.readline().decode().lower():
for line in lines:
line = line.decode().lower()
if 'frame=' in line:
_, frame_number = line.split('frame=')
if int(frame_number) > 0:
update_progress(int(frame_number))
update_progress(int(frame_number))
if log_level == 'debug':
log_debug(process)

View File

@ -1,23 +1,13 @@
import pytest
from facefusion.download import conditional_download, get_static_download_size, ping_static_url
from .helper import get_test_examples_directory
@pytest.fixture(scope = 'module', autouse = True)
def before_all() -> None:
conditional_download(get_test_examples_directory(),
[
'https://github.com/facefusion/facefusion-assets/releases/download/examples-3.0.0/target-240p.mp4'
])
from facefusion.download import get_static_download_size, ping_static_url
def test_get_static_download_size() -> None:
assert get_static_download_size('https://github.com/facefusion/facefusion-assets/releases/download/examples-3.0.0/target-240p.mp4') == 191675
assert get_static_download_size('https://github.com/facefusion/facefusion-assets/releases/download/examples-3.0.0/target-360p.mp4') == 370732
assert get_static_download_size('https://github.com/facefusion/facefusion-assets/releases/download/models-3.0.0/fairface.onnx') == 85170772
assert get_static_download_size('https://huggingface.co/facefusion/models-3.0.0/resolve/main/fairface.onnx') == 85170772
assert get_static_download_size('invalid') == 0
def test_static_ping_url() -> None:
assert ping_static_url('https://github.com') is True
assert ping_static_url('https://huggingface.co') is True
assert ping_static_url('invalid') is False