Commit 3c56a1a3 authored by Fabien Morin's avatar Fabien Morin

improve temporary file deletion (some files where forgot to be deleted).

Now, at the end of the test, tmp folder is the same size than before the 
test (it could not be tested in a multitest environement).


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@20244 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent f8caba4f
...@@ -632,14 +632,12 @@ class ManageFiles: ...@@ -632,14 +632,12 @@ class ManageFiles:
raise ValueError, 'Error: convert command failed with the following'\ raise ValueError, 'Error: convert command failed with the following'\
'error message : \n%s' % result[1] 'error message : \n%s' % result[1]
finally: finally:
if os.path.exists(temp_pdf.name):
temp_pdf.close() temp_pdf.close()
background_image_list = makeImageList() background_image_list = makeImageList()
if not len(background_image_list): if not len(background_image_list):
LOG('ScribusUtils.setBackgroundPictures :', ERROR, 'no background '\ LOG('ScribusUtils.setBackgroundPictures :', ERROR, 'no background '\
'image found') 'image found')
if os.path.exists(temp_image.name):
temp_image.close() temp_image.close()
raise ValueError, 'Error: ScribusUtils.setBackgroundPictures : '\ raise ValueError, 'Error: ScribusUtils.setBackgroundPictures : '\
'no background' 'no background'
...@@ -662,23 +660,19 @@ class ManageFiles: ...@@ -662,23 +660,19 @@ class ManageFiles:
addImageMethod(form_page_id, temp_background_file, "background image") addImageMethod(form_page_id, temp_background_file, "background image")
image_number += 1 image_number += 1
# delete all images created in this method before raise an error
background_image_list = makeImageList() background_image_list = makeImageList()
if len(background_image_list) > 1:
# if there is more than one page : delete all the pages created by
# convert using remove method, else, the unique page will be delete at
# the close() call
for background_image in background_image_list[1:]: for background_image in background_image_list[1:]:
# remove the file from the system # remove the file from the system
if os.path.exists(background_image): if os.path.exists(background_image):
os.remove(background_image) os.remove(background_image)
temp_image.close()
size_x = int(real_size_x) size_x = int(real_size_x)
size_y = int(real_size_y) size_y = int(real_size_y)
LOG('ScribusUtils.setBackgroundPictures :', INFO, LOG('ScribusUtils.setBackgroundPictures :', INFO,
'return size : x=%s, y=%s' % (size_x, size_y)) 'return size : x=%s, y=%s' % (size_x, size_y))
if os.path.exists(temp_image.name):
temp_image.close()
return (size_x, size_y) return (size_x, size_y)
security.declarePublic('getPageattributes') security.declarePublic('getPageattributes')
...@@ -690,7 +684,7 @@ class ManageFiles: ...@@ -690,7 +684,7 @@ class ManageFiles:
from tempfile import NamedTemporaryFile from tempfile import NamedTemporaryFile
# opening new file on HDD to save PDF content # opening new file on HDD to save PDF content
ScribusUtilsOriginalTempPDF= NamedTemporaryFile(mode= "w+b") ScribusUtilsOriginalTempPDF= NamedTemporaryFile(mode= "w+b")
ScribusUtilsOriginaltempsPDFName= NamedTemporaryFile().name ScribusUtilsOriginaltempsPDFName = ScribusUtilsOriginalTempPDF.name
# going to the begining of the input file # going to the begining of the input file
...@@ -706,7 +700,7 @@ class ManageFiles: ...@@ -706,7 +700,7 @@ class ManageFiles:
height_groups = [] height_groups = []
# launching first soft to convert from PDF to PPM # launching first soft to convert from PDF to PPM
ScribusUtilsOriginaltempsPPM = NamedTemporaryFile(mode="w+b") ScribusUtilsOriginaltempsPPM = NamedTemporaryFile(mode="w+b")
ScribusUtilsOriginaltempsPPMName = NamedTemporaryFile().name ScribusUtilsOriginaltempsPPMName = ScribusUtilsOriginaltempsPPM.name
original_result = commands.getstatusoutput('pdftoppm -r %s %s %s' % (72, original_result = commands.getstatusoutput('pdftoppm -r %s %s %s' % (72,
ScribusUtilsOriginaltempsPDFName, ScribusUtilsOriginaltempsPPMName)) ScribusUtilsOriginaltempsPDFName, ScribusUtilsOriginaltempsPPMName))
original_result= commands.getstatusoutput('identify %s' % \ original_result= commands.getstatusoutput('identify %s' % \
...@@ -717,26 +711,28 @@ class ManageFiles: ...@@ -717,26 +711,28 @@ class ManageFiles:
# pdftoppm add a '-N' string a the end of the file name if there is more # pdftoppm add a '-N' string a the end of the file name if there is more
# than one page in the pdf file (where N is the number of the page, # than one page in the pdf file (where N is the number of the page,
# begining at 1) # begining at 1)
if os.path.exists(ScribusUtilsOriginaltempsPDFName): if os.path.exists(ScribusUtilsOriginaltempsPPMName):
# thats mean there's only one page in the pdf file # thats mean there's only one page in the pdf file
ppm_list.append(ScribusUtilsOriginaltempsPDFName) ppm_list.append(ScribusUtilsOriginaltempsPPMName)
else: # try to find the other pages if there is more than one (in case of
# in the case of multi-pages pdf file, we must find all files # multi-pages pdf file)
image_number = 1 image_number = 1
while os.path.exists(ScribusUtilsOriginaltempsPDFName + '-%s' %\ while os.path.exists(ScribusUtilsOriginaltempsPPMName + '-%s.ppm' %\
image_number): image_number):
ppm_list.append(ScribusUtilsOriginaltempsPDFName + '-%s' % \ ppm_list.append(ScribusUtilsOriginaltempsPPMName + '-%s.ppm' % \
image_number) image_number)
image_number += 1 image_number += 1
return ppm_list return ppm_list
# this line permit to delete tempory files (about 2.4 Mo for each file !) # this lines permit to delete tempory files (about 2.4 Mo for each file !)
# it's temporary because this function must be rewrited or deleted # it's temporary because this function must be rewrited or deleted
# (perhaps setBackgroundPictures could return attributes list) # (perhaps setBackgroundPictures could return attributes list)
ppm_list = makePPMFileList() ppm_list = makePPMFileList()
for ppm in ppm_list: for ppm in ppm_list[1:]:
if os.path.exists(ppm): if os.path.exists(ppm):
os.remove(ppm) os.remove(ppm)
ScribusUtilsOriginaltempsPPM.close()
ScribusUtilsOriginalTempPDF.close()
pg_nbr = len(original_result[1].split('\n')) pg_nbr = len(original_result[1].split('\n'))
real_size_x = {} real_size_x = {}
...@@ -751,6 +747,7 @@ class ManageFiles: ...@@ -751,6 +747,7 @@ class ManageFiles:
actual_page_width = real_size_y[page_iterator] actual_page_width = real_size_y[page_iterator]
width_groups.append(actual_page_width) width_groups.append(actual_page_width)
height_groups.append(actual_page_height) height_groups.append(actual_page_height)
return (width_groups, height_groups) return (width_groups, height_groups)
security.declarePublic('setPropertySheetAndDocument') security.declarePublic('setPropertySheetAndDocument')
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment