Add --socket argument to all clients

Makes it not needed to kill daemon for testing, but disables implicit
`powerline-config shell command` testing.
This commit is contained in:
ZyX 2014-08-23 14:55:59 +04:00
parent 77a7a26782
commit 0232d3215b
6 changed files with 68 additions and 28 deletions

View File

@ -60,20 +60,28 @@ int main(int argc, char *argv[]) {
int i; int i;
ptrdiff_t read_size; ptrdiff_t read_size;
struct sockaddr_un server; struct sockaddr_un server;
char address[ADDRESS_SIZE]; char address_buf[ADDRESS_SIZE];
const char eof[2] = "\0\0"; const char eof[2] = "\0\0";
char num_args[NUM_ARGS_SIZE]; char num_args[NUM_ARGS_SIZE];
char buf[BUF_SIZE]; char buf[BUF_SIZE];
char *newargv[NEW_ARGV_SIZE]; char *newargv[NEW_ARGV_SIZE];
char *wd = NULL; char *wd = NULL;
char **envp; char **envp;
const char *address;
if (argc < 2) { if (argc < 2) {
printf("Must provide at least one argument.\n"); printf("Must provide at least one argument.\n");
return EXIT_FAILURE; return EXIT_FAILURE;
} }
snprintf(address, ADDRESS_SIZE, ADDRESS_TEMPLATE, getuid()); if (argc > 3 && strcmp(argv[1], "--socket") == 0) {
address = argv[2];
argv += 2;
argc -= 2;
} else {
snprintf(address_buf, ADDRESS_SIZE, ADDRESS_TEMPLATE, getuid());
address = &(address_buf[0]);
}
sd = socket(AF_UNIX, SOCK_STREAM, 0); sd = socket(AF_UNIX, SOCK_STREAM, 0);
if (sd == -1) if (sd == -1)

View File

@ -15,10 +15,15 @@ if len(sys.argv) < 2:
platform = sys.platform.lower() platform = sys.platform.lower()
use_filesystem = 'darwin' in platform use_filesystem = 'darwin' in platform
# use_filesystem = True
del platform del platform
address = ('/tmp/powerline-ipc-%d' if use_filesystem else '\0powerline-ipc-%d') % os.getuid() if sys.argv[1] == '--socket':
address = sys.argv[2]
if not use_filesystem:
address = '\0' + address
del sys.argv[1:3]
else:
address = ('/tmp/powerline-ipc-%d' if use_filesystem else '\0powerline-ipc-%d') % os.getuid()
sock = socket.socket(family=socket.AF_UNIX) sock = socket.socket(family=socket.AF_UNIX)

View File

@ -1,6 +1,12 @@
#!/bin/sh #!/bin/sh
ADDRESS="powerline-ipc-${UID:-`id -u`}" if test "$1" = "--socket" ; then
shift
ADDRESS="$1"
shift
else
ADDRESS="powerline-ipc-${UID:-`id -u`}"
fi
# Warning: env -0 does not work in busybox. Consider switching to parsing # Warning: env -0 does not work in busybox. Consider switching to parsing
# `set` output in this case # `set` output in this case

View File

@ -66,6 +66,7 @@ def get_argparser(parser=None, *args, **kwargs):
p.add_argument('-t', '--theme_option', metavar='THEME.KEY.KEY=VALUE', action='append', help='Like above, but theme-specific. THEME should point to an existing and used theme to have any effect, but it is fine to use any theme here.') p.add_argument('-t', '--theme_option', metavar='THEME.KEY.KEY=VALUE', action='append', help='Like above, but theme-specific. THEME should point to an existing and used theme to have any effect, but it is fine to use any theme here.')
p.add_argument('-R', '--renderer_arg', metavar='KEY=VAL', action='append', help='Like above, but provides argument for renderer. Is supposed to be used only by shell bindings to provide various data like last_exit_code or last_pipe_status (they are not using --renderer_arg for historical resons: renderer_arg was added later).') p.add_argument('-R', '--renderer_arg', metavar='KEY=VAL', action='append', help='Like above, but provides argument for renderer. Is supposed to be used only by shell bindings to provide various data like last_exit_code or last_pipe_status (they are not using --renderer_arg for historical resons: renderer_arg was added later).')
p.add_argument('-p', '--config_path', action='append', metavar='PATH', help='Path to configuration directory. If it is present then configuration files will only be seeked in the provided path. May be provided multiple times to search in a list of directories.') p.add_argument('-p', '--config_path', action='append', metavar='PATH', help='Path to configuration directory. If it is present then configuration files will only be seeked in the provided path. May be provided multiple times to search in a list of directories.')
p.add_argument('--socket', metavar='ADDRESS', type=str, help='Socket address to use in daemon clients. Is always UNIX domain socket on linux and file socket on Mac OS X. Not used here, present only for compatibility with other powerline clients. This argument must always be the first one and be in a form `--socket ADDRESS\': no `=\' or short form allowed (in other powerline clients, not here).')
return p return p

View File

@ -17,20 +17,13 @@ from io import StringIO
from powerline.shell import get_argparser, finish_args, ShellPowerline, write_output from powerline.shell import get_argparser, finish_args, ShellPowerline, write_output
from powerline.lib.monotonic import monotonic from powerline.lib.monotonic import monotonic
is_daemon = False is_daemon = False
platform = sys.platform.lower() platform = sys.platform.lower()
use_filesystem = 'darwin' in platform use_filesystem = 'darwin' in platform
# use_filesystem = True
del platform
if use_filesystem: address = None
address = '/tmp/powerline-ipc-%d' pidfile = None
pidfile = address + '.pid'
else:
# Use the abstract namespace for sockets rather than the filesystem
# (Available only in linux)
address = '\0powerline-ipc-%d'
address = address % os.getuid()
class NonInteractiveArgParser(ArgumentParser): class NonInteractiveArgParser(ArgumentParser):
@ -334,8 +327,11 @@ def lockpidfile():
import fcntl import fcntl
import atexit import atexit
import stat import stat
fd = os.open(pidfile, os.O_WRONLY | os.O_CREAT, fd = os.open(
stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IROTH) pidfile,
os.O_WRONLY | os.O_CREAT,
stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IROTH
)
try: try:
fcntl.lockf(fd, fcntl.LOCK_EX | fcntl.LOCK_NB) fcntl.lockf(fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
except EnvironmentError: except EnvironmentError:
@ -352,15 +348,34 @@ def lockpidfile():
def main(): def main():
p = ArgumentParser(description= global address
'Daemon to improve the performance of powerline') global pidfile
p = ArgumentParser(description='Daemon to improve the performance of powerline')
p.add_argument('--quiet', '-q', action='store_true', help='Without other options: do not complain about already running powerline-daemon instance. Will still exit with 1. With `--kill\' and `--replace\': do not show any messages. With `--foreground\': ignored. Does not silence exceptions in any case.') p.add_argument('--quiet', '-q', action='store_true', help='Without other options: do not complain about already running powerline-daemon instance. Will still exit with 1. With `--kill\' and `--replace\': do not show any messages. With `--foreground\': ignored. Does not silence exceptions in any case.')
p.add_argument('--socket', '-s', help='Specify socket which will be used for connecting to daemon.')
a = p.add_mutually_exclusive_group().add_argument a = p.add_mutually_exclusive_group().add_argument
a('--kill', '-k', action='store_true', help='Kill an already running instance') a('--kill', '-k', action='store_true', help='Kill an already running instance')
a('--foreground', '-f', action='store_true', help='Run in the foreground (dont daemonize)') a('--foreground', '-f', action='store_true', help='Run in the foreground (dont daemonize)')
a('--replace', '-r', action='store_true', help='Replace an already running instance') a('--replace', '-r', action='store_true', help='Replace an already running instance')
args = p.parse_args() args = p.parse_args()
if args.socket:
address = args.socket
if not use_filesystem:
address = '\0' + address
else:
if use_filesystem:
address = '/tmp/powerline-ipc-%d'
else:
# Use the abstract namespace for sockets rather than the filesystem
# (Available only in linux)
address = '\0powerline-ipc-%d'
address = address % os.getuid()
if use_filesystem:
pidfile = address + '.pid'
if args.kill: if args.kill:
if kill_daemon(): if kill_daemon():
if not args.quiet: if not args.quiet:

View File

@ -16,6 +16,7 @@ ONLY_SHELL: execute only tests for given shell
ONLY_TEST_TYPE: execute only "daemon" or "nodaemon" tests ONLY_TEST_TYPE: execute only "daemon" or "nodaemon" tests
COMMAND_PATTERN: use only commands that match given pattern for testing COMMAND_PATTERN: use only commands that match given pattern for testing
EOF EOF
exit 0
fi fi
check_screen_log() { check_screen_log() {
@ -213,21 +214,25 @@ done
unset ENV unset ENV
if test -z "${ONLY_SHELL}" || test "x${ONLY_SHELL%sh}" != "x${ONLY_SHELL}" || test "x${ONLY_SHELL}" = xbusybox ; then export ADDRESS="powerline-ipc-test-$RANDOM"
powerline-daemon -k || true export PYTHON
sleep 1s echo "Powerline address: $ADDRESS"
if test -z "${ONLY_SHELL}" || test "x${ONLY_SHELL%sh}" != "x${ONLY_SHELL}" || test "x${ONLY_SHELL}" = xbusybox ; then
scripts/powerline-config shell command scripts/powerline-config shell command
for TEST_TYPE in "daemon" "nodaemon" ; do for TEST_TYPE in "daemon" "nodaemon" ; do
if test $TEST_TYPE = daemon ; then if test $TEST_TYPE = daemon ; then
sh -c 'echo $$ > tests/shell/daemon_pid; $PYTHON ./scripts/powerline-daemon -f &>tests/shell/daemon_log' & sh -c '
echo $$ > tests/shell/daemon_pid
$PYTHON ./scripts/powerline-daemon -s$ADDRESS -f &>tests/shell/daemon_log
' &
fi fi
if test "x$ONLY_TEST_TYPE" != "x" && test "x$ONLY_TEST_TYPE" != "x$TEST_TYPE" ; then if test "x$ONLY_TEST_TYPE" != "x" && test "x$ONLY_TEST_TYPE" != "x$TEST_TYPE" ; then
continue continue
fi fi
echo "> Testing $TEST_TYPE" echo "> Testing $TEST_TYPE"
for POWERLINE_COMMAND in "" \ for POWERLINE_COMMAND in \
$PWD/scripts/powerline \ $PWD/scripts/powerline \
$PWD/scripts/powerline-render \ $PWD/scripts/powerline-render \
"$PYTHON $PWD/client/powerline.py" \ "$PYTHON $PWD/client/powerline.py" \
@ -238,7 +243,6 @@ if test -z "${ONLY_SHELL}" || test "x${ONLY_SHELL%sh}" != "x${ONLY_SHELL}" || te
*powerline-render) TEST_CLIENT=render ;; *powerline-render) TEST_CLIENT=render ;;
*powerline.py) TEST_CLIENT=python ;; *powerline.py) TEST_CLIENT=python ;;
*powerline.sh) TEST_CLIENT=shell ;; *powerline.sh) TEST_CLIENT=shell ;;
"") TEST_CLIENT=auto ;;
esac esac
if test "$TEST_CLIENT" = "C" && ! test -x scripts/powerline ; then if test "$TEST_CLIENT" = "C" && ! test -x scripts/powerline ; then
if which powerline >/dev/null ; then if which powerline >/dev/null ; then
@ -258,6 +262,7 @@ if test -z "${ONLY_SHELL}" || test "x${ONLY_SHELL%sh}" != "x${ONLY_SHELL}" || te
then then
continue continue
fi fi
POWERLINE_COMMAND="$POWERLINE_COMMAND --socket $ADDRESS"
export POWERLINE_COMMAND export POWERLINE_COMMAND
echo ">> powerline command is ${POWERLINE_COMMAND:-empty}" echo ">> powerline command is ${POWERLINE_COMMAND:-empty}"
for TEST_COMMAND in \ for TEST_COMMAND in \
@ -286,7 +291,7 @@ if test -z "${ONLY_SHELL}" || test "x${ONLY_SHELL%sh}" != "x${ONLY_SHELL}" || te
done done
done done
if test $TEST_TYPE = daemon ; then if test $TEST_TYPE = daemon ; then
$PYTHON ./scripts/powerline-daemon -k $PYTHON ./scripts/powerline-daemon -s$ADDRESS -k
wait $(cat tests/shell/daemon_pid) wait $(cat tests/shell/daemon_pid)
if ! test -z "$(cat tests/shell/daemon_log)" ; then if ! test -z "$(cat tests/shell/daemon_log)" ; then
echo '____________________________________________________________' echo '____________________________________________________________'
@ -299,12 +304,12 @@ if test -z "${ONLY_SHELL}" || test "x${ONLY_SHELL%sh}" != "x${ONLY_SHELL}" || te
done done
fi fi
if ! $PYTHON scripts/powerline-daemon &> tests/shell/daemon_log_2 ; then if ! $PYTHON scripts/powerline-daemon -s$ADDRESS &> tests/shell/daemon_log_2 ; then
echo "Daemon exited with status $?" echo "Daemon exited with status $?"
FAILED=1 FAILED=1
else else
sleep 1 sleep 1
$PYTHON scripts/powerline-daemon -k $PYTHON scripts/powerline-daemon -s$ADDRESS -k
fi fi
if ! test -z "$(cat tests/shell/daemon_log_2)" ; then if ! test -z "$(cat tests/shell/daemon_log_2)" ; then