Commit 8400e2ed authored by Jason Madden's avatar Jason Madden

Unify three checks for a file being changed into one function. Note that...

Unify three checks for a file being changed into one function. Note that python < 3.3 raise IOError if a file is not found, while python >= 3.3 raise FileNotFound, a kind of OSError. EnvironmentError is a common superclass on all versions and in fact is where `errno` is defined.
parent fe953bf7
...@@ -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
...@@ -1130,15 +1131,21 @@ def _distutils_script(path, dest, script_content, initialization, rsetup): ...@@ -1130,15 +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 = True changed = _file_changed(dest, contents)
if os.path.exists(dest):
with open(dest) as f:
changed = f.read() != 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:
...@@ -1151,11 +1158,8 @@ def _create_script(contents, dest): ...@@ -1151,11 +1158,8 @@ 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')
win32_changed = True
if os.path.exists(win32_exe): if _file_changed(win32_exe, new_data, 'rb'):
with open(win32_exe, 'rb') as f:
win32_changed = f.read() != new_data
if win32_changed:
# Only write it if it's different. # Only write it if it's different.
with open(win32_exe, 'wb') as f: with open(win32_exe, 'wb') as f:
f.write(new_data) f.write(new_data)
...@@ -1226,10 +1230,7 @@ def _pyscript(path, dest, rsetup, initialization=''): ...@@ -1226,10 +1230,7 @@ def _pyscript(path, dest, rsetup, initialization=''):
relative_paths_setup = rsetup, relative_paths_setup = rsetup,
initialization=initialization, initialization=initialization,
) )
changed = True changed = _file_changed(dest, contents)
if os.path.exists(dest):
with open(dest) as f:
changed = f.read() != 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:
......
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