diff options
author | Étienne Loks <etienne.loks@iggdrasil.net> | 2020-08-31 11:55:49 +0200 |
---|---|---|
committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2020-08-31 11:55:49 +0200 |
commit | fc9a0fdeaef6920c3f7f0a6c1dd5cca40811f8d1 (patch) | |
tree | 1e38ca9770d04c9465a0c5cd8077ef9da6226863 /ishtar_common | |
parent | 89ce0dd5677d1673af9d6b2ee9e22999e9012117 (diff) | |
download | Ishtar-fc9a0fdeaef6920c3f7f0a6c1dd5cca40811f8d1.tar.bz2 Ishtar-fc9a0fdeaef6920c3f7f0a6c1dd5cca40811f8d1.zip |
"hard" option for media_find_missing_files command
Diffstat (limited to 'ishtar_common')
-rw-r--r-- | ishtar_common/management/commands/media_find_missing_files.py | 15 | ||||
-rw-r--r-- | ishtar_common/utils.py | 22 |
2 files changed, 30 insertions, 7 deletions
diff --git a/ishtar_common/management/commands/media_find_missing_files.py b/ishtar_common/management/commands/media_find_missing_files.py index 226699842..73f4e0ccf 100644 --- a/ishtar_common/management/commands/media_find_missing_files.py +++ b/ishtar_common/management/commands/media_find_missing_files.py @@ -38,7 +38,7 @@ class Command(BaseCommand): ) parser.add_argument( '--limit', nargs='?', dest='limit', default=None, - help="Field to limit to separated with \",\"." + help="Field to limit to. Separate field names with \",\"." ) parser.add_argument( '--try-fix', dest='try-fix', action='store_true', @@ -48,16 +48,24 @@ class Command(BaseCommand): '--find-fix', dest='find-fix', action='store_true', default=False, help='Try to find file with similar names and print them.') + parser.add_argument( + '--hard', dest='hard', action='store_true', + default=False, + help='Search on the whole media dir.') def handle(self, *args, **options): exclude = options['exclude'].split(',') if options['exclude'] else [] limit = options['limit'].split(',') if options['limit'] else [] try_fix = options['try-fix'] find_fix = options['find-fix'] + hard = options['hard'] if try_fix and find_fix: self.stdout.write("try-fix and find-fix options are not " "compatible.\n") return + if hard and not (try_fix or find_fix): + self.stdout.write("hard option has no effect if try-fix or " + "find-fix are not set.\n") missing = [] for media in get_used_media(exclude=exclude, limit=limit): @@ -70,7 +78,8 @@ class Command(BaseCommand): else: self.stdout.write("* fixes files:\n") for item in missing[:]: - source_file = try_fix_file(item, make_copy=try_fix) + source_file = try_fix_file(item, make_copy=try_fix, + hard=hard) if source_file: missing.pop(missing.index(item)) sys.stdout.write( @@ -82,6 +91,6 @@ class Command(BaseCommand): self.stdout.write("* missing file with no similar file " "found:\n") for item in missing: - sys.stdout.write(item.encode('utf-8') + "\n") + sys.stdout.write(item + "\n") else: self.stdout.write("No missing files.\n") diff --git a/ishtar_common/utils.py b/ishtar_common/utils.py index 6183cdc49..2eacce013 100644 --- a/ishtar_common/utils.py +++ b/ishtar_common/utils.py @@ -1717,12 +1717,13 @@ def remove_empty_dirs(path=None): return False -def try_fix_file(filename, make_copy=True): +def try_fix_file(filename, make_copy=True, hard=False): """ Try to find a file with a similar name on the same dir. :param filename: filename (full path) :param make_copy: make the copy of the similar file found + :param hard: search on the whole media dir :return: name of the similar file found or None """ path, current_name, simplified_ref_name = simplify_name( @@ -1733,8 +1734,8 @@ def try_fix_file(filename, make_copy=True): except UnicodeDecodeError: dirs = list(sorted(os.listdir(path.encode('utf-8')))) # check existing files in the path - for file in dirs: - full_file = os.sep.join([path, file]) + for f in dirs: + full_file = os.sep.join([path, f]) if not os.path.isfile(full_file): # must be a file continue _, _, name = simplify_name(full_file, check_existing=False) @@ -1742,4 +1743,17 @@ def try_fix_file(filename, make_copy=True): # a candidate is found if make_copy: shutil.copy2(full_file, filename) - return file + return f + if not hard: + return + for root, __, files in os.walk(settings.MEDIA_ROOT): + for f in files: + full_file = os.sep.join([root, f]) + if not os.path.isfile(full_file): # must be a file + continue + _, _, name = simplify_name(full_file, check_existing=False) + if simplified_ref_name.lower() == name.lower(): + # a candidate is found + if make_copy: + shutil.copy2(full_file, filename) + return f |