From 779f5238197f94cb10f7e3166cae1976a28fb163 Mon Sep 17 00:00:00 2001 From: Avery Pennarun Date: Sun, 13 Feb 2011 01:50:50 -0800 Subject: [PATCH] _helpers.c: Remove ugly 'python' junk from argv[0] so 'ps' is prettier. 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 --- cmd/newliner-cmd.py | 1 + lib/bup/_helpers.c | 42 ++++++++++++++++++++++++++++++++++++++++++ lib/bup/helpers.py | 2 +- 3 files changed, 44 insertions(+), 1 deletion(-) diff --git a/cmd/newliner-cmd.py b/cmd/newliner-cmd.py index 2c86d5d..90f88d8 100755 --- a/cmd/newliner-cmd.py +++ b/cmd/newliner-cmd.py @@ -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 diff --git a/lib/bup/_helpers.c b/lib/bup/_helpers.c index 0af9411..4befda2 100644 --- a/lib/bup/_helpers.c +++ b/lib/bup/_helpers.c @@ -10,6 +10,47 @@ 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(); } diff --git a/lib/bup/helpers.py b/lib/bup/helpers.py index da27edb..e7c2f81 100644 --- a/lib/bup/helpers.py +++ b/lib/bup/helpers.py @@ -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. -- 2.39.2