Merge pull request #787 from scop/with
Handle more stream closing with "with"
This commit is contained in:
@@ -70,21 +70,17 @@ class GzipCompressorFileStorage(CompressorFileStorage):
|
|||||||
orig_path = self.path(filename)
|
orig_path = self.path(filename)
|
||||||
compressed_path = '%s.gz' % orig_path
|
compressed_path = '%s.gz' % orig_path
|
||||||
|
|
||||||
f_in = open(orig_path, 'rb')
|
with open(orig_path, 'rb') as f_in, open(compressed_path, 'wb') as f_out:
|
||||||
f_out = open(compressed_path, 'wb')
|
with gzip.GzipFile(fileobj=f_out) as gz_out:
|
||||||
try:
|
gz_out.write(f_in.read())
|
||||||
f_out = gzip.GzipFile(fileobj=f_out)
|
|
||||||
f_out.write(f_in.read())
|
# Ensure the file timestamps match.
|
||||||
finally:
|
# os.stat() returns nanosecond resolution on Linux, but os.utime()
|
||||||
f_out.close()
|
# only sets microsecond resolution. Set times on both files to
|
||||||
f_in.close()
|
# ensure they are equal.
|
||||||
# Ensure the file timestamps match.
|
stamp = time.time()
|
||||||
# os.stat() returns nanosecond resolution on Linux, but os.utime()
|
os.utime(orig_path, (stamp, stamp))
|
||||||
# only sets microsecond resolution. Set times on both files to
|
os.utime(compressed_path, (stamp, stamp))
|
||||||
# ensure they are equal.
|
|
||||||
stamp = time.time()
|
|
||||||
os.utime(orig_path, (stamp, stamp))
|
|
||||||
os.utime(compressed_path, (stamp, stamp))
|
|
||||||
|
|
||||||
return filename
|
return filename
|
||||||
|
|
||||||
|
@@ -16,9 +16,8 @@ def main():
|
|||||||
options, arguments = p.parse_args()
|
options, arguments = p.parse_args()
|
||||||
|
|
||||||
if options.filename:
|
if options.filename:
|
||||||
f = open(options.filename)
|
with open(options.filename) as f:
|
||||||
content = f.read()
|
content = f.read()
|
||||||
f.close()
|
|
||||||
else:
|
else:
|
||||||
content = sys.stdin.read()
|
content = sys.stdin.read()
|
||||||
|
|
||||||
|
@@ -405,7 +405,8 @@ class CompressorInDebugModeTestCase(SimpleTestCase):
|
|||||||
# files can be outdated
|
# files can be outdated
|
||||||
css_filename = os.path.join(settings.COMPRESS_ROOT, "css", "one.css")
|
css_filename = os.path.join(settings.COMPRESS_ROOT, "css", "one.css")
|
||||||
# Store the hash of the original file's content
|
# Store the hash of the original file's content
|
||||||
css_content = open(css_filename).read()
|
with open(css_filename) as f:
|
||||||
|
css_content = f.read()
|
||||||
hashed = get_hexdigest(css_content, 12)
|
hashed = get_hexdigest(css_content, 12)
|
||||||
# Now modify the file in the STATIC_ROOT
|
# Now modify the file in the STATIC_ROOT
|
||||||
test_css_content = "p { font-family: 'test' }"
|
test_css_content = "p { font-family: 'test' }"
|
||||||
@@ -419,6 +420,7 @@ class CompressorInDebugModeTestCase(SimpleTestCase):
|
|||||||
compressor.storage = DefaultStorage()
|
compressor.storage = DefaultStorage()
|
||||||
output = compressor.output()
|
output = compressor.output()
|
||||||
self.assertEqual(expected, output)
|
self.assertEqual(expected, output)
|
||||||
result = open(os.path.join(settings.COMPRESS_ROOT, "CACHE", "css",
|
with open(os.path.join(settings.COMPRESS_ROOT, "CACHE", "css",
|
||||||
"%s.css" % hashed), "r").read()
|
"%s.css" % hashed), "r") as f:
|
||||||
|
result = f.read()
|
||||||
self.assertTrue(test_css_content not in result)
|
self.assertTrue(test_css_content not in result)
|
||||||
|
Reference in New Issue
Block a user