Skip to content

Commit

Permalink
Merge pull request #1 from theyoprst/fix-samefile
Browse files Browse the repository at this point in the history
Fix os.path.samefile() usage on Windows + python 2.
  • Loading branch information
matysek committed Oct 26, 2015
2 parents 318630d + cddfcde commit cd96283
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions pip_accel/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,22 @@ def __init__(self, config, validate=True):
# temporary sources after pip-accel has finished.
self.reported_requirements = []

@staticmethod
def samedir(path1, path2):
"""
Return True if both path1 and path2 refer to the same directory.
Return False if one of them does not exist or is not directory.
"""
for path in (path1, path2):
if not os.path.isdir(path) or not os.path.exists(path):
return False
try:
return os.path.samefile(path1, path2)
except AttributeError:
# On Windows and py2* there is no os.path.samefile function.
return os.path.realpath(path1) == os.path.realpath(path2)

def validate_environment(self):
"""
Make sure :py:data:`sys.prefix` matches ``$VIRTUAL_ENV`` (if defined).
Expand All @@ -118,12 +134,7 @@ def validate_environment(self):
"""
environment = os.environ.get('VIRTUAL_ENV')
if environment:
try:
# Because os.path.samefile() itself can raise exceptions, e.g.
# when $VIRTUAL_ENV points to a non-existing directory, we use
# an assertion to allow us to use a single code path :-)
assert os.path.samefile(sys.prefix, environment)
except Exception:
if not self.samedir(sys.prefix, environment):
raise EnvironmentMismatchError("""
You are trying to install packages in environment #1 which
is different from environment #2 where pip-accel is
Expand Down

0 comments on commit cd96283

Please sign in to comment.