mirror of
https://github.com/asterisk/asterisk.git
synced 2025-09-06 04:30:28 +00:00
sip_to_pjsip: Enable python3 compatibility.
The script remains compatible with Python 2.7 but now also works with Python 3.3 and newer; to ease the migration from chan_sip to chan_pjsip. ASTERISK-27811 Change-Id: I59cc6b52a1a89777eebcf25b3023bdf93babf835
This commit is contained in:
committed by
Corey Farrell
parent
9f1e1d153a
commit
2d81709ab1
@@ -49,7 +49,7 @@ class Section(MultiOrderedDict):
|
|||||||
"""
|
"""
|
||||||
Use self.id as means of determining equality
|
Use self.id as means of determining equality
|
||||||
"""
|
"""
|
||||||
return cmp(self.id, other.id)
|
return (self.id > other.id) - (self.id < other.id)
|
||||||
|
|
||||||
def __eq__(self, other):
|
def __eq__(self, other):
|
||||||
"""
|
"""
|
||||||
@@ -445,7 +445,7 @@ class MultiOrderedConfigParser:
|
|||||||
with open(filename, 'rt') as config_file:
|
with open(filename, 'rt') as config_file:
|
||||||
self._read(config_file, sect)
|
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, sect):
|
def _read(self, config_file, sect):
|
||||||
"""Parse configuration information from the config_file"""
|
"""Parse configuration information from the config_file"""
|
||||||
@@ -490,4 +490,4 @@ class MultiOrderedConfigParser:
|
|||||||
with open(config_file, 'wt') as fp:
|
with open(config_file, 'wt') as fp:
|
||||||
self.write(fp)
|
self.write(fp)
|
||||||
except IOError:
|
except IOError:
|
||||||
print "Could not open file ", config_file, " for writing"
|
print("Could not open file " + config_file + " for writing")
|
||||||
|
@@ -3,10 +3,12 @@
|
|||||||
# copied from http://code.activestate.com/recipes/576693/
|
# copied from http://code.activestate.com/recipes/576693/
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from thread import get_ident as _get_ident
|
from threading import get_ident as _get_ident
|
||||||
except ImportError:
|
except ImportError:
|
||||||
from dummy_thread import get_ident as _get_ident
|
try:
|
||||||
|
from thread import get_ident as _get_ident
|
||||||
|
except ImportError:
|
||||||
|
from dummy_thread import get_ident as _get_ident
|
||||||
try:
|
try:
|
||||||
from _abcoll import KeysView, ValuesView, ItemsView
|
from _abcoll import KeysView, ValuesView, ItemsView
|
||||||
except ImportError:
|
except ImportError:
|
||||||
@@ -267,11 +269,11 @@ class MultiOrderedDict(OrderedDict):
|
|||||||
|
|
||||||
def __setitem__(self, key, val, i=None):
|
def __setitem__(self, key, val, i=None):
|
||||||
if key not in self:
|
if key not in self:
|
||||||
# print "__setitem__ key = ", key, " val = ", val
|
# print("__setitem__ key = " + key + " val = " + val)
|
||||||
OrderedDict.__setitem__(
|
OrderedDict.__setitem__(
|
||||||
self, key, val if isinstance(val, list) else [val])
|
self, key, val if isinstance(val, list) else [val])
|
||||||
return
|
return
|
||||||
# print "inserting key = ", key, " val = ", val
|
# print("inserting key = " + key + " val = " + val)
|
||||||
vals = self[key]
|
vals = self[key]
|
||||||
if i is None:
|
if i is None:
|
||||||
i = len(vals)
|
i = len(vals)
|
||||||
|
@@ -1,9 +1,11 @@
|
|||||||
#!/usr/bin/python
|
#!/usr/bin/env python
|
||||||
|
|
||||||
import optparse
|
import optparse
|
||||||
import socket
|
import socket
|
||||||
import urlparse # Python 2.7 required for Literal IPv6 Addresses
|
try:
|
||||||
|
from urllib.parse import urlparse
|
||||||
|
except ImportError:
|
||||||
|
from urlparse import urlparse # Python 2.7 required for Literal IPv6 Addresses
|
||||||
import astdicts
|
import astdicts
|
||||||
import astconfigparser
|
import astconfigparser
|
||||||
|
|
||||||
@@ -90,32 +92,32 @@ def merge_codec_value(key=None, val=None, section=None, pjsip=None,
|
|||||||
return _merge_codec_value
|
return _merge_codec_value
|
||||||
|
|
||||||
if key == 'allow':
|
if key == 'allow':
|
||||||
try:
|
try:
|
||||||
disallow = sip.get(section, 'disallow')[0]
|
disallow = sip.get(section, 'disallow')[0]
|
||||||
if disallow == 'all':
|
if disallow == 'all':
|
||||||
#don't inherit
|
#don't inherit
|
||||||
for i in sip.get(section, 'allow'):
|
for i in sip.get(section, 'allow'):
|
||||||
set_value(key, i, section, pjsip, nmapped, type)
|
set_value(key, i, section, pjsip, nmapped, type)
|
||||||
else:
|
else:
|
||||||
merge_value(key, val, section, pjsip, nmapped, type, section_to, key_to)
|
merge_value(key, val, section, pjsip, nmapped, type, section_to, key_to)
|
||||||
except LookupError:
|
except LookupError:
|
||||||
print "lookup error"
|
print("lookup error")
|
||||||
merge_value(key, val, section, pjsip, nmapped, type, section_to, key_to)
|
merge_value(key, val, section, pjsip, nmapped, type, section_to, key_to)
|
||||||
return
|
return
|
||||||
elif key == 'disallow':
|
elif key == 'disallow':
|
||||||
try:
|
try:
|
||||||
allow = sip.get(section, 'allow')[0]
|
allow = sip.get(section, 'allow')[0]
|
||||||
if allow == 'all':
|
if allow == 'all':
|
||||||
#don't inherit
|
#don't inherit
|
||||||
for i in sip.get(section, 'disallow'):
|
for i in sip.get(section, 'disallow'):
|
||||||
set_value(key, i, section, pjsip, nmapped, type)
|
set_value(key, i, section, pjsip, nmapped, type)
|
||||||
else:
|
else:
|
||||||
merge_value(key, val, section, pjsip, nmapped, type, section_to, key_to)
|
merge_value(key, val, section, pjsip, nmapped, type, section_to, key_to)
|
||||||
except LookupError:
|
except LookupError:
|
||||||
merge_value(key, val, section, pjsip, nmapped, type, section_to, key_to)
|
merge_value(key, val, section, pjsip, nmapped, type, section_to, key_to)
|
||||||
return
|
return
|
||||||
else:
|
else:
|
||||||
merge_value(key, val, section, pjsip, nmapped, type, section_to, key_to)
|
merge_value(key, val, section, pjsip, nmapped, type, section_to, key_to)
|
||||||
|
|
||||||
|
|
||||||
def non_mapped(nmapped):
|
def non_mapped(nmapped):
|
||||||
@@ -162,9 +164,9 @@ def setup_udptl(section, pjsip, nmapped):
|
|||||||
val = sip.get(section, 't38pt_udptl')[0]
|
val = sip.get(section, 't38pt_udptl')[0]
|
||||||
except LookupError:
|
except LookupError:
|
||||||
try:
|
try:
|
||||||
val = sip.get('general', 't38pt_udptl')[0]
|
val = sip.get('general', 't38pt_udptl')[0]
|
||||||
except LookupError:
|
except LookupError:
|
||||||
return
|
return
|
||||||
|
|
||||||
ec = 'none'
|
ec = 'none'
|
||||||
if 'yes' in val:
|
if 'yes' in val:
|
||||||
@@ -291,7 +293,7 @@ def build_host(config, host, section='general', port_key=None):
|
|||||||
|
|
||||||
# Literal IPv6 (like [::]), IPv4, or hostname
|
# Literal IPv6 (like [::]), IPv4, or hostname
|
||||||
# does not work for IPv6 without brackets; case catched above
|
# does not work for IPv6 without brackets; case catched above
|
||||||
url = urlparse.urlparse('sip://' + host)
|
url = urlparse('sip://' + host)
|
||||||
|
|
||||||
if port_key:
|
if port_key:
|
||||||
try:
|
try:
|
||||||
@@ -435,8 +437,7 @@ def from_dtlsenable(key, val, section, pjsip, nmapped):
|
|||||||
###############################################################################
|
###############################################################################
|
||||||
|
|
||||||
# options in pjsip.conf on an endpoint that have no sip.conf equivalent:
|
# options in pjsip.conf on an endpoint that have no sip.conf equivalent:
|
||||||
# type, 100rel, trust_id_outbound, aggregate_mwi,
|
# type, 100rel, trust_id_outbound, aggregate_mwi, connected_line_method
|
||||||
# connected_line_method
|
|
||||||
|
|
||||||
# known sip.conf peer keys that can be mapped to a pjsip.conf section/key
|
# known sip.conf peer keys that can be mapped to a pjsip.conf section/key
|
||||||
peer_map = [
|
peer_map = [
|
||||||
@@ -591,7 +592,7 @@ def split_hostport(addr):
|
|||||||
|
|
||||||
# Literal IPv6 (like [::]), IPv4, or hostname
|
# Literal IPv6 (like [::]), IPv4, or hostname
|
||||||
# does not work for IPv6 without brackets; case catched above
|
# does not work for IPv6 without brackets; case catched above
|
||||||
url = urlparse.urlparse('sip://' + addr)
|
url = urlparse('sip://' + addr)
|
||||||
# TODO Does not compress IPv6, for example 0:0:0:0:0:0:0:0 should get [::]
|
# TODO Does not compress IPv6, for example 0:0:0:0:0:0:0:0 should get [::]
|
||||||
return (url.hostname, url.port)
|
return (url.hostname, url.port)
|
||||||
|
|
||||||
@@ -839,11 +840,11 @@ def create_tls(sip, pjsip, nmapped):
|
|||||||
method = sip.multi_get('general', ['tlsclientmethod',
|
method = sip.multi_get('general', ['tlsclientmethod',
|
||||||
'sslclientmethod'])[0]
|
'sslclientmethod'])[0]
|
||||||
if section != 'transport-' + protocol + '6': # print only once
|
if section != 'transport-' + protocol + '6': # print only once
|
||||||
print 'In chan_sip, you specified the TLS version. With chan_sip,' \
|
print('In chan_sip, you specified the TLS version. With chan_sip,' \
|
||||||
' this was just for outbound client connections. In' \
|
' this was just for outbound client connections. In' \
|
||||||
' chan_pjsip, this value is for client and server. Instead,' \
|
' chan_pjsip, this value is for client and server. Instead,' \
|
||||||
' consider not to specify \'tlsclientmethod\' for chan_sip' \
|
' consider not to specify \'tlsclientmethod\' for chan_sip' \
|
||||||
' and \'method = sslv23\' for chan_pjsip.'
|
' and \'method = sslv23\' for chan_pjsip.')
|
||||||
except LookupError:
|
except LookupError:
|
||||||
"""
|
"""
|
||||||
OpenSSL emerged during the 90s. SSLv2 and SSLv3 were the only
|
OpenSSL emerged during the 90s. SSLv2 and SSLv3 were the only
|
||||||
@@ -1246,7 +1247,7 @@ def write_pjsip(filename, pjsip, non_mappings):
|
|||||||
pjsip.write(fp)
|
pjsip.write(fp)
|
||||||
|
|
||||||
except IOError:
|
except IOError:
|
||||||
print "Could not open file ", filename, " for writing"
|
print("Could not open file " + filename + " for writing")
|
||||||
|
|
||||||
###############################################################################
|
###############################################################################
|
||||||
|
|
||||||
@@ -1277,11 +1278,11 @@ 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 'Please, report any issue at:'
|
print('Please, report any issue at:')
|
||||||
print ' https://issues.asterisk.org/'
|
print(' https://issues.asterisk.org/')
|
||||||
print 'Reading', sip_filename
|
print('Reading ' + sip_filename)
|
||||||
sip.read(sip_filename)
|
sip.read(sip_filename)
|
||||||
print 'Converting to PJSIP...'
|
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
|
print('Writing ' + pjsip_filename)
|
||||||
write_pjsip(pjsip_filename, pjsip, non_mappings)
|
write_pjsip(pjsip_filename, pjsip, non_mappings)
|
||||||
|
@@ -1,6 +1,5 @@
|
|||||||
#!/usr/bin/python
|
#!/usr/bin/env python
|
||||||
|
|
||||||
from sip_to_pjsip import cli_options
|
|
||||||
from sip_to_pjsip import convert
|
from sip_to_pjsip import convert
|
||||||
import sip_to_pjsip
|
import sip_to_pjsip
|
||||||
import optparse
|
import optparse
|
||||||
@@ -18,7 +17,7 @@ def write_pjsip(filename, pjsip, non_mappings):
|
|||||||
pjsip.write(fp)
|
pjsip.write(fp)
|
||||||
|
|
||||||
except IOError:
|
except IOError:
|
||||||
print "Could not open file ", filename, " for writing"
|
print("Could not open file " + filename + " for writing")
|
||||||
|
|
||||||
def cli_options():
|
def cli_options():
|
||||||
"""
|
"""
|
||||||
@@ -70,12 +69,12 @@ if __name__ == "__main__":
|
|||||||
sip = sqlconfigparser.SqlConfigParser(table)
|
sip = sqlconfigparser.SqlConfigParser(table)
|
||||||
sip_to_pjsip.sip = sip
|
sip_to_pjsip.sip = sip
|
||||||
sip.connect(user,password,host,port,database)
|
sip.connect(user,password,host,port,database)
|
||||||
print 'Please, report any issue at:'
|
print('Please, report any issue at:')
|
||||||
print ' https://issues.asterisk.org/'
|
print(' https://issues.asterisk.org/')
|
||||||
print 'Reading', sip_filename
|
print('Reading ' + sip_filename)
|
||||||
sip.read(sip_filename)
|
sip.read(sip_filename)
|
||||||
print 'Converting to PJSIP realtime sql...'
|
print('Converting to PJSIP realtime sql...')
|
||||||
pjsip, non_mappings = convert(sip, pjsip_filename, dict(), False)
|
pjsip, non_mappings = convert(sip, pjsip_filename, dict(), False)
|
||||||
print 'Writing', pjsip_filename
|
print('Writing ' + pjsip_filename)
|
||||||
write_pjsip(pjsip_filename, pjsip, non_mappings)
|
write_pjsip(pjsip_filename, pjsip, non_mappings)
|
||||||
|
|
||||||
|
@@ -1,6 +1,13 @@
|
|||||||
from astconfigparser import MultiOrderedConfigParser
|
from astconfigparser import MultiOrderedConfigParser
|
||||||
|
|
||||||
import MySQLdb
|
try:
|
||||||
|
import pymysql as MySQLdb
|
||||||
|
MySQLdb.install_as_MySQLdb()
|
||||||
|
except ImportError:
|
||||||
|
# MySQLdb is compatible with Python 2 only. Try it as a
|
||||||
|
# fallback if pymysql is unavailable.
|
||||||
|
import MySQLdb
|
||||||
|
|
||||||
import traceback
|
import traceback
|
||||||
|
|
||||||
class SqlConfigParser(MultiOrderedConfigParser):
|
class SqlConfigParser(MultiOrderedConfigParser):
|
||||||
@@ -61,9 +68,6 @@ class SqlConfigParser(MultiOrderedConfigParser):
|
|||||||
"""Write configuration information out to a file"""
|
"""Write configuration information out to a file"""
|
||||||
try:
|
try:
|
||||||
self.write_dicts(config_file, self._sections)
|
self.write_dicts(config_file, self._sections)
|
||||||
except Exception,e:
|
except:
|
||||||
print "Could not open file ", config_file, " for writing"
|
print("Could not open file " + config_file + " for writing")
|
||||||
traceback.print_exc()
|
traceback.print_exc()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user