Skip to content

Instantly share code, notes, and snippets.

@rmehta
Last active December 3, 2015 11:17
Show Gist options
  • Save rmehta/298b3d76d5372509c90e to your computer and use it in GitHub Desktop.
Save rmehta/298b3d76d5372509c90e to your computer and use it in GitHub Desktop.
Purge unused image files in docs
# 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