1#! /bin/env python 2 3""" 4This script will return the paths that distutils will use for installing 5a package. To use this script, execute it the same way that you would 6execute setup.py, but instead of providing 'install' or 'build' as the 7command, specify 'purelib' or 'platlib' and the corresponding path 8will be printed. The 'purelib' command will print the install location 9for .py files, while the 'platlib' command will print the install location 10of binary modules (.so or .dll). 11 12Written by David Gobbi, Feb 25, 2006. 13""" 14 15 16import string 17import sys 18import os 19 20def get_install_path(command, *args): 21 """Return the module install path, given the arguments that were 22 provided to setup.py. The paths that you can request are 'purelib' 23 for the .py installation directory and 'platlib' for the binary 24 module installation directory. 25 """ 26 27 # convert setup args into an option dictionary 28 options = {} 29 30 for arg in args: 31 if arg == '--': 32 break 33 if arg[0:2] == "--": 34 try: 35 option, value = string.split(arg,"=") 36 options[option] = value 37 except ValueError: 38 options[option] = 1 39 40 # check for the prefix and exec_prefix 41 try: 42 prefix = options["--prefix"] 43 except KeyError: 44 prefix = None 45 46 try: 47 exec_prefix = options["--exec-prefix"] 48 except KeyError: 49 exec_prefix = prefix 50 51 # if prefix or exec_prefix aren't set, use default system values 52 if prefix == None: 53 prefix = sys.prefix 54 55 if exec_prefix == None: 56 exec_prefix = sys.exec_prefix 57 58 # replace backslashes with slashes 59 if os.name != 'posix': 60 prefix = string.replace(prefix, os.sep, "/") 61 exec_prefix = string.replace(exec_prefix, os.sep, "/") 62 63 # get rid of trailing separator 64 if prefix != "" and prefix[-1] == "/": 65 prefix = prefix[0:-1] 66 67 if exec_prefix != "" and exec_prefix[-1] == "/": 68 exec_prefix = exec_prefix[0:-1] 69 70 # check for "home" install scheme 71 try: 72 home = options["--home"] 73 if os.name != 'posix': 74 home = string.replace(home, os.sep, "/") 75 if home != "" and home[-1] == "/": 76 home = home[0:-1] 77 except KeyError: 78 home = None 79 80 # apply "home" install scheme, but not for Windows with python < 2.4 81 # (distutils didn't allow home scheme for windows until 2.4) 82 if home != None and not (os.name != 'posix' and sys.version < '2.4'): 83 purelib = home+'/lib/python' 84 platlib = home+'/lib/python' 85 scripts = home+'/bin' 86 data = home 87 elif os.name == 'posix': 88 ver = sys.version[0:3] 89 purelib = prefix+'/lib/python'+ver+'/site-packages' 90 platlib = exec_prefix+'/lib/python'+ver+'/site-packages' 91 scripts = prefix+'/bin' 92 data = prefix 93 elif sys.version < '2.2': 94 purelib = prefix 95 platlib = prefix 96 scripts = prefix+'/Scripts' 97 data = prefix 98 else: 99 purelib = prefix+'/Lib/site-packages' 100 platlib = prefix+'/Lib/site-packages' 101 scripts = prefix+'/Scripts' 102 data = prefix 103 104 # allow direct setting of install directories 105 try: 106 purelib = options["--install-purelib"] 107 except KeyError: 108 pass 109 110 try: 111 platlib = options["--install-platlib"] 112 except KeyError: 113 pass 114 115 try: 116 scripts = options["--install-scripts"] 117 except KeyError: 118 pass 119 120 try: 121 data = options["--install-data"] 122 except KeyError: 123 pass 124 125 # return the information that was asked for 126 if command == 'purelib': 127 return purelib 128 elif command == 'platlib': 129 return platlib 130 elif command == 'scripts': 131 return scripts 132 elif command == 'data': 133 return data 134 135 136if __name__ == "__main__": 137 print apply(get_install_path, sys.argv[1:]) 138 139