]> git.michaelhowe.org Git - packages/b/bup.git/commitdiff
Handle $BUP_MAIN_EXE more carefully.
authorAvery Pennarun <apenwarr@gmail.com>
Wed, 26 Jan 2011 03:35:21 +0000 (19:35 -0800)
committerAvery Pennarun <apenwarr@gmail.com>
Wed, 26 Jan 2011 03:45:17 +0000 (19:45 -0800)
In some cases, we might have been using sys.argv[0] *after* doing a chdir(),
which doesn't work reliably since argv[0] might be a relative path.

Signed-off-by: Avery Pennarun <apenwarr@gmail.com>
cmd/help-cmd.py
cmd/on-cmd.py
lib/bup/git.py
lib/bup/path.py [new file with mode: 0644]
lib/bup/ssh.py

index c507a3775cf45ea31f4d953405db8742a84d345e..796ddbfabcae0d94b3b0c43a5bb49c1adfebd790 100755 (executable)
@@ -1,6 +1,6 @@
 #!/usr/bin/env python
 import sys, os, glob
-from bup import options
+from bup import options, path
 
 optspec = """
 bup help <command>
@@ -13,9 +13,8 @@ if len(extra) == 0:
     os.execvp(os.environ['BUP_MAIN_EXE'], ['bup'])
 elif len(extra) == 1:
     docname = (extra[0]=='bup' and 'bup' or ('bup-%s' % extra[0]))
-    exe = sys.argv[0]
-    (exepath, exefile) = os.path.split(exe)
-    manpath = os.path.join(exepath, '../Documentation/' + docname + '.[1-9]')
+    manpath = os.path.join(path.exedir(),
+                           '../Documentation/' + docname + '.[1-9]')
     g = glob.glob(manpath)
     if g:
         os.execvp('man', ['man', '-l', g[0]])
index e9a4a23887580b830d2600f93a8df03fec306ffd..cebff1aab52ee13aab85cf86e03b7486fa66ef04 100755 (executable)
@@ -1,6 +1,6 @@
 #!/usr/bin/env python
 import sys, os, struct, getopt, subprocess, signal
-from bup import options, ssh
+from bup import options, ssh, path
 from bup.helpers import *
 
 optspec = """
@@ -41,9 +41,8 @@ try:
     p.stdin.write(struct.pack('!I', len(argvs)) + argvs)
     p.stdin.flush()
 
-    main_exe = os.environ.get('BUP_MAIN_EXE') or sys.argv[0]
-    sp = subprocess.Popen([main_exe, 'server'], stdin=p.stdout, stdout=p.stdin)
-
+    sp = subprocess.Popen([path.exe(), 'server'],
+                          stdin=p.stdout, stdout=p.stdin)
     p.stdin.close()
     p.stdout.close()
 
index 7781a515842de7ed8ea5564b4bfd307693f994fe..fc48e51ee8eee4122aa3ae175bc433827de188e5 100644 (file)
@@ -4,7 +4,7 @@ interact with the Git data structures.
 """
 import os, sys, zlib, time, subprocess, struct, stat, re, tempfile, heapq
 from bup.helpers import *
-from bup import _helpers
+from bup import _helpers, path
 
 MIDX_VERSION = 2
 
@@ -39,8 +39,7 @@ def repo(sub = ''):
 
 
 def auto_midx(objdir):
-    main_exe = os.environ.get('BUP_MAIN_EXE') or sys.argv[0]
-    args = [main_exe, 'midx', '--auto', '--dir', objdir]
+    args = [path.exe(), 'midx', '--auto', '--dir', objdir]
     try:
         rv = subprocess.call(args, stdout=open('/dev/null', 'w'))
     except OSError, e:
diff --git a/lib/bup/path.py b/lib/bup/path.py
new file mode 100644 (file)
index 0000000..820c78b
--- /dev/null
@@ -0,0 +1,16 @@
+"""This is a separate module so we can cleanly getcwd() before anyone
+   does chdir().
+"""
+import sys, os
+
+startdir = os.getcwd()
+
+def exe():
+    return (os.environ.get('BUP_MAIN_EXE') or
+            os.path.join(startdir, sys.argv[0]))
+
+def exedir():
+    return os.path.split(exe())[0]
+
+def exefile():
+    return os.path.split(exe())[1]
index f91e16dcb34bc9ce1d755704b2c1a8f96db439b3..344355aa1f2b537140b36367d5d4b4d4e56021ab 100644 (file)
@@ -1,20 +1,14 @@
 """SSH connection.
 Connect to a remote host via SSH and execute a command on the host.
 """
-import os
-import sys
-import re
-import subprocess
-
-from bup import helpers
+import sys, os, re, subprocess
+from bup import helpers, path
 
 
 def connect(rhost, port, subcmd):
     """Connect to 'rhost' and execute the bup subcommand 'subcmd' on it."""
     assert(not re.search(r'[^\w-]', subcmd))
-    main_exe = os.environ.get('BUP_MAIN_EXE') or sys.argv[0]
-    nicedir = os.path.split(os.path.abspath(main_exe))[0]
-    nicedir = re.sub(r':', "_", nicedir)
+    nicedir = re.sub(r':', "_", path.exedir())
     if rhost == '-':
         rhost = None
     if not rhost: