#!/usr/bin/python import re import subprocess import sys from math import floor from os import path from os import remove from pydoc import pipepager from time import sleep # Configurations max_file_size_display_width = 9 # For displaying file size in KB col_spacing = 3 # Column spacing for files list # Functions def find_files_in_dir(dir, ext): found = subprocess.check_output(["find", dir, "-type", "f", "-name", '*.' + ext]) return found.rstrip("\n").split("\n") def fit_string_in_terminal_width(string, reserved_space=0): """Adjusts string to fit in terminal, taking into account reserved space""" string_width_available = int(subprocess.check_output(["stty", "size"]).split()[1]) - reserved_space if(len(string) > string_width_available): truncate_filler = '...' half_string_width = int(floor(string_width_available/2)) string = string[0 : half_string_width - len(truncate_filler)] + truncate_filler + string[-half_string_width:] return string.ljust(string_width_available) if len(sys.argv) != 2: print "Usage: %s " % sys.argv[0] exit(1) audio_dir = sys.argv[1] if not path.isdir(audio_dir): print "No such directory: %s" % audio_dir exit(1) audio_dir = path.join(audio_dir, '') # Find FLACs and mp3s in the given audio_dir mp3s = find_files_in_dir(dir=audio_dir, ext='mp3') flacs = find_files_in_dir(dir=audio_dir, ext='flac') # Intersect the lists of FLACs/mp3s to find unnecessary flacs mp3s_noext = [re.sub(".mp3$", "", mp3) for mp3 in mp3s] flacs_noext = [re.sub(".flac$", "", flac) for flac in flacs] files_found_noext = list(set(mp3s_noext) & set(flacs_noext)) if len(files_found_noext) == 0: print "No unnecessary FLACs found" exit(0) # Prepare a list of mp3s names/sizes for the user to check mp3s_to_check = [ file.replace(audio_dir, '') + '.mp3' for file in files_found_noext ] mp3s_to_check.sort() space_outside_file_name = max_file_size_display_width + col_spacing * 2 col_spacer = ' ' * col_spacing verify_dialog = "\033[95mPossibly unnecessary FLACs found for the following mp3s\033[0m\n\033[95mPlease double-check and hit 'q' when done\033[0m\n\n" verify_dialog += fit_string_in_terminal_width("FILENAME", space_outside_file_name) + col_spacer + "SIZE (KB)\n" for file_name in mp3s_to_check: file_name_ascii = ''.join([i if ord(i) < 128 else ' ' for i in file_name]) # Strip non-ASCII chars to make displaying consistent file_name_fit = fit_string_in_terminal_width(file_name_ascii, space_outside_file_name) file_size_fit = str(path.getsize(audio_dir + file_name) / 1000).rjust(max_file_size_display_width) verify_dialog += file_name_fit + col_spacer + file_size_fit + "\n" # Output list for user to check pipepager(verify_dialog, cmd='less -R') # Prompt for deletion of files files_found_count = len(files_found_noext) print "\nDelete %d FLAC(s) [y/N]? :" % files_found_count, if raw_input().lower() != 'y': print "Exiting: cold feet" exit(0) # Delete unnecessary FLACs for index, file_name_noext in enumerate(files_found_noext): print "\rDeleting FLAC %d/%d" % (index+1, files_found_count), remove(file_name_noext + '.flac') print "\nAll done."