-
Notifications
You must be signed in to change notification settings - Fork 0
/
remove_unused_assets.py
37 lines (31 loc) · 1.26 KB
/
remove_unused_assets.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
#!python
import os
import shutil
from collections import defaultdict
ROOT_DIR = "tutorial"
ASSETS_DIR = os.path.join(ROOT_DIR, ".assets")
TRASH_DIR = os.path.join(ROOT_DIR, ".trash")
# 遍历 ROOT_DIR 目录下的所有 md 文件,记录每个文件被引用的次数
_refs = defaultdict(int)
for root, dirs, files in os.walk(ROOT_DIR):
for file in files:
if file.endswith(".md"):
with open(os.path.join(root, file), "r", encoding="utf-8") as f:
for line in f:
line = line.strip()
# find first ![ in line
r = line.find("![")
if r == -1:
continue
line = line[r:]
if line.startswith("!["):
file_name = line.split("]")[1].strip("()")
file_name = file_name.removeprefix("./.assets/")
_refs[file_name] += 1
if not os.path.exists(TRASH_DIR):
os.makedirs(TRASH_DIR)
for file_name in os.listdir(ASSETS_DIR):
file_path = os.path.join(ASSETS_DIR, file_name)
if os.path.isfile(file_path) and _refs[file_name] == 0:
print(f"Moving {file_name} to .trash")
shutil.move(file_path, os.path.join(TRASH_DIR, file_name))