Browse Source

upload-logs-swift: Cleanup temporary directories

Add a function to the FileList context manager to get a temporary
directory; keep track and remove these on exit.  Use this in the index
creation.

Change-Id: I9d9220ad70ce191af02ae0331c98eafe487d96d4
Ian Wienand 8 months ago
parent
commit
928d0bb051
1 changed files with 18 additions and 4 deletions
  1. 18
    4
      roles/upload-logs-swift/library/zuul_swift_upload.py

+ 18
- 4
roles/upload-logs-swift/library/zuul_swift_upload.py View File

@@ -32,6 +32,7 @@ try:
32 32
     import queue as queuelib
33 33
 except ImportError:
34 34
     import Queue as queuelib
35
+import shutil
35 36
 import stat
36 37
 import sys
37 38
 import tempfile
@@ -193,12 +194,14 @@ class FileList(Sequence):
193 194
     def __init__(self):
194 195
         self.file_list = []
195 196
         self.file_list.append(FileDetail(None, '', ''))
197
+        self.tempdirs = []
196 198
 
197 199
     def __enter__(self):
198 200
         return self
199 201
 
200 202
     def __exit__(self, type, value, traceback):
201
-        pass
203
+        for tempdir in self.tempdirs:
204
+            shutil.rmtree(tempdir)
202 205
 
203 206
     def __getitem__(self, item):
204 207
         return self.file_list.__getitem__(item)
@@ -206,6 +209,16 @@ class FileList(Sequence):
206 209
     def __len__(self):
207 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 222
     @staticmethod
210 223
     def _path_in_tree(root, path):
211 224
         full_path = os.path.realpath(os.path.abspath(
@@ -274,6 +287,7 @@ class Indexer():
274 287
     def __init__(self, file_list, create_parent_links=True,
275 288
                  create_topdir_parent_link=False,
276 289
                  append_footer='index_footer.html'):
290
+
277 291
         '''
278 292
         Args:
279 293
             file_list (FileList): A FileList object to be updated
@@ -319,7 +333,8 @@ class Indexer():
319 333
                 files = [parent_file_detail] + files
320 334
 
321 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 339
             if full_path:
325 340
                 filename = os.path.basename(full_path)
@@ -347,7 +362,7 @@ class Indexer():
347 362
         new_list.reverse()
348 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 366
         """Writes an index into a file for pushing"""
352 367
         for file_details in folder_links:
353 368
             # Do not generate an index file if one exists already.
@@ -356,7 +371,6 @@ class Indexer():
356 371
             if self.index_filename == file_details.filename:
357 372
                 return
358 373
         index_content = self.generate_log_index(folder_links, title)
359
-        tempdir = tempfile.mkdtemp()
360 374
         fd = open(os.path.join(tempdir, self.index_filename), 'w')
361 375
         fd.write(index_content)
362 376
         return os.path.join(tempdir, self.index_filename)

Loading…
Cancel
Save