[Python-ideas] Implement POSIX ln via shutil.link and shutil.symlink

Inada Naoki songofacandy at gmail.com
Tue Jun 4 03:25:54 EDT 2019


On Sat, Jun 1, 2019 at 4:10 PM Serhiy Storchaka <storchaka at gmail.com> wrote:
>
> Why do you need to replace a symlink atomically? This is a solution,
> what problem it solves?
>

There is another, more common / realistic usage of atomic symlink
replacing.

When deploy PHP application or static web contents to web servers,
symlink is used for "atomic deployment".  Roughly speaking, it is like this:

```
rsync -avK v2/ web:contents/v2/
ssh web "cd contents && ln -sf v2 current"  # current/ is exposed by web server
```

If "ln -sf" is not atomic and do remove & symlink, web server or php
will return 404 error between remove and symlink.

I feel this use case is more real world application than
"I don't want exception even when other process creates file
at the same time".

These are some links referring about "atomic deployment":

* https://github.com/deployphp/deployer/blob/master/recipe/deploy/symlink.php
* https://hackernoon.com/truly-atomic-deployments-with-nginx-and-php-fpm-aed8a8ac1cd9
* https://blog.forrest79.net/?p=537


Then, should Python support it?  Maybe.

Python is used tools like Ansible.  While "atomic deploy" is widely used
in PHP community, it's welcome that PHPer use Python for tools.

So I think it would be nice if shutil.symlink can do same thing
`ln -sf` can do.

On the other hand, I am not sure we should support "make tmp,
rename it to target, or remove tmp if failed" idiom natively
everywhere in shutil and pathlib.

When writing file or copying file into directly which is exposed by
WEB server, this idiom is to avoid exposing partial files to users.

But implementing this idiom in everywhere in shutil and pathlib
is still controversial.

In case of symlink, I'm weak +1 to implement this idiom directly in stdlib.
tempfile.mkstemp doesn't support symlink.  So people need to
use external command `ln -sf` for now.

Regards,
-- 
Inada Naoki  <songofacandy at gmail.com>


More information about the Python-ideas mailing list