mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Compat tree for code dealing with compatibility between python versions
This commit is contained in:
parent
da9d87b1d4
commit
3f27e5e080
6 changed files with 172 additions and 5 deletions
27
v2/ansible/compat/__init__.py
Normal file
27
v2/ansible/compat/__init__.py
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
# (c) 2014, Toshio Kuratomi <tkuratomi@ansible.com>
|
||||||
|
#
|
||||||
|
# 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 <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
# 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.
|
||||||
|
'''
|
||||||
|
|
30
v2/ansible/compat/configparser.py
Normal file
30
v2/ansible/compat/configparser.py
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
# (c) 2014, Toshio Kuratomi <tkuratomi@ansible.com>
|
||||||
|
#
|
||||||
|
# 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 <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
# 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 *
|
40
v2/ansible/compat/tests/__init__.py
Normal file
40
v2/ansible/compat/tests/__init__.py
Normal file
|
@ -0,0 +1,40 @@
|
||||||
|
# (c) 2014, Toshio Kuratomi <tkuratomi@ansible.com>
|
||||||
|
#
|
||||||
|
# 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 <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
# 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__'
|
||||||
|
|
38
v2/ansible/compat/tests/mock.py
Normal file
38
v2/ansible/compat/tests/mock.py
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
# (c) 2014, Toshio Kuratomi <tkuratomi@ansible.com>
|
||||||
|
#
|
||||||
|
# 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 <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
# 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')
|
36
v2/ansible/compat/tests/unittest.py
Normal file
36
v2/ansible/compat/tests/unittest.py
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
# (c) 2014, Toshio Kuratomi <tkuratomi@ansible.com>
|
||||||
|
#
|
||||||
|
# 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 <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
# 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 *
|
|
@ -23,11 +23,7 @@ import os
|
||||||
import pwd
|
import pwd
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
try:
|
from . compat import configparser
|
||||||
import configparser
|
|
||||||
except ImportError:
|
|
||||||
# Python 2.7
|
|
||||||
import ConfigParser as configparser
|
|
||||||
|
|
||||||
from string import ascii_letters, digits
|
from string import ascii_letters, digits
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue