-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathiphoto_cleanup
executable file
·76 lines (65 loc) · 2.41 KB
/
iphoto_cleanup
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/env python
# Replaces the original files in iPhoto Library with modified ones to
# gain some space.
# Baris Metin <baris _at_ metin.org>
import os
import sys
import shutil
INFO = """
WARNING:
Now you'll need to launch iPhoto, select all your Events
and run 'Revert to Originals' from the Photos menu.
"""
HOME = os.getenv("HOME")
PICTURES = os.path.join(HOME, "Pictures")
IPHOTO_LIBRARY = os.path.join(PICTURES, "iPhoto Library")
MODIFIED_DIR = os.path.join(IPHOTO_LIBRARY, "Modified")
ORIGINALS_DIR = os.path.join(IPHOTO_LIBRARY, "Originals")
def find_original_file(root, f):
o_f = os.path.join(root.replace(MODIFIED_DIR, ORIGINALS_DIR), f)
if os.path.exists(o_f):
return o_f
# try harder to find the original. maybe it doesn't have the same
# extension (possible with RAW files). or maybe they don't have
# the same base directory.
fname, ext = os.path.splitext(os.path.basename(f))
o_dir = os.path.dirname(o_f)
# check for a base directory
if not os.path.exists(o_dir):
o_parent = os.path.dirname(o_dir)
o_dir_base = os.path.basename(o_dir)
if not os.path.exists(o_parent):
print " * Can not find the Originals directory * :", o_parent
return None
o_parent_dirs = os.listdir(o_parent)
for d in o_parent_dirs:
if o_dir_base.startswith(d) and o_dir_base[len(d):].startswith("_"):
o_dir = os.path.join(o_parent, d)
break
else:
return None
o_f = os.path.join(o_dir, f)
if os.path.exists(o_f):
return o_f
# check of a different extension
o_files = os.listdir(o_dir)
for o in o_files:
if o.startswith(fname) and o[len(fname):].startswith(os.path.extsep):
print " * Original file has a different extension! * :", o
return os.path.join(o_dir, o)
return None
def main():
for root, dirs, files in os.walk(MODIFIED_DIR):
for f in files:
modified_file = os.path.join(root, f)
print "Trying for :", modified_file
original_file = find_original_file(root, f)
if original_file:
print "Moving to " + original_file
shutil.move(modified_file, original_file)
else:
print " * Original file not found! * :", f
print
print "\n"
if __name__ == "__main__":
main()