Skip to content

Instantly share code, notes, and snippets.

@chwnam
Created November 23, 2014 14:37
Show Gist options
  • Save chwnam/8809523dae3cae9c13d9 to your computer and use it in GitHub Desktop.
Save chwnam/8809523dae3cae9c13d9 to your computer and use it in GitHub Desktop.
Dropbox Clash Cleaner (korean ver.)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 드롭박스 동기화 중 파일 충돌로 인해 중복된 파일이 엄청나게 쌓이는 경우, 쌓이는 파일들을 별도로 추려내는 스크립트
import os
import re
class ClashItem:
def __init__(self, original_file_name, location, clash_file_name, date, dir_path):
self.original_file_name = original_file_name
self.clash_file_name = clash_file_name
self.location = location
self.dir_path = dir_path
self.date = date
def __str__(self):
text = '=' * 80 + '\n'
text += 'CLASHED FILE NAME: %s\n' % (self.clash_file_name, )
text += 'ORIGINAL FILE NAME: %s\n' % (self.original_file_name, )
text += 'LOCATION: %s\n' % (self.location, )
text += 'DATE: %s\n' % (self.date, )
text += 'DIRECTORY: %s\n' % (self.dir_path, )
text += '=' * 80 + '\n\n'
return text
class DropboxClashCleaner(object):
clash_expr = re.compile(r'(.+?)\((.+)와\(과\) 충돌하는 사본 (\d{4}-\d{2}-\d{2})\)(\..+)?')
def __init__(self):
pass
@classmethod
def walk_and_inspect(cls, dropbox_path):
if not os.path.exists(dropbox_path):
return []
clash_list = []
for dir_path, dir_names, file_names in os.walk(dropbox_path):
for file_name in file_names:
m = cls.clash_expr.match(file_name)
if m:
original_file = m.group(1) + m.group(4) if m.group(4) else ''
location = m.group(2)
date = m.group(3)
clash_list.append(ClashItem(original_file, location, file_name, date, dir_path))
return clash_list
if __name__ == '__main__':
clash_list = DropboxClashCleaner.walk_and_inspect('/home/changwoo/Dropbox')
for item in clash_list:
print item
if clash_list:
target_dir = os.path.join(os.getcwd(), 'clashed_files')
if not os.path.exists(target_dir):
os.mkdir(target_dir)
for item in clash_list:
item_path = os.path.join(item.dir_path, item.clash_file_name)
target_path = os.path.join(target_dir, item.clash_file_name)
os.rename(item_path, target_path)
print "Processed total %d items" % (len(clash_list), )
else:
print "Nothing to process! :)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment