File: _findpy.py

package info (click to toggle)
codespeak-lib 0.9.1-3
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 3,212 kB
  • ctags: 5,409
  • sloc: python: 33,390; ansic: 961; xml: 582; makefile: 90
file content (37 lines) | stat: -rwxr-xr-x 1,146 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/usr/bin/env python 

#
# find and import a version of 'py'
#
import sys
import os
from os.path import dirname as opd, exists, join, basename, abspath

def searchpy(current):
    while 1:
        last = current
        initpy = join(current, '__init__.py')
        if not exists(initpy):
            pydir = join(current, 'py')
            # recognize py-package and ensure it is importable
            if exists(pydir) and exists(join(pydir, '__init__.py')):
                #for p in sys.path:
                #    if p == current:
                #        return True
                if current != sys.path[0]:  # if we are already first, then ok
                    print >>sys.stderr, "inserting into sys.path:", current
                    sys.path.insert(0, current)
                return True
        current = opd(current)
        if last == current:
            return False

if not searchpy(abspath(os.curdir)):
    if not searchpy(opd(abspath(sys.argv[0]))):
        if not searchpy(opd(__file__)):
            pass # let's hope it is just on sys.path 

import py

if __name__ == '__main__': 
    print "py lib is at", py.__file__