TMU (Too Many Updates)

This commit is contained in:
Geoffrey Frogeye 2019-10-17 12:44:30 +02:00
parent 392dfed89a
commit 789f26d925
12 changed files with 147 additions and 76 deletions

View file

@ -1,8 +1,11 @@
#!/usr/bin/env python3
# pylint: disable=C0103
import logging
import os
import subprocess
import typing
import re
import coloredlogs
import progressbar
@ -18,6 +21,9 @@ FORBIDDEN_EXTENSIONS = ["jpg", "png", "pdf", "ffs_db"]
FORGIVEN_FILENAMES = ["cover.jpg", "front.jpg", "folder.jpg",
"cover.png", "front.png", "folder.png"]
IGNORED_EMPTY_FOLDER = [".stfolder"]
RESTRICT_CHARACTERS = '[\0\\/:*"<>|]' # FAT32, NTFS
# RESTRICT_CHARACTERS = '[:/]' # HFS, HFS+
# RESTRICT_CHARACTERS = '[\0/]' # ext2-4, linux-based?
# TODO FEAT Make the directory structure the same as the base one and
# remove IGNORED_EMPTY_FOLDER variable
@ -43,16 +49,20 @@ remainingConversions = dict()
extraFiles = set(outputFiles.keys())
def convertPath(path):
def convertPath(path: str) -> typing.Optional[str]:
filename, extension = os.path.splitext(path)
extension = extension[1:].lower()
# Remove unwanted characters from filename
filename_parts = os.path.normpath(filename).split(os.path.sep)
filename_parts = [re.sub(RESTRICT_CHARACTERS, '_', part) for part in filename_parts]
filename = os.path.sep.join(filename_parts)
# If the extension isn't allowed
if extension in FORBIDDEN_EXTENSIONS:
basename = os.path.basename(path)
# And the filename is not an exception
if basename not in FORGIVEN_FILENAMES:
# This file shouldn't be copied nor converted
return False
return None
# If this needs a conversion
elif extension in CONVERSIONS:
extension = CONVERSIONS[extension]
@ -61,11 +71,11 @@ def convertPath(path):
return path
log.info("Determining action over {} files".format(len(sourceFiles)))
log.info("Determining action over %d files", len(sourceFiles))
for sourceFile in sourceFiles:
outputFile = convertPath(sourceFile)
# If the file should not be converted, do nothing
if outputFile == False:
if not outputFile:
continue
# If the file already has something as an output
elif outputFile in outputFiles:
@ -77,7 +87,7 @@ for sourceFile in sourceFiles:
# If the file needs to be converted, do it
remainingConversions[sourceFile] = outputFile
log.debug("{} actions will need to be taken".format(len(remainingConversions)))
log.debug("%d actions will need to be taken", len(remainingConversions))
log.info("Copying files that do not require a conversion")
conversions = set()
for sourceFile in remainingConversions:
@ -91,7 +101,7 @@ for sourceFile in remainingConversions:
# Converting
fullSourceFile = os.path.join(SOURCE_FOLDER, sourceFile)
if sourceFile == outputFile:
log.debug('{} → {}'.format(fullSourceFile, fullOutputFile))
log.debug('%s → %s', fullSourceFile, fullOutputFile)
if os.path.isfile(fullOutputFile):
os.remove(fullOutputFile)
os.link(fullSourceFile, fullOutputFile)
@ -101,12 +111,12 @@ for sourceFile in remainingConversions:
log.info("Removing extra files")
for extraFile in extraFiles:
fullExtraFile = os.path.join(OUTPUT_FOLDER, extraFile)
log.debug('× {}'.format(fullExtraFile))
log.debug('× %s', fullExtraFile)
os.remove(fullExtraFile)
log.info("Listing files that will be converted")
for fullSourceFile, fullOutputFile in conversions:
log.debug('{} ⇒ {}'.format(fullSourceFile, fullOutputFile))
log.debug('%s ⇒ %s', fullSourceFile, fullOutputFile)
log.info("Converting files")
for fullSourceFile, fullOutputFile in progressbar.progressbar(conversions):