diff options
Diffstat (limited to 'paste/util')
| -rw-r--r-- | paste/util/findpackage.py | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/paste/util/findpackage.py b/paste/util/findpackage.py new file mode 100644 index 0000000..276e216 --- /dev/null +++ b/paste/util/findpackage.py @@ -0,0 +1,22 @@ +import sys +import os + +def find_package(dir): + """ + Given a directory, finds the equivalent package name. If it + is directly in sys.path, returns ''. + """ + orig_dir = dir + path = 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 + |
