Commit 8df1e8db authored by Marius Gedminas's avatar Marius Gedminas

Merge pull request #180 from NextThought/master

Close various files when finished writing to them; fixes ResourceWarnings and some PyPy uses.
parents dac64121 8400e2ed
......@@ -10,6 +10,9 @@ Unreleased
- Add ``BUILDOUT_HOME`` as an alternate way to control how the user default
configuration is found.
- Close various files when finished writing to them. This avoids
ResourceWarnings on Python 3, and better supports doctests under PyPy.
2.2.1 (2013-09-05)
==================
......
......@@ -19,6 +19,7 @@ installed.
"""
import distutils.errors
import errno
import glob
import logging
import os
......@@ -868,7 +869,8 @@ def develop(setup, dest,
os.rename(setup_cfg+'-develop-aside', setup_cfg)
undo.append(restore_old_setup)
else:
open(setup_cfg, 'w')
f = open(setup_cfg, 'w')
f.close()
undo.append(lambda: os.remove(setup_cfg))
setuptools.command.setopt.edit_config(
setup_cfg, dict(build_ext=build_ext))
......@@ -1129,12 +1131,21 @@ def _distutils_script(path, dest, script_content, initialization, rsetup):
)
return _create_script(contents, dest)
def _file_changed(filename, old_contents, mode='r'):
try:
with open(filename, mode) as f:
return f.read() != old_contents
except EnvironmentError as e:
if e.errno == errno.ENOENT:
return True
else:
raise
def _create_script(contents, dest):
generated = []
script = dest
changed = not (os.path.exists(dest) and open(dest).read() == contents)
changed = _file_changed(dest, contents)
if is_win32:
# generate exe file and give the script a magic name:
......@@ -1147,15 +1158,16 @@ def _create_script(contents, dest):
except AttributeError:
# fall back for compatibility with older Distribute versions
new_data = pkg_resources.resource_string('setuptools', 'cli.exe')
if (not os.path.exists(win32_exe) or
(open(win32_exe, 'rb').read() != new_data)
):
if _file_changed(win32_exe, new_data, 'rb'):
# Only write it if it's different.
open(win32_exe, 'wb').write(new_data)
with open(win32_exe, 'wb') as f:
f.write(new_data)
generated.append(win32_exe)
if changed:
open(dest, 'w').write(contents)
with open(dest, 'w') as f:
f.write(contents)
logger.info(
"Generated script %r.",
# Normalize for windows
......@@ -1218,18 +1230,20 @@ def _pyscript(path, dest, rsetup, initialization=''):
relative_paths_setup = rsetup,
initialization=initialization,
)
changed = not (os.path.exists(dest) and open(dest).read() == contents)
changed = _file_changed(dest, contents)
if is_win32:
# generate exe file and give the script a magic name:
exe = script + '.exe'
open(exe, 'wb').write(
with open(exe, 'wb') as f:
f.write(
pkg_resources.resource_string('setuptools', 'cli.exe')
)
generated.append(exe)
if changed:
open(dest, 'w').write(contents)
with open(dest, 'w') as f:
f.write(contents)
try:
os.chmod(dest, _execute_permission())
except (AttributeError, os.error):
......
......@@ -56,7 +56,8 @@ def cat(dir, *names):
and os.path.exists(path+'-script.py')
):
path = path+'-script.py'
print_(open(path).read(), end='')
with open(path) as f:
print_(f.read(), end='')
def ls(dir, *subs):
if subs:
......@@ -240,9 +241,8 @@ def buildoutSetUp(test):
os.chdir(sample)
# Create a basic buildout.cfg to avoid a warning from buildout:
open('buildout.cfg', 'w').write(
"[buildout]\nparts =\n"
)
with open('buildout.cfg', 'w') as f:
f.write("[buildout]\nparts =\n")
# Use the buildout bootstrap command to create a buildout
zc.buildout.buildout.Buildout(
......@@ -375,7 +375,8 @@ class Handler(BaseHTTPRequestHandler):
self.send_header('Content-Length', str(len(out)))
self.send_header('Content-Type', 'text/html')
else:
out = open(path, 'rb').read()
with open(path, 'rb') as f:
out = f.read()
self.send_header('Content-Length', len(out))
if path.endswith('.egg'):
self.send_header('Content-Type', 'application/zip')
......@@ -471,8 +472,8 @@ def install(project, destination):
shutil.copyfile(dist.location, destination)
else:
# copy link
open(os.path.join(destination, project+'.egg-link'), 'w'
).write(dist.location)
with open(os.path.join(destination, project+'.egg-link'), 'w') as f:
f.write(dist.location)
def install_develop(project, destination):
if not isinstance(destination, str):
......@@ -481,8 +482,8 @@ def install_develop(project, destination):
dist = pkg_resources.working_set.find(
pkg_resources.Requirement.parse(project))
open(os.path.join(destination, project+'.egg-link'), 'w'
).write(dist.location)
with open(os.path.join(destination, project+'.egg-link'), 'w') as f:
f.write(dist.location)
def _normalize_path(match):
path = match.group(1)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment