Skip to content

Commit

Permalink
Remove support for gzipped files in load_json_file
Browse files Browse the repository at this point in the history
Compression related functions were removed from securesystemslib as it was
deemed more appropriate for callers to handle compression/decompression at
a different level. However, the automatic decompression functionality in
load_json_file() had remained.

Remove that change and tidy up load_json_file() to only close the
fileobject once. Since Python 2.5 a finally block is always called,
regardless of whether there is an exception or not. Therefore there's no
need to close the fileobject in the else block and the finally block.

Signed-off-by: Joshua Lock <jlock@vmware.com>
  • Loading branch information
joshuagl committed Apr 8, 2020
1 parent 7a9e620 commit 91f4deb
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 41 deletions.
12 changes: 1 addition & 11 deletions securesystemslib/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
from __future__ import unicode_literals

import os
import gzip
import shutil
import logging
import tempfile
Expand Down Expand Up @@ -327,15 +326,7 @@ def load_json_file(filepath):
securesystemslib.formats.PATH_SCHEMA.check_match(filepath)

deserialized_object = None

# The file is mostly likely gzipped.
if filepath.endswith('.gz'):
logger.debug('gzip.open(' + str(filepath) + ')')
fileobject = six.StringIO(gzip.open(filepath).read().decode('utf-8'))

else:
logger.debug('open(' + str(filepath) + ')')
fileobject = open(filepath)
fileobject = open(filepath)

try:
deserialized_object = json.load(fileobject)
Expand All @@ -345,7 +336,6 @@ def load_json_file(filepath):
' Python object: ' + repr(filepath))

else:
fileobject.close()
return deserialized_object

finally:
Expand Down
30 changes: 0 additions & 30 deletions tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@

import os
import sys
import gzip
import shutil
import logging
import tempfile
Expand Down Expand Up @@ -57,30 +56,6 @@ def tearDown(self):



def _compress_existing_file(self, filepath):
"""
[Helper]Compresses file 'filepath' and returns file path of
the compresses file.
"""

# NOTE: DO NOT forget to remove the newly created compressed file!
if os.path.exists(filepath):
compressed_filepath = filepath+'.gz'
f_in = open(filepath, 'rb')
f_out = gzip.open(compressed_filepath, 'wb')
f_out.writelines(f_in)
f_out.close()
f_in.close()

return compressed_filepath

else:
logger.error('Compression of ' + repr(filepath) +' failed.'
' Path does not exist.')
sys.exit(1)



def test_B1_get_file_details(self):
# Goal: Verify proper output given certain expected/unexpected input.

Expand Down Expand Up @@ -190,11 +165,6 @@ def test_B6_load_json_file(self):
fileobj.close()
self.assertEqual(data, securesystemslib.util.load_json_file(filepath))

# Test a gzipped file.
compressed_filepath = self._compress_existing_file(filepath)
self.assertEqual(data,
securesystemslib.util.load_json_file(compressed_filepath))

# Improperly formatted arguments.
for bogus_arg in [1, [b'a'], {'a':b'b'}]:
self.assertRaises(securesystemslib.exceptions.FormatError,
Expand Down

0 comments on commit 91f4deb

Please sign in to comment.