sokoban
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
#!/usr/bin/python3

import re
from glob import iglob
import json
import os

used = set()
unused = set()

re_dest_files = re.compile(r'^dest_files=(\[.*\])$')

# for all *.import files in project:
for path in iglob("**/*.import", recursive=True):
    with open(path, 'r') as f:
        # find the dest_files line in the [deps] section
        deps = False
        found = False
        for line in f:
            if not deps:
                deps = (line == '[deps]\n')
                continue
            else:
                if line[0] == '[':
                    # new section encountered, somehow there's no dest_files
                    break

                # extract the imported file path(s)
                match = re_dest_files.match(line)
                if match:
                    paths_str = match.group(1)
                    paths = json.loads(paths_str)
                    md5s = set()
                    for i, p in enumerate(paths):
                        # remove 'res://'
                        paths[i] = p[6:]
                        md5s.add(os.path.splitext(paths[i])[0] + '.md5')

                    used |= set(paths)
                    used |= md5s
                    found = True
                    break

        if not found:
            print(f"warning: {path}: no deps.dest_files")

# find every file in .import/ that isn't referenced in a *.import file
for path in iglob('.import/*'):
    if path not in used:
        unused.add(path)

# do this alphabetically for nicer terminal output
unused = list(unused)
unused.sort()
n = 0

for path in unused:
    print(path)
    os.remove(path)
    n += 1

print('---')
print(f'removed {n} files from .import/')