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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
|
import py
import sys, os, re
from distutils import sysconfig
from distutils import core
winextensions = 1
if sys.platform == 'win32':
try:
import _winreg, win32gui, win32con
except ImportError:
winextensions = 0
class Params:
""" a crazy hack to convince distutils to please
install all of our files inside the package.
"""
_sitepackages = py.path.local(sysconfig.get_python_lib())
def __init__(self, pkgmod):
name = pkgmod.__name__
self._pkgdir = py.path.local(pkgmod.__file__).dirpath()
self._rootdir = self._pkgdir.dirpath()
self._pkgtarget = self._sitepackages.join(name)
self._datadict = {}
self.packages = []
self.scripts = []
self.hacktree()
self.data_files = self._datadict.items()
self.data_files.sort()
self.packages.sort()
self.scripts.sort()
def hacktree(self):
for p in self._pkgdir.visit(None, lambda x: x.basename != '.svn'):
if p.check(file=1):
if p.ext in ('.pyc', '.pyo'):
continue
if p.dirpath().basename == 'bin':
self.scripts.append(p.relto(self._rootdir))
self.adddatafile(p)
elif p.ext == '.py':
self.addpythonfile(p)
else:
self.adddatafile(p)
#else:
# if not p.listdir():
# self.adddatafile(p.ensure('dummy'))
def adddatafile(self, p):
if p.ext in ('.pyc', 'pyo'):
return
target = self._pkgtarget.join(p.dirpath().relto(self._pkgdir))
l = self._datadict.setdefault(str(target), [])
l.append(p.relto(self._rootdir))
def addpythonfile(self, p):
parts = p.parts()
for above in p.parts(reverse=True)[1:]:
if self._pkgdir.relto(above):
dottedname = p.dirpath().relto(self._rootdir).replace(p.sep, '.')
if dottedname not in self.packages:
self.packages.append(dottedname)
break
if not above.join('__init__.py').check():
self.adddatafile(p)
#print "warning, added data file", p
break
#if sys.platform != 'win32':
# scripts.remove('py/bin/pytest.cmd')
#else:
# scripts.remove('py/bin/py.test')
#
### helpers:
def checknonsvndir(p):
if p.basename != '.svn' and p.check(dir=1):
return True
def dump(params):
print "packages"
for x in params.packages:
print "package ", x
print
print "scripts"
for x in params.scripts:
print "script ", x
print
print "data files"
for x in params.data_files:
print "data file ", x
print
def addbindir2path():
if sys.platform != 'win32' or not winextensions:
return
# Add py/bin to PATH environment variable
bindir = os.path.join(sysconfig.get_python_lib(), "py", "bin", "win32")
reg = _winreg.ConnectRegistry(None, _winreg.HKEY_LOCAL_MACHINE)
key = r"SYSTEM\CurrentControlSet\Control\Session Manager\Environment"
path = get_registry_value(reg, key, "Path")
path += ";" + bindir
print "Setting PATH to:", path
set_registry_value(reg, key, "Path", path)
#print "Current PATH is:", get_registry_value(reg, key, "Path")
# Propagate changes throughout the system
win32gui.SendMessageTimeout(win32con.HWND_BROADCAST,
win32con.WM_SETTINGCHANGE, 0, "Environment",
win32con.SMTO_ABORTIFHUNG, 5000)
# Propagate changes to current command prompt
os.system("set PATH=%s" % path)
def get_registry_value(reg, key, value_name):
k = _winreg.OpenKey(reg, key)
value = _winreg.QueryValueEx(k, value_name)[0]
_winreg.CloseKey(k)
return value
def set_registry_value(reg, key, value_name, value):
k = _winreg.OpenKey(reg, key, 0, _winreg.KEY_WRITE)
value_type = _winreg.REG_SZ
# if we handle the Path value, then set its type to REG_EXPAND_SZ
# so that things like %SystemRoot% get automatically expanded by the
# command prompt
if value_name == "Path":
value_type = _winreg.REG_EXPAND_SZ
_winreg.SetValueEx(k, value_name, 0, value_type, value)
_winreg.CloseKey(k)
### end helpers
def setup(pkg, **kw):
""" invoke distutils on a given package.
"""
if 'install' in sys.argv[1:]:
print "precompiling greenlet module"
try:
x = py.magic.greenlet()
except (RuntimeError, ImportError):
print "could not precompile greenlet module, skipping"
params = Params(pkg)
#dump(params)
source = getattr(pkg, '__pkg__', pkg)
namelist = list(core.setup_keywords)
namelist.extend(['packages', 'scripts', 'data_files'])
for name in namelist:
for ns in (source, params):
if hasattr(ns, name):
kw[name] = getattr(ns, name)
break
#script_args = sys.argv[1:]
#if 'install' in script_args:
# script_args = ['--quiet'] + script_args
# #print "installing", py
#py.std.pprint.pprint(kw)
core.setup(**kw)
if 'install' in sys.argv[1:]:
addbindir2path()
x = params._rootdir.join('build')
if x.check():
print "removing", x
x.remove()
|