This repository has been archived by the owner on Jun 21, 2018. It is now read-only.
forked from fasaxc/shotwell-iphoto-import
-
Notifications
You must be signed in to change notification settings - Fork 1
/
fs.py
55 lines (44 loc) · 1.44 KB
/
fs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import hashlib
import os
import shutil
import logging
logger = logging.getLogger("iphotoimport")
class FileSystem:
def __init__(self, forceCopy):
self.forceCopy = forceCopy
def safe_link_file(self, src, dst):
assert os.path.exists(src), "%s didn't exist" % src
if self.forceCopy:
self.mkdir(dst)
shutil.copy(src, dst)
return
if os.path.exists(dst):
if self.is_file_same(src, dst):
# Nothing to do
return
else:
raise Exception("Destination file %s exists and not equal to %s" % (dst, src))
else:
self.mkdir(dst)
# Try to link the file
try:
os.link(src, dst)
except:
logger.debug("Hard link failed, falling back on copy")
shutil.copy(src, dst)
def is_file_same(self, f1, f2):
return os.path.samefile(f1, f2) or self.md5_for_file(f1) == self.md5_for_file(f2)
def md5_for_file(self, filename, block_size=2**20):
with open(filename, "rb") as f:
md5 = hashlib.md5()
while True:
data = f.read(block_size)
if not data:
break
md5.update(data)
return md5.hexdigest()
def mkdir(self,dir):
try:
os.makedirs(os.path.dirname(dir))
except Exception:
pass