forked from bwesterb/blame-bird
-
Notifications
You must be signed in to change notification settings - Fork 0
/
blame-bird.py
executable file
·73 lines (58 loc) · 2.25 KB
/
blame-bird.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env python
""" This script tries to figure out the app responsible for
a huge Library/Caches/com.apple.bird folder. """
import os
import uuid
import sqlite3
import os.path
def maybe_uuid(x):
""" Tries to parse x as an UUID and return that, otherwise return None. """
try:
return uuid.UUID(x)
except ValueError:
return None
def main():
cache_path = os.path.expanduser('~/Library/Caches/com.apple.bird/session/g')
# if there is no cache we can exit right away
if not (os.path.isdir(cache_path)):
print("{}: does not exist at all".format(cache_path))
return
# Ignore files in the bird cache directory that do not appear to be UUIDs
unaccounted_for = set(filter(bool, map(uuid.UUID, os.listdir(cache_path))))
client_db = sqlite3.connect(os.path.expanduser(
'~/Library/Application Support/CloudDocs/session/db/client.db'))
c = client_db.cursor()
zones = {}
zonesize = {}
for rowid, name in c.execute('select rowid, zone_name from client_zones'):
zones[rowid] = name
zonesize[rowid] = 0
for raw_guid, zone_id in c.execute(
'select item_id, zone_rowid from client_unapplied_table'):
# Apparently, not every item_id is an UUID.
if len(raw_guid) != 16:
continue
guid = uuid.UUID(bytes=raw_guid)
if guid not in unaccounted_for:
continue
path = os.path.join(cache_path, str(guid).upper())
if not os.path.exists(path):
continue
zonesize[zone_id] += os.stat(path).st_size
unaccounted_for.remove(guid)
accounted_size = 0
unaccounted_size = 0
for zone_id, size in sorted(zonesize.items(), key=lambda x: x[1]):
if size == 0:
continue
print('{:<45} {:>10.2f}MB'.format(zones[zone_id],
zonesize[zone_id] / 1000000.))
accounted_size += zonesize[zone_id]
for guid in unaccounted_for:
path = os.path.join(cache_path, str(guid).upper())
unaccounted_size += os.stat(path).st_size
print('')
print('Accounted for: {}MB. Still unaccounted: {}MB'.format(
accounted_size // 1000000, unaccounted_size // 1000000))
if __name__ == '__main__':
main()