Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docbuild: Use lexists when testing whether a symlink exists #22438

Closed
infinity0 mannequin opened this issue Feb 25, 2017 · 21 comments
Closed

docbuild: Use lexists when testing whether a symlink exists #22438

infinity0 mannequin opened this issue Feb 25, 2017 · 21 comments

Comments

@infinity0
Copy link
Mannequin

infinity0 mannequin commented Feb 25, 2017

Otherwise the build fails if the symlink is already there but points to a non-existing path.

Component: build

Author: Ximin Luo

Branch/Commit: 05ee235

Reviewer: Jeroen Demeyer

Issue created by migration from https://trac.sagemath.org/ticket/22438

@infinity0 infinity0 mannequin added this to the sage-7.6 milestone Feb 25, 2017
@infinity0 infinity0 mannequin added the p: major / 3 label Feb 25, 2017
@infinity0
Copy link
Mannequin Author

infinity0 mannequin commented Feb 25, 2017

@infinity0

This comment has been minimized.

@infinity0
Copy link
Mannequin Author

infinity0 mannequin commented Feb 25, 2017

New commits:

0c8139dUse lexists instead of exists

@infinity0
Copy link
Mannequin Author

infinity0 mannequin commented Feb 25, 2017

Author: Ximin Luo

@infinity0
Copy link
Mannequin Author

infinity0 mannequin commented Feb 25, 2017

Commit: 0c8139d

@videlec
Copy link
Contributor

videlec commented Feb 25, 2017

comment:3

This looks annoying. Why the symlink is pointing to a non-existing directory in the first place?

@infinity0
Copy link
Mannequin Author

infinity0 mannequin commented Feb 25, 2017

comment:4

I can't remember now. Possibly I was messing around with a solution to #21732 or #22088 or just getting the Debian package working in the first place.

But the logic shouldn't break just because I might've been doing unusual things. There's already logic to remove a regular file or a working symlink to a file, so we should also remove broken symlinks.

@jdemeyer
Copy link

comment:5

Instead of trying to figure out whether to run unlink() or rmtree(), why not use EAFP principles and use try/except? Something like

try:
    os.unlink(path)
except OSError:
    try:
        shutil.rmtree(path)
    except OSError:
        if os.path.lexists(path):
            raise

PS: I sometimes wonder why Python makes it so hard to implement common shell commands like rm -rf or mkdir -p correctly.

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Feb 25, 2017

Branch pushed to git repo; I updated commit sha1. This was a forced push. New commits:

077f8bcdocbuild: More reliable way of trying to remove static_dir

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Feb 25, 2017

Changed commit from 0c8139d to 077f8bc

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Feb 25, 2017

Branch pushed to git repo; I updated commit sha1. This was a forced push. New commits:

c4860bddocbuild: More reliable way of trying to remove static_dir

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Feb 25, 2017

Changed commit from 077f8bc to c4860bd

@infinity0
Copy link
Mannequin Author

infinity0 mannequin commented Feb 25, 2017

comment:8

I tweaked what you suggested to be a bit shorter. It's not fully EAFP but I think the brevity makes up for it.

Interestingly on Python3 they have a special subclass of OSError called IsADirectoryError that would make this code even shorter, but never mind.

@infinity0 infinity0 mannequin added s: needs review and removed s: needs work labels Feb 25, 2017
@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Feb 25, 2017

Changed commit from c4860bd to 58c1e3f

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Feb 25, 2017

Branch pushed to git repo; I updated commit sha1. This was a forced push. New commits:

58c1e3fdocbuild: More reliable way of trying to remove static_dir

@infinity0
Copy link
Mannequin Author

infinity0 mannequin commented Feb 25, 2017

comment:11

whoops, I forgot to re-raise for other types of errors. ¬.¬

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Mar 11, 2017

Branch pushed to git repo; I updated commit sha1. This was a forced push. New commits:

05ee235docbuild: More reliable way of trying to remove static_dir

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Mar 11, 2017

Changed commit from 58c1e3f to 05ee235

@infinity0
Copy link
Mannequin Author

infinity0 mannequin commented Mar 11, 2017

comment:13

I realised the previous version (in comment:10) is wrong, here is an updated version. If you don't like it, I can also do the longer EAFP version that you posted earlier.

I actually think the first one (my original commit) is best, it's slightly long-winded but at least the error messages are exact. With both this recent version and your EAFP version, the error messages could be misleading, e.g.

Recent version:

  • real problem: grand-child file can't be removed because child directory is not writable
  • raised error: static_dir is a directory

EAFP version:

  • real problem: parent directory is not writable
  • raised error: static_dir is not a directory

@jdemeyer
Copy link

Reviewer: Jeroen Demeyer

@vbraun
Copy link
Member

vbraun commented Jun 22, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants