From 3f27e5e0804b7b301fabab45bfd882a39a3b495c Mon Sep 17 00:00:00 2001 From: Toshio Kuratomi Date: Tue, 21 Oct 2014 01:09:11 -0400 Subject: [PATCH] Compat tree for code dealing with compatibility between python versions --- v2/ansible/compat/__init__.py | 27 +++++++++++++++++++ v2/ansible/compat/configparser.py | 30 ++++++++++++++++++++++ v2/ansible/compat/tests/__init__.py | 40 +++++++++++++++++++++++++++++ v2/ansible/compat/tests/mock.py | 38 +++++++++++++++++++++++++++ v2/ansible/compat/tests/unittest.py | 36 ++++++++++++++++++++++++++ v2/ansible/constants.py | 6 +---- 6 files changed, 172 insertions(+), 5 deletions(-) create mode 100644 v2/ansible/compat/__init__.py create mode 100644 v2/ansible/compat/configparser.py create mode 100644 v2/ansible/compat/tests/__init__.py create mode 100644 v2/ansible/compat/tests/mock.py create mode 100644 v2/ansible/compat/tests/unittest.py diff --git a/v2/ansible/compat/__init__.py b/v2/ansible/compat/__init__.py new file mode 100644 index 0000000000..ab861135c7 --- /dev/null +++ b/v2/ansible/compat/__init__.py @@ -0,0 +1,27 @@ +# (c) 2014, Toshio Kuratomi +# +# This file is part of Ansible +# +# Ansible is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Ansible is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Ansible. If not, see . + +# Make coding more python3-ish +from __future__ import (absolute_import, division, print_function) +__metaclass__ = type + +''' +Compat library for ansible. This contains compatiblity definitions for older python +When we need to import a module differently depending on python version, do it +here. Then in the code we can simply import from compat in order to get what we want. +''' + diff --git a/v2/ansible/compat/configparser.py b/v2/ansible/compat/configparser.py new file mode 100644 index 0000000000..7cce642376 --- /dev/null +++ b/v2/ansible/compat/configparser.py @@ -0,0 +1,30 @@ +# (c) 2014, Toshio Kuratomi +# +# This file is part of Ansible +# +# Ansible is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Ansible is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Ansible. If not, see . + +# Make coding more python3-ish +from __future__ import (absolute_import, division, print_function) +__metaclass__ = type + +''' +Compat module for Python3.x's configparser +''' + +# Python 2.7 +try: + from configparser import * +except ImportError: + from ConfigParser import * diff --git a/v2/ansible/compat/tests/__init__.py b/v2/ansible/compat/tests/__init__.py new file mode 100644 index 0000000000..fc05b2549b --- /dev/null +++ b/v2/ansible/compat/tests/__init__.py @@ -0,0 +1,40 @@ +# (c) 2014, Toshio Kuratomi +# +# This file is part of Ansible +# +# Ansible is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Ansible is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Ansible. If not, see . + +# Make coding more python3-ish +from __future__ import (absolute_import, division, print_function) +__metaclass__ = type + +''' +This module contains things that are only needed for compat in the testsuites, +not in ansible itself. If you are not installing the test suite, you can +safely remove this subdirectory. +''' + +# +# Compat for python2.7 +# + +# One unittest needs to import builtins via __import__() so we need to have +# the string that represents it +try: + import __builtin__ +except ImportError: + BUILTINS = 'builtins' +else: + BUILTINS = '__builtin__' + diff --git a/v2/ansible/compat/tests/mock.py b/v2/ansible/compat/tests/mock.py new file mode 100644 index 0000000000..0614391c4b --- /dev/null +++ b/v2/ansible/compat/tests/mock.py @@ -0,0 +1,38 @@ +# (c) 2014, Toshio Kuratomi +# +# This file is part of Ansible +# +# Ansible is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Ansible is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Ansible. If not, see . + +# Make coding more python3-ish +from __future__ import (absolute_import, division, print_function) +__metaclass__ = type + +''' +Compat module for Python3.x's unittest.mock module +''' + +# Python 2.7 + +# Note: Could use the pypi mock library on python3.x as well as python2.x. It +# is the same as the python3 stdlib mock library + +try: + from unittest.mock import * +except ImportError: + # Python 2 + try: + from mock import * + except ImportError: + print('You need the mock library installed on python2.x to run tests') diff --git a/v2/ansible/compat/tests/unittest.py b/v2/ansible/compat/tests/unittest.py new file mode 100644 index 0000000000..a629849b31 --- /dev/null +++ b/v2/ansible/compat/tests/unittest.py @@ -0,0 +1,36 @@ +# (c) 2014, Toshio Kuratomi +# +# This file is part of Ansible +# +# Ansible is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Ansible is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Ansible. If not, see . + +# Make coding more python3-ish +from __future__ import (absolute_import, division, print_function) +__metaclass__ = type + +''' +Compat module for Python2.7's unittest module +''' + +import sys + +# Python 2.6 +if sys.version_info < (2, 7): + try: + # Need unittest2 on python2.6 + from unittest2 import * + except ImportError: + print('You need unittest2 installed on python2.6.x to run tests') +else: + from unittest import * diff --git a/v2/ansible/constants.py b/v2/ansible/constants.py index 97d6870a3d..e74720b8a6 100644 --- a/v2/ansible/constants.py +++ b/v2/ansible/constants.py @@ -23,11 +23,7 @@ import os import pwd import sys -try: - import configparser -except ImportError: - # Python 2.7 - import ConfigParser as configparser +from . compat import configparser from string import ascii_letters, digits