summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZuul <zuul@review.openstack.org>2018-11-30 04:49:14 +0000
committerGerrit Code Review <review@openstack.org>2018-11-30 04:49:14 +0000
commit947e49a270707274dbf328ddf694cb95794029c7 (patch)
tree317503fc032f4ec35f2ed98b40da2cc768958420
parent05fa2786afe86e74b45b04e204a6c3df668a2829 (diff)
parent928d0bb051b02538f2c97f3357f4c9b3b1b454f7 (diff)
Merge "upload-logs-swift: Cleanup temporary directories"
-rwxr-xr-xroles/upload-logs-swift/library/zuul_swift_upload.py22
1 files changed, 18 insertions, 4 deletions
diff --git a/roles/upload-logs-swift/library/zuul_swift_upload.py b/roles/upload-logs-swift/library/zuul_swift_upload.py
index bac09cc..04d9070 100755
--- a/roles/upload-logs-swift/library/zuul_swift_upload.py
+++ b/roles/upload-logs-swift/library/zuul_swift_upload.py
@@ -32,6 +32,7 @@ try:
32 import queue as queuelib 32 import queue as queuelib
33except ImportError: 33except ImportError:
34 import Queue as queuelib 34 import Queue as queuelib
35import shutil
35import stat 36import stat
36import sys 37import sys
37import tempfile 38import tempfile
@@ -193,12 +194,14 @@ class FileList(Sequence):
193 def __init__(self): 194 def __init__(self):
194 self.file_list = [] 195 self.file_list = []
195 self.file_list.append(FileDetail(None, '', '')) 196 self.file_list.append(FileDetail(None, '', ''))
197 self.tempdirs = []
196 198
197 def __enter__(self): 199 def __enter__(self):
198 return self 200 return self
199 201
200 def __exit__(self, type, value, traceback): 202 def __exit__(self, type, value, traceback):
201 pass 203 for tempdir in self.tempdirs:
204 shutil.rmtree(tempdir)
202 205
203 def __getitem__(self, item): 206 def __getitem__(self, item):
204 return self.file_list.__getitem__(item) 207 return self.file_list.__getitem__(item)
@@ -206,6 +209,16 @@ class FileList(Sequence):
206 def __len__(self): 209 def __len__(self):
207 return self.file_list.__len__() 210 return self.file_list.__len__()
208 211
212 def get_tempdir(self):
213 '''Get a temporary directory
214
215 Returns path to a private temporary directory which will be
216 cleaned on exit
217 '''
218 tempdir = tempfile.mkdtemp(prefix='s-u-l-tmp')
219 self.tempdirs.append(tempdir)
220 return tempdir
221
209 @staticmethod 222 @staticmethod
210 def _path_in_tree(root, path): 223 def _path_in_tree(root, path):
211 full_path = os.path.realpath(os.path.abspath( 224 full_path = os.path.realpath(os.path.abspath(
@@ -274,6 +287,7 @@ class Indexer():
274 def __init__(self, file_list, create_parent_links=True, 287 def __init__(self, file_list, create_parent_links=True,
275 create_topdir_parent_link=False, 288 create_topdir_parent_link=False,
276 append_footer='index_footer.html'): 289 append_footer='index_footer.html'):
290
277 ''' 291 '''
278 Args: 292 Args:
279 file_list (FileList): A FileList object to be updated 293 file_list (FileList): A FileList object to be updated
@@ -319,7 +333,8 @@ class Indexer():
319 files = [parent_file_detail] + files 333 files = [parent_file_detail] + files
320 334
321 # Do generate a link to the parent directory 335 # Do generate a link to the parent directory
322 full_path = self.make_index_file(files, 'Index of %s' % (folder,)) 336 full_path = self.make_index_file(files, 'Index of %s' % (folder,),
337 self.file_list.get_tempdir())
323 338
324 if full_path: 339 if full_path:
325 filename = os.path.basename(full_path) 340 filename = os.path.basename(full_path)
@@ -347,7 +362,7 @@ class Indexer():
347 new_list.reverse() 362 new_list.reverse()
348 self.file_list.file_list = new_list 363 self.file_list.file_list = new_list
349 364
350 def make_index_file(self, folder_links, title): 365 def make_index_file(self, folder_links, title, tempdir):
351 """Writes an index into a file for pushing""" 366 """Writes an index into a file for pushing"""
352 for file_details in folder_links: 367 for file_details in folder_links:
353 # Do not generate an index file if one exists already. 368 # Do not generate an index file if one exists already.
@@ -356,7 +371,6 @@ class Indexer():
356 if self.index_filename == file_details.filename: 371 if self.index_filename == file_details.filename:
357 return 372 return
358 index_content = self.generate_log_index(folder_links, title) 373 index_content = self.generate_log_index(folder_links, title)
359 tempdir = tempfile.mkdtemp()
360 fd = open(os.path.join(tempdir, self.index_filename), 'w') 374 fd = open(os.path.join(tempdir, self.index_filename), 'w')
361 fd.write(index_content) 375 fd.write(index_content)
362 return os.path.join(tempdir, self.index_filename) 376 return os.path.join(tempdir, self.index_filename)