]> arthur.barton.de Git - bup.git/commitdiff
_helpers.c: Remove ugly 'python' junk from argv[0] so 'ps' is prettier.
authorAvery Pennarun <apenwarr@gmail.com>
Sun, 13 Feb 2011 09:50:50 +0000 (01:50 -0800)
committerAvery Pennarun <apenwarr@gmail.com>
Sun, 13 Feb 2011 13:02:16 +0000 (05:02 -0800)
Okay, this is pretty gross.  But the 'ps' output was looking ugly, and
now it doesn't.  We remove the 'python' interpreter string and the expanded
pathname of the command being run, so it now shows as (eg.) "bup-join" instead
of "python /blah/blah/blah/cmd/bup-join".

Signed-off-by: Avery Pennarun <apenwarr@gmail.com>
cmd/newliner-cmd.py
lib/bup/_helpers.c
lib/bup/helpers.py

index 2c86d5dfb255f85ddf34be21fe0e6f7919aedaf4..90f88d8629845b92c86f6f4f9d0b30d463a75e78 100755 (executable)
@@ -1,6 +1,7 @@
 #!/usr/bin/env python
 import sys, os, re
 from bup import options
+from bup import _helpers   # fixes up sys.argv on import
 
 optspec = """
 bup newliner
index 0af94113bd87d0116576f165b93e533b702c067e..4befda24a3917cf3a26bb7ba1d06fd981dfcab70 100644 (file)
 
 static int istty = 0;
 
+// For some reason this isn't declared in Python.h
+extern void Py_GetArgcArgv(int *argc, char ***argv);
+
+static void unpythonize_argv(void)
+{
+    int argc, i;
+    char **argv, *arge;
+    
+    Py_GetArgcArgv(&argc, &argv);
+    
+    for (i = 0; i < argc-1; i++)
+    {
+       if (argv[i] + strlen(argv[i]) + 1 != argv[i+1])
+       {
+           // The argv block doesn't work the way we expected; it's unsafe
+           // to mess with it.
+           return;
+       }
+    }
+    
+    arge = argv[argc-1] + strlen(argv[argc-1]) + 1;
+    
+    if (strstr(argv[0], "python") && argv[1] == argv[0] + strlen(argv[0]) + 1)
+    {
+       char *p;
+       size_t len, diff;
+       p = strrchr(argv[1], '/');
+       if (p)
+       {
+           p++;
+           diff = p - argv[0];
+           len = arge - p;
+           memmove(argv[0], p, len);
+           memset(arge - diff, 0, diff);
+           for (i = 0; i < argc; i++)
+               argv[i] = argv[i+1] ? argv[i+1]-diff : NULL;
+       }
+    }
+}
+
+
 static PyObject *selftest(PyObject *self, PyObject *args)
 {
     if (!PyArg_ParseTuple(args, ""))
@@ -526,4 +567,5 @@ PyMODINIT_FUNC init_helpers(void)
 {
     Py_InitModule("_helpers", faster_methods);
     istty = isatty(2) || getenv("BUP_FORCE_TTY");
+    unpythonize_argv();
 }
index da27edb44b29330e6a9998e7862a9cb1747ba248..e7c2f813bd4dae89465d950eecc3042a6ef2b238 100644 (file)
@@ -2,7 +2,7 @@
 
 import sys, os, pwd, subprocess, errno, socket, select, mmap, stat, re, struct
 import heapq, operator
-from bup import _version
+from bup import _version, _helpers
 
 # This function should really be in helpers, not in bup.options.  But we
 # want options.py to be standalone so people can include it in other projects.