Skip to content

Commit 91c6a24

Browse files
authored
Merge pull request #1506 from redis/find-unused-images
image_report.py: Add find unused images functionality
2 parents 822703d + ca92e23 commit 91c6a24

File tree

1 file changed

+29
-5
lines changed

1 file changed

+29
-5
lines changed

build/image_report.py

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,19 @@
77
import os
88

99

10-
def scan_file(path: str) -> int:
10+
def scan_file(path: str, verbose: bool = True) -> list:
1111
"""Scans a file for all `image` shortcodes.
1212
1313
Args:
1414
path (str): Path to file.
15+
verbose (bool, optional): If True, prints the shortcodes found. Defaults to True.
1516
1617
Returns:
17-
(int) Number of shortcodes found.
18+
(list) A list of all image shortcodes found.
1819
"""
1920

2021
img_list = []
22+
img_names = []
2123

2224
with open(path, encoding="utf_8") as md_file:
2325
text = md_file.read()
@@ -26,16 +28,18 @@ def scan_file(path: str) -> int:
2628
text, lambda t: t.tag == "image"
2729
):
2830
img_list.append((img, pos_info))
31+
img_names.append(img.named_params["filename"])
2932

30-
if len(img_list) > 0:
33+
if len(img_list) > 0 and verbose:
3134
print(f"File '{path}':")
3235

3336
for img in img_list:
3437
print(
3538
f" Line {img[1].line}: '{img[0].named_params['filename']}'"
3639
)
3740

38-
return len(img_list)
41+
# return len(img_list)
42+
return img_names
3943

4044

4145
parser = argparse.ArgumentParser(
@@ -44,20 +48,40 @@ def scan_file(path: str) -> int:
4448
)
4549

4650
parser.add_argument("pathname", help="Path of the folder to scan")
51+
parser.add_argument("--find-unused", nargs=1, help="Prints out all images in IMAGEFOLDER that are not found in pathname. Set pathname to the top content folder to scan all content for images.", metavar="IMAGEFOLDER")
4752

4853
args = parser.parse_args()
4954

5055
print(f"Scanning '{args.pathname}'")
5156

5257
num_found = 0
58+
all_images_found = []
5359

5460
for root, dirs, files in os.walk(args.pathname):
5561
for file in files:
5662
if file.endswith(".md"):
5763
fullpath = os.path.join(root, file)
58-
num_found += scan_file(fullpath)
64+
images_found = scan_file(fullpath, verbose=args.find_unused is None)
65+
num_found += len(images_found)
66+
all_images_found.extend(images_found)
5967

6068
if num_found == 0:
6169
print(f"No image shortcodes found in '{args.pathname}'")
6270
else:
6371
print(f"Found {num_found} image shortcodes.")
72+
73+
if args.find_unused and num_found > 0:
74+
unique_images = list(set(all_images_found))
75+
print(f"Found {len(unique_images)} unique images in '{args.pathname}'.")
76+
print(f"Checking for images not found in '{args.pathname}' that are in '{args.find_unused[0]}'")
77+
78+
unused_images = []
79+
80+
for root, dirs, files in os.walk(args.find_unused[0]):
81+
for file in files:
82+
if (file.endswith(".png") or file.endswith(".jpg") or file.endswith(".webp")) and not any(file in img for img in unique_images):
83+
img_filepath = os.path.join(root, file)
84+
print(f" Image '{img_filepath}' not found in '{args.pathname}'")
85+
unused_images.append(img_filepath)
86+
87+
print(f"Found {len(unused_images)} unused images.")

0 commit comments

Comments
 (0)