]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blob - bin/tahoe-script.template
Merge pull request #236 from daira/2725.timezone-test.0
[tahoe-lafs/tahoe-lafs.git] / bin / tahoe-script.template
1 #!/bin/false # You must specify a python interpreter.
2 import sys; assert sys.version_info < (3,), ur"Tahoe-LAFS does not run under Python 3. Please use a version of Python between 2.6 and 2.7.x inclusive."
3
4 import os, subprocess
5
6 where = os.path.realpath(sys.argv[0])
7 base = os.path.dirname(os.path.dirname(where))
8
9 if sys.platform == "win32":
10     perhaps_installed_tahoe = os.path.join(os.path.dirname(sys.executable), 'Scripts', 'tahoe.pyscript')
11 else:
12     perhaps_installed_tahoe = "/usr/bin/tahoe"
13
14 whoami = '''\
15 I am a "bin%stahoe" executable for the convenience of running Tahoe-LAFS
16 from its source distribution -- I work only when invoked as the "tahoe"
17 script that lives in the "bin" subdirectory of a Tahoe source code
18 distribution, and only if you have already run "python setup.py build".
19 ''' % (os.path.sep,)
20
21 # look for Tahoe.home .
22 homemarker = os.path.join(base, "Tahoe.home")
23 if not os.path.exists(homemarker):
24     print(whoami)
25     print('''\
26 I just tried to run and found that I am not living in such a directory, so I
27 am stopping now. To run Tahoe after it has been is installed, please execute
28 my brother, who gets installed into the appropriate place for executables
29 when you run "make install" (perhaps as "%s").
30 ''' % (perhaps_installed_tahoe,))
31     sys.exit(1)
32
33 # we've found our home. Put the tahoe support/lib etc. in our PYTHONPATH.
34 if sys.platform == "win32":
35     supportdir = os.path.join(base, "support", "Lib", "site-packages")
36 else:
37     supportdir = os.path.join(base, "support",
38                               "lib",
39                               "python%d.%d" % sys.version_info[:2],
40                               "site-packages")
41
42 # update PYTHONPATH so that child processes (like twistd) will use this too
43 pp = os.environ.get("PYTHONPATH")
44 if pp:
45     pp = os.pathsep.join([supportdir] + pp.split(os.pathsep))
46 else:
47     pp = supportdir
48 os.environ["PYTHONPATH"] = pp
49
50 # find commandline args and the location of the tahoe executable.
51 if sys.platform == "win32":
52     import re
53     from ctypes import WINFUNCTYPE, POINTER, byref, c_wchar_p, c_int, windll
54
55     GetCommandLineW = WINFUNCTYPE(c_wchar_p)(("GetCommandLineW", windll.kernel32))
56     CommandLineToArgvW = WINFUNCTYPE(POINTER(c_wchar_p), c_wchar_p, POINTER(c_int)) \
57                             (("CommandLineToArgvW", windll.shell32))
58
59     argc = c_int(0)
60     argv_unicode = CommandLineToArgvW(GetCommandLineW(), byref(argc))
61
62     # See src/allmydata/scripts/runner.py for the corresponding unmangler.
63     # Note that this doesn't escape \x7F. If it did, test_unicode_arguments_and_output
64     # in test_runner.py wouldn't work.
65     def mangle(s):
66         return str(re.sub(u'[^\\x20-\\x7F]', lambda m: u'\x7F%x;' % (ord(m.group(0)),), s))
67
68     argv = [mangle(argv_unicode[i]) for i in xrange(0, argc.value)]
69
70     # Take only the suffix with the same number of arguments as sys.argv.
71     # This accounts for anything that can cause initial arguments to be stripped,
72     # for example, the Python interpreter or any options passed to it, or runner
73     # scripts such as 'coverage run'. It works even if there are no such arguments,
74     # as in the case of a frozen executable created by bb-freeze or similar.
75
76     argv = argv[-len(sys.argv):]
77
78     # On Windows, the script is not directly executable and must be run via python.
79     prefix = [sys.executable]
80     script = os.path.join(base, "support", "Scripts", "tahoe.pyscript")
81     args = argv[1:]
82 else:
83     # On non-Windows, invoke the script directly, so that 'top' for example shows 'tahoe'.
84     prefix = []
85     script = os.path.join(base, "support", "bin", "tahoe")
86     args = sys.argv[1:]
87
88 # Support indirection via another "runner" script (e.g. coverage).
89 # For example: bin/tahoe @RUNNER RUNNER_ARGS @tahoe TAHOE_ARGS
90
91 if len(args) >= 1 and args[0].startswith('@'):
92     runner = args[0][1:]
93     if runner.endswith('.py') or runner.endswith('.pyscript'):
94         prefix = [sys.executable]
95     else:
96         prefix = []
97         if runner == "python":
98             runner = sys.executable
99
100     def _subst(a):
101         if a == '@tahoe': return script
102         return a
103     command = prefix + [runner] + map(_subst, args[1:])
104 else:
105     runner = script
106     command = prefix + [script] + args
107
108     if not os.path.exists(script):
109         print(whoami)
110         print('''\
111 I could not find the support script
112 "%s".
113
114 To run an installed version of Tahoe-LAFS, please execute the "tahoe"
115 script that is installed into the appropriate place for executables
116 when you run "python setup.py install" (perhaps as "%s").
117 ''' % (script, perhaps_installed_tahoe))
118         sys.exit(1)
119
120 try:
121     res = subprocess.call(command, env=os.environ)
122 except Exception as le:
123     print(whoami)
124     print('''\
125 I just tried to invoke "%s"
126 and got an exception.
127 ''' % (runner,))
128     raise
129 else:
130     sys.exit(res)