]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blob - bin/tahoe-script.template
48bdbf181ded70a9f24a54b07ffce9d2ed14ea3f
[tahoe-lafs/tahoe-lafs.git] / bin / tahoe-script.template
1 #!/bin/false # You must specify a python interpreter.
2
3 import errno, sys, os, subprocess
4
5 where = os.path.realpath(sys.argv[0])
6 base = os.path.dirname(os.path.dirname(where))
7
8 if sys.platform == "win32":
9     installed_tahoe = os.path.join(os.path.dirname(sys.executable), 'Scripts', 'tahoe.pyscript')
10 else:
11     installed_tahoe = "/usr/bin/tahoe"
12
13 whoami = '''\
14 I am a "bin%stahoe" executable who is only for the convenience of running
15 Tahoe from its source distribution -- I work only when invoked as the "tahoe"
16 script that lives in the "bin/" subdirectory of a Tahoe source code
17 distribution, and only if you have already run "make".
18 ''' % (os.path.sep,)
19
20 # look for Tahoe.home .
21 homemarker = os.path.join(base, "Tahoe.home")
22 if not os.path.exists(homemarker):
23     print whoami
24     print '''\
25 I just tried to run and found that I am not living in such a directory, so I
26 am stopping now. To run Tahoe after it has been is installed, please execute
27 my brother, who gets installed into the appropriate place for executables
28 when you run "make install" (perhaps as "%s").
29 ''' % (installed_tahoe,)
30     sys.exit(1)
31
32 # we've found our home. Put the tahoe support/lib etc. in our PYTHONPATH.
33 if sys.platform == "win32":
34     supportdir = os.path.join(base, "support", "Lib", "site-packages")
35 else:
36     supportdir = os.path.join(base, "support",
37                               "lib",
38                               "python%d.%d" % sys.version_info[:2],
39                               "site-packages")
40
41 # update PYTHONPATH so that child processes (like twistd) will use this too
42 pp = os.environ.get("PYTHONPATH")
43 if pp:
44     pp = os.pathsep.join([supportdir] + pp.split(os.pathsep))
45 else:
46     pp = supportdir
47 os.environ["PYTHONPATH"] = pp
48
49 # find commandline args and the location of the tahoe executable.
50 if sys.platform == "win32":
51     import re
52     from ctypes import WINFUNCTYPE, POINTER, byref, c_wchar_p, c_int, windll
53
54     GetCommandLineW = WINFUNCTYPE(c_wchar_p)(("GetCommandLineW", windll.kernel32))
55     CommandLineToArgvW = WINFUNCTYPE(POINTER(c_wchar_p), c_wchar_p, POINTER(c_int)) \
56                             (("CommandLineToArgvW", windll.shell32))
57
58     argc = c_int(0)
59     argv_unicode = CommandLineToArgvW(GetCommandLineW(), byref(argc))
60
61     # See src/allmydata/scripts/runner.py for the corresponding unmangler.
62     # Note that this doesn't escape \x7F. If it did, test_unicode_arguments_and_output
63     # in test_runner.py wouldn't work.
64     def mangle(s):
65         return str(re.sub(ur'[^\x20-\x7F]', lambda m: u'\x7F%x;' % (ord(m.group(0)),), s))
66
67     argv = [mangle(argv_unicode[i]) for i in xrange(1, argc.value)]
68     local_tahoe = "Scripts\\tahoe.pyscript"
69 else:
70     argv = sys.argv
71     local_tahoe = "bin/tahoe"
72
73 script = os.path.join(base, "support", local_tahoe)
74
75 try:
76     res = subprocess.call([sys.executable, script] + argv[1:], env=os.environ)
77 except (OSError, IOError), le:
78     if le.args[0] == errno.ENOENT:
79         print whoami
80         print '''\
81 I just tried to run and could not find my brother at
82 "%s". To run Tahoe when it is installed, please execute my
83 brother, who gets installed into the appropriate place for executables
84 when you run "make install" (perhaps as "%s").
85 ''' % (script, installed_tahoe)
86         raise
87 except Exception, le:
88     print whoami
89     print '''\
90 I just tried to invoke my brother at "%s"
91 and got an exception.
92 ''' % (script,)
93     raise
94 else:
95     sys.exit(res)
96