21
21
from xml .etree import ElementTree
22
22
import MySQLdb as mdb
23
23
import re
24
+ import tarfile
24
25
25
26
class MagentoImageCleanup :
26
27
@@ -38,13 +39,16 @@ def parse(self):
38
39
parser .add_argument ('--dry-run' , action = 'store_true' , default = False ,
39
40
help = 'dry run' )
40
41
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' )
41
45
parser .add_argument ('magentoPath' , metavar = 'MAGENTOPATH' ,
42
46
help = 'base path of magento' )
43
- parser .add_argument ('--force-host' , metavar = 'HOST' , help = "force this host" )
44
47
args = parser .parse_args ()
45
48
self .magentoPath = args .magentoPath
46
49
self .really = not args .dry_run
47
50
self .force_host = args .force_host
51
+ self .imageArchive = args .image_archive
48
52
logging .basicConfig (level = (logging .DEBUG if args .verbose else logging .INFO ))
49
53
50
54
@staticmethod
@@ -73,18 +77,63 @@ def getAllImagePath(self):
73
77
conn = mdb .connect (host = hostname , user = username , passwd = password , db = database )
74
78
75
79
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"
77
81
% {'prefix' :prefix })
78
82
images = {}
79
83
for v in cur .fetchall ():
80
- images [v [0 ]] = True
84
+ images [v [0 ]] = v [ 1 ]
81
85
cur .close ()
82
86
conn .close ()
83
87
self .log .info ("fetched %d image paths" % (len (images ), ))
84
88
return images
85
89
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
+
86
132
def run (self ):
87
133
images = self .getAllImagePath ()
134
+ if self .imageArchive :
135
+ self .createImageArchive (images )
136
+ return
88
137
89
138
productsPath = os .path .join (self .magentoPath , self .MEDIA_PRODUCT )
90
139
index = len (productsPath )
0 commit comments