11#!/usr/bin/env python
22
33import os , sys , shutil , pathlib
4+ from collections import namedtuple
5+ from PIL import Image
46
57# boilerplate
68basedir = os .path .abspath (os .path .join (os .path .dirname (__file__ ), "../../" ))
@@ -10,35 +12,37 @@ os.environ.setdefault("DJANGO_SETTINGS_MODULE", "ietf.settings")
1012import django
1113django .setup ()
1214
15+ from django .conf import settings
1316import debug
1417
1518from ietf .group .models import Role , Person
1619
17- old_images_dir = django .conf .settings .OLD_PHOTOS_DIR
18- new_images_dir = django .conf .settings .PHOTOS_DIR
1920
20- if not os .path .exists (old_images_dir ):
21- print ("Old images directory does not exist: %s" % old_images_dir )
22- sys .exit (1 )
21+
22+
23+ old_images_dir = ''
24+ new_images_dir = settings .PHOTOS_DIR
25+
2326if not os .path .exists (new_images_dir ):
2427 print ("New images directory does not exist: %s" % new_images_dir )
2528 sys .exit (1 )
2629
2730old_image_files = []
31+ for dir in settings .OLD_PHOTO_DIRS :
32+ if not os .path .exists (dir ):
33+ print ("Old images directory does not exist: %s" % dir )
34+ sys .exit (1 )
35+ old_image_files += [ f for f in pathlib .Path (dir ).iterdir () if f .is_file () and f .suffix .lower () in ['.jpg' , '.jpeg' , '.png' ] ]
2836
37+ photo = namedtuple ('photo' , ['path' , 'name' , 'ext' , 'width' , 'height' , 'time' , 'file' ])
2938
30- for (dirpath , dirnames , filenames ) in os .walk (old_images_dir ):
31- if len (filenames ) == 0 :
32- print ("No image files found in %s" % old_images_dir )
33- sys .exit (2 )
34- old_image_files .extend (filenames )
35- break # Only interested in the files in the top directory
39+ old_images = []
40+ for f in old_image_files :
41+ path = str (f )
42+ img = Image .open (path )
43+ old_images .append (photo (path , f .stem .decode ('utf8' ), f .suffix , img .size [0 ], img .size [1 ], f .stat ().st_mtime , f ))
3644
37- old_image_files = [ f .name .decode ('utf8' ) for f in pathlib .Path (old_images_dir ).iterdir () if f .is_file () and not f .suffix .lower () in ['.lck' ] ]
38-
39- interesting_persons = set ()
40-
41- interesting_persons .update (list (Person .objects .all ()))
45+ interesting_persons = set (Person .objects .all ())
4246
4347name_alias = {
4448 "andy" : ["andrew" , ],
@@ -121,62 +125,63 @@ for person in sorted(list(interesting_persons),key=lambda x:x.last_name()+x.asci
121125 if not substr_pattern :
122126 substr_pattern = u'-' .join (name_parts [- 1 :]+ name_parts [0 :1 ])
123127
124- candidates = [x for x in old_image_files if x .lower ().startswith (substr_pattern )]
128+ candidates = [x for x in old_images if x .name .lower ().startswith (substr_pattern )]
129+ # Also check the reverse the name order (necessary for Deng Hui, for instance)
130+ substr_pattern = u'-' .join (name_parts [0 :1 ]+ name_parts [- 1 :])
131+ candidates += [x for x in old_images if x .name .lower ().startswith (substr_pattern )]
132+ if candidates :
133+ print (" Used '%s %s' instead of '%s %s'" % (name_parts [- 1 ], name_parts [0 ], name_parts [0 ], name_parts [- 1 ], ))
125134 # If no joy, try a short name
126- if not candidates and name_parts [0 ] in name_alias :
135+ if name_parts [0 ] in name_alias :
127136 for alias in name_alias [name_parts [0 ]]:
128137 substr_pattern = u'-' .join (name_parts [- 1 :]+ [alias ])
129- candidates += [x for x in old_image_files if x .lower ().startswith (substr_pattern )]
138+ candidates += [x for x in old_images if x . name .lower ().startswith (substr_pattern )]
130139 if candidates :
131140 print (" Used '%s %s' instead of '%s %s'" % (alias , name_parts [- 1 ], name_parts [0 ], name_parts [- 1 ], ))
132- # If still no joy, reverse the name order (necessary for Deng Hui, for instance)
133- if not candidates :
134- substr_pattern = u'-' .join (name_parts [0 :1 ]+ name_parts [- 1 :])
135- candidates = [x for x in old_image_files if x .lower ().startswith (substr_pattern )]
136- if candidates :
137- print (" Used '%s %s' instead of '%s %s'" % (name_parts [- 1 ], name_parts [0 ], name_parts [0 ], name_parts [- 1 ], ))
138141 # If still no joy, try with Person.plain_name() (necessary for Donald Eastlake)
139142 if not candidates :
140143 name_parts = person .plain_name ().lower ().split ()
141144 substr_pattern = u'-' .join (name_parts [- 1 :]+ name_parts [0 :1 ])
142- candidates = [x for x in old_image_files if x .lower ().startswith (substr_pattern )]
145+ candidates = [x for x in old_images if x . name .lower ().startswith (substr_pattern )]
143146 # If no joy, try a short name
144147 if not candidates and name_parts [0 ] in name_alias :
145148 for alias in name_alias [name_parts [0 ]]:
146149 substr_pattern = u'-' .join (name_parts [- 1 :]+ [alias ])
147- candidates += [x for x in old_image_files if x .lower ().startswith (substr_pattern )]
150+ candidates += [x for x in old_images if x . name .lower ().startswith (substr_pattern )]
148151 if candidates :
149152 print (" Used '%s %s' instead of '%s %s'" % (alias , name_parts [- 1 ], name_parts [0 ], name_parts [- 1 ], ))
150153
151- # Fixup for other exceptional cases
152-
153- if person .ascii == "David Oran" :
154- candidates = ['oran-dave-th.jpg' ,'oran-david.jpg' ]
155-
156- if person .ascii == "Susan Hares" :
157- candidates = ['hares-sue-th.jpg' ,'hares-susan.JPG' ]
158-
159- if person .ascii == "Mahesh Jethanandani" :
160- candidates = ['Mahesh-Jethanandani-th.jpg' ,'Jethanandani-Mahesh.jpg' ]
161-
162- processed_files += [ c for c in candidates ]
163-
164- if len (candidates ) not in [0 ,1 ,2 ]:
165- candidates = [x for x in candidates if not '00' in x ]
166-
167- if len (candidates ) == 1 :
168- candidates = candidates + candidates
169-
170- if len (candidates ) not in [0 ,2 ]:
171- thumb = [ c for c in candidates if '-th.' in c ][0 ]
172- photo = [ c for c in candidates if '-th.' not in c ][0 ]
173- trunc = [thumb , photo ]
174- print (" Truncating %s to %s" % (candidates , trunc ))
175- candidates = trunc
176-
177- if candidates and '-th' in candidates [1 ]:
178- candidates .reverse ()
179-
154+ # # Fixup for other exceptional cases
155+ # if person.ascii=="David Oran":
156+ # candidates = ['oran-dave-th.jpg','oran-david.jpg']
157+ #
158+ # if person.ascii=="Susan Hares":
159+ # candidates = ['hares-sue-th.jpg','hares-susan.JPG']
160+ #
161+ # if person.ascii=="Mahesh Jethanandani":
162+ # candidates = ['Mahesh-Jethanandani-th.jpg','Jethanandani-Mahesh.jpg']
163+
164+ processed_files += [ c .path for c in candidates ]
165+
166+ # We now have a list of candidate photos.
167+ # * Consider anything less than 200x200 a thumbnail
168+ # * For the full photo, sort by size (width) and time
169+ # * For the thumbnail:
170+ # - first look for a square photo less than 200x200
171+ # - if none found, then for the first in the sorted list less than 200x200
172+ # - if none found, then the smallest photo
173+ if candidates :
174+ candidates .sort (key = lambda x : "%04d-%d" % (x .width , x .time ))
175+ full = candidates [- 1 ]
176+ thumbs = [ c for c in candidates if c .width == c .height and c .width <= 200 ]
177+ if not thumbs :
178+ thumbs = [ c for c in candidates if c .width == c .height ]
179+ if not thumbs :
180+ thumbs = [ c for c in candidates if c .width <= 200 ]
181+ if not thumbs :
182+ thumbs = candidates [:1 ]
183+ thumb = thumbs [- 1 ]
184+ candidates = [ thumb , full ]
180185
181186 # At this point we either have no candidates or two. If two, the first will be the thumb
182187
@@ -186,33 +191,31 @@ for person in sorted(list(interesting_persons),key=lambda x:x.last_name()+x.asci
186191 shutil .copy (old , new )
187192 shutil .copystat (old , new )
188193
194+ assert (len (candidates ) in [0 ,2 ])
189195 if len (candidates )== 2 :
190- old_name = candidates [1 ]
191- old_thumb_name = candidates [0 ]
192- old_name_ext = os .path .splitext (old_name )[1 ]
193- old_thumb_name_ext = os .path .splitext (old_thumb_name )[1 ]
196+ thumb , full = candidates
194197
195- new_name = person .photo_name (thumb = False )+ old_name_ext .lower ()
196- new_thumb_name = person .photo_name (thumb = True )+ old_thumb_name_ext .lower ()
198+ new_name = person .photo_name (thumb = False )+ full . ext .lower ()
199+ new_thumb_name = person .photo_name (thumb = True )+ thumb . ext .lower ()
197200
198- copy ( os .path . join ( old_images_dir , old_name ) , os .path .join (new_images_dir ,new_name ) )
201+ copy ( full .path , os .path .join (new_images_dir ,new_name ) )
199202
200203 #
201- copy ( os .path . join ( old_images_dir , old_thumb_name ) , os .path .join (new_images_dir ,new_thumb_name ) )
204+ copy ( thumb .path , os .path .join (new_images_dir ,new_thumb_name ) )
202205
203206print ("" )
204207not_processed = 0
205- for file in pathlib . Path ( old_images_dir ). iterdir () :
208+ for file in old_image_files :
206209 if ( file .is_file ()
207210 and not file .suffix .lower () in ['.txt' , '.lck' , '.html' ,]
208211 and not file .name .startswith ('index.' )
209212 and not file .name .startswith ('milestoneupdate' )
210213 and not file .name .startswith ('nopicture' )
211214 and not file .name .startswith ('robots.txt' )
212215 ):
213- if not file . name .decode ('utf8' ) in processed_files :
216+ if not str ( file ) .decode ('utf8' ) in processed_files :
214217 not_processed += 1
215- print (u"Not processed: " + file . name .decode ('utf8' ))
218+ print (u"Not processed: " + str ( file ) .decode ('utf8' ))
216219print ("" )
217220print ("" )
218221print ("Not processed: %s files" % not_processed )
0 commit comments