From b3da31456080a2f1aed07ce34671873c3ccea9ae Mon Sep 17 00:00:00 2001 From: fonfon Date: Thu, 22 Jan 2015 15:56:24 +0000 Subject: [PATCH] use util.service_is_running for tor also --- actions/pagekite | 4 ++-- actions/tor | 17 +++-------------- actions/util.py | 5 +++-- 3 files changed, 8 insertions(+), 18 deletions(-) diff --git a/actions/pagekite b/actions/pagekite index ff5a33546..33c11647c 100755 --- a/actions/pagekite +++ b/actions/pagekite @@ -28,7 +28,7 @@ import augeas import os import subprocess -from util import is_running +import util from pagekite_util import SERVICE_PARAMS, convert_to_service, \ convert_service_to_string, get_augeas_servicefile_path, CONF_PATH @@ -92,7 +92,7 @@ def _service(action): def subcommand_is_running(_): """Print whether pagekite is enabled (yes or no)""" - print 'yes' if is_running('pagekite') else 'no' + print 'yes' if util.service_is_running('pagekite') else 'no' def subcommand_start_and_enable(_): diff --git a/actions/tor b/actions/tor index f72c7fd5e..5a546fd4b 100755 --- a/actions/tor +++ b/actions/tor @@ -25,6 +25,8 @@ import argparse import os import subprocess +import util + SERVICE_CONFIG = '/etc/default/tor' TOR_CONFIG = '/etc/tor/torrc' @@ -57,20 +59,7 @@ def parse_arguments(): def subcommand_is_running(_): """Get whether Tor is running""" - try: - output = subprocess.check_output(['service', 'tor', 'status']) - except subprocess.CalledProcessError: - # If Tor is not running we get a status code != 0 and a - # CalledProcessError - print('no') - else: - running = False - for line in output.decode().split('\n'): - if 'Active' in line and 'running' in line: - running = True - break - - print('yes' if running else 'no') + print('yes' if util.service_is_running('tor') else 'no') def subcommand_start(_): diff --git a/actions/util.py b/actions/util.py index d6a262dd5..1f097f09d 100644 --- a/actions/util.py +++ b/actions/util.py @@ -23,7 +23,8 @@ Python action utility functions import subprocess -def is_running(servicename): + +def service_is_running(servicename): """Evaluates whether a service is currently running. Returns boolean""" try: output = subprocess.check_output(['service', servicename, 'status']) @@ -32,7 +33,7 @@ def is_running(servicename): # thus a CalledProcessError return False else: - running = False # default value + running = False # default value for line in output.decode('utf-8').split('\n'): if 'Active' in line and 'running' in line: running = True