commit 16b62b790a33022c27d408e897c0a6bf23cf2e36 Author: Thomas Woerner Date: Mon Jul 25 19:07:30 2016 +0200 firewall[-offline]-cmd, firewallctl, firewall.command: Use sys.{stdout,stderr} Using sys.stdout and sys.stderr instead of print to have error output to stderr and to calm down broken pipe output. diff --git a/src/firewall-cmd b/src/firewall-cmd index d408c09..df0747d 100755 --- a/src/firewall-cmd +++ b/src/firewall-cmd @@ -44,7 +44,7 @@ from firewall.core.io.icmptype import icmptype_reader from firewall.command import FirewallCommand def __usage(): - print (""" + sys.stdout.write(""" Usage: firewall-cmd [OPTIONS...] General Options @@ -401,6 +401,7 @@ Panic Options --panic-on Enable panic mode --panic-off Disable panic mode --query-panic Query whether panic mode is enabled + """) def try_set_zone_of_interface(_zone, interface): @@ -1811,7 +1812,7 @@ elif a.direct: cmd.fail("usage: --direct --passthrough { ipv4 | ipv6 | eb } ") msg = fw.passthrough(cmd.check_ipv(a.passthrough[0]), splitArgs(a.passthrough[1])) if msg: - print(msg) + sys.stdout.write(msg + "\n") elif a.add_passthrough: if len(a.add_passthrough) < 2: diff --git a/src/firewall-offline-cmd b/src/firewall-offline-cmd index a296441..4a9432f 100755 --- a/src/firewall-offline-cmd +++ b/src/firewall-offline-cmd @@ -43,13 +43,13 @@ from firewall.command import FirewallCommand # check for root user if os.getuid() != 0: - print("You need to be root to run %s." % sys.argv[0]) + sys.stderr.write("You need to be root to run %s.\n" % sys.argv[0]) sys.exit(-1) SYSTEM_CONFIG_FIREWALL = config.SYSCONFIGDIR + '/system-config-firewall' def __usage(): - print (""" + sys.stdout.write(""" Usage: firewall-offline-cmd [OPTIONS...] If no options are given, configuration from '%s' will be migrated. @@ -418,7 +418,7 @@ Polkit Options --policy-server Change Polkit actions to 'server' (more restricted) --policy-desktop Change Polkit actions to 'desktop' (less restricted) - """ % (SYSTEM_CONFIG_FIREWALL, config.SYSCONFIGDIR)) +""" % (SYSTEM_CONFIG_FIREWALL, config.SYSCONFIGDIR)) def parse_port_lokkit(value): try: diff --git a/src/firewall/command.py b/src/firewall/command.py index 58618b0..74ee8b2 100644 --- a/src/firewall/command.py +++ b/src/firewall/command.py @@ -55,19 +55,23 @@ class FirewallCommand(object): def print_msg(self, msg=None): if msg is not None and not self.quiet: - print(msg) + sys.stdout.write(msg + "\n") + + def print_error_msg(self, msg=None): + if msg is not None and not self.quiet: + sys.stderr.write(msg + "\n") def print_warning(self, msg=None): FAIL = '\033[91m' END = '\033[00m' - self.print_msg(FAIL + msg + END) + self.print_error_msg(FAIL + msg + END) def print_and_exit(self, msg=None, exit_code=0): #OK = '\033[92m' FAIL = '\033[91m' END = '\033[00m' if exit_code > 1: - self.print_msg(FAIL + msg + END) + self.print_error_msg(FAIL + msg + END) else: self.print_msg(msg) #self.print_msg(OK + msg + END) @@ -77,8 +81,8 @@ class FirewallCommand(object): self.print_and_exit(msg, 2) def print_if_verbose(self, msg=None): - if msg and self.verbose: - print(msg) + if msg is not None and self.verbose: + sys.stdout.write(msg + "\n") def __cmd_sequence(self, cmd_type, option, action_method, query_method, parse_method, message, start_args=None, end_args=None, diff --git a/src/firewallctl b/src/firewallctl index 249736c..20cccac 100755 --- a/src/firewallctl +++ b/src/firewallctl @@ -3365,7 +3365,7 @@ elif args.parser == "direct": msg = fw.passthrough(cmd.check_ipv(args.passthrough[0]), splitArgs(args.passthrough[1])) if msg: - print(msg) + sys.stdout.write(msg + "\n") elif args.action == "get": commit 560a0e4e9f66da97ceeb69f295a989251e86a342 Author: Thomas Woerner Date: Thu Jul 28 13:28:22 2016 +0200 tests/firewall-[offline-]cmd_test.sh: Hide errors and warnings This change is needed because of the use of stderr for warnings and errors in the command line tools. diff --git a/src/tests/firewall-cmd_test.sh b/src/tests/firewall-cmd_test.sh index f9348ca..dd7c4f8 100755 --- a/src/tests/firewall-cmd_test.sh +++ b/src/tests/firewall-cmd_test.sh @@ -10,7 +10,7 @@ readonly RESTORE='\033[0m' assert_good() { local args="${1}" - ${path}firewall-cmd ${args} > /dev/null + ${path}firewall-cmd ${args} > /dev/null 2>&1 if [[ "$?" -eq 0 ]]; then echo "${args} ... OK" else @@ -23,7 +23,7 @@ assert_good_notempty() { local args="${1}" local ret - ret=$(${path}firewall-cmd ${args}) > /dev/null + ret=$(${path}firewall-cmd ${args}) > /dev/null 2>&1 if [[ ( "$?" -eq 0 ) && ( -n "${ret}" ) ]]; then echo "${args} ... OK" else @@ -36,7 +36,7 @@ assert_good_empty() { local args="${1}" local ret - ret=$(${path}firewall-cmd ${args}) > /dev/null + ret=$(${path}firewall-cmd ${args}) > /dev/null 2>&1 if [[ ( "$?" -eq 0 ) && ( -z "${ret}" ) ]]; then echo "${args} ... OK" else @@ -50,7 +50,7 @@ assert_good_equals() { local value="${2}" local ret - ret=$(${path}firewall-cmd ${args}) > /dev/null + ret=$(${path}firewall-cmd ${args}) > /dev/null 2>&1 if [[ ( "$?" -eq 0 ) && ( "${ret}" = "${value}" ) ]]; then echo "${args} ... OK" else @@ -64,7 +64,7 @@ assert_good_contains() { local value="${2}" local ret - ret=$(${path}firewall-cmd ${args}) > /dev/null + ret=$(${path}firewall-cmd ${args}) > /dev/null 2>&1 if [[ ( "$?" -eq 0 ) && ( "${ret}" = *${value}* ) ]]; then echo "${args} ... OK" else @@ -90,7 +90,7 @@ assert_bad_contains() { local value="${2}" local ret - ret=$(${path}firewall-cmd ${args}) > /dev/null + ret=$(${path}firewall-cmd ${args}) > /dev/null 2>&1 if [[ ( "$?" -ne 0 ) || ( "${ret}" = *${value}* ) ]]; then ((failures++)) echo -e "${args} ... ${RED}${failures}. FAILED (non-zero exit status or '${ret}' does contain '${value}')${RESTORE}" @@ -115,7 +115,7 @@ assert_rich_good() { command="--query-rich-rule" fi - ${path}firewall-cmd ${permanent} ${command} "${args}" > /dev/null + ${path}firewall-cmd ${permanent} ${command} "${args}" > /dev/null 2>&1 if [[ "$?" -eq 0 ]]; then echo ${permanent} ${command} "${args} ... OK" else @@ -139,7 +139,7 @@ assert_rich_bad() { command="--query-rich-rule" fi - ${path}firewall-cmd ${permanent} ${command} "${args}" > /dev/null + ${path}firewall-cmd ${permanent} ${command} "${args}" > /dev/null 2>&1 if [[ "$?" -ne 0 ]]; then echo ${permanent} ${command} "${args} ... OK" else diff --git a/src/tests/firewall-offline-cmd_test.sh b/src/tests/firewall-offline-cmd_test.sh index 54fad3d..344b825 100755 --- a/src/tests/firewall-offline-cmd_test.sh +++ b/src/tests/firewall-offline-cmd_test.sh @@ -22,7 +22,7 @@ assert_cmd_good() { assert_good() { local args="${1}" - ${path}firewall-offline-cmd ${args} > /dev/null + ${path}firewall-offline-cmd ${args} > /dev/null 2>&1 if [[ "$?" -eq 0 ]]; then echo "${args} ... OK" else @@ -35,7 +35,7 @@ assert_good_notempty() { local args="${1}" local ret - ret=$(${path}firewall-offline-cmd ${args}) > /dev/null + ret=$(${path}firewall-offline-cmd ${args}) > /dev/null 2>&1 if [[ ( "$?" -eq 0 ) && ( -n "${ret}" ) ]]; then echo "${args} ... OK" else @@ -48,7 +48,7 @@ assert_good_empty() { local args="${1}" local ret - ret=$(${path}firewall-offline-cmd ${args}) > /dev/null + ret=$(${path}firewall-offline-cmd ${args}) > /dev/null 2>&1 if [[ ( "$?" -eq 0 ) && ( -z "${ret}" ) ]]; then echo "${args} ... OK" else @@ -62,7 +62,7 @@ assert_good_equals() { local value="${2}" local ret - ret=$(${path}firewall-offline-cmd ${args}) > /dev/null + ret=$(${path}firewall-offline-cmd ${args}) > /dev/null 2>&1 if [[ ( "$?" -eq 0 ) && ( "${ret}" = "${value}" ) ]]; then echo "${args} ... OK" else @@ -76,7 +76,7 @@ assert_good_contains() { local value="${2}" local ret - ret=$(${path}firewall-offline-cmd ${args}) > /dev/null + ret=$(${path}firewall-offline-cmd ${args}) > /dev/null 2>&1 if [[ ( "$?" -eq 0 ) && ( "${ret}" = *${value}* ) ]]; then echo "${args} ... OK" else @@ -88,7 +88,7 @@ assert_good_contains() { assert_bad() { local args="${1}" - ${path}firewall-offline-cmd ${args} 1> /dev/null 2>&1 + ${path}firewall-offline-cmd ${args} 1> /dev/null 2>&1 2>&1 if [[ "$?" -ne 0 ]]; then echo "${args} ... OK" else @@ -102,7 +102,7 @@ assert_bad_contains() { local value="${2}" local ret - ret=$(${path}firewall-offline-cmd ${args}) > /dev/null + ret=$(${path}firewall-offline-cmd ${args}) > /dev/null 2>&1 if [[ ( "$?" -ne 0 ) || ( "${ret}" = *${value}* ) ]]; then ((failures++)) echo -e "${args} ... ${RED}${failures}. FAILED (non-zero exit status or '${ret}' does contain '${value}')${RESTORE}" @@ -125,7 +125,7 @@ assert_rich_good() { command="--query-rich-rule" fi - ${path}firewall-offline-cmd ${command} "${args}" > /dev/null + ${path}firewall-offline-cmd ${command} "${args}" > /dev/null 2>&1 if [[ "$?" -eq 0 ]]; then echo ${command} "${args} ... OK" else @@ -147,7 +147,7 @@ assert_rich_bad() { command="--query-rich-rule" fi - ${path}firewall-offline-cmd ${command} "${args}" > /dev/null + ${path}firewall-offline-cmd ${command} "${args}" > /dev/null 2>&1 if [[ "$?" -ne 0 ]]; then echo ${command} "${args} ... OK" else