From 5c43bd3baec1f10968a1b971edcdd1100b821e62 Mon Sep 17 00:00:00 2001 From: kkjang Date: Wed, 24 May 2017 15:59:59 -0700 Subject: [PATCH] Cmp python3 compat (#25008) * Use double-quotes for expect integration tests * Cast user input to string for expect integration tests * Remove usage of cmp() for python3 compatibility - Add code smell test to look for cmp usage - Fixes #24756 --- lib/ansible/modules/cloud/docker/_docker.py | 3 ++- lib/ansible/modules/network/cloudengine/ce_ntp.py | 2 +- .../modules/network/cloudengine/ce_rollback.py | 2 +- .../modules/web_infrastructure/letsencrypt.py | 2 +- test/sanity/code-smell/no-list-cmp.sh | 15 +++++++++++++++ 5 files changed, 20 insertions(+), 4 deletions(-) create mode 100755 test/sanity/code-smell/no-list-cmp.sh diff --git a/lib/ansible/modules/cloud/docker/_docker.py b/lib/ansible/modules/cloud/docker/_docker.py index 3512e1ed51..cba5c29fc5 100644 --- a/lib/ansible/modules/cloud/docker/_docker.py +++ b/lib/ansible/modules/cloud/docker/_docker.py @@ -1755,7 +1755,8 @@ def present(manager, containers, count, name): if delta < 0: # If both running and stopped containers exist, remove # stopped containers first. - containers.deployed.sort(lambda cx, cy: cmp(is_running(cx), is_running(cy))) + # Use key param for python 2/3 compatibility. + containers.deployed.sort(key=is_running) to_stop = [] to_remove = [] diff --git a/lib/ansible/modules/network/cloudengine/ce_ntp.py b/lib/ansible/modules/network/cloudengine/ce_ntp.py index 21acb6ad80..b9cf4e3055 100644 --- a/lib/ansible/modules/network/cloudengine/ce_ntp.py +++ b/lib/ansible/modules/network/cloudengine/ce_ntp.py @@ -478,7 +478,7 @@ class Ntp(object): peer_type=ntp_dict['type'], prefer=is_preferred, key_id=key_id) exp_ntp_cfg = dict(vpn_name=self.vpn_name, source_int=self.interface.lower(), address=self.address, peer_type=self.peer_type, prefer=self.is_preferred, key_id=self.key_id) - if cmp(cur_ntp_cfg, exp_ntp_cfg) == 0: + if cur_ntp_cfg == exp_ntp_cfg: self.conf_exsit = True vpn_name = ntp_dict['vpnName'] diff --git a/lib/ansible/modules/network/cloudengine/ce_rollback.py b/lib/ansible/modules/network/cloudengine/ce_rollback.py index 95ad587f4b..bff44c470d 100644 --- a/lib/ansible/modules/network/cloudengine/ce_rollback.py +++ b/lib/ansible/modules/network/cloudengine/ce_rollback.py @@ -402,7 +402,7 @@ class RollBack(object): self.module.fail_json( msg='Error: Commit label which should not start with a number.') if len(self.label.replace(' ', '')) == 1: - if cmp(self.label, '-') == 0: + if self.label == '-': self.module.fail_json( msg='Error: Commit label which should not be "-"') if len(self.label.replace(' ', '')) < 1 or len(self.label) > 256: diff --git a/lib/ansible/modules/web_infrastructure/letsencrypt.py b/lib/ansible/modules/web_infrastructure/letsencrypt.py index e2e4285a0c..e12194da93 100644 --- a/lib/ansible/modules/web_infrastructure/letsencrypt.py +++ b/lib/ansible/modules/web_infrastructure/letsencrypt.py @@ -568,7 +568,7 @@ class ACMEClient(object): for index, cur_auth in enumerate(self.authorizations): if (cur_auth['uri'] == auth['uri']): # does the auth parameter contain updated data? - if cmp(cur_auth, auth) != 0: + if cur_auth != auth: # yes, update our current authorization list self.authorizations[index] = auth return True diff --git a/test/sanity/code-smell/no-list-cmp.sh b/test/sanity/code-smell/no-list-cmp.sh new file mode 100755 index 0000000000..d5db30ba75 --- /dev/null +++ b/test/sanity/code-smell/no-list-cmp.sh @@ -0,0 +1,15 @@ +#!/bin/sh + +CMP_USERS=$(grep -rI ' cmp[^a-zA-Z0-9_]' . \ + --exclude-dir .tox \ + | grep -v \ + -e lib/ansible/module_utils/six/_six.py \ + -e test/sanity/code-smell/no-list-cmp.sh + ) + +if [ "${CMP_USERS}" ]; then + echo 'cmp has been removed in python3. Alternatives:' + echo ' http://python3porting.com/preparing.html#when-sorting-use-key-instead-of-cmp' + echo "${CMP_USERS}" + exit 1 +fi