def _uploader(resize_size, final_size): img = get_image_path('mozilla.png') original_size = (339, 128) src = tempfile.NamedTemporaryFile(mode='r+w+b', suffix=".png", delete=False) # resize_icon removes the original shutil.copyfile(img, src.name) src_image = Image.open(src.name) assert src_image.size == original_size if isinstance(final_size, list): uploadto = user_media_path('addon_icons') try: os.makedirs(uploadto) except OSError: pass for rsize, fsize in zip(resize_size, final_size): dest_name = os.path.join(uploadto, '1234') tasks.resize_icon(src.name, dest_name, resize_size, locally=True) dest_image = Image.open(open('%s-%s.png' % (dest_name, rsize))) assert dest_image.size == fsize if os.path.exists(dest_image.filename): os.remove(dest_image.filename) assert not os.path.exists(dest_image.filename) shutil.rmtree(uploadto) else: dest = tempfile.mktemp(suffix='.png') tasks.resize_icon(src.name, dest, resize_size, locally=True) dest_image = Image.open(dest) assert dest_image.size == final_size assert not os.path.exists(src.name)
def _uploader(resize_size, final_size): img = get_image_path('mozilla.png') original_size = (339, 128) src = tempfile.NamedTemporaryFile(mode='r+w+b', suffix=".png", delete=False) # resize_icon removes the original shutil.copyfile(img, src.name) src_image = Image.open(src.name) assert src_image.size == original_size if isinstance(final_size, list): uploadto = user_media_path('addon_icons') try: os.makedirs(uploadto) except OSError: pass for rsize, fsize in zip(resize_size, final_size): dest_name = os.path.join(uploadto, '1234') tasks.resize_icon(src.name, dest_name, resize_size, locally=True) dest_image = Image.open(open('%s-%s.png' % (dest_name, rsize))) assert dest_image.size == fsize if os.path.exists(dest_image.filename): os.remove(dest_image.filename) assert not os.path.exists(dest_image.filename) shutil.rmtree(uploadto) else: dest = tempfile.mktemp(suffix='.png') tasks.resize_icon(src.name, dest, resize_size, locally=True) dest_image = Image.open(dest) assert dest_image.size == final_size assert not os.path.exists(src.name)
def _uploader(resize_size, final_size): img = get_image_path('mozilla.png') original_size = (339, 128) src = tempfile.NamedTemporaryFile(mode='r+b', suffix='.png', delete=False, dir=settings.TMP_PATH) if not isinstance(final_size, list): final_size = [final_size] resize_size = [resize_size] uploadto = user_media_path('addon_icons') try: os.makedirs(uploadto) except OSError: pass for rsize, expected_size in zip(resize_size, final_size): # resize_icon moves the original shutil.copyfile(img, src.name) src_image = Image.open(src.name) assert src_image.size == original_size dest_name = os.path.join(uploadto, '1234') with mock.patch('olympia.amo.utils.pngcrush_image') as pngcrush_mock: return_value = tasks.resize_icon(src.name, dest_name, [rsize]) dest_image = '%s-%s.png' % (dest_name, rsize) assert pngcrush_mock.call_count == 1 assert pngcrush_mock.call_args_list[0][0][0] == dest_image assert image_size(dest_image) == expected_size # original should have been moved to -original orig_image = '%s-original.png' % dest_name assert os.path.exists(orig_image) # Return value of the task should be a dict with an icon_hash key # containing the 8 first chars of the md5 hash of the source file, # which is bb362450b00f0461c6bddc6b97b3c30b. assert return_value == {'icon_hash': 'bb362450'} os.remove(dest_image) assert not os.path.exists(dest_image) os.remove(orig_image) assert not os.path.exists(orig_image) shutil.rmtree(uploadto) assert not os.path.exists(src.name)
def _uploader(resize_size, final_size): img = get_image_path('mozilla.png') original_size = (339, 128) src = tempfile.NamedTemporaryFile( mode='r+w+b', suffix='.png', delete=False, dir=settings.TMP_PATH) if not isinstance(final_size, list): final_size = [final_size] resize_size = [resize_size] uploadto = user_media_path('addon_icons') try: os.makedirs(uploadto) except OSError: pass for rsize, expected_size in zip(resize_size, final_size): # resize_icon moves the original shutil.copyfile(img, src.name) src_image = Image.open(src.name) assert src_image.size == original_size dest_name = os.path.join(uploadto, '1234') with mock.patch('olympia.amo.utils.pngcrush_image') as pngcrush_mock: return_value = tasks.resize_icon(src.name, dest_name, [rsize]) dest_image = '%s-%s.png' % (dest_name, rsize) assert pngcrush_mock.call_count == 1 assert pngcrush_mock.call_args_list[0][0][0] == dest_image assert image_size(dest_image) == expected_size # original should have been moved to -original orig_image = '%s-original.png' % dest_name assert os.path.exists(orig_image) # Return value of the task should be a dict with an icon_hash key # containing the 8 first chars of the md5 hash of the source file, # which is bb362450b00f0461c6bddc6b97b3c30b. assert return_value == {'icon_hash': 'bb362450'} os.remove(dest_image) assert not os.path.exists(dest_image) os.remove(orig_image) assert not os.path.exists(orig_image) shutil.rmtree(uploadto) assert not os.path.exists(src.name)