Added lock files to export to minimize name collisions (#981)

This commit is contained in:
Rhet Turnbull
2023-02-11 18:21:15 -08:00
committed by GitHub
parent c4788c0fd2
commit ce297ced0a
7 changed files with 148 additions and 28 deletions

View File

@@ -9,8 +9,8 @@ import os.path
import pathlib
import re
import shutil
import subprocess
import sqlite3
import subprocess
import tempfile
import time
from tempfile import TemporaryDirectory
@@ -498,10 +498,9 @@ CLI_EXPORTED_FILENAME_TEMPLATE_FILENAMES_KEYWORD_PATHSEP = [
]
CLI_EXPORTED_FILENAME_TEMPLATE_LONG_DESCRIPTION = [
"Lorem ipsum dolor sit amet, consectetuer adipiscing elit. "
"Aenean commodo ligula eget dolor. Aenean massa. "
"Cum sociis natoque penatibus et magnis dis parturient montes, "
"nascetur ridiculus mus. Donec quam felis, ultricies nec, "
"Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo"
" ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis "
"dis parturient montes, nascetu. Donec quam felis, ultricies nec, "
"pellentesque eu, pretium q.tif"
]
@@ -4008,6 +4007,7 @@ def test_export_filename_template_long_description():
],
)
assert result.exit_code == 0
files = glob.glob("*.*")
for fname in CLI_EXPORTED_FILENAME_TEMPLATE_LONG_DESCRIPTION:
assert pathlib.Path(fname).is_file()

View File

@@ -1,8 +1,10 @@
""" Test path_utils.py """
from osxphotos._constants import _OSXPHOTOS_LOCK_EXTENSION, MAX_FILENAME_LEN
from osxphotos.path_utils import sanitize_filename
def test_sanitize_filename():
from osxphotos.path_utils import sanitize_filename
from osxphotos._constants import MAX_FILENAME_LEN
# basic sanitize
filenames = {
@@ -30,30 +32,33 @@ def test_sanitize_filename():
filename = "foo" + "x" * 512
new_filename = sanitize_filename(filename)
assert len(new_filename) == MAX_FILENAME_LEN
assert new_filename == "foo" + "x" * 252
assert new_filename == "foo" + "x" * (252 - len(_OSXPHOTOS_LOCK_EXTENSION))
# filename too long with extension
filename = "x" * 512 + ".jpeg"
new_filename = sanitize_filename(filename)
assert len(new_filename) == MAX_FILENAME_LEN
assert new_filename == "x" * 250 + ".jpeg"
assert new_filename == "x" * (250 - len(_OSXPHOTOS_LOCK_EXTENSION)) + ".jpeg"
# more than one extension
filename = "foo.bar" + "x" * 255 + ".foo.bar.jpeg"
new_filename = sanitize_filename(filename)
assert len(new_filename) == MAX_FILENAME_LEN
assert new_filename == "foo.bar" + "x" * 243 + ".jpeg"
assert (
new_filename
== "foo.bar" + "x" * (243 - len(_OSXPHOTOS_LOCK_EXTENSION)) + ".jpeg"
)
# shorter than drop count
filename = "foo." + "x" * 256
new_filename = sanitize_filename(filename)
assert len(new_filename) == MAX_FILENAME_LEN
assert new_filename == "foo." + "x" * 251
assert new_filename == "foo." + "x" * (251 - len(_OSXPHOTOS_LOCK_EXTENSION))
def test_sanitize_dirname():
from osxphotos.path_utils import sanitize_dirname
from osxphotos._constants import MAX_DIRNAME_LEN
from osxphotos.path_utils import sanitize_dirname
# basic sanitize
dirnames = {
@@ -83,9 +88,10 @@ def test_sanitize_dirname():
assert len(new_dirname) == MAX_DIRNAME_LEN
assert new_dirname == "foo" + "x" * 252
def test_sanitize_pathpart():
from osxphotos.path_utils import sanitize_pathpart
from osxphotos._constants import MAX_DIRNAME_LEN
from osxphotos.path_utils import sanitize_pathpart
# basic sanitize
dirnames = {
@@ -114,4 +120,3 @@ def test_sanitize_pathpart():
new_dirname = sanitize_pathpart(dirname)
assert len(new_dirname) == MAX_DIRNAME_LEN
assert new_dirname == "foo" + "x" * 252

View File

@@ -125,6 +125,42 @@ def test_increment_filename():
)
def test_increment_filename_with_lock():
# test that increment_filename works with lock=True
with tempfile.TemporaryDirectory(prefix="osxphotos_") as temp_dir:
temp_dir = pathlib.Path(temp_dir)
filename = str(temp_dir / "file.jpg")
assert increment_filename(filename, lock=True) == str(temp_dir / "file.jpg")
new_file = temp_dir / "file.jpg"
new_file.touch()
assert increment_filename(filename, lock=True) == str(temp_dir / "file (1).jpg")
# test increment_filename_with_count
filename = str(temp_dir / "file2.jpg")
assert increment_filename_with_count(filename, count=2, lock=True) == (
str(temp_dir / "file2 (2).jpg"),
2,
)
new_file = temp_dir / "file2 (2).jpg"
new_file.touch()
assert increment_filename_with_count(filename, count=2, lock=True) == (
str(temp_dir / "file2 (3).jpg"),
3,
)
def test_increment_filename_with_lock_exists():
# test that increment_filename works with lock=True when lock file already exists
with tempfile.TemporaryDirectory(prefix="osxphotos_") as temp_dir:
temp_dir = pathlib.Path(temp_dir)
filename = str(temp_dir / "file.jpg")
pathlib.Path(f"{filename}.osxphotos.lock").touch()
assert increment_filename(filename, lock=True) == str(temp_dir / "file (1).jpg")
def test_shortuuid_uuid():
"""Test shortuuid_to_uuid and uuid_to_shortuuid"""
uuid = "5CF8D91E-DCEB-4CC3-BFF7-920B05564EB0"