From bea47d5db293f6845bd9aec10eae3f73d8b69c6e Mon Sep 17 00:00:00 2001 From: Ian Wienand Date: Fri, 17 Aug 2018 12:29:47 +1000 Subject: [PATCH] upload-logs-swift: Keep the FileList in the indexer class Moving this reference to the file_list into the class encapsulates things better when the Indexer class becomes a more a collection of tools to modify a FileList before upload. Change-Id: I2bedee35ce178df40c15d5867edf560a62232c57 --- .../library/test_zuul_swift_upload.py | 18 ++++++----- .../library/zuul_swift_upload.py | 32 +++++++++++-------- 2 files changed, 29 insertions(+), 21 deletions(-) diff --git a/roles/upload-logs-swift/library/test_zuul_swift_upload.py b/roles/upload-logs-swift/library/test_zuul_swift_upload.py index 24daf6ee8..be69c78dc 100644 --- a/roles/upload-logs-swift/library/test_zuul_swift_upload.py +++ b/roles/upload-logs-swift/library/test_zuul_swift_upload.py @@ -156,8 +156,8 @@ class TestFileList(testtools.TestCase): '''Test index generation''' with FileList() as fl: fl.add(os.path.join(FIXTURE_DIR, 'logs')) - ix = Indexer() - ix.make_indexes(fl) + ix = Indexer(fl) + ix.make_indexes() self.assert_files(fl, [ ('', 'application/directory', None), @@ -207,8 +207,8 @@ class TestFileList(testtools.TestCase): '''Test index generation with a trailing slash''' with FileList() as fl: fl.add(os.path.join(FIXTURE_DIR, 'logs/')) - ix = Indexer() - ix.make_indexes(fl) + ix = Indexer(fl) + ix.make_indexes() self.assert_files(fl, [ ('', 'application/directory', None), @@ -257,9 +257,10 @@ class TestFileList(testtools.TestCase): '''Test index generation creates topdir parent link''' with FileList() as fl: fl.add(os.path.join(FIXTURE_DIR, 'logs/')) - ix = Indexer(create_parent_links=True, + ix = Indexer(fl, + create_parent_links=True, create_topdir_parent_link=True) - ix.make_indexes(fl) + ix.make_indexes() self.assert_files(fl, [ ('', 'application/directory', None), @@ -311,9 +312,10 @@ class TestFileList(testtools.TestCase): '''Test index generation creates topdir parent link''' with FileList() as fl: fl.add(os.path.join(FIXTURE_DIR, 'logs/')) - ix = Indexer(create_parent_links=False, + ix = Indexer(fl, + create_parent_links=False, create_topdir_parent_link=False) - ix.make_indexes(fl) + ix.make_indexes() self.assert_files(fl, [ ('', 'application/directory', None), diff --git a/roles/upload-logs-swift/library/zuul_swift_upload.py b/roles/upload-logs-swift/library/zuul_swift_upload.py index 7e64c1760..65ee4aa6b 100755 --- a/roles/upload-logs-swift/library/zuul_swift_upload.py +++ b/roles/upload-logs-swift/library/zuul_swift_upload.py @@ -268,27 +268,32 @@ class FileList(Sequence): class Indexer(): """generates index.html files if requested.""" - def __init__(self, create_parent_links=True, + def __init__(self, file_list, create_parent_links=True, create_topdir_parent_link=False, append_footer='index_footer.html'): + ''' + Args: + file_list (FileList): A FileList object to be updated + with index files for each directory. + create_parent_links (bool): + create_topdir_parent_link (bool): + append_footer (str): + ''' + assert isinstance(file_list, FileList) + self.file_list = file_list self.create_parent_links = create_parent_links self.create_topdir_parent_link = create_topdir_parent_link self.append_footer = append_footer self.index_filename = 'index.html' - def make_indexes(self, file_list): + def make_indexes(self): '''Make index files - Args: - file_list (FileList): A FileList object to be updated - with index files for each directory. Return: - No value, the file_list will be updated + No value, the self.file_list will be updated ''' - assert isinstance(file_list, FileList) - folders = collections.OrderedDict() - for f in file_list: + for f in self.file_list: if f.folder: folders[f.relative_path] = [] folder = os.path.dirname(os.path.dirname( @@ -322,7 +327,7 @@ class Indexer(): # for each directory. new_list = [] last_dirname = None - for f in reversed(list(file_list)): + for f in reversed(list(self.file_list)): if f.folder: relative_path = f.relative_path + '/' else: @@ -337,7 +342,7 @@ class Indexer(): last_dirname = dirname new_list.append(f) new_list.reverse() - file_list.file_list = new_list + self.file_list.file_list = new_list def make_index_file(self, folder_links, title): """Writes an index into a file for pushing""" @@ -566,7 +571,8 @@ def run(cloud, container, files, # Create the objects to make sure the arguments are sound. with FileList() as file_list: - indexer = Indexer(create_parent_links=parent_links, + indexer = Indexer(file_list, + create_parent_links=parent_links, create_topdir_parent_link=topdir_parent_link, append_footer=footer) @@ -576,7 +582,7 @@ def run(cloud, container, files, # (Possibly) make indexes. if indexes: - indexer.make_indexes(file_list) + indexer.make_indexes() logging.debug("List of files prepared to upload:") for x in file_list: