[DEBUG] Correct internal TOOLS

This commit is contained in:
Edouard DUPIN 2018-05-18 00:15:35 +00:00
parent b54d87ca55
commit e744412d30
2 changed files with 137 additions and 148 deletions

View File

@ -36,11 +36,10 @@ def get_console_size():
} }
CONSOLE_SIZE = get_console_size() CONSOLE_SIZE = get_console_size()
print("lkqjsdmlkqjsdmflkjqsmdlkfjmqslkdjfmlqksjdfmlkqsjdmfmlkqjsdmlkfjqsmlkdfjqmlskdjfmlqksjdfmlkqjsdmf", end="")
def clear_line(): def clear_line():
print("\r" + " "*CONSOLE_SIZE["x"] + "\r", end="") print("\r" + " "*CONSOLE_SIZE["x"] + "\r", end="")
print("Console size = " + str(CONSOLE_SIZE)) #print("Console size = " + str(CONSOLE_SIZE))
def get_sha512(filename, reduce=True): def get_sha512(filename, reduce=True):
BLOCKSIZE = 16000 BLOCKSIZE = 16000
@ -53,7 +52,7 @@ def get_sha512(filename, reduce=True):
if reduce == True: if reduce == True:
return hasher.hexdigest() return hasher.hexdigest()
buf = afile.read(BLOCKSIZE) buf = afile.read(BLOCKSIZE)
return hasher.hexdigest() return copy.deepcopy(hasher.hexdigest())
## ##
@ -192,14 +191,22 @@ for elem in list_files_ref:
value_src_sha512_full = get_sha512(elem,reduce=False) value_src_sha512_full = get_sha512(elem,reduce=False)
# a reduce sha512 can have many distint file depending on it ... # a reduce sha512 can have many distint file depending on it ...
for elem_previous in curent_DB[value_sha512]: for elem_previous in curent_DB[value_sha512]:
if "sha512" not in elem_previous: if "sha512" not in elem_previous.keys():
#print("calculate previous from : '" + elem_previous["file"] + "'")
elem_previous["sha512"] = get_sha512(elem_previous["file"],reduce=False) elem_previous["sha512"] = get_sha512(elem_previous["file"],reduce=False)
find_double = False add_in_db = True
for elem_previous in curent_DB[value_sha512]: for elem_previous in curent_DB[value_sha512]:
"""
print("check: ")
print(" " + elem_previous["sha512"])
print(" " + value_src_sha512_full)
print(" " + value_sha512)
"""
if elem_previous["sha512"] == value_src_sha512_full: if elem_previous["sha512"] == value_src_sha512_full:
if args.ref_remove_double == True: if args.ref_remove_double == True:
print("\n ==> remove double in reference: '" + check_file(elem) + "'") print("\n ==> remove double in reference: '" + check_file(elem) + "'")
os.remove(elem) os.remove(elem)
add_in_db = False
else: else:
ref_duplicate += 1 ref_duplicate += 1
print("\n[ERROR] Double element in the data-base: " + check_file(ref_duplicate)) print("\n[ERROR] Double element in the data-base: " + check_file(ref_duplicate))
@ -209,10 +216,11 @@ for elem in list_files_ref:
file_in_double.write(check_file(elem_previous["file"]) + "\n") file_in_double.write(check_file(elem_previous["file"]) + "\n")
file_in_double.write(check_file(elem) + "\n") file_in_double.write(check_file(elem) + "\n")
file_in_double.write("--------------------------------------------------------------------\n") file_in_double.write("--------------------------------------------------------------------\n")
find_double = True
break break
if find_double == True: if add_in_db == True:
print("apend new : " + str(len(curent_DB[value_sha512])))
curent_DB[value_sha512].append({"file":elem,"sha512":value_src_sha512_full}) curent_DB[value_sha512].append({"file":elem,"sha512":value_src_sha512_full})
print("apend new : " + str(len(curent_DB[value_sha512])))
else: else:
curent_DB[value_sha512] = [{"file":elem}] curent_DB[value_sha512] = [{"file":elem}]
print("") print("")
@ -238,38 +246,53 @@ for elem in list_files_src:
value_progress1 = int(value_progress*100) value_progress1 = int(value_progress*100)
value_progress2 = int(value_progress*10000 - value_progress1*100) value_progress2 = int(value_progress*10000 - value_progress1*100)
clear_line() clear_line()
print("[I] processing " + str(iii) + "/" + str(len(list_files_ref)) + " " + str(value_progress1) + "." + str(value_progress2) + "/100 " + check_file(elem), end='') print("[I] processing " + str(iii) + "/" + str(len(list_files_src)) + " " + str(value_progress1) + "." + str(value_progress2) + "/100 " + check_file(elem), end='')
last_x = len(elem) last_x = len(elem)
sys.stdout.flush() sys.stdout.flush()
value_sha512 = get_sha512(elem, reduce=True) value_sha512 = get_sha512(elem, reduce=True)
# check if the element is not a duplication ... # check if the element is not a duplication ...
if value_sha512 not in curent_DB.keys(): if value_sha512 not in curent_DB.keys():
src_missing += 1 src_missing += 1
"""
print("\n[INFO] Find element not in the dB: " + str(src_missing)) print("\n[INFO] Find element not in the dB: " + str(src_missing))
print(" sha512=" + str(value_sha512)) print(" sha512=" + str(value_sha512))
print(" src=" + check_file(elem)) print(" src=" + check_file(elem))
"""
print(" (add)")
curent_DB[value_sha512] = [{"file":elem}]
file_not_in_ref.write(str(elem + "\n")) file_not_in_ref.write(str(elem + "\n"))
else: else:
value_src_sha512_full = get_sha512(elem, reduce=False) value_src_sha512_full = get_sha512(elem, reduce=False)
for elem_previous in curent_DB[value_sha512]: for elem_previous in curent_DB[value_sha512]:
if "sha512" not in elem_previous: if "sha512" not in elem_previous.keys():
elem_previous["sha512"] = get_sha512(elem_previous["file"],reduce=False) print("calculate previous from : '" + elem_previous["file"] + "'")
find_double = False elem_previous["sha512"] = get_sha512(elem_previous["file"], reduce=False)
add_in_db = True
for elem_previous in curent_DB[value_sha512]: for elem_previous in curent_DB[value_sha512]:
"""
print("check: ")
print(" " + elem_previous["sha512"])
print(" " + value_src_sha512_full)
print(" " + value_sha512)
"""
if elem_previous["sha512"] == value_src_sha512_full: if elem_previous["sha512"] == value_src_sha512_full:
find_double = True
if args.src_remove_double == True: if args.src_remove_double == True:
print("\n ==> remove double in source: '" + check_file(elem) + "'") print("\n ==> remove double in source: '" + check_file(elem) + "'")
os.remove(elem) os.remove(elem)
add_in_db = False
else: else:
print("\n ==> must move double in source in destination: '" + check_file(elem) + "'") print("\n ==> must move double in source in destination: '" + check_file(elem) + "'")
break break
if find_double == False: if add_in_db == True:
src_missing += 1 src_missing += 1
"""
print("\n[INFO] Find element not in the dB (FULL): " + str(src_missing)) print("\n[INFO] Find element not in the dB (FULL): " + str(src_missing))
print(" sha512=" + str(value_sha512)) print(" sha512=" + str(value_sha512))
print(" src=" + check_file(elem)) print(" src=" + check_file(elem))
"""
print(" (add 2)")
file_not_in_ref.write(check_file(elem) + "\n") file_not_in_ref.write(check_file(elem) + "\n")
curent_DB[value_sha512].append({"file":elem,"sha512":value_src_sha512_full})
print("") print("")
file_not_in_ref.close() file_not_in_ref.close()

View File

@ -13,12 +13,8 @@ Options:
-d ..., --directory=... Specify which directory to work in -d ..., --directory=... Specify which directory to work in
(default is the current directory) (default is the current directory)
-f ..., --format=... Specify the naming format -f ..., --format=... Specify the naming format
-l, --flatten Move all files into the same root
directory
-r, --recursive Work recursively on the specified -r, --recursive Work recursively on the specified
directory directory
-t, --test Only display the new file names; nothing
will be renamed
-h, --help Display this help -h, --help Display this help
Formatting: Formatting:
@ -35,7 +31,7 @@ Formatting:
The following characters are of special importance to the operating system The following characters are of special importance to the operating system
and cannot be used in the file name: and cannot be used in the file name:
\ : * ? " < > | \ / : * ? " < > |
(=) is replaced by the directory path separator, so to move files into (=) is replaced by the directory path separator, so to move files into
artist and album subdirectories, the following format can be used: artist and album subdirectories, the following format can be used:
@ -51,7 +47,7 @@ Examples:
renameMP3.py -f "title -- artist" Renames music files in the current renameMP3.py -f "title -- artist" Renames music files in the current
directory with the name format: directory with the name format:
Sample Title -- Sample Artist.mp3 Sample Title -- Sample Artist.mp3
renameMP3.py -d . -r --flatten renameMP3.py -d . -r
pip install mutagen --user pip install mutagen --user
pip install easyid3 --user pip install easyid3 --user
@ -71,6 +67,9 @@ import fnmatch
import mutagen.easyid3 import mutagen.easyid3
import mutagen.oggvorbis import mutagen.oggvorbis
restrictedCharPattern = re.compile('[\\\:\/\*\?"<>\|]')
formatPattern = re.compile('artist|album|title|track')
### Exceptions ### ### Exceptions ###
class FormatError(Exception): class FormatError(Exception):
""" """
@ -164,7 +163,9 @@ def get_list_of_file_in_path(path, filter="*", recursive = False, remove_path=""
print(" len out " + str(len(out))) print(" len out " + str(len(out)))
return out; return out;
list_of_artist = []
list_of_album = []
list_of_title = []
class AudioFile: class AudioFile:
""" """
@ -204,6 +205,12 @@ class AudioFile:
self.track = "0" + self.track self.track = "0" + self.track
except: except:
pass pass
if self.artist == "" or self.artist == "No Artist":
self.data = None
if self.title == "" or self.title == "No Title":
self.data = None
if self.album == "" or self.album == "No Album":
self.data = None
def parse_mp3(self): def parse_mp3(self):
data = mutagen.easyid3.EasyID3(self.file_name) data = mutagen.easyid3.EasyID3(self.file_name)
@ -212,21 +219,24 @@ class AudioFile:
def parse_ogg(self): def parse_ogg(self):
return mutagen.oggvorbis.Open(self.file_name) return mutagen.oggvorbis.Open(self.file_name)
def rename(self, newfile_name, flatten=False): def rename(self, newfile_name):
def unique_name(newfile_name, count=0): def unique_name(newfile_name, count=0):
""" """
Returns a unique name if a file already exists with the supplied Returns a unique name if a file already exists with the supplied
name name
""" """
c = "_(%s)" % str(count) if count else "" if count == 0:
prefix = directory + os.path.sep if flatten else self.file_path c = ""
else:
c = "_(%s)" % str(count)
prefix = directory + os.path.sep
testfile_name = prefix + newfile_name + c + self.file_ext testfile_name = prefix + newfile_name + c + self.file_ext
if os.path.isfile(testfile_name): if os.path.isfile(testfile_name) == True:
count += 1 return unique_name(newfile_name, count + 1)
return unique_name(newfile_name, count)
else: else:
return testfile_name return testfile_name
if self.file_name == newfile_name: if self.file_name == newfile_name:
print("get same file : " + self.file_name)
return return
new_name = unique_name(newfile_name) new_name = unique_name(newfile_name)
create_directory_of_file(new_name) create_directory_of_file(new_name)
@ -240,7 +250,7 @@ class AudioFile:
Generate a clean file name based on metadata Generate a clean file name based on metadata
""" """
if self.data == None: if self.data == None:
return self.move_folder + self.file_name return (self.file_name, self.move_folder + self.file_name)
rawfile_name = format % {"artist": self.artist, rawfile_name = format % {"artist": self.artist,
"album": self.album, "album": self.album,
"title": self.title, "title": self.title,
@ -252,100 +262,11 @@ class AudioFile:
# remove restricted file_name characters (\, :, *, ?, ", <, >, |) from # remove restricted file_name characters (\, :, *, ?, ", <, >, |) from
# the supplied string # the supplied string
if self.move_folder != "": if self.move_folder != "":
clean_file_name = self.move_folder + self.move_folder clean_file_name = self.move_folder + clean_file_name
print("******** move in ZZ " + clean_file_name)
return (self.file_name, clean_file_name.replace("(=)", os.path.sep)) return (self.file_name, clean_file_name.replace("(=)", os.path.sep))
### Main ###
def main(argv):
global directory
directory = os.getcwd()
format = "%(artist)s/%(album)s/%(track)s-%(title)s"
flatten = False
recursive = False
test = False
def verifyFormat(format):
"""
Verify the supplied file_name format
"""
if re.search(restrictedCharPattern, format):
raise(FormatError, "supplied format contains restricted characters")
if not re.search(formatPattern, format):
raise(FormatError, "supplied format does not contain any metadata keys")
# the supplied format must contain at least one of "artist",
# "album", "title", or "track", or all files will be named
# identically
format = format.replace("artist", "%(artist)s")
format = format.replace("album", "%(album)s")
format = format.replace("title", "%(title)s")
format = format.replace("track", "%(track)s")
return format
def verifyDirectory(directory):
"""
Verify the supplied directory path
"""
if os.path.isdir(directory):
return os.path.abspath(directory)
else:
raise(DirectoryError, "supplied directory cannot be found")
def usage():
print(__doc__)
try:
opts, args = getopt.getopt(argv, "d:f:hlrt", ["directory=",
"format=",
"help",
"flatten",
"recursive",
"test"])
except getopt.error:
usage()
print("\n***Error: %s***" % error)
sys.exit(1)
except error:
usage()
print("\n***Error: %s***" % error)
sys.exit(1)
for opt, arg in opts:
if opt in ("-h", "--help"):
usage()
sys.exit()
elif opt in ("-f", "--format"):
try:
format = verifyFormat(arg)
except FormatError:
print("\n***Error: %s***" % error)
sys.exit(2)
except error:
print("\n***Error: %s***" % error)
sys.exit(2)
elif opt in ("-d", "--directory"):
"""
try:
directory = verifyDirectory(arg)
except DirectoryError:
print("\n***Error: %s***" % error)
sys.exit(3)
except error:
print("\n***Error: %s***" % error)
sys.exit(3)
"""
directory = arg
elif opt in ("-l", "--flatten"):
flatten = True
elif opt in ("-r", "--recursive"):
recursive = True
elif opt in ("-t", "--test"):
test = True
work(directory, format, flatten, recursive, test)
def safety(message): def safety(message):
print("\n***Attention: %s***" % message) print("\n***Attention: %s***" % message)
@ -357,40 +278,85 @@ def safety(message):
def work(directory, format, flatten, recursive, test): def work(directory, format, recursive):
#fileList = get_list_of_file_in_path(directory, [".mp3", ".ogg"], recursive) #fileList = get_list_of_file_in_path(directory, [".mp3", ".ogg"], recursive)
fileList = get_list_of_file_in_path(directory, ["*.*"], recursive=recursive) fileList = get_list_of_file_in_path(directory, ["*.*"], recursive=recursive)
try: try:
if test: count = 0
safety("testing mode; nothing will be renamed") total = len(fileList)
print("\n***Attention: starting***") safety("all audio files in %s will be renamed : %d " % (directory, total))
for f in fileList: print("\n***Attention: starting***")
current = AudioFile(f) start = time.time()
print(current.clean_file_name(format)) for file in fileList:
count += 1
else: current = AudioFile(file)
count = 0 src_file_name, new_tmp_file_name = current.clean_file_name(format)
total = len(fileList) #if src_file_name == new_tmp_file_name:
safety("all audio files in %s will be renamed : %d " % (directory, total)) # continue
print("\n***Attention: starting***") current.rename(new_tmp_file_name)
start = time.time() new_tmp_file_name = print_mangle(new_tmp_file_name)
for file in fileList: print("Renamed %d/%d %d/100: %s" % (count, total, int((count*100)/total), new_tmp_file_name))
count += 1 print("\n%d files renamed in %f seconds" % (len(fileList),
current = AudioFile(file) time.time() - start))
src_file_name, new_tmp_file_name = current.clean_file_name(format)
if src_file_name == new_tmp_file_name:
continue
current.rename(new_tmp_file_name, flatten)
new_tmp_file_name = print_mangle(new_tmp_file_name)
print("Renamed %d of %d : %s" % (count, total, new_tmp_file_name))
print("\n%d files renamed in %f seconds" % (len(fileList),
time.time() - start))
except: except:
print("\n***Error: %s***" % file) print("\n***Error: %s***" % file)
raise raise
if __name__ == "__main__":
restrictedCharPattern = re.compile('[\\\:\*\?"<>\|]')
formatPattern = re.compile('artist|album|title|track')
main(sys.argv[1:])
### Main ###
directory = os.getcwd()
import argparse
parser = argparse.ArgumentParser(description='Check comparison between 2 path.')
parser.add_argument('--format',
type=str,
action='store',
default="%(artist)s(=)%(album)s(=)%(track)s-%(title)s",
help='data formating of the output')
parser.add_argument('--directory',
type=str,
action='store',
default=directory,
help='Path to ordering all the data')
parser.add_argument('--recursive',
action='store_true',
default=False,
help='parse recursively all the directory')
args = parser.parse_args()
def verifyFormat(format):
"""
Verify the supplied file_name format
"""
if re.search(restrictedCharPattern, format):
raise(FormatError, "supplied format contains restricted characters")
if not re.search(formatPattern, format):
raise(FormatError, "supplied format does not contain any metadata keys")
# the supplied format must contain at least one of "artist",
# "album", "title", or "track", or all files will be named
# identically
format = format.replace("artist", "%(artist)s")
format = format.replace("album", "%(album)s")
format = format.replace("title", "%(title)s")
format = format.replace("track", "%(track)s")
return format
try:
format = verifyFormat(args.format)
except FormatError:
print("\n***Error: %s***" % error)
sys.exit(2)
except error:
print("\n***Error: %s***" % error)
sys.exit(2)
work(args.directory, args.format, args.recursive)