Remove redundancy: convert with closing(open(...)) to with open(...)

This commit is contained in:
Todd Gamblin 2015-05-18 17:11:03 -07:00
parent f813d823a1
commit c622337802
15 changed files with 14 additions and 30 deletions

View File

@ -26,7 +26,6 @@
import os import os
import hashlib import hashlib
import re import re
from contextlib import closing
from external.ordereddict import OrderedDict from external.ordereddict import OrderedDict
import llnl.util.tty as tty import llnl.util.tty as tty
@ -192,7 +191,7 @@ def create(parser, args):
tty.die("Could not fetch any tarballs for %s." % name) tty.die("Could not fetch any tarballs for %s." % name)
# Write out a template for the file # Write out a template for the file
with closing(open(pkg_path, "w")) as pkg_file: with open(pkg_path, "w") as pkg_file:
pkg_file.write( pkg_file.write(
package_template.substitute( package_template.substitute(
name=name, name=name,

View File

@ -103,7 +103,7 @@ def mirror_list(args):
def _read_specs_from_file(filename): def _read_specs_from_file(filename):
with closing(open(filename, "r")) as stream: with open(filename, "r") as stream:
for i, string in enumerate(stream): for i, string in enumerate(stream):
try: try:
s = Spec(string) s = Spec(string)

View File

@ -27,7 +27,6 @@
import code import code
from external import argparse from external import argparse
import platform import platform
from contextlib import closing
import spack import spack
@ -44,13 +43,13 @@ def python(parser, args):
if "PYTHONSTARTUP" in os.environ: if "PYTHONSTARTUP" in os.environ:
startup_file = os.environ["PYTHONSTARTUP"] startup_file = os.environ["PYTHONSTARTUP"]
if os.path.isfile(startup_file): if os.path.isfile(startup_file):
with closing(open(startup_file)) as startup: with open(startup_file) as startup:
console.runsource(startup.read(), startup_file, 'exec') console.runsource(startup.read(), startup_file, 'exec')
python_args = args.python_args python_args = args.python_args
if python_args: if python_args:
sys.argv = python_args sys.argv = python_args
with closing(open(python_args[0])) as file: with open(python_args[0]) as file:
console.runsource(file.read(), python_args[0], 'exec') console.runsource(file.read(), python_args[0], 'exec')
else: else:
console.interact("Spack version %s\nPython %s, %s %s""" console.interact("Spack version %s\nPython %s, %s %s"""

View File

@ -94,7 +94,6 @@
from llnl.util.lang import memoized from llnl.util.lang import memoized
import spack.error import spack.error
from contextlib import closing
from external import yaml from external import yaml
from external.yaml.error import MarkedYAMLError from external.yaml.error import MarkedYAMLError
import llnl.util.tty as tty import llnl.util.tty as tty
@ -325,7 +324,7 @@ def remove_from_config(category_name, key_to_rm, scope=None):
continue continue
if not key_to_rm in result[category_name]: if not key_to_rm in result[category_name]:
continue continue
with closing(open(path, 'w')) as f: with open(path, 'w') as f:
result[category_name].pop(key_to_rm, None) result[category_name].pop(key_to_rm, None)
yaml.dump(result, stream=f, default_flow_style=False) yaml.dump(result, stream=f, default_flow_style=False)
category.result_dict.pop(key_to_rm, None) category.result_dict.pop(key_to_rm, None)
@ -339,4 +338,3 @@ def print_category(category_name):
tty.die("Unknown config category %s. Valid options are: %s" % tty.die("Unknown config category %s. Valid options are: %s" %
(category_name, ", ".join([s for s in _config_sections]))) (category_name, ", ".join([s for s in _config_sections])))
yaml.dump(get_config(category_name), stream=sys.stdout, default_flow_style=False) yaml.dump(get_config(category_name), stream=sys.stdout, default_flow_style=False)

View File

@ -50,7 +50,6 @@
import textwrap import textwrap
import shutil import shutil
from glob import glob from glob import glob
from contextlib import closing
import llnl.util.tty as tty import llnl.util.tty as tty
from llnl.util.filesystem import join_path, mkdirp from llnl.util.filesystem import join_path, mkdirp
@ -152,7 +151,7 @@ def write(self):
if not self.paths: if not self.paths:
return return
with closing(open(self.file_name, 'w')) as f: with open(self.file_name, 'w') as f:
self._write(f) self._write(f)

View File

@ -29,7 +29,6 @@
import tempfile import tempfile
import shutil import shutil
import os import os
from contextlib import closing
from llnl.util.filesystem import * from llnl.util.filesystem import *
@ -52,7 +51,7 @@ def setUp(self):
def tearDown(self): def tearDown(self):
#shutil.rmtree(self.tmpdir, ignore_errors=True) shutil.rmtree(self.tmpdir, ignore_errors=True)
self.layout = None self.layout = None
@ -95,7 +94,7 @@ def test_read_and_write_spec(self):
self.assertTrue(spec_from_file.concrete) self.assertTrue(spec_from_file.concrete)
# Ensure that specs that come out "normal" are really normal. # Ensure that specs that come out "normal" are really normal.
with closing(open(spec_path)) as spec_file: with open(spec_path) as spec_file:
read_separately = Spec.from_yaml(spec_file.read()) read_separately = Spec.from_yaml(spec_file.read())
read_separately.normalize() read_separately.normalize()

View File

@ -26,7 +26,6 @@
import unittest import unittest
import shutil import shutil
import tempfile import tempfile
from contextlib import closing
from llnl.util.filesystem import * from llnl.util.filesystem import *

View File

@ -24,9 +24,6 @@
############################################################################## ##############################################################################
import os import os
import unittest import unittest
import shutil
import tempfile
from contextlib import closing
from llnl.util.filesystem import * from llnl.util.filesystem import *

View File

@ -26,7 +26,6 @@
import unittest import unittest
import shutil import shutil
import tempfile import tempfile
from contextlib import closing
from llnl.util.filesystem import * from llnl.util.filesystem import *

View File

@ -26,7 +26,6 @@
import unittest import unittest
import shutil import shutil
import tempfile import tempfile
from contextlib import closing
from llnl.util.filesystem import * from llnl.util.filesystem import *
from llnl.util.link_tree import LinkTree from llnl.util.link_tree import LinkTree

View File

@ -24,7 +24,6 @@
############################################################################## ##############################################################################
import os import os
import shutil import shutil
from contextlib import closing
from llnl.util.filesystem import * from llnl.util.filesystem import *
@ -67,7 +66,7 @@ def __init__(self):
with working_dir(self.path): with working_dir(self.path):
configure = join_path(self.path, 'configure') configure = join_path(self.path, 'configure')
with closing(open(configure, 'w')) as cfg_file: with open(configure, 'w') as cfg_file:
cfg_file.write( cfg_file.write(
"#!/bin/sh\n" "#!/bin/sh\n"
"prefix=$(echo $1 | sed 's/--prefix=//')\n" "prefix=$(echo $1 | sed 's/--prefix=//')\n"

View File

@ -31,7 +31,6 @@
import unittest import unittest
import os import os
import re import re
from contextlib import closing
import llnl.util.tty as tty import llnl.util.tty as tty
@ -62,7 +61,7 @@ def test_python_versions(self):
all_issues = {} all_issues = {}
for fn in self.spack_python_files(): for fn in self.spack_python_files():
with closing(open(fn)) as pyfile: with open(fn) as pyfile:
versions = pyqver2.get_versions(pyfile.read()) versions = pyqver2.get_versions(pyfile.read())
for ver, reasons in versions.items(): for ver, reasons in versions.items():
if ver > spack_max_version: if ver > spack_max_version:

View File

@ -76,7 +76,7 @@ def setUp(self):
mkdirp(archive_dir_path) mkdirp(archive_dir_path)
mkdirp(test_tmp_path) mkdirp(test_tmp_path)
with closing(open(test_readme, 'w')) as readme: with open(test_readme, 'w') as readme:
readme.write(readme_text) readme.write(readme_text)
with working_dir(test_files_dir): with working_dir(test_files_dir):
@ -161,7 +161,7 @@ def check_expand_archive(self, stage, stage_name):
readme = join_path(stage_path, archive_dir, readme_name) readme = join_path(stage_path, archive_dir, readme_name)
self.assertTrue(os.path.isfile(readme)) self.assertTrue(os.path.isfile(readme))
with closing(open(readme)) as file: with open(readme) as file:
self.assertEqual(readme_text, file.read()) self.assertEqual(readme_text, file.read())
@ -289,7 +289,7 @@ def test_restage(self):
self.check_chdir_to_source(stage, stage_name) self.check_chdir_to_source(stage, stage_name)
# Try to make a file in the old archive dir # Try to make a file in the old archive dir
with closing(open('foobar', 'w')) as file: with open('foobar', 'w') as file:
file.write("this file is to be destroyed.") file.write("this file is to be destroyed.")
self.assertTrue('foobar' in os.listdir(stage.source_path)) self.assertTrue('foobar' in os.listdir(stage.source_path))

View File

@ -27,7 +27,6 @@
import unittest import unittest
import shutil import shutil
import tempfile import tempfile
from contextlib import closing
from llnl.util.filesystem import * from llnl.util.filesystem import *

View File

@ -23,7 +23,6 @@
# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
############################################################################## ##############################################################################
import hashlib import hashlib
from contextlib import closing
"""Set of acceptable hashes that Spack will use.""" """Set of acceptable hashes that Spack will use."""
_acceptable_hashes = [ _acceptable_hashes = [
@ -44,7 +43,7 @@ def checksum(hashlib_algo, filename, **kwargs):
""" """
block_size = kwargs.get('block_size', 2**20) block_size = kwargs.get('block_size', 2**20)
hasher = hashlib_algo() hasher = hashlib_algo()
with closing(open(filename)) as file: with open(filename) as file:
while True: while True:
data = file.read(block_size) data = file.read(block_size)
if not data: if not data: