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 ...@@ -10,6 +10,9 @@ Unreleased
- Add ``BUILDOUT_HOME`` as an alternate way to control how the user default - Add ``BUILDOUT_HOME`` as an alternate way to control how the user default
configuration is found. 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) 2.2.1 (2013-09-05)
================== ==================
......
...@@ -19,6 +19,7 @@ installed. ...@@ -19,6 +19,7 @@ installed.
""" """
import distutils.errors import distutils.errors
import errno
import glob import glob
import logging import logging
import os import os
...@@ -868,7 +869,8 @@ def develop(setup, dest, ...@@ -868,7 +869,8 @@ def develop(setup, dest,
os.rename(setup_cfg+'-develop-aside', setup_cfg) os.rename(setup_cfg+'-develop-aside', setup_cfg)
undo.append(restore_old_setup) undo.append(restore_old_setup)
else: else:
open(setup_cfg, 'w') f = open(setup_cfg, 'w')
f.close()
undo.append(lambda: os.remove(setup_cfg)) undo.append(lambda: os.remove(setup_cfg))
setuptools.command.setopt.edit_config( setuptools.command.setopt.edit_config(
setup_cfg, dict(build_ext=build_ext)) setup_cfg, dict(build_ext=build_ext))
...@@ -1129,12 +1131,21 @@ def _distutils_script(path, dest, script_content, initialization, rsetup): ...@@ -1129,12 +1131,21 @@ def _distutils_script(path, dest, script_content, initialization, rsetup):
) )
return _create_script(contents, dest) 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): def _create_script(contents, dest):
generated = [] generated = []
script = dest script = dest
changed = not (os.path.exists(dest) and open(dest).read() == contents) changed = _file_changed(dest, contents)
if is_win32: if is_win32:
# generate exe file and give the script a magic name: # generate exe file and give the script a magic name:
...@@ -1147,15 +1158,16 @@ def _create_script(contents, dest): ...@@ -1147,15 +1158,16 @@ def _create_script(contents, dest):
except AttributeError: except AttributeError:
# fall back for compatibility with older Distribute versions # fall back for compatibility with older Distribute versions
new_data = pkg_resources.resource_string('setuptools', 'cli.exe') 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. # 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) generated.append(win32_exe)
if changed: if changed:
open(dest, 'w').write(contents) with open(dest, 'w') as f:
f.write(contents)
logger.info( logger.info(
"Generated script %r.", "Generated script %r.",
# Normalize for windows # Normalize for windows
...@@ -1218,18 +1230,20 @@ def _pyscript(path, dest, rsetup, initialization=''): ...@@ -1218,18 +1230,20 @@ def _pyscript(path, dest, rsetup, initialization=''):
relative_paths_setup = rsetup, relative_paths_setup = rsetup,
initialization=initialization, initialization=initialization,
) )
changed = not (os.path.exists(dest) and open(dest).read() == contents) changed = _file_changed(dest, contents)
if is_win32: if is_win32:
# generate exe file and give the script a magic name: # generate exe file and give the script a magic name:
exe = script + '.exe' exe = script + '.exe'
open(exe, 'wb').write( with open(exe, 'wb') as f:
f.write(
pkg_resources.resource_string('setuptools', 'cli.exe') pkg_resources.resource_string('setuptools', 'cli.exe')
) )
generated.append(exe) generated.append(exe)
if changed: if changed:
open(dest, 'w').write(contents) with open(dest, 'w') as f:
f.write(contents)
try: try:
os.chmod(dest, _execute_permission()) os.chmod(dest, _execute_permission())
except (AttributeError, os.error): except (AttributeError, os.error):
......
...@@ -56,7 +56,8 @@ def cat(dir, *names): ...@@ -56,7 +56,8 @@ def cat(dir, *names):
and os.path.exists(path+'-script.py') and os.path.exists(path+'-script.py')
): ):
path = 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): def ls(dir, *subs):
if subs: if subs:
...@@ -240,9 +241,8 @@ def buildoutSetUp(test): ...@@ -240,9 +241,8 @@ def buildoutSetUp(test):
os.chdir(sample) os.chdir(sample)
# Create a basic buildout.cfg to avoid a warning from buildout: # Create a basic buildout.cfg to avoid a warning from buildout:
open('buildout.cfg', 'w').write( with open('buildout.cfg', 'w') as f:
"[buildout]\nparts =\n" f.write("[buildout]\nparts =\n")
)
# Use the buildout bootstrap command to create a buildout # Use the buildout bootstrap command to create a buildout
zc.buildout.buildout.Buildout( zc.buildout.buildout.Buildout(
...@@ -375,7 +375,8 @@ class Handler(BaseHTTPRequestHandler): ...@@ -375,7 +375,8 @@ class Handler(BaseHTTPRequestHandler):
self.send_header('Content-Length', str(len(out))) self.send_header('Content-Length', str(len(out)))
self.send_header('Content-Type', 'text/html') self.send_header('Content-Type', 'text/html')
else: else:
out = open(path, 'rb').read() with open(path, 'rb') as f:
out = f.read()
self.send_header('Content-Length', len(out)) self.send_header('Content-Length', len(out))
if path.endswith('.egg'): if path.endswith('.egg'):
self.send_header('Content-Type', 'application/zip') self.send_header('Content-Type', 'application/zip')
...@@ -471,8 +472,8 @@ def install(project, destination): ...@@ -471,8 +472,8 @@ def install(project, destination):
shutil.copyfile(dist.location, destination) shutil.copyfile(dist.location, destination)
else: else:
# copy link # copy link
open(os.path.join(destination, project+'.egg-link'), 'w' with open(os.path.join(destination, project+'.egg-link'), 'w') as f:
).write(dist.location) f.write(dist.location)
def install_develop(project, destination): def install_develop(project, destination):
if not isinstance(destination, str): if not isinstance(destination, str):
...@@ -481,8 +482,8 @@ def install_develop(project, destination): ...@@ -481,8 +482,8 @@ def install_develop(project, destination):
dist = pkg_resources.working_set.find( dist = pkg_resources.working_set.find(
pkg_resources.Requirement.parse(project)) pkg_resources.Requirement.parse(project))
open(os.path.join(destination, project+'.egg-link'), 'w' with open(os.path.join(destination, project+'.egg-link'), 'w') as f:
).write(dist.location) f.write(dist.location)
def _normalize_path(match): def _normalize_path(match):
path = match.group(1) 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