From 0b440a928908b6b8c973a16f6fc7b26f893cce04 Mon Sep 17 00:00:00 2001 From: Toshio Kuratomi Date: Wed, 14 Dec 2016 12:16:16 -0800 Subject: [PATCH] Code smell test for iteritems and itervalues (#19292) * Code smell test for iteritems and itervalues * Change the keydict object in authorized_keys so it doesn't throw a false postive keydict is a bad data structure anyway. We don't use the iteritems and itervalues methods so just disable them so that the code-smell tests do not trigger on it. * Change release templates so they work with py3 --- lib/ansible/modules/system/authorized_key.py | 8 ++++---- packaging/release/templates/RELEASES.tmpl | 4 ++-- test/sanity/code-smell/no-dict-iteritems.sh | 21 ++++++++++++++++++++ test/sanity/code-smell/no-dict-itervalues.sh | 21 ++++++++++++++++++++ 4 files changed, 48 insertions(+), 6 deletions(-) create mode 100755 test/sanity/code-smell/no-dict-iteritems.sh create mode 100755 test/sanity/code-smell/no-dict-itervalues.sh diff --git a/lib/ansible/modules/system/authorized_key.py b/lib/ansible/modules/system/authorized_key.py index 2dc4191973..c3bdaf5e8b 100644 --- a/lib/ansible/modules/system/authorized_key.py +++ b/lib/ansible/modules/system/authorized_key.py @@ -233,16 +233,16 @@ class keydict(dict): yield key, self[key][indexes[key]] def iteritems(self): - return self._item_generator() + raise NotImplementedError("Do not use this as it's not available on py3") def items(self): - return list(self.iteritems()) + return list(self._item_generator()) def itervalues(self): - return (item[1] for item in self.iteritems()) + raise NotImplementedError("Do not use this as it's not available on py3") def values(self): - return list(self.itervalues()) + return [item[1] for item in self.items()] def keyfile(module, user, write=False, path=None, manage_dir=True): diff --git a/packaging/release/templates/RELEASES.tmpl b/packaging/release/templates/RELEASES.tmpl index 537f16d80b..dbe76c6896 100644 --- a/packaging/release/templates/RELEASES.tmpl +++ b/packaging/release/templates/RELEASES.tmpl @@ -5,9 +5,9 @@ VERSION RELEASE CODE NAME ++++++++++++++++++++++++++++++ {% for version in versions %} -{% for vkey, vdata in version.iteritems() %} +{% for vkey, vdata in version.items() %} {% for release in vdata.releases %} -{% for rkey, rdata in release.iteritems() %} +{% for rkey, rdata in release.items() %} {% set major_minor = vkey + "." + rkey %} {{"%-8s"|format(major_minor)}} {{"%-10s"|format(rdata)}} "{{vdata.code_name}}" {% endfor %} diff --git a/test/sanity/code-smell/no-dict-iteritems.sh b/test/sanity/code-smell/no-dict-iteritems.sh new file mode 100755 index 0000000000..9984c9c7bc --- /dev/null +++ b/test/sanity/code-smell/no-dict-iteritems.sh @@ -0,0 +1,21 @@ +#!/bin/sh + +ITERITEMS_USERS=$(grep -rI '\.iteritems' . \ + --exclude-dir .git \ + --exclude-dir .tox \ + --exclude-dir docsite \ + | grep -v \ + -e 'six\.iteritems' \ + -e lib/ansible/compat/six/_six.py \ + -e lib/ansible/module_utils/six.py \ + -e test/sanity/code-smell/no-dict-iteritems.sh \ + ) + +if [ "${ITERITEMS_USERS}" ]; then + echo 'iteritems has been removed in python3. Alternatives:' + echo ' for KEY, VALUE in DICT.items():' + echo ' from ansible.module_utils.six import iteritems ; for KEY, VALUE in iteritems(DICT):' + echo ' from ansible.compat.six import iteritems ; for KEY, VALUE in iteritems(DICT):' + echo "${ITERITEMS_USERS}" + exit 1 +fi diff --git a/test/sanity/code-smell/no-dict-itervalues.sh b/test/sanity/code-smell/no-dict-itervalues.sh new file mode 100755 index 0000000000..7a7494860f --- /dev/null +++ b/test/sanity/code-smell/no-dict-itervalues.sh @@ -0,0 +1,21 @@ +#!/bin/sh + +ITERVALUES_USERS=$(grep -rI '\.itervalues' . \ + --exclude-dir .git \ + --exclude-dir .tox \ + --exclude-dir docsite \ + | grep -v \ + -e 'six\.itervalues' \ + -e lib/ansible/compat/six/_six.py \ + -e lib/ansible/module_utils/six.py \ + -e test/sanity/code-smell/no-dict-itervalues.sh \ + ) + +if [ "${ITERVALUES_USERS}" ]; then + echo 'itervalues has been removed in python3. Alternatives:' + echo ' for VALUE in DICT.values():' + echo ' from ansible.module_utils.six import itervalues ; for VALUE in itervalues(DICT):' + echo ' from ansible.compat.six import itervalues ; for VALUE in itervalues(DICT):' + echo "${ITERVALUES_USERS}" + exit 1 +fi