Skip to content
Snippets Groups Projects
Commit fba836cc authored by Scott Griepentrog's avatar Scott Griepentrog
Browse files

sip_to_pjsip: improve ability to parse input files

General improvements to SIP to PJSIP conversion utility:

1) track default section of input file to allow parsing
   an include file that doesn't specify a [section]

2) informatively handle case of assignment without [section]

3) correctly handle getting sections from included files
   - [section]'s are inherited by included file

4) provide null string as default transport bind ip

5) gracefully handle missing portions of registration string

6) denote steps of operation during conversion and confirm
   top level files as a convenience

ASTERISK-24474 #close
Review: https://reviewboard.asterisk.org/r/4280/
Reported by: John Kiniston
........

Merged revisions 430469 from http://svn.asterisk.org/svn/asterisk/branches/13


git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@430470 65c4cc65-6c06-0410-ace0-fbb531ad65f3
parent 5b309383
Branches
Tags
No related merge requests found
import re import re
import itertools
from astdicts import OrderedDict from astdicts import OrderedDict
from astdicts import MultiOrderedDict from astdicts import MultiOrderedDict
...@@ -331,7 +332,9 @@ class MultiOrderedConfigParser: ...@@ -331,7 +332,9 @@ class MultiOrderedConfigParser:
res = sections[key] if key in sections else [] res = sections[key] if key in sections else []
searched.append(self) searched.append(self)
if self._includes: if self._includes:
res += self._includes.get_sections(key, attr, searched) res.extend(list(itertools.chain(*[
incl.get_sections(key, attr, searched)
for incl in self._includes.itervalues()])))
if self._parent: if self._parent:
res += self._parent.get_sections(key, attr, searched) res += self._parent.get_sections(key, attr, searched)
return res return res
...@@ -415,15 +418,15 @@ class MultiOrderedConfigParser: ...@@ -415,15 +418,15 @@ class MultiOrderedConfigParser:
else: else:
self.defaults(section)[0][key] = val self.defaults(section)[0][key] = val
def read(self, filename): def read(self, filename, sect=None):
"""Parse configuration information from a file""" """Parse configuration information from a file"""
try: try:
with open(filename, 'rt') as config_file: with open(filename, 'rt') as config_file:
self._read(config_file) self._read(config_file, sect)
except IOError: except IOError:
print "Could not open file ", filename, " for reading" print "Could not open file ", filename, " for reading"
def _read(self, config_file): def _read(self, config_file, sect):
"""Parse configuration information from the config_file""" """Parse configuration information from the config_file"""
is_comment = False # used for multi-lined comments is_comment = False # used for multi-lined comments
for line in config_file: for line in config_file:
...@@ -435,7 +438,7 @@ class MultiOrderedConfigParser: ...@@ -435,7 +438,7 @@ class MultiOrderedConfigParser:
include_name = try_include(line) include_name = try_include(line)
if include_name: if include_name:
parser = self.add_include(include_name) parser = self.add_include(include_name)
parser.read(include_name) parser.read(include_name, sect)
continue continue
section, is_template, templates = try_section(line) section, is_template, templates = try_section(line)
...@@ -447,6 +450,8 @@ class MultiOrderedConfigParser: ...@@ -447,6 +450,8 @@ class MultiOrderedConfigParser:
continue continue
key, val = try_option(line) key, val = try_option(line)
if sect is None:
raise Exception("Section not defined before assignment")
sect[key] = val sect[key] = val
def write(self, config_file): def write(self, config_file):
......
...@@ -587,7 +587,11 @@ def create_udp(sip, pjsip, nmapped): ...@@ -587,7 +587,11 @@ def create_udp(sip, pjsip, nmapped):
externhost externhost
""" """
bind = sip.multi_get('general', ['udpbindaddr', 'bindaddr'])[0] try:
bind = sip.multi_get('general', ['udpbindaddr', 'bindaddr'])[0]
except LookupError:
bind = ''
bind = build_host(sip, bind, 'general', 'bindport') bind = build_host(sip, bind, 'general', 'bindport')
try: try:
...@@ -974,11 +978,12 @@ class Registration: ...@@ -974,11 +978,12 @@ class Registration:
auth_section = 'auth_reg_' + self.host auth_section = 'auth_reg_' + self.host
if self.secret: if hasattr(self, 'secret') and self.secret:
set_value('password', self.secret, auth_section, pjsip, nmapped, set_value('password', self.secret, auth_section, pjsip, nmapped,
'auth') 'auth')
set_value('username', self.authuser or self.user, auth_section, if hasattr(self, 'authuser'):
pjsip, nmapped, 'auth') set_value('username', self.authuser or self.user, auth_section,
pjsip, nmapped, 'auth')
set_value('outbound_auth', auth_section, section, pjsip, nmapped, set_value('outbound_auth', auth_section, section, pjsip, nmapped,
'registration') 'registration')
...@@ -988,7 +993,7 @@ class Registration: ...@@ -988,7 +993,7 @@ class Registration:
else: else:
client_uri += self.host client_uri += self.host
if self.domainport: if hasattr(self, 'domainport') and self.domainport:
client_uri += ":" + self.domainport client_uri += ":" + self.domainport
elif self.port: elif self.port:
client_uri += ":" + self.port client_uri += ":" + self.port
...@@ -1136,8 +1141,9 @@ def cli_options(): ...@@ -1136,8 +1141,9 @@ def cli_options():
""" """
global PREFIX global PREFIX
usage = "usage: %prog [options] [input-file [output-file]]\n\n" \ usage = "usage: %prog [options] [input-file [output-file]]\n\n" \
"input-file defaults to 'sip.conf'\n" \ "Converts the chan_sip configuration input-file to the chan_pjsip output-file.\n"\
"output-file defaults to 'pjsip.conf'" "The input-file defaults to 'sip.conf'.\n" \
"The output-file defaults to 'pjsip.conf'."
parser = optparse.OptionParser(usage=usage) parser = optparse.OptionParser(usage=usage)
parser.add_option('-p', '--prefix', dest='prefix', default=PREFIX, parser.add_option('-p', '--prefix', dest='prefix', default=PREFIX,
help='output prefix for include files') help='output prefix for include files')
...@@ -1154,6 +1160,9 @@ if __name__ == "__main__": ...@@ -1154,6 +1160,9 @@ if __name__ == "__main__":
sip_filename, pjsip_filename = cli_options() sip_filename, pjsip_filename = cli_options()
# configuration parser for sip.conf # configuration parser for sip.conf
sip = astconfigparser.MultiOrderedConfigParser() sip = astconfigparser.MultiOrderedConfigParser()
print 'Reading', sip_filename
sip.read(sip_filename) sip.read(sip_filename)
print 'Converting to PJSIP...'
pjsip, non_mappings = convert(sip, pjsip_filename, dict(), False) pjsip, non_mappings = convert(sip, pjsip_filename, dict(), False)
print 'Writing', pjsip_filename
write_pjsip(pjsip_filename, pjsip, non_mappings) write_pjsip(pjsip_filename, pjsip, non_mappings)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment