Created
February 15, 2018 19:16
-
-
Save sgobin/82fbcf6a83622963579e52cdd1dc58e6 to your computer and use it in GitHub Desktop.
Check for imagens not used or referenced in HTML or CSS files in a site project
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
# Recursively check for images in a directory and compare with referenced images in CSS and HTML files | |
# in tha folder, generating two CSV files: | |
# One with used images and one with not referenced ones | |
# (does not look into JS files) | |
import glob | |
import csv | |
# Create the list of file names | |
pasta = "src/" #root of the project source files | |
pastaIMG = "src/img/" #root of images folder | |
buscaPNG = "**/*.png" | |
buscaJPG = "**/*.jpg" | |
buscaGIF = "**/*.gif" | |
pngs = glob.glob(pastaIMG + buscaPNG, recursive = True) | |
jpgs = glob.glob(pastaIMG + buscaJPG, recursive = True) | |
gifs = glob.glob(pastaIMG + buscaGIF, recursive = True) | |
htmls = glob.glob(pasta + "**/*.html", recursive = True) | |
csss = glob.glob(pasta + "**/*.css", recursive = True) | |
imagens = pngs + jpgs + gifs # join all image lists into one | |
imagens[:] = [i[4:] for i in imagens] # remove o /src from the path, must be changed for other names | |
fontes = htmls + csss | |
# Search files looking for references for the image | |
imgsUsadas = [] | |
for arquivo in fontes: | |
fonte = open(arquivo, "r") | |
for l in fonte: | |
for img in imagens: | |
if img in l: | |
imgsUsadas.append(img) | |
imagens.remove(img) | |
# Write lists to csv files | |
arqImgsUsadas = "site-used-images.csv" | |
arqImgsNaoUsadas = "site-images-NOT-referenced.csv" | |
with open(arqImgsUsadas, "w") as output: | |
writer = csv.writer(output) | |
for val in imgsUsadas: | |
writer.writerow([val]) | |
with open(arqImgsNaoUsadas, "w") as output: | |
writer = csv.writer(output) | |
for val in imagens: | |
writer.writerow([val]) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment