Skip to content

Commit 7f11758

Browse files
committed
Added an image extractor to tar.gz
1 parent 8ef69f3 commit 7f11758

File tree

2 files changed

+56
-4
lines changed

2 files changed

+56
-4
lines changed

README.rst

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@
22
magento-imagecleanup
33
====================
44

5-
Magento Image Cleanup
5+
Setup. In order to local install:
6+
7+
$ cd magento-imagecleanup
8+
$ pip install --user ./

src/magentoimagecleanup.py

+52-3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from xml.etree import ElementTree
2222
import MySQLdb as mdb
2323
import re
24+
import tarfile
2425

2526
class MagentoImageCleanup:
2627

@@ -38,13 +39,16 @@ def parse(self):
3839
parser.add_argument('--dry-run', action='store_true', default=False,
3940
help='dry run')
4041
parser.add_argument('-v', '--verbose', action='store_true', default=False)
42+
parser.add_argument('--force-host', metavar='HOST', help="force this host")
43+
parser.add_argument('--image-archive', metavar='IMAGEARCHIVE',
44+
help='Tar archive for images')
4145
parser.add_argument('magentoPath', metavar='MAGENTOPATH',
4246
help='base path of magento')
43-
parser.add_argument('--force-host', metavar='HOST', help="force this host")
4447
args = parser.parse_args()
4548
self.magentoPath = args.magentoPath
4649
self.really = not args.dry_run
4750
self.force_host = args.force_host
51+
self.imageArchive = args.image_archive
4852
logging.basicConfig(level=(logging.DEBUG if args.verbose else logging.INFO))
4953

5054
@staticmethod
@@ -73,18 +77,63 @@ def getAllImagePath(self):
7377
conn = mdb.connect(host=hostname, user=username, passwd=password, db=database)
7478

7579
cur = conn.cursor()
76-
cur.execute("SELECT value FROM %(prefix)scatalog_product_entity_media_gallery"
80+
cur.execute("SELECT value, entity_id FROM %(prefix)scatalog_product_entity_media_gallery"
7781
% {'prefix':prefix})
7882
images = {}
7983
for v in cur.fetchall():
80-
images[v[0]] = True
84+
images[v[0]] = v[1]
8185
cur.close()
8286
conn.close()
8387
self.log.info("fetched %d image paths" % (len(images), ))
8488
return images
8589

90+
def notFound(self):
91+
images = self.getAllImagePath()
92+
93+
productsPath = os.path.join(self.magentoPath, self.MEDIA_PRODUCT)
94+
index = len(productsPath)
95+
96+
exists = set()
97+
for dirname, dirnames, filenames in os.walk(productsPath):
98+
for filename in filenames:
99+
if self.EXTENSIONS.match(filename):
100+
path = os.path.join(dirname, filename)
101+
value = path[index:]
102+
exists.add(value)
103+
104+
for name in dirnames:
105+
if 'cache' in name or 'google' in name:
106+
self.log.debug("skip %s", os.path.join(dirname, name))
107+
dirnames.remove(name)
108+
109+
for value in images.keys():
110+
if not value in exists:
111+
print value
112+
113+
def createImageArchive(self, images):
114+
tar = tarfile.open(self.imageArchive, 'w:gz')
115+
counters = {}
116+
for imagePath, productId in images.items():
117+
try:
118+
counters[productId] += 1
119+
except KeyError:
120+
counters[productId] = 1
121+
name = "%s_%d.jpg" % (productId, counters[productId])
122+
info = tarfile.TarInfo(name=name)
123+
img = os.path.join(self.magentoPath, self.MEDIA_PRODUCT, imagePath[1:])
124+
info.size = os.path.getsize(img)
125+
info.mtime = os.path.getmtime(img)
126+
with open(img, 'r') as fileobj:
127+
tar.addfile(tarinfo=info, fileobj=fileobj)
128+
tar.addfile(info)
129+
self.log.debug('add %s to archive', imagePath)
130+
self.log.info('%s successfully created', self.imageArchive)
131+
86132
def run(self):
87133
images = self.getAllImagePath()
134+
if self.imageArchive:
135+
self.createImageArchive(images)
136+
return
88137

89138
productsPath = os.path.join(self.magentoPath, self.MEDIA_PRODUCT)
90139
index = len(productsPath)

0 commit comments

Comments
 (0)