forked from john-guerra/webDevelopmentSlides
-
Notifications
You must be signed in to change notification settings - Fork 1
/
filter_unused_images.py
47 lines (34 loc) · 988 Bytes
/
filter_unused_images.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
38
39
40
41
42
43
44
45
46
47
import os
import sys
import re
import shutil
import glob
if len(sys.argv) < 2:
print """Usage
python filter_unused_images.py folder"""
sys.exit(-1)
def getImageList(path):
images = []
for line in open(path).readlines():
imagesFound = re.findall("images/.*\.\w*", line)
if len(imagesFound) > 0:
images.append(imagesFound[0])
return images
def moveImages(path, imagesUsed):
prev_path = os.getcwd()
os.chdir(path)
unused_folder = os.path.join("images", "unused_images")
if not os.path.exists(unused_folder):
os.mkdir(unused_folder)
for imagePath in glob.glob(os.path.join("images", "*.*")):
# print imagePath
if not imagePath in imagesUsed:
print "%s unused, moving it" % imagePath
shutil.move(imagePath, unused_folder)
os.chdir(prev_path)
folder = sys.argv[1]
imagesUsed = getImageList(os.path.join(folder, "index.jade"))
print "Images found:"
print imagesUsed
print "moving images"
moveImages(folder, imagesUsed)