Skip to content

Commit

Permalink
Added logic to the packaged file saving to check for existing directo…
Browse files Browse the repository at this point in the history
…ries (mostly the 'data' directory), so that the cleanup doesn't remove anything that was already on disk
  • Loading branch information
Bryant Howell committed Jan 13, 2020
1 parent 8d254cc commit 9dc235c
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions tableau_documents/tableau_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -417,14 +417,23 @@ def save_new_file(self, new_filename_no_extension: str):
os.remove(self.packaged_filename)

temp_directories_to_remove = {}

# Store any directories that already existed, so you don't clean up exiting directory on disk
existing_directories = []
all_in_working_dir = os.listdir()
for l in all_in_working_dir:
if (os.path.isdir(l)):
existing_directories.append(l)
# Now actually go through all the other files, extract them, put into the new ZIP file, then clean up
if len(self.other_files) > 0:
file_obj = open(self.orig_filename, 'rb')
o_zf = zipfile.ZipFile(file_obj)

for filename in self.other_files:
self.log('Looking into additional files: {}'.format(filename))

lowest_level = filename.split('/')
# Only delete out if it didn't exist prior to saving out of the original ZIPfile
if lowest_level[0] not in existing_directories:
temp_directories_to_remove[lowest_level[0]] = True
if self.file_replacement_map and filename in self.file_replacement_map:
new_zf.write(self.file_replacement_map[filename], "/" + filename)
# Delete from the data_file_replacement_map to reduce down to end
Expand All @@ -434,8 +443,7 @@ def save_new_file(self, new_filename_no_extension: str):
new_zf.write(filename)
os.remove(filename)
self.log('Removed file {}'.format(filename))
lowest_level = filename.split('/')
temp_directories_to_remove[lowest_level[0]] = True

file_obj.close()

# Loop through remaining files in data_file_replacement_map to just add
Expand Down

0 comments on commit 9dc235c

Please sign in to comment.