From f527e908b2730e4661b7013ef40747bf7169eb8d Mon Sep 17 00:00:00 2001 From: Marco Capetta Date: Tue, 8 Oct 2019 17:45:31 +0200 Subject: [PATCH] TT#68004 Switch from python2 to python3 Change-Id: I6208b14a32f27f74d4eaa13db1376c8a547ccfba --- Makefile | 7 ++---- README | 4 ++-- bin/cdr_check.py | 30 ++++++++++++------------- bin/check.py | 22 +++++++++---------- bin/mem_stats.py | 46 +++++++++++++++++++-------------------- bin/pid_watcher.py | 4 ++-- debian/control | 8 +++---- t/Dockerfile | 4 ++-- tests/test_check.py | 10 ++++----- tests/test_yaml_format.py | 4 ++-- 10 files changed, 68 insertions(+), 71 deletions(-) diff --git a/Makefile b/Makefile index dab30338..8ab0dd20 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,6 @@ TESTS=$(addprefix test_,$(SCENARIOS)) BASH_SCRIPTS = bench.sh get_results.sh run_tests.sh t/testrunner bin/*.sh PERL_SCRIPTS = bin/*.pl -PYTHON_SCRIPTS = tests/test_check.py tests/test_yaml_format.py bin/*.py SHELL_SCRIPTS = tests/do_test_yaml_format.sh RESULTS ?= reports @@ -35,10 +34,8 @@ perlcheck: pythoncheck: @echo -n "Checking for python syntax errors: "; \ - for SCRIPT in $(PYTHON_SCRIPTS); do \ - python -m py_compile $${SCRIPT} || exit 1 ; \ - rm $${SCRIPT}c # get rid of pyc files ; \ - done; \ + python3 -m compileall ./ || exit 1 ; \ + py3clean ./ # get rid of pyc files and __pycache__ dirs ; \ echo "done."; \ shellcheck: diff --git a/README b/README index f749499d..0e43b217 100644 --- a/README +++ b/README @@ -147,7 +147,7 @@ The test file is divided in tree different parts: 'P-NGCP-Authorized: 1' ] - [0] http://docs.python.org/2/library/re.html#regular-expression-syntax + [0] https://docs.python.org/3/library/re.html#regular-expression-syntax Cdr test file syntax (cdr_test.yml.tt2): @@ -174,7 +174,7 @@ The test file contains only one part: call_status: 'other' call_code: '403' - [0] http://docs.python.org/2/library/re.html#regular-expression-syntax + [0] https://docs.python.org/3/library/re.html#regular-expression-syntax scenario.yml file syntax: diff --git a/bin/cdr_check.py b/bin/cdr_check.py index 44a84a7f..3c5a51b9 100755 --- a/bin/cdr_check.py +++ b/bin/cdr_check.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/python3 # # Copyright: 2013-2015 Sipwise Development Team # @@ -57,9 +57,9 @@ class Test: @classmethod def compare(cls, val0, val1): logging.debug("val0:[%s]:'%s' val1:[%s]:'%s'" % - (type(val0), unicode(val0), type(val1), - unicode(val1))) - if isinstance(val0, basestring): + (type(val0), str(val0), type(val1), + str(val1))) + if isinstance(val0, str): if re.search(val0, str(val1)) is not None: return True else: @@ -77,7 +77,7 @@ class Test: for k in range(size): try: result = result and cls.compare(val0[k], val1[k]) - except Exception, e: + except Exception as e: logging.debug(e) return False else: @@ -129,7 +129,7 @@ def check_single_cdr(scen, msg, test): return else: msg = msg[0] - for rule, value in scen.iteritems(): + for rule, value in scen.items(): value = str(value) if rule not in msg: test.error('%s not in cdr' % rule) @@ -169,7 +169,7 @@ def check_single_cdr_recursive(scen, msg): validated = True comments = [] oks = [] - for rule, value in scen.iteritems(): + for rule, value in scen.items(): value = str(value) if rule not in msg: validated = False @@ -219,12 +219,12 @@ def check_cdr_recursive(scen, msgs, test): def usage(): - print 'Usage: mysql_check.py [OPTIONS] cdr_file cdr_test.yml' - print '-h: this help' - print '-d: debug' - print '-y: cdr_file in .yaml format' - print '-j: cdr_file in .json format' - print '-t: cdr_file in .text format' + print('Usage: mysql_check.py [OPTIONS] cdr_file cdr_test.yml') + print('-h: this help') + print('-d: debug') + print('-y: cdr_file in .yaml format') + print('-j: cdr_file in .json format') + print('-t: cdr_file in .text format') def load_yaml(filepath): @@ -275,7 +275,7 @@ def main(): sys.argv[1:], "hyjtd", ["help", "yaml", "json", "text", "debug"]) except getopt.GetoptError as err: # print help information and exit: - print str(err) # will print something like "option -a not recognized" + print(str(err)) # will print something like "option -a not recognized" usage() sys.exit(2) for o, a in opts: @@ -309,7 +309,7 @@ def main(): test.comment('check cdr record') check_cdr_recursive(scen['cdr'], check['cdr'], test) - print test + print(test) if test.isError(): sys.exit(1) diff --git a/bin/check.py b/bin/check.py index 72e80f26..b0ec7b44 100755 --- a/bin/check.py +++ b/bin/check.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/python3 # # Copyright: 2013-2015 Sipwise Development Team # @@ -124,9 +124,9 @@ class Test: @classmethod def compare(cls, val0, val1): logging.debug("val0:[%s]:'%s' val1:[%s]:'%s'" % - (type(val0), unicode(val0), type(val1), - unicode(val1))) - if isinstance(val0, basestring): + (type(val0), str(val0), type(val1), + str(val1))) + if isinstance(val0, str): if re.search(val0, str(val1)) is not None: return True else: @@ -144,7 +144,7 @@ class Test: for k in range(size): try: result = result and cls.compare(val0[k], val1[k]) - except Exception, e: + except Exception as e: logging.debug(e) return False else: @@ -191,7 +191,7 @@ class Test: def check_flow_vars(sk, sv, cv, test): """ check the vars on a flow level""" - for k in sv.iterkeys(): + for k in sv.keys(): logging.debug("check k:'%s'" % k) if(k not in cv): try: @@ -290,9 +290,9 @@ def check_sip_out(scen, msgs, test): def usage(): - print 'Usage: check.py [-h] [-d] [-j] [-y] scenario_file test.yml' - print '-h: this help' - print '-d: debug' + print('Usage: check.py [-h] [-d] [-j] [-y] scenario_file test.yml') + print('-h: this help') + print('-d: debug') def load_yaml(filepath): @@ -320,7 +320,7 @@ def main(): sys.argv[1:], "hyjd", ["help", "yaml", "json", "debug"]) except getopt.GetoptError as err: # print help information and exit: - print str(err) # will print something like "option -a not recognized" + print(str(err)) # will print something like "option -a not recognized" usage() sys.exit(2) for o, a in opts: @@ -356,7 +356,7 @@ def main(): check_sip(scen['sip_in'], check['sip_in'], test) test.comment('check sip_out') check_sip_out(scen['sip_out'], check['sip_out'], test) - print test + print(test) if test.isError(): sys.exit(1) diff --git a/bin/mem_stats.py b/bin/mem_stats.py index 04cc2132..0c0a2375 100755 --- a/bin/mem_stats.py +++ b/bin/mem_stats.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/python3 # # Copyright: 2014 Sipwise Development Team # @@ -22,12 +22,12 @@ import argparse import csv import os import sys -import xmlrpclib +import xmlrpc.client KAM_URL = 'http://127.0.0.1:5062' KAM_LINES = 10 -proxy = xmlrpclib.ServerProxy(KAM_URL) +proxy = xmlrpc.client.ServerProxy(KAM_URL) def get_headers(l, prefix): @@ -62,17 +62,17 @@ def get_pvm(private_file): row = proxy.pkg.stats("index", i)[0] res.append(row) sum_row(res[0], row) - except xmlrpclib.Fault as err: - print "A fault occurred" - print "Fault code: %d" % err.faultCode - print "Fault string: %s" % err.faultString + except xmlrpc.client.Fault as err: + print("A fault occurred") + print("Fault code: %d" % err.faultCode) + print("Fault string: %s" % err.faultString) sys.exit(-1) - except xmlrpclib.ProtocolError as err: - print "A protocol error occurred" - print "URL: %s" % err.url - print "HTTP/HTTPS headers: %s" % err.headers - print "Error code: %d" % err.errcode - print "Error message: %s" % err.errmsg + except xmlrpc.client.ProtocolError as err: + print("A protocol error occurred") + print("URL: %s" % err.url) + print("HTTP/HTTPS headers: %s" % err.headers) + print("Error code: %d" % err.errcode) + print("Error message: %s" % err.errmsg) sys.exit(-2) save_data(private_file, headers, prefix, res[:1]) fileName, fileExtension = os.path.splitext(private_file) @@ -87,17 +87,17 @@ def get_shm(share_file): prefix = 'shared_' try: res = proxy.core.shmmem('b') - except xmlrpclib.Fault as err: - print "A fault occurred" - print "Fault code: %d" % err.faultCode - print "Fault string: %s" % err.faultString + except xmlrpc.client.Fault as err: + print("A fault occurred") + print("Fault code: %d" % err.faultCode) + print("Fault string: %s" % err.faultString) sys.exit(-1) - except xmlrpclib.ProtocolError as err: - print "A protocol error occurred" - print "URL: %s" % err.url - print "HTTP/HTTPS headers: %s" % err.headers - print "Error code: %d" % err.errcode - print "Error message: %s" % err.errmsg + except xmlrpc.client.ProtocolError as err: + print("A protocol error occurred") + print("URL: %s" % err.url) + print("HTTP/HTTPS headers: %s" % err.headers) + print("Error code: %d" % err.errcode) + print("Error message: %s" % err.errmsg) sys.exit(-2) save_data(share_file, headers, prefix, [res, ]) diff --git a/bin/pid_watcher.py b/bin/pid_watcher.py index 2812fdf4..5c84a430 100755 --- a/bin/pid_watcher.py +++ b/bin/pid_watcher.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/python3 """ Copyright: 2014 Sipwise Development Team @@ -70,7 +70,7 @@ class Handler(pyinotify.ProcessEvent): def check_all(self): all = True - for k, v in self.watched.iteritems(): + for k, v in self.watched.items(): all = (all and (v['created'] or v['modified'])) logging.info("checking: %s[%s] all:%s" % (k, v, all)) return all diff --git a/debian/control b/debian/control index ce8c2aa4..e0d2fcfa 100644 --- a/debian/control +++ b/debian/control @@ -11,7 +11,7 @@ Build-Depends: libtemplate-perl, libtext-csv-perl, libyaml-libyaml-perl, - python, + python3:any, Recommends: libgraphviz-perl, Standards-Version: 3.9.8 @@ -31,9 +31,9 @@ Depends: libyaml-libyaml-perl, ngcp-provisioning-tools, parallel, - python, - python-pyinotify, - python-yaml, + python3:any, + python3-pyinotify, + python3-yaml, sip-tester (>= 1:3.3-1~), tcpdump, ${misc:Depends}, diff --git a/t/Dockerfile b/t/Dockerfile index 6ca1ddd3..1f566f9b 100644 --- a/t/Dockerfile +++ b/t/Dockerfile @@ -5,7 +5,7 @@ FROM docker.mgm.sipwise.com/sipwise-buster:latest # is updated with the current date. It will force refresh of all # of the base images and things like `apt-get update` won't be using # old cached versions when the Dockerfile is built. -ENV REFRESHED_AT 2019-06-27 +ENV REFRESHED_AT 2019-10-15 # test execution; we need the backport of python3-junitxml from our own # repository since it's not part of Debian/buster @@ -14,7 +14,7 @@ COPY t/sources.list.d/builddeps.list /etc/apt/sources.list.d/ COPY t/sources.list.d/preferences /etc/apt/preferences.d/ RUN apt-get update -RUN apt-get install --assume-yes python-yaml python-junitxml \ +RUN apt-get install --assume-yes python3-yaml python3-junitxml \ make libtemplate-perl libyaml-perl RUN echo './t/testrunner' >>/root/.bash_history diff --git a/tests/test_check.py b/tests/test_check.py index a2361d5c..69af09d4 100755 --- a/tests/test_check.py +++ b/tests/test_check.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/python3 # # Copyright: 2013-2015 Sipwise Development Team # @@ -45,15 +45,15 @@ class TestXAvp(unittest.TestCase): def test_init(self): self.assertEqual(self.name, self.xavp._name) - self.assertItemsEqual(self.data, self.xavp._data) + self.assertCountEqual(self.data, self.xavp._data) def test_init_wrong_type(self): self.assertRaises(Exception, self.xavp, '$var(whatever)', None) def test_parse_type(self): - self.assertRaisesRegexp( + self.assertRaisesRegex( Exception, 'no xavp', XAvp.parse, '$var(whatever)') - self.assertRaisesRegexp(Exception, 'no xavp', XAvp.parse, '$fU') + self.assertRaisesRegex(Exception, 'no xavp', XAvp.parse, '$fU') def test_get_wrong_name(self): self.assertRaises(KeyError, self.xavp.get, '$xavp(otro)') @@ -77,7 +77,7 @@ class TestXAvp(unittest.TestCase): self.assertEqual(self.xavp.get('$xavp(test[2]=>lola[0])'), 7) def test_get_value_all(self): - self.assertItemsEqual(self.xavp.get('$xavp(test[1]=>koko[*])'), [1, 2]) + self.assertCountEqual(self.xavp.get('$xavp(test[1]=>koko[*])'), [1, 2]) class TestCheckFlowVars(unittest.TestCase): diff --git a/tests/test_yaml_format.py b/tests/test_yaml_format.py index 135dac63..4db97af8 100755 --- a/tests/test_yaml_format.py +++ b/tests/test_yaml_format.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/python3 # # Copyright: 2013-2015 Sipwise Development Team # @@ -63,7 +63,7 @@ class ParametrizedTestCase(unittest.TestCase): class TestYmlLint(ParametrizedTestCase): def setUp(self): - self.yaml = load(file(self.param, 'r')) + self.yaml = load(open(self.param, 'r')) def testFlow(self): self.assertTrue('flow' in self.yaml)