From 0a4e8a43e391376db3ccfec2b6565a8611d3e2d5 Mon Sep 17 00:00:00 2001 From: Pascal Cadotte Michaud <pcm@wazo.io> Date: Fri, 26 Oct 2018 11:53:40 -0400 Subject: [PATCH] contrib/sip_to_pjsip: add a --quiet option to avoid prints Using the --quiet or -q option in conjonction with /dev/stdout as the output file allow the output to be used as a valid configuration. Given a script that generates a valid sip.conf I can pipe the output of that script into `sip_to_pjsip.py -q /dev/stdin /dev/stdout`. This allow me to use that piped command in my pjsip.conf using the `exec` command. ASTERISK-28136 Change-Id: I7b0e2e90e2549f3f8e01dc96701f111b5874c88d --- contrib/scripts/sip_to_pjsip/sip_to_pjsip.py | 32 +++++++++++++++----- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/contrib/scripts/sip_to_pjsip/sip_to_pjsip.py b/contrib/scripts/sip_to_pjsip/sip_to_pjsip.py index 1860a317f2..fc32d915fb 100755 --- a/contrib/scripts/sip_to_pjsip/sip_to_pjsip.py +++ b/contrib/scripts/sip_to_pjsip/sip_to_pjsip.py @@ -1,5 +1,8 @@ #!/usr/bin/env python +from __future__ import print_function + +import sys import optparse import socket try: @@ -10,6 +13,7 @@ import astdicts import astconfigparser PREFIX = 'pjsip_' +QUIET = False ############################################################################### ### some utility functions @@ -106,7 +110,7 @@ def merge_codec_value(key=None, val=None, section=None, pjsip=None, else: merge_value(key, val, section, pjsip, nmapped, type, section_to, key_to) except LookupError: - print("lookup error") + print("lookup error", file=sys.stderr) merge_value(key, val, section, pjsip, nmapped, type, section_to, key_to) return elif key == 'disallow': @@ -850,7 +854,7 @@ def create_tls(sip, pjsip, nmapped): ' this was just for outbound client connections. In' \ ' chan_pjsip, this value is for client and server. Instead,' \ ' consider not to specify \'tlsclientmethod\' for chan_sip' \ - ' and \'method = sslv23\' for chan_pjsip.') + ' and \'method = sslv23\' for chan_pjsip.', file=sys.stderr) except LookupError: """ OpenSSL emerged during the 90s. SSLv2 and SSLv3 were the only @@ -1267,7 +1271,7 @@ def write_pjsip(filename, pjsip, non_mappings): pjsip.write(fp) except IOError: - print("Could not open file " + filename + " for writing") + print("Could not open file " + filename + " for writing", file=sys.stderr) ############################################################################### @@ -1278,6 +1282,7 @@ def cli_options(): print usage information """ global PREFIX + global QUIET usage = "usage: %prog [options] [input-file [output-file]]\n\n" \ "Converts the chan_sip configuration input-file to the chan_pjsip output-file.\n" \ "The input-file defaults to 'sip.conf'.\n" \ @@ -1285,24 +1290,35 @@ def cli_options(): parser = optparse.OptionParser(usage=usage) parser.add_option('-p', '--prefix', dest='prefix', default=PREFIX, help='output prefix for include files') + parser.add_option('-q', '--quiet', dest='quiet', default=False, action='store_true', + help="don't print messages to stdout") options, args = parser.parse_args() PREFIX = options.prefix + if options.quiet: + QUIET = True sip_filename = args[0] if len(args) else 'sip.conf' pjsip_filename = args[1] if len(args) == 2 else 'pjsip.conf' return sip_filename, pjsip_filename + +def info(msg): + if QUIET: + return + print(msg) + + if __name__ == "__main__": sip_filename, pjsip_filename = cli_options() # configuration parser for sip.conf sip = astconfigparser.MultiOrderedConfigParser() - print('Please, report any issue at:') - print(' https://issues.asterisk.org/') - print('Reading ' + sip_filename) + info('Please, report any issue at:') + info(' https://issues.asterisk.org/') + info('Reading ' + sip_filename) sip.read(sip_filename) - print('Converting to PJSIP...') + info('Converting to PJSIP...') pjsip, non_mappings = convert(sip, pjsip_filename, dict(), False) - print('Writing ' + pjsip_filename) + info('Writing ' + pjsip_filename) write_pjsip(pjsip_filename, pjsip, non_mappings) -- GitLab