[Asterisk-code-review] core: Begin work on python3 compatability. (asterisk[15])

Corey Farrell asteriskteam at digium.com
Fri Mar 23 09:47:40 CDT 2018


Corey Farrell has uploaded this change for review. ( https://gerrit.asterisk.org/8645


Change subject: core: Begin work on python3 compatability.
......................................................................

core: Begin work on python3 compatability.

* Consistently use spaces in rest-api-templates/asterisk_processor.py.
* Exclude third-party from docs/full-en_US.xml.
* Add docs/full-en_US.xml to .gitignore.
* Use list() to convert python3 view.
* Use python3 print function.
* Replace cmp() with equivalent equation.
* Replace reference to out of scope subtype variable with name
  parameter.

'make ari-stubs' still does not work when 'python --version' is 3.x.
Systems that still use python2 by default can be tested by setting
PYTHON=/usr/bin/python3 in ./makeopts.

I have not tested spandspflow2pcap.py or voicemailpwcheck.py, only the
print syntax has been fixed.

Change-Id: If5c5b556a2800d41a3e2cfef080ac2e151178c33
---
M Makefile
M contrib/scripts/refcounter.py
M contrib/scripts/spandspflow2pcap.py
M contrib/scripts/voicemailpwcheck.py
M doc/.gitignore
M rest-api-templates/asterisk_processor.py
M rest-api-templates/make_ari_stubs.py
M rest-api-templates/swagger_model.py
M rest-api-templates/transform.py
9 files changed, 33 insertions(+), 29 deletions(-)



  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/45/8645/1

diff --git a/Makefile b/Makefile
index 54368f7..55bc2b5 100644
--- a/Makefile
+++ b/Makefile
@@ -511,7 +511,7 @@
 	@echo "<!DOCTYPE docs SYSTEM \"appdocsxml.dtd\">" >> $@
 	@echo "<?xml-stylesheet type=\"text/xsl\" href=\"appdocsxml.xslt\"?>" >> $@
 	@echo "<docs xmlns:xi=\"http://www.w3.org/2001/XInclude\">" >> $@
-	@for x in $(MOD_SUBDIRS); do \
+	@for x in $(filter-out third-party,$(MOD_SUBDIRS)); do \
 		printf "$$x " ; \
 		for i in `find $$x -name '*.c'`; do \
 			$(PYTHON) build_tools/get_documentation.py < $$i >> $@ ; \
diff --git a/contrib/scripts/refcounter.py b/contrib/scripts/refcounter.py
index 1f4b375..de3cda0 100755
--- a/contrib/scripts/refcounter.py
+++ b/contrib/scripts/refcounter.py
@@ -18,6 +18,7 @@
  Matt Jordan <mjordan at digium.com>
 """
 
+from __future__ import print_function
 import sys
 import os
 
@@ -35,8 +36,8 @@
     """
     tokens = line.strip().split(',', 7)
     if len(tokens) < 8:
-        print "ERROR: ref debug line '%s' contains fewer tokens than " \
-              "expected: %d" % (line.strip(), len(tokens))
+        print("ERROR: ref debug line '%s' contains fewer tokens than "
+              "expected: %d" % (line.strip(), len(tokens)))
         return None
 
     processed_line = {'addr': tokens[0],
@@ -142,7 +143,7 @@
                 del current_objects[obj]
 
     if options.leaks:
-        for key, lines in current_objects.iteritems():
+        for (key, lines) in current_objects.items():
             leaked_objects.append((key, lines))
     return (finished_objects, invalid_objects, leaked_objects, skewed_objects)
 
@@ -156,13 +157,13 @@
             this object
     """
 
-    print "======== %s Objects ========" % prefix
-    print "\n"
+    print("======== %s Objects ========" % prefix)
+    print("\n")
     for obj in objects:
-        print "==== %s Object %s history ====" % (prefix, obj[0])
+        print("==== %s Object %s history ====" % (prefix, obj[0]))
         for line in obj[1]['log']:
-            print line
-        print "\n"
+            print(line)
+        print("\n")
 
 
 def main(argv=None):
@@ -198,11 +199,11 @@
 
     if not options.invalid and not options.leaks and not options.normal \
             and not options.skewed:
-        print >>sys.stderr, "All options disabled"
+        print("All options disabled", file=sys.stderr)
         return -1
 
     if not os.path.isfile(options.filepath):
-        print >>sys.stderr, "File not found: %s" % options.filepath
+        print("File not found: %s" % options.filepath, file=sys.stderr)
         return -1
 
     try:
@@ -227,7 +228,7 @@
             print_objects(finished_objects, "Finalized")
 
     except (KeyboardInterrupt, SystemExit, IOError):
-        print >>sys.stderr, "File processing cancelled"
+        print("File processing cancelled", file=sys.stderr)
         return -1
 
     return ret_code
diff --git a/contrib/scripts/spandspflow2pcap.py b/contrib/scripts/spandspflow2pcap.py
index a6546b6..7c403f1 100755
--- a/contrib/scripts/spandspflow2pcap.py
+++ b/contrib/scripts/spandspflow2pcap.py
@@ -119,7 +119,7 @@
         else:
             self.date += timedelta(microseconds=9000)
 
-        print seqno, '\t', self.date + self.dateoff
+        print(seqno, '\t', self.date + self.dateoff)
 
         # Make packet.
         packet, prev_data = self.data2packet(self.date + self.dateoff,
diff --git a/contrib/scripts/voicemailpwcheck.py b/contrib/scripts/voicemailpwcheck.py
index d7a66d4..452255c 100755
--- a/contrib/scripts/voicemailpwcheck.py
+++ b/contrib/scripts/voicemailpwcheck.py
@@ -46,20 +46,20 @@
 
 # Enforce a password length of at least 6 characters
 if len(new_pw) < REQUIRED_LENGTH:
-    print "INVALID: Password is too short (%d) - must be at least %d" % \
-            (len(new_pw), REQUIRED_LENGTH)
+    print("INVALID: Password is too short (%d) - must be at least %d" % \
+            (len(new_pw), REQUIRED_LENGTH))
     sys.exit(0)
 
 for regex, error in REGEX_BLACKLIST:
     if re.search(regex, new_pw):
-        print "INVALID: %s" % error
+        print("INVALID: %s" % error)
         sys.exit(0)
 
 for pw in PW_BLACKLIST:
     if new_pw.find(pw) != -1:
-        print "INVALID: %s is forbidden in a password" % pw
+        print("INVALID: %s is forbidden in a password" % pw)
         sys.exit(0)
 
-print "VALID"
+print("VALID")
 
 sys.exit(0)
diff --git a/doc/.gitignore b/doc/.gitignore
index 3461c58..49bfe42 100644
--- a/doc/.gitignore
+++ b/doc/.gitignore
@@ -1,4 +1,5 @@
 core-en_US.xml
+full-en_US.xml
 rest-api
 api
 asterisk-ng-doxygen
diff --git a/rest-api-templates/asterisk_processor.py b/rest-api-templates/asterisk_processor.py
index 9812946..413dd1f 100644
--- a/rest-api-templates/asterisk_processor.py
+++ b/rest-api-templates/asterisk_processor.py
@@ -183,7 +183,7 @@
                 raise SwaggerError(
                     "Should not mix resources in one API declaration", context)
             # root_path isn't needed any more
-            resource_api.root_path = resource_api.root_path.children()[0]
+            resource_api.root_path = list(resource_api.root_path.children())[0]
             if resource_api.name != resource_api.root_path.name:
                 raise SwaggerError(
                     "API declaration name should match", context)
@@ -206,10 +206,10 @@
 
     def process_parameter(self, parameter, context):
         if parameter.param_type == 'body':
-	    parameter.is_body_parameter = True;
+            parameter.is_body_parameter = True;
             parameter.c_data_type = 'struct ast_json *'
         else:
-	    parameter.is_body_parameter = False;
+            parameter.is_body_parameter = False;
             if not parameter.data_type in self.type_mapping:
                 raise SwaggerError(
                     "Invalid parameter type %s" % parameter.data_type, context)
diff --git a/rest-api-templates/make_ari_stubs.py b/rest-api-templates/make_ari_stubs.py
index f597426..6e29513 100755
--- a/rest-api-templates/make_ari_stubs.py
+++ b/rest-api-templates/make_ari_stubs.py
@@ -16,12 +16,13 @@
 # at the top of the source tree.
 #
 
+from __future__ import print_function
 import sys
 
 try:
     import pystache
 except ImportError:
-    print >> sys.stderr, "Pystache required. Please sudo pip install pystache."
+    print("Pystache required. Please sudo pip install pystache.", file=sys.stderr)
     sys.exit(1)
 
 import os.path
diff --git a/rest-api-templates/swagger_model.py b/rest-api-templates/swagger_model.py
index 0885241..03e6c7d 100644
--- a/rest-api-templates/swagger_model.py
+++ b/rest-api-templates/swagger_model.py
@@ -26,6 +26,7 @@
 See https://github.com/wordnik/swagger-core/wiki/API-Declaration for the spec.
 """
 
+from __future__ import print_function
 import json
 import os.path
 import pprint
@@ -75,7 +76,7 @@
     '''
     lhs = [int(v) for v in lhs.split('.')]
     rhs = [int(v) for v in rhs.split('.')]
-    return cmp(lhs, rhs)
+    return (lhs > rhs) - (lhs < rhs)
 
 
 class ParsingContext(object):
@@ -611,7 +612,7 @@
         except SwaggerError:
             raise
         except Exception as e:
-            print >> sys.stderr, "Error: ", traceback.format_exc()
+            print("Error: ", traceback.format_exc(), file=sys.stderr)
             raise SwaggerError(
                 "Error loading %s" % api_declaration_file, context, e)
 
@@ -624,8 +625,8 @@
             .replace(".json", ".{format}")
 
         if self.resource_path != expected_resource_path:
-            print >> sys.stderr, \
-                "%s != %s" % (self.resource_path, expected_resource_path)
+            print("%s != %s" % (self.resource_path, expected_resource_path),
+                file=sys.stderr)
             raise SwaggerError("resourcePath has incorrect value", context)
 
         return self
@@ -665,7 +666,7 @@
         model_dict = dict((m.id, m) for m in self.models)
         for m in self.models:
             def link_subtype(name):
-                res = model_dict.get(subtype)
+                res = model_dict.get(name)
                 if not res:
                     raise SwaggerError("%s has non-existing subtype %s",
                                        m.id, name)
@@ -724,7 +725,7 @@
         except SwaggerError:
             raise
         except Exception as e:
-            print >> sys.stderr, "Error: ", traceback.format_exc()
+            print("Error: ", traceback.format_exc(), file=sys.stderr)
             raise SwaggerError(
                 "Error loading %s" % resource_file, context, e)
 
diff --git a/rest-api-templates/transform.py b/rest-api-templates/transform.py
index c3a0300..73c375a 100644
--- a/rest-api-templates/transform.py
+++ b/rest-api-templates/transform.py
@@ -57,5 +57,5 @@
             out.flush()
 
             if not dest_exists or not filecmp.cmp(out.name, dest_file):
-                print "Writing %s" % dest_file
+                print("Writing %s" % dest_file)
                 shutil.copyfile(out.name, dest_file)

-- 
To view, visit https://gerrit.asterisk.org/8645
To unsubscribe, visit https://gerrit.asterisk.org/settings

Gerrit-Project: asterisk
Gerrit-Branch: 15
Gerrit-MessageType: newchange
Gerrit-Change-Id: If5c5b556a2800d41a3e2cfef080ac2e151178c33
Gerrit-Change-Number: 8645
Gerrit-PatchSet: 1
Gerrit-Owner: Corey Farrell <git at cfware.com>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20180323/bfa1652e/attachment-0001.html>


More information about the asterisk-code-review mailing list