def check_for_backend_python_library_inconsistencies(): """Checks the state of the 'third_party/python_libs' folder and compares it to the required libraries specified in 'requirements.txt'. If any inconsistencies are found, the script displays the inconsistencies and exits. """ mismatches = install_backend_python_libs.get_mismatches() if mismatches: python_utils.PRINT( 'Your currently installed python libraries do not match the\n' 'libraries listed in your "requirements.txt" file. Here is a\n' 'full list of library/version discrepancies:\n') python_utils.PRINT( '{:<35} |{:<25}|{:<25}'.format( 'Library', 'Requirements Version', 'Currently Installed Version')) for library_name, version_strings in mismatches.items(): python_utils.PRINT('{!s:<35} |{!s:<25}|{!s:<25}'.format( library_name, version_strings[0], version_strings[1])) python_utils.PRINT('\n') common.print_each_string_after_two_new_lines([ 'Please fix these discrepancies by editing the `requirements.in`\n' 'file or running `scripts.install_third_party` to regenerate\n' 'the `third_party/python_libs` directory.\n']) sys.exit(1) else: python_utils.PRINT( 'Python dependencies consistency check succeeded.')
def test_multiple_discrepancies_returns_correct_mismatches(self): swap_requirements = self.swap(common, 'COMPILED_REQUIREMENTS_FILE_PATH', self.REQUIREMENTS_TEST_TXT_FILE_PATH) def mock_find_distributions(paths): # pylint: disable=unused-argument return [ Distribution('dependency1', '1.5.1', {}), Distribution('dependency2', '4.9.1.2', {}), Distribution( 'dependency5', '0.5.3', { 'direct_url.json': json.dumps({ 'url': 'git://github.com/oppia/dependency5', 'vcs_info': { 'vcs': 'git', 'commit_id': 'b' * 40 }, }) }), Distribution( 'dependency6', '0.5.3', { 'direct_url.json': json.dumps({ 'url': 'git://github.com/oppia/dependency6', 'vcs_info': { 'vcs': 'git', 'commit_id': 'z' * 40 }, }) }) ] swap_find_distributions = self.swap(pkg_resources, 'find_distributions', mock_find_distributions) with swap_requirements, swap_find_distributions: self.assertEqual( install_backend_python_libs.get_mismatches(), { u'dependency1': (u'1.6.1', u'1.5.1'), u'dependency2': (u'4.9.1', u'4.9.1.2'), u'dependency3': (u'3.1.5', None), u'dependency4': (u'0.3.0.1', None), u'dependency5': (self.get_git_version_string('dependency5', 'a'), self.get_git_version_string('dependency5', 'b')), u'dependency6': (None, self.get_git_version_string('dependency6', 'z')), u'dependency7': (self.get_git_version_string('dependency7', 'b'), None), })
def test_multiple_discrepancies_returns_correct_mismatches(self): swap_requirements = self.swap(common, 'COMPILED_REQUIREMENTS_FILE_PATH', self.TEST_REQUIREMENTS_TXT_FILE_PATH) def mock_find_distributions(paths): # pylint: disable=unused-argument class Distribution(python_utils.OBJECT): """Distribution object containing python library information.""" def __init__(self, library_name, version_string): """Creates mock distribution metadata class that contains the name and version information for a python library. Args: library_name: str. The name of the library this object is representing. version_string: str. The stringified version of this library. """ self.project_name = library_name self.version = version_string return [ Distribution('dependency1', '1.5.1'), Distribution('dependency2', '4.9.1.2'), Distribution('dependency5', '0.5.3') ] swap_find_distributions = self.swap(pkg_resources, 'find_distributions', mock_find_distributions) with swap_requirements, swap_find_distributions: self.assertEqual( { u'dependency1': (u'1.6.1', u'1.5.1'), u'dependency2': (u'4.9.1', u'4.9.1.2'), u'dependency3': (u'3.1.5', None), u'dependency4': (u'0.3.0.1', None), u'dependency5': (None, u'0.5.3') }, install_backend_python_libs.get_mismatches())
def test_ignored_library_name_mismatches_are_respected(self): swap_requirements = self.swap( common, 'COMPILED_REQUIREMENTS_FILE_PATH', self.TEST_IGNORABLE_REQUIREMENTS_TXT_FILE_PATH) def mock_find_distributions(paths): # pylint: disable=unused-argument class Distribution(python_utils.OBJECT): """Distribution object containing python library information.""" def __init__(self, library_name, version_string): """Creates mock distribution metadata class that contains the name and version information for a python library. Args: library_name: str. The name of the library this object is representing. version_string: str. The stringified version of this library. """ self.project_name = library_name self.version = version_string return [ Distribution('dependency1', '1.5.1'), Distribution('dependency2', '4.9.1.2'), Distribution('dependency3', '0.5.3'), ] swap_find_distributions = self.swap( pkg_resources, 'find_distributions', mock_find_distributions) swap_ignored_names = self.swap( install_backend_python_libs, 'IGNORED_LIBRARY_NAME_MISMATCHES', ('dependency3', 'dependency4')) with swap_requirements, swap_find_distributions, swap_ignored_names: self.assertEqual( { u'dependency1': (u'1.6.1', u'1.5.1'), u'dependency2': (u'4.9.1', u'4.9.1.2'), }, install_backend_python_libs.get_mismatches())