Improve python install prefix determination.

This commit is contained in:
tgoff0@gmail.com 2015-02-19 18:12:44 +00:00
parent 9979e476cf
commit 00a2fdaecb
2 changed files with 17 additions and 6 deletions

View file

@ -5,19 +5,30 @@ import os.path
import site
def main():
if len(sys.argv) != 2:
msg = 'usage: %s <prefix>\n' % os.path.basename(sys.argv[0])
'''\
Check if the given prefix is included in sys.path for the given
python version; if not find an alternate valid prefix. Print the
result to standard out.
'''
if len(sys.argv) != 3:
msg = 'usage: %s <prefix> <python version>\n' % \
os.path.basename(sys.argv[0])
sys.stderr.write(msg)
return 1
python_prefix = sys.argv[1]
python_version = sys.argv[2]
path = '%s/lib/python%s' % (python_prefix, python_version)
path = os.path.normpath(path)
if path[-1] != '/':
path = path + '/'
prefix = None
for p in sys.path:
if python_prefix in p:
prefix = python_prefix
if p.startswith(path):
prefix = path
break
if not prefix:
prefix = site.PREFIXES[-1]
print prefix
sys.stdout.write('%s\n' % prefix)
return 0
if __name__ == '__main__':