sort now just copies to same filesystem
adjusted to run on kubernetes
This commit is contained in:
parent
4372822080
commit
d7fdf8a6dc
@ -1,7 +1,7 @@
|
|||||||
FROM python:3.12-slim
|
FROM python:3.12-slim
|
||||||
|
|
||||||
# Create working directory
|
# Create working directory
|
||||||
RUN mkdir -p /app
|
RUN mkdir -p /app && mkdir /gallery
|
||||||
|
|
||||||
# Create and set user
|
# Create and set user
|
||||||
RUN useradd -u 1000 -d /app -UM appuser
|
RUN useradd -u 1000 -d /app -UM appuser
|
||||||
@ -12,7 +12,10 @@ RUN apt-get update \
|
|||||||
&& apt-get clean
|
&& apt-get clean
|
||||||
|
|
||||||
# Chown appuser home
|
# Chown appuser home
|
||||||
RUN chown -R appuser:appuser /app && chmod 750 /app
|
RUN chown -R appuser:appuser /app \
|
||||||
|
&& chown -R appuser:appuser /gallery \
|
||||||
|
&& chmod 750 /app \
|
||||||
|
&& chmod 775 /gallery
|
||||||
|
|
||||||
# Switch to appuser
|
# Switch to appuser
|
||||||
USER appuser
|
USER appuser
|
||||||
@ -33,5 +36,7 @@ RUN pip install -r /app/requirements.txt
|
|||||||
# Copy the sort.py file to the working directory
|
# Copy the sort.py file to the working directory
|
||||||
COPY sort.py /app/sort.py
|
COPY sort.py /app/sort.py
|
||||||
|
|
||||||
|
VOLUME /gallery
|
||||||
|
|
||||||
# Set the entry point to sort.py
|
# Set the entry point to sort.py
|
||||||
ENTRYPOINT ["python", "/app/sort.py"]
|
ENTRYPOINT ["python", "/app/sort.py"]
|
@ -16,20 +16,8 @@ photos_directory: Path = Path(getenv('PHOTOS_DIRECTORY', '/photos')).resolve()
|
|||||||
recordings_directory: Path = Path(getenv('RECORDINGS_DIRECTORY', '/recordings')).resolve()
|
recordings_directory: Path = Path(getenv('RECORDINGS_DIRECTORY', '/recordings')).resolve()
|
||||||
photo_extensions: list[str] = ['.jpg', '.jpeg']
|
photo_extensions: list[str] = ['.jpg', '.jpeg']
|
||||||
video_extensions: list[str] = ['.mp4', '.mov', '.avi', '.mkv']
|
video_extensions: list[str] = ['.mp4', '.mov', '.avi', '.mkv']
|
||||||
sleep_duration: int = 30
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
|
||||||
# Check the source directories exist and create the ignore files if they don't exist
|
|
||||||
for directory in source_directories:
|
|
||||||
if not directory.exists() or not directory.is_dir():
|
|
||||||
print(f"Source directory {directory} does not exist or is not a directory. Exiting.")
|
|
||||||
sys.exit(1)
|
|
||||||
ignore_file: Path = directory.joinpath("ignored_files.json")
|
|
||||||
if not ignore_file.exists():
|
|
||||||
print(f"Ignored files list {ignore_file} does not exist. Creating.")
|
|
||||||
with open(ignore_file, 'w') as f:
|
|
||||||
json.dump([], f)
|
|
||||||
|
|
||||||
# Check the destination directories exist and are writable
|
# Check the destination directories exist and are writable
|
||||||
for directory in [photos_directory, recordings_directory]:
|
for directory in [photos_directory, recordings_directory]:
|
||||||
@ -44,25 +32,27 @@ def main():
|
|||||||
print(f"Destination directory {directory} is not writable")
|
print(f"Destination directory {directory} is not writable")
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
# Start the main loop
|
# Check the source directories exist and create the ignore files if they don't exist
|
||||||
while True:
|
for directory in source_directories:
|
||||||
for directory in source_directories:
|
if not directory.exists() or not directory.is_dir():
|
||||||
print(f"Starting sort of directory {directory}.")
|
print(f"Source directory {directory} does not exist or is not a directory. Exiting.")
|
||||||
start_time = time.time()
|
sys.exit(1)
|
||||||
sort_count, ignore_count = sort_directory(directory = directory)
|
ignore_file: Path = directory.parent.joinpath(f"{directory.name}-ignore-file.json")
|
||||||
end_time = time.time()
|
if not ignore_file.exists():
|
||||||
print(f"Finished sort of directory {directory}.")
|
print(f"Ignored files list {ignore_file} does not exist. Creating.")
|
||||||
print(f"Sorted {sort_count} files.")
|
with open(ignore_file, 'w') as f:
|
||||||
print(f"Added {ignore_count} files to the ignore list.")
|
json.dump([], f)
|
||||||
print(f"Processing took {end_time - start_time} seconds.")
|
print(f"Starting sort of directory {directory}.")
|
||||||
print(f"Sleeping for {sleep_duration} seconds.")
|
sort_count, ignore_count = sort_directory(directory = directory, ignore_file = ignore_file)
|
||||||
time.sleep(sleep_duration)
|
print(f"Finished sort of directory {directory}.")
|
||||||
|
print(f"Sorted {sort_count} files.")
|
||||||
|
print(f"Added {ignore_count} files to the ignore list.")
|
||||||
|
print(f"Finished sorting {len(source_directories)} directories, exiting.")
|
||||||
|
|
||||||
def sort_directory(directory: Path) -> tuple[int, int]:
|
def sort_directory(directory: Path, ignore_file: Path) -> tuple[int, int]:
|
||||||
sort_count: int = 0
|
sort_count: int = 0
|
||||||
ignore_count: int = 0
|
ignore_count: int = 0
|
||||||
# Load the ignored files list
|
# Load the ignored files list
|
||||||
ignore_file = directory.joinpath("ignored_files.json")
|
|
||||||
with open(file = ignore_file) as f:
|
with open(file = ignore_file) as f:
|
||||||
ignore_list: list[dict] = json.load(f)
|
ignore_list: list[dict] = json.load(f)
|
||||||
# Get all files in the directory
|
# Get all files in the directory
|
||||||
@ -146,7 +136,7 @@ def sort_photos(*,
|
|||||||
ignore_list.append({"name": photo.name, "reason": "No exif timestamp"})
|
ignore_list.append({"name": photo.name, "reason": "No exif timestamp"})
|
||||||
ignore_count += 1
|
ignore_count += 1
|
||||||
continue
|
continue
|
||||||
previous_name, ignore_list, sort_count, ignore_count = rename_file(
|
previous_name, ignore_list, sort_count, ignore_count = copy_file(
|
||||||
file = photo,
|
file = photo,
|
||||||
timestamp = timestamp,
|
timestamp = timestamp,
|
||||||
previous_name = previous_name,
|
previous_name = previous_name,
|
||||||
@ -194,7 +184,7 @@ def sort_videos(*,
|
|||||||
ignore_list.append({"name": video.name, "reason": "No creation_time metadata"})
|
ignore_list.append({"name": video.name, "reason": "No creation_time metadata"})
|
||||||
ignore_count += 1
|
ignore_count += 1
|
||||||
continue
|
continue
|
||||||
previous_name, ignore_list, sort_count, ignore_count = rename_file(
|
previous_name, ignore_list, sort_count, ignore_count = copy_file(
|
||||||
file = video,
|
file = video,
|
||||||
timestamp = timestamp,
|
timestamp = timestamp,
|
||||||
previous_name = previous_name,
|
previous_name = previous_name,
|
||||||
@ -206,7 +196,7 @@ def sort_videos(*,
|
|||||||
)
|
)
|
||||||
return (ignore_list, sort_count, ignore_count)
|
return (ignore_list, sort_count, ignore_count)
|
||||||
|
|
||||||
def rename_file(*,
|
def copy_file(*,
|
||||||
file: Path,
|
file: Path,
|
||||||
timestamp: time.struct_time,
|
timestamp: time.struct_time,
|
||||||
previous_name: str,
|
previous_name: str,
|
||||||
@ -229,8 +219,9 @@ def rename_file(*,
|
|||||||
new_path.parent.mkdir(parents=True, exist_ok=True)
|
new_path.parent.mkdir(parents=True, exist_ok=True)
|
||||||
# Move the file
|
# Move the file
|
||||||
if not new_path.exists():
|
if not new_path.exists():
|
||||||
print(f"Moving {file.as_posix()} to {new_path.as_posix()}")
|
print(f"Copying {file.as_posix()} to {new_path.as_posix()}")
|
||||||
shutil.move(file, new_path)
|
shutil.copy2(src=file, dst=new_path)
|
||||||
|
ignore_list.append({"name": file.name, "reason": "Already copied"})
|
||||||
sort_count += 1
|
sort_count += 1
|
||||||
else:
|
else:
|
||||||
print(f"File {new_file_name} already exists, ignoring.")
|
print(f"File {new_file_name} already exists, ignoring.")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user