Last active
December 3, 2015 11:17
-
-
Save rmehta/298b3d76d5372509c90e to your computer and use it in GitHub Desktop.
Purge unused image files in docs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# run this from the docs folder | |
from __future__ import unicode_literals | |
import os, re | |
images = [] | |
for basepath, folders, files in os.walk("."): | |
for f in files: | |
key, extn = f.rsplit(".", 1) | |
if extn in ("html", "md"): | |
f = open(os.path.join(basepath, f), "r") | |
content = f.read() | |
images += re.findall("""{{.?docs_base_url.?}}([^'"\)]+)""", content) | |
missing, total = [], [] | |
for basepath, folders, files in os.walk("."): | |
if "assets" in basepath: | |
for f in files: | |
total.append(f) | |
iname = os.path.join(basepath, f)[1:] | |
if iname.encode("utf-8") not in images: | |
missing.append(iname) | |
os.remove(os.path.join(basepath, f)) | |
print len(images), len(missing) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment