]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blob - misc/build_helpers/run-with-pythonpath.py
4421192af2c6939f35d0d48b1b42697b6f504664
[tahoe-lafs/tahoe-lafs.git] / misc / build_helpers / run-with-pythonpath.py
1 # -*- python -*-
2 # you must invoke this with an explicit python, from the tree root
3
4 """Run an arbitrary command with a PYTHONPATH that will include the Tahoe
5 code, including dependent libraries. Run this like:
6
7  python misc/build_helpers/run-with-pythonpath.py python foo.py
8 """
9
10 import os, sys, subprocess
11
12 # figure out where support/lib/pythonX.X/site-packages is
13 # add it to os.environ["PYTHONPATH"]
14 # spawn the child process
15
16
17 def pylibdir(prefixdir):
18     pyver = "python%d.%d" % (sys.version_info[:2])
19     if sys.platform == "win32":
20         return os.path.join(prefixdir, "Lib", "site-packages")
21     else:
22         return os.path.join(prefixdir, "lib", pyver, "site-packages")
23
24 basedir = os.path.dirname(os.path.abspath(__file__))
25 supportlib = pylibdir(os.path.abspath("support"))
26
27 oldpp = os.environ.get("PYTHONPATH", "").split(os.pathsep)
28 if oldpp == [""]:
29     # grr silly split() behavior
30     oldpp = []
31 newpp = os.pathsep.join(oldpp + [supportlib,])
32 os.environ['PYTHONPATH'] = newpp
33
34 from twisted.python.procutils import which
35 cmd = sys.argv[1]
36 if cmd and cmd[0] not in "/~.":
37     cmds = which(cmd)
38     if not cmds:
39         print >>sys.stderr, "'%s' not found on PATH" % (cmd,)
40         sys.exit(-1)
41     cmd = cmds[0]
42
43 os.execve(cmd, sys.argv[1:], os.environ)