diff options
author | matt <matt@xcolour.net> | 2013-01-28 11:32:18 -0500 |
---|---|---|
committer | matt <matt@xcolour.net> | 2013-01-28 11:32:18 -0500 |
commit | 1afcb52d73271bbbd78f885451aa1b0e78c09871 (patch) | |
tree | 9145840d6036fcbc0b6647c88f679a567fa8c54d /paste/util/findpackage.py | |
download | paste-git-stringio.tar.gz |
Import StringIO so it can be used.stringio
Diffstat (limited to 'paste/util/findpackage.py')
-rw-r--r-- | paste/util/findpackage.py | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/paste/util/findpackage.py b/paste/util/findpackage.py new file mode 100644 index 0000000..68b5e8b --- /dev/null +++ b/paste/util/findpackage.py @@ -0,0 +1,26 @@ +# (c) 2005 Ian Bicking and contributors; written for Paste (http://pythonpaste.org) +# Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php + +import sys +import os + +def find_package(dir): + """ + Given a directory, finds the equivalent package name. If it + is directly in sys.path, returns ''. + """ + dir = os.path.abspath(dir) + orig_dir = dir + path = map(os.path.abspath, sys.path) + packages = [] + last_dir = None + while 1: + if dir in path: + return '.'.join(packages) + packages.insert(0, os.path.basename(dir)) + dir = os.path.dirname(dir) + if last_dir == dir: + raise ValueError( + "%s is not under any path found in sys.path" % orig_dir) + last_dir = dir + |