Add backward compatibility for python pathlib module
Pathlib is only available in python 3.6, this patch allow to run lower version of python with pathlib2. Change-Id: I36c3560815624eb3f4bc3d427d9549d73c449198
This commit is contained in:
parent
68adc1fe16
commit
17e689536f
@ -17,7 +17,11 @@
|
|||||||
|
|
||||||
import logging
|
import logging
|
||||||
import re
|
import re
|
||||||
from pathlib import Path
|
# @matbu backward compatibility for stable/train
|
||||||
|
try:
|
||||||
|
from pathlib import Path
|
||||||
|
except ImportError:
|
||||||
|
from pathlib2 import Path
|
||||||
|
|
||||||
from validations_libs import constants, utils
|
from validations_libs import constants, utils
|
||||||
|
|
||||||
|
@ -22,7 +22,11 @@ or as a fallback, when custom locations fail.
|
|||||||
|
|
||||||
import os
|
import os
|
||||||
|
|
||||||
from pathlib import Path
|
# @matbu backward compatibility for stable/train
|
||||||
|
try:
|
||||||
|
from pathlib import Path
|
||||||
|
except ImportError:
|
||||||
|
from pathlib2 import Path
|
||||||
|
|
||||||
DEFAULT_VALIDATIONS_BASEDIR = '/usr/share/ansible'
|
DEFAULT_VALIDATIONS_BASEDIR = '/usr/share/ansible'
|
||||||
|
|
||||||
|
@ -18,7 +18,14 @@ try:
|
|||||||
except ImportError:
|
except ImportError:
|
||||||
import mock
|
import mock
|
||||||
|
|
||||||
from pathlib import PosixPath
|
# @matbu backward compatibility for stable/train
|
||||||
|
try:
|
||||||
|
from pathlib import PosixPath
|
||||||
|
PATHLIB = 'pathlib'
|
||||||
|
except ImportError:
|
||||||
|
from pathlib2 import PosixPath
|
||||||
|
PATHLIB = 'pathlib2'
|
||||||
|
|
||||||
from unittest import TestCase
|
from unittest import TestCase
|
||||||
|
|
||||||
from validations_libs import constants
|
from validations_libs import constants
|
||||||
@ -83,10 +90,10 @@ class TestCommunityValidation(TestCase):
|
|||||||
self.assertEqual(co_val.playbook_basedir,
|
self.assertEqual(co_val.playbook_basedir,
|
||||||
constants.COMMUNITY_PLAYBOOKS_DIR)
|
constants.COMMUNITY_PLAYBOOKS_DIR)
|
||||||
|
|
||||||
@mock.patch('pathlib.Path.iterdir',
|
@mock.patch('{}.Path.iterdir'.format(PATHLIB),
|
||||||
return_value=fakes.FAKE_ROLES_ITERDIR2)
|
return_value=fakes.FAKE_ROLES_ITERDIR2)
|
||||||
@mock.patch('pathlib.Path.is_dir')
|
@mock.patch('{}.Path.is_dir'.format(PATHLIB))
|
||||||
@mock.patch('pathlib.Path.exists', side_effect=[False, True])
|
@mock.patch('{}.Path.exists'.format(PATHLIB), side_effect=[False, True])
|
||||||
def test_role_already_exists_in_comval(self,
|
def test_role_already_exists_in_comval(self,
|
||||||
mock_play_path_exists,
|
mock_play_path_exists,
|
||||||
mock_path_is_dir,
|
mock_path_is_dir,
|
||||||
@ -95,10 +102,10 @@ class TestCommunityValidation(TestCase):
|
|||||||
co_val = cv(validation_name)
|
co_val = cv(validation_name)
|
||||||
self.assertTrue(co_val.is_role_exists())
|
self.assertTrue(co_val.is_role_exists())
|
||||||
|
|
||||||
@mock.patch('pathlib.Path.iterdir',
|
@mock.patch('{}.Path.iterdir'.format(PATHLIB),
|
||||||
return_value=fakes.FAKE_ROLES_ITERDIR1)
|
return_value=fakes.FAKE_ROLES_ITERDIR1)
|
||||||
@mock.patch('pathlib.Path.is_dir')
|
@mock.patch('{}.Path.is_dir'.format(PATHLIB))
|
||||||
@mock.patch('pathlib.Path.exists', side_effect=[True, False])
|
@mock.patch('{}.Path.exists'.format(PATHLIB), side_effect=[True, False])
|
||||||
def test_role_already_exists_in_non_comval(self,
|
def test_role_already_exists_in_non_comval(self,
|
||||||
mock_play_path_exists,
|
mock_play_path_exists,
|
||||||
mock_path_is_dir,
|
mock_path_is_dir,
|
||||||
@ -107,10 +114,10 @@ class TestCommunityValidation(TestCase):
|
|||||||
co_val = cv(validation_name)
|
co_val = cv(validation_name)
|
||||||
self.assertTrue(co_val.is_role_exists())
|
self.assertTrue(co_val.is_role_exists())
|
||||||
|
|
||||||
@mock.patch('pathlib.Path.iterdir',
|
@mock.patch('{}.Path.iterdir'.format(PATHLIB),
|
||||||
return_value=fakes.FAKE_ROLES_ITERDIR2)
|
return_value=fakes.FAKE_ROLES_ITERDIR2)
|
||||||
@mock.patch('pathlib.Path.is_dir')
|
@mock.patch('{}.Path.is_dir'.format(PATHLIB))
|
||||||
@mock.patch('pathlib.Path.exists', side_effect=[True, False])
|
@mock.patch('{}.Path.exists'.format(PATHLIB), side_effect=[True, False])
|
||||||
def test_role_not_exists(self,
|
def test_role_not_exists(self,
|
||||||
mock_path_exists,
|
mock_path_exists,
|
||||||
mock_path_is_dir,
|
mock_path_is_dir,
|
||||||
@ -119,10 +126,10 @@ class TestCommunityValidation(TestCase):
|
|||||||
co_val = cv(validation_name)
|
co_val = cv(validation_name)
|
||||||
self.assertFalse(co_val.is_role_exists())
|
self.assertFalse(co_val.is_role_exists())
|
||||||
|
|
||||||
@mock.patch('pathlib.Path.iterdir',
|
@mock.patch('{}.Path.iterdir'.format(PATHLIB),
|
||||||
return_value=fakes.FAKE_PLAYBOOKS_ITERDIR1)
|
return_value=fakes.FAKE_PLAYBOOKS_ITERDIR1)
|
||||||
@mock.patch('pathlib.Path.is_file')
|
@mock.patch('{}.Path.is_file'.format(PATHLIB))
|
||||||
@mock.patch('pathlib.Path.exists', side_effect=[True, False])
|
@mock.patch('{}.Path.exists'.format(PATHLIB), side_effect=[True, False])
|
||||||
def test_playbook_already_exists_in_non_comval(self,
|
def test_playbook_already_exists_in_non_comval(self,
|
||||||
mock_path_exists,
|
mock_path_exists,
|
||||||
mock_path_is_file,
|
mock_path_is_file,
|
||||||
@ -131,10 +138,10 @@ class TestCommunityValidation(TestCase):
|
|||||||
co_val = cv(validation_name)
|
co_val = cv(validation_name)
|
||||||
self.assertTrue(co_val.is_playbook_exists())
|
self.assertTrue(co_val.is_playbook_exists())
|
||||||
|
|
||||||
@mock.patch('pathlib.Path.iterdir',
|
@mock.patch('{}.Path.iterdir'.format(PATHLIB),
|
||||||
return_value=fakes.FAKE_PLAYBOOKS_ITERDIR2)
|
return_value=fakes.FAKE_PLAYBOOKS_ITERDIR2)
|
||||||
@mock.patch('pathlib.Path.is_file')
|
@mock.patch('{}.Path.is_file'.format(PATHLIB))
|
||||||
@mock.patch('pathlib.Path.exists', side_effect=[False, True])
|
@mock.patch('{}.Path.exists'.format(PATHLIB), side_effect=[False, True])
|
||||||
def test_playbook_already_exists_in_comval(self,
|
def test_playbook_already_exists_in_comval(self,
|
||||||
mock_path_exists,
|
mock_path_exists,
|
||||||
mock_path_is_file,
|
mock_path_is_file,
|
||||||
@ -143,10 +150,10 @@ class TestCommunityValidation(TestCase):
|
|||||||
co_val = cv(validation_name)
|
co_val = cv(validation_name)
|
||||||
self.assertTrue(co_val.is_playbook_exists())
|
self.assertTrue(co_val.is_playbook_exists())
|
||||||
|
|
||||||
@mock.patch('pathlib.Path.iterdir',
|
@mock.patch('{}.Path.iterdir'.format(PATHLIB),
|
||||||
return_value=fakes.FAKE_PLAYBOOKS_ITERDIR2)
|
return_value=fakes.FAKE_PLAYBOOKS_ITERDIR2)
|
||||||
@mock.patch('pathlib.Path.is_file')
|
@mock.patch('{}.Path.is_file'.format(PATHLIB))
|
||||||
@mock.patch('pathlib.Path.exists', side_effect=[True, False])
|
@mock.patch('{}.Path.exists'.format(PATHLIB), side_effect=[True, False])
|
||||||
def test_playbook_not_exists(self,
|
def test_playbook_not_exists(self,
|
||||||
mock_path_exists,
|
mock_path_exists,
|
||||||
mock_path_is_file,
|
mock_path_is_file,
|
||||||
|
@ -13,7 +13,12 @@
|
|||||||
# under the License.
|
# under the License.
|
||||||
#
|
#
|
||||||
|
|
||||||
from pathlib import PosixPath
|
# @matbu backward compatibility for stable/train
|
||||||
|
try:
|
||||||
|
from pathlib import PosixPath
|
||||||
|
except ImportError:
|
||||||
|
from pathlib2 import PosixPath
|
||||||
|
|
||||||
from validations_libs import constants
|
from validations_libs import constants
|
||||||
|
|
||||||
VALIDATIONS_LIST = [{
|
VALIDATIONS_LIST = [{
|
||||||
|
@ -22,7 +22,14 @@ try:
|
|||||||
except ImportError:
|
except ImportError:
|
||||||
import mock
|
import mock
|
||||||
|
|
||||||
from pathlib import PosixPath
|
# @matbu backward compatibility for stable/train
|
||||||
|
try:
|
||||||
|
from pathlib import PosixPath
|
||||||
|
PATHLIB = 'pathlib'
|
||||||
|
except ImportError:
|
||||||
|
from pathlib2 import PosixPath
|
||||||
|
PATHLIB = 'pathlib2'
|
||||||
|
|
||||||
from unittest import TestCase
|
from unittest import TestCase
|
||||||
|
|
||||||
from validations_libs import utils, constants
|
from validations_libs import utils, constants
|
||||||
@ -542,13 +549,13 @@ class TestUtils(TestCase):
|
|||||||
fakes.ANSIBLE_ENVIRONNMENT_CONFIG['ANSIBLE_STDOUT_CALLBACK'])
|
fakes.ANSIBLE_ENVIRONNMENT_CONFIG['ANSIBLE_STDOUT_CALLBACK'])
|
||||||
|
|
||||||
@mock.patch('validations_libs.utils.LOG', autospec=True)
|
@mock.patch('validations_libs.utils.LOG', autospec=True)
|
||||||
@mock.patch('pathlib.Path.exists',
|
@mock.patch('{}.Path.exists'.format(PATHLIB),
|
||||||
return_value=False)
|
return_value=False)
|
||||||
@mock.patch('pathlib.Path.is_dir',
|
@mock.patch('{}.Path.is_dir'.format(PATHLIB),
|
||||||
return_value=False)
|
return_value=False)
|
||||||
@mock.patch('pathlib.Path.iterdir',
|
@mock.patch('{}.Path.iterdir'.format(PATHLIB),
|
||||||
return_value=iter([]))
|
return_value=iter([]))
|
||||||
@mock.patch('pathlib.Path.mkdir')
|
@mock.patch('{}.Path.mkdir'.format(PATHLIB))
|
||||||
def test_check_creation_community_validations_dir(self, mock_mkdir,
|
def test_check_creation_community_validations_dir(self, mock_mkdir,
|
||||||
mock_iterdir,
|
mock_iterdir,
|
||||||
mock_isdir,
|
mock_isdir,
|
||||||
@ -566,11 +573,11 @@ class TestUtils(TestCase):
|
|||||||
)
|
)
|
||||||
|
|
||||||
@mock.patch('validations_libs.utils.LOG', autospec=True)
|
@mock.patch('validations_libs.utils.LOG', autospec=True)
|
||||||
@mock.patch('pathlib.Path.is_dir', return_value=True)
|
@mock.patch('{}.Path.is_dir'.format(PATHLIB), return_value=True)
|
||||||
@mock.patch('pathlib.Path.exists', return_value=True)
|
@mock.patch('{}.Path.exists'.format(PATHLIB), return_value=True)
|
||||||
@mock.patch('pathlib.Path.iterdir',
|
@mock.patch('{}.Path.iterdir'.format(PATHLIB),
|
||||||
return_value=fakes.FAKE_COVAL_MISSING_SUBDIR_ITERDIR1)
|
return_value=fakes.FAKE_COVAL_MISSING_SUBDIR_ITERDIR1)
|
||||||
@mock.patch('pathlib.Path.mkdir')
|
@mock.patch('{}.Path.mkdir'.format(PATHLIB))
|
||||||
def test_check_community_validations_dir_with_missing_subdir(self,
|
def test_check_community_validations_dir_with_missing_subdir(self,
|
||||||
mock_mkdir,
|
mock_mkdir,
|
||||||
mock_iterdir,
|
mock_iterdir,
|
||||||
|
@ -24,7 +24,12 @@ import subprocess
|
|||||||
import uuid
|
import uuid
|
||||||
|
|
||||||
from os.path import join
|
from os.path import join
|
||||||
from pathlib import Path
|
# @matbu backward compatibility for stable/train
|
||||||
|
try:
|
||||||
|
from pathlib import Path
|
||||||
|
except ImportError:
|
||||||
|
from pathlib2 import Path
|
||||||
|
|
||||||
from validations_libs import constants
|
from validations_libs import constants
|
||||||
from validations_libs.group import Group
|
from validations_libs.group import Group
|
||||||
from validations_libs.validation import Validation
|
from validations_libs.validation import Validation
|
||||||
|
Loading…
x
Reference in New Issue
Block a user