Home | History | Annotate | Download | only in util
      1 # (c) 2005 Ian Bicking and contributors; written for Paste (http://pythonpaste.org)
      2 # Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php
      3 
      4 import sys
      5 import os
      6 
      7 def find_package(dir):
      8     """
      9     Given a directory, finds the equivalent package name.  If it
     10     is directly in sys.path, returns ''.
     11     """
     12     dir = os.path.abspath(dir)
     13     orig_dir = dir
     14     path = map(os.path.abspath, sys.path)
     15     packages = []
     16     last_dir = None
     17     while 1:
     18         if dir in path:
     19             return '.'.join(packages)
     20         packages.insert(0, os.path.basename(dir))
     21         dir = os.path.dirname(dir)
     22         if last_dir == dir:
     23             raise ValueError(
     24                 "%s is not under any path found in sys.path" % orig_dir)
     25         last_dir = dir
     26 
     27