Browse Source

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
changes/29/823729/1 1.5.0
matbu 5 months ago
parent
commit
17e689536f
  1. 6
      validations_libs/community/init_validation.py
  2. 6
      validations_libs/constants.py
  3. 45
      validations_libs/tests/community/test_init_validation.py
  4. 7
      validations_libs/tests/fakes.py
  5. 25
      validations_libs/tests/test_utils.py
  6. 7
      validations_libs/utils.py

6
validations_libs/community/init_validation.py

@ -17,7 +17,11 @@
import logging
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

6
validations_libs/constants.py

@ -22,7 +22,11 @@ or as a fallback, when custom locations fail.
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'

45
validations_libs/tests/community/test_init_validation.py

@ -18,7 +18,14 @@ try:
except ImportError:
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 validations_libs import constants
@ -83,10 +90,10 @@ class TestCommunityValidation(TestCase):
self.assertEqual(co_val.playbook_basedir,
constants.COMMUNITY_PLAYBOOKS_DIR)
@mock.patch('pathlib.Path.iterdir',
@mock.patch('{}.Path.iterdir'.format(PATHLIB),
return_value=fakes.FAKE_ROLES_ITERDIR2)
@mock.patch('pathlib.Path.is_dir')
@mock.patch('pathlib.Path.exists', side_effect=[False, True])
@mock.patch('{}.Path.is_dir'.format(PATHLIB))
@mock.patch('{}.Path.exists'.format(PATHLIB), side_effect=[False, True])
def test_role_already_exists_in_comval(self,
mock_play_path_exists,
mock_path_is_dir,
@ -95,10 +102,10 @@ class TestCommunityValidation(TestCase):
co_val = cv(validation_name)
self.assertTrue(co_val.is_role_exists())
@mock.patch('pathlib.Path.iterdir',
@mock.patch('{}.Path.iterdir'.format(PATHLIB),
return_value=fakes.FAKE_ROLES_ITERDIR1)
@mock.patch('pathlib.Path.is_dir')
@mock.patch('pathlib.Path.exists', side_effect=[True, False])
@mock.patch('{}.Path.is_dir'.format(PATHLIB))
@mock.patch('{}.Path.exists'.format(PATHLIB), side_effect=[True, False])
def test_role_already_exists_in_non_comval(self,
mock_play_path_exists,
mock_path_is_dir,
@ -107,10 +114,10 @@ class TestCommunityValidation(TestCase):
co_val = cv(validation_name)
self.assertTrue(co_val.is_role_exists())
@mock.patch('pathlib.Path.iterdir',
@mock.patch('{}.Path.iterdir'.format(PATHLIB),
return_value=fakes.FAKE_ROLES_ITERDIR2)
@mock.patch('pathlib.Path.is_dir')
@mock.patch('pathlib.Path.exists', side_effect=[True, False])
@mock.patch('{}.Path.is_dir'.format(PATHLIB))
@mock.patch('{}.Path.exists'.format(PATHLIB), side_effect=[True, False])
def test_role_not_exists(self,
mock_path_exists,
mock_path_is_dir,
@ -119,10 +126,10 @@ class TestCommunityValidation(TestCase):
co_val = cv(validation_name)
self.assertFalse(co_val.is_role_exists())
@mock.patch('pathlib.Path.iterdir',
@mock.patch('{}.Path.iterdir'.format(PATHLIB),
return_value=fakes.FAKE_PLAYBOOKS_ITERDIR1)
@mock.patch('pathlib.Path.is_file')
@mock.patch('pathlib.Path.exists', side_effect=[True, False])
@mock.patch('{}.Path.is_file'.format(PATHLIB))
@mock.patch('{}.Path.exists'.format(PATHLIB), side_effect=[True, False])
def test_playbook_already_exists_in_non_comval(self,
mock_path_exists,
mock_path_is_file,
@ -131,10 +138,10 @@ class TestCommunityValidation(TestCase):
co_val = cv(validation_name)
self.assertTrue(co_val.is_playbook_exists())
@mock.patch('pathlib.Path.iterdir',
@mock.patch('{}.Path.iterdir'.format(PATHLIB),
return_value=fakes.FAKE_PLAYBOOKS_ITERDIR2)
@mock.patch('pathlib.Path.is_file')
@mock.patch('pathlib.Path.exists', side_effect=[False, True])
@mock.patch('{}.Path.is_file'.format(PATHLIB))
@mock.patch('{}.Path.exists'.format(PATHLIB), side_effect=[False, True])
def test_playbook_already_exists_in_comval(self,
mock_path_exists,
mock_path_is_file,
@ -143,10 +150,10 @@ class TestCommunityValidation(TestCase):
co_val = cv(validation_name)
self.assertTrue(co_val.is_playbook_exists())
@mock.patch('pathlib.Path.iterdir',
@mock.patch('{}.Path.iterdir'.format(PATHLIB),
return_value=fakes.FAKE_PLAYBOOKS_ITERDIR2)
@mock.patch('pathlib.Path.is_file')
@mock.patch('pathlib.Path.exists', side_effect=[True, False])
@mock.patch('{}.Path.is_file'.format(PATHLIB))
@mock.patch('{}.Path.exists'.format(PATHLIB), side_effect=[True, False])
def test_playbook_not_exists(self,
mock_path_exists,
mock_path_is_file,

7
validations_libs/tests/fakes.py

@ -13,7 +13,12 @@
# 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
VALIDATIONS_LIST = [{

25
validations_libs/tests/test_utils.py

@ -22,7 +22,14 @@ try:
except ImportError:
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 validations_libs import utils, constants
@ -542,13 +549,13 @@ class TestUtils(TestCase):
fakes.ANSIBLE_ENVIRONNMENT_CONFIG['ANSIBLE_STDOUT_CALLBACK'])
@mock.patch('validations_libs.utils.LOG', autospec=True)
@mock.patch('pathlib.Path.exists',
@mock.patch('{}.Path.exists'.format(PATHLIB),
return_value=False)
@mock.patch('pathlib.Path.is_dir',
@mock.patch('{}.Path.is_dir'.format(PATHLIB),
return_value=False)
@mock.patch('pathlib.Path.iterdir',
@mock.patch('{}.Path.iterdir'.format(PATHLIB),
return_value=iter([]))
@mock.patch('pathlib.Path.mkdir')
@mock.patch('{}.Path.mkdir'.format(PATHLIB))
def test_check_creation_community_validations_dir(self, mock_mkdir,
mock_iterdir,
mock_isdir,
@ -566,11 +573,11 @@ class TestUtils(TestCase):
)
@mock.patch('validations_libs.utils.LOG', autospec=True)
@mock.patch('pathlib.Path.is_dir', return_value=True)
@mock.patch('pathlib.Path.exists', return_value=True)
@mock.patch('pathlib.Path.iterdir',
@mock.patch('{}.Path.is_dir'.format(PATHLIB), return_value=True)
@mock.patch('{}.Path.exists'.format(PATHLIB), return_value=True)
@mock.patch('{}.Path.iterdir'.format(PATHLIB),
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,
mock_mkdir,
mock_iterdir,

7
validations_libs/utils.py

@ -24,7 +24,12 @@ import subprocess
import uuid
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.group import Group
from validations_libs.validation import Validation

Loading…
Cancel
Save