[Distutils] setuptools' install_script overwrites symlinked files rather than replacing symlinks

Michał Górny mgorny at gentoo.org
Sun Aug 4 13:12:27 CEST 2013


Hello,

We've got a pretty specific setup where various Python scripts
in /usr/bin are symlinked to a common wrapper. For example:

  /usr/bin/easy_install -> python-exec

As a result, calling 'setup.py install' in a package that installs
setuptools' legacy script wrappers (e.g. setuptools itself) rewrites
python-exec (which -- shortly saying -- breaks a lot) rather than
replacing the 'easy_install' symlink.

I believe that the core issue is in command/easy_install.py. There,
the write_script() command writes directly onto the destination file
with no precautions:

        if not self.dry_run:
            ensure_directory(target)
            f = open(target,"w"+mode)
            f.write(contents)
            f.close()

Therefore, if 'target' is a symlink, the symlink target is opened
rather than the expected path.

distutils itself is free of this issue since it removes the target
before writing (distutils/file_util.py):

        if os.path.exists(dst):
            try:
                os.unlink(dst)
            except os.error, (errno, errstr):
                raise DistutilsFileError(
                      "could not delete '%s': %s" % (dst, errstr))

This suffers a race condition but is better than nothing. Other tools 
usually create a temporary file in the target directory and use
rename() to atomically replace the target.

I'm willing to write a patch. Please just tell me which solution would
you prefer.

(please keep python at g.o in CC when replying)

-- 
Best regards,
Michał Górny
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 966 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/distutils-sig/attachments/20130804/64cfc9b7/attachment.pgp>


More information about the Distutils-SIG mailing list