]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blob - bin/tahoe-script.template
edit docs for English usage, rename "Tahoe" to "Tahoe-LAFS" in docs/configuration...
[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     perhaps_installed_tahoe = os.path.join(os.path.dirname(sys.executable), 'Scripts', 'tahoe.pyscript')
10 else:
11     perhaps_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 ''' % (perhaps_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
69     # Skip option arguments to the Python interpreter.
70     while len(argv) > 0:
71         arg = argv[0]
72         if not arg.startswith(u"-") or arg == u"-":
73             break
74         argv = argv[1:]
75         if arg == u'-m':
76             # sys.argv[0] should really be the absolute path of the module source, but never mind
77             break
78         if arg == u'-c':
79             argv[0] = u'-c'
80             break
81
82     local_tahoe = "Scripts\\tahoe.pyscript"
83 else:
84     argv = sys.argv
85     local_tahoe = "bin/tahoe"
86
87 script = os.path.join(base, "support", local_tahoe)
88
89 try:
90     res = subprocess.call([sys.executable, script] + argv[1:], env=os.environ)
91 except (OSError, IOError), le:
92     if le.args[0] == errno.ENOENT:
93         print whoami
94         print '''\
95 I just tried to run and could not find my brother at
96 "%s". To run Tahoe when it is installed, please execute my
97 brother, who gets installed into the appropriate place for executables
98 when you run "make install" (perhaps as "%s").
99 ''' % (script, perhaps_installed_tahoe)
100         raise
101 except Exception, le:
102     print whoami
103     print '''\
104 I just tried to invoke my brother at "%s"
105 and got an exception.
106 ''' % (script,)
107     raise
108 else:
109     sys.exit(res)
110