Symlink chain resolver module
Hi there, I thought it would have been good sharing this code I used in a project of mine. I thought it could eventually look good incorporated in Python stdlib through os or os.path modules. Otherwise I'm merely offering it as a community service to anyone who might be interested. -- Giampaolo #!/usr/bin/env python # linkchainresolver.py import os, sys, errno def resolvelinkchain(path): """Resolve a chain of symbolic links by returning the absolute path name of the final target. Raise os.error exception in case of circular link. Do not raise exception if the symlink is broken (pointing to a non-existent path). Return a normalized absolutized version of the pathname if it is not a symbolic link. Examples: >>> resolvelinkchain('validlink') /abstarget >>> resolvelinkchain('brokenlink') # resolved anyway /abstarget >>> resolvelinkchain('circularlink') Traceback (most recent call last): File "<stdin>", line 1, in <module> File "module.py", line 19, in resolvelinkchain os.stat(path) OSError: [Errno 40] Too many levels of symbolic links: '3' >>> """ try: os.stat(path) except os.error, err: # do not raise exception in case of broken symlink; # we want to know the final target anyway if err.errno == errno.ENOENT: pass else: raise if not os.path.isabs(path): basedir = os.path.dirname(os.path.abspath(path)) else: basedir = os.path.dirname(path) p = path while os.path.islink(p): p = os.readlink(p) if not os.path.isabs(p): p = os.path.join(basedir, p) basedir = os.path.dirname(p) return os.path.join(basedir, p)
Isn't this pretty close to os.path.realpath()? On Dec 4, 2007 10:46 AM, Giampaolo Rodola' <gnewsg@gmail.com> wrote:
Hi there, I thought it would have been good sharing this code I used in a project of mine. I thought it could eventually look good incorporated in Python stdlib through os or os.path modules. Otherwise I'm merely offering it as a community service to anyone who might be interested.
-- Giampaolo
#!/usr/bin/env python # linkchainresolver.py
import os, sys, errno
def resolvelinkchain(path): """Resolve a chain of symbolic links by returning the absolute path name of the final target.
Raise os.error exception in case of circular link. Do not raise exception if the symlink is broken (pointing to a non-existent path). Return a normalized absolutized version of the pathname if it is not a symbolic link.
Examples:
>>> resolvelinkchain('validlink') /abstarget >>> resolvelinkchain('brokenlink') # resolved anyway /abstarget >>> resolvelinkchain('circularlink') Traceback (most recent call last): File "<stdin>", line 1, in <module> File "module.py", line 19, in resolvelinkchain os.stat(path) OSError: [Errno 40] Too many levels of symbolic links: '3' >>> """ try: os.stat(path) except os.error, err: # do not raise exception in case of broken symlink; # we want to know the final target anyway if err.errno == errno.ENOENT: pass else: raise if not os.path.isabs(path): basedir = os.path.dirname(os.path.abspath(path)) else: basedir = os.path.dirname(path) p = path while os.path.islink(p): p = os.readlink(p) if not os.path.isabs(p): p = os.path.join(basedir, p) basedir = os.path.dirname(p) return os.path.join(basedir, p) _______________________________________________ Python-ideas mailing list Python-ideas@python.org http://mail.python.org/mailman/listinfo/python-ideas
-- --Guido van Rossum (home page: http://www.python.org/~guido/)
On 4 Dic, 20:06, "Guido van Rossum" <gu...@python.org> wrote:
Isn't this pretty close to os.path.realpath()?
On Dec 4, 2007 10:46 AM, Giampaolo Rodola' <gne...@gmail.com> wrote:
Hi there, I thought it would have been good sharing this code I used in a project of mine. I thought it could eventually look good incorporated in Python stdlib through os or os.path modules. Otherwise I'm merely offering it as a community service to anyone who might be interested.
-- Giampaolo
#!/usr/bin/env python # linkchainresolver.py
import os, sys, errno
def resolvelinkchain(path): """Resolve a chain of symbolic links by returning the absolute path name of the final target.
Raise os.error exception in case of circular link. Do not raise exception if the symlink is broken (pointing to a non-existent path). Return a normalized absolutized version of the pathname if it is not a symbolic link.
Examples:
>>> resolvelinkchain('validlink') /abstarget >>> resolvelinkchain('brokenlink') # resolved anyway /abstarget >>> resolvelinkchain('circularlink') Traceback (most recent call last): File "<stdin>", line 1, in <module> File "module.py", line 19, in resolvelinkchain os.stat(path) OSError: [Errno 40] Too many levels of symbolic links: '3'
""" try: os.stat(path) except os.error, err: # do not raise exception in case of broken symlink; # we want to know the final target anyway if err.errno == errno.ENOENT: pass else: raise if not os.path.isabs(path): basedir = os.path.dirname(os.path.abspath(path)) else: basedir = os.path.dirname(path) p = path while os.path.islink(p): p = os.readlink(p) if not os.path.isabs(p): p = os.path.join(basedir, p) basedir = os.path.dirname(p) return os.path.join(basedir, p) _______________________________________________ Python-ideas mailing list Python-id...@python.org http://mail.python.org/mailman/listinfo/python-ideas
-- --Guido van Rossum (home page:http://www.python.org/~guido/) _______________________________________________ Python-ideas mailing list Python-id...@python.orghttp://mail.python.org/mailman/listinfo/python-ideas- Nascondi testo tra virgolette -
- Mostra testo tra virgolette -
Are you trying to tell me that I've lost the whole evening when such a thing was already available in os.path? :-) Wait a moment. I'm going to check what os.path.realpath() does.
On Tue, Dec 04, 2007, Giampaolo Rodola' wrote:
Ok, it's been a wasted evening. :-) Sorry.
(I'm feeling so sad...)
Another victim of Guido's Time Machine. -- Aahz (aahz@pythoncraft.com) <*> http://www.pythoncraft.com/ "Typing is cheap. Thinking is expensive." --Roy Smith
On Dec 4, 2007 11:53 AM, Giampaolo Rodola' <gnewsg@gmail.com> wrote:
Ok, it's been a wasted evening. :-) Sorry.
(I'm feeling so sad...)
Don't fret. It's not been wasted. You learned a couple of things: you learned how to resolve symlinks recursively and safely, you probably improved your Python debugging skills, *and* you learned to do research before rolling up your sleeves. That's three valuable lessons! -- --Guido van Rossum (home page: http://www.python.org/~guido/)
participants (3)
-
Aahz
-
Giampaolo Rodola'
-
Guido van Rossum