From bb14dec7f563e69e6ffba93e48c8f0eefcd31834 Mon Sep 17 00:00:00 2001 From: Christophe Vandeplas Date: Tue, 5 Nov 2024 11:17:42 +0100 Subject: [PATCH] fix: [main] tarfile python3.11 compatibility fixes #117 --- src/sysdiagnose/__init__.py | 10 ++++++++-- tests/test_sysdiagnose.py | 19 +++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/sysdiagnose/__init__.py b/src/sysdiagnose/__init__.py index a09f571..1d6b086 100644 --- a/src/sysdiagnose/__init__.py +++ b/src/sysdiagnose/__init__.py @@ -131,6 +131,12 @@ def create_case(self, sysdiagnose_file: str, force: bool = False, case_id: bool # extract sysdiagnose files try: tf.extractall(path=case_data_folder, filter=None) + except TypeError: + # python 3.11 compatibility + try: + tf.extractall(path=case_data_folder) + except Exception as e: + raise Exception(f'Error while decompressing sysdiagnose file. Reason: {str(e)}') except Exception as e: raise Exception(f'Error while decompressing sysdiagnose file. Reason: {str(e)}') @@ -146,8 +152,8 @@ def create_case(self, sysdiagnose_file: str, force: bool = False, case_id: bool case['serial_number'] = remotectl_dumpstate_json['Local device']['Properties']['SerialNumber'] case['unique_device_id'] = remotectl_dumpstate_json['Local device']['Properties']['UniqueDeviceID'] case['ios_version'] = remotectl_dumpstate_json['Local device']['Properties']['OSVersion'] - except (KeyError, TypeError) as e: - logger.warning(f"WARNING: Could not parse remotectl_dumpstate, and therefore extract serial numbers.", exc_info=True) + except (KeyError, TypeError): + logger.warning("WARNING: Could not parse remotectl_dumpstate, and therefore extract serial numbers.", exc_info=True) try: case['date'] = remotectl_dumpstate_parser.sysdiagnose_creation_datetime.isoformat(timespec='microseconds') diff --git a/tests/test_sysdiagnose.py b/tests/test_sysdiagnose.py index e3f0271..adb66f7 100644 --- a/tests/test_sysdiagnose.py +++ b/tests/test_sysdiagnose.py @@ -1,6 +1,9 @@ from tests import SysdiagnoseTestCase from sysdiagnose import Sysdiagnose import unittest +import tempfile +import shutil +import glob class TestSysdiagnose(SysdiagnoseTestCase): @@ -33,6 +36,22 @@ def test_get_case_ids(self): ids = sd.get_case_ids() self.assertListEqual(ids, ['1', '2', 'foo']) + def test_create_case(self): + # Create a temporary directory + try: + temp_dir = tempfile.mkdtemp() + sd = Sysdiagnose(cases_path=temp_dir) + # take the first sysdiagnose tar archive that's there and try it out + sd_archive_files = [name for name in glob.glob('tests/testdata*/**/*.tar.gz', recursive=True)] + for archive_file in sd_archive_files: + print(f"Creating case from {archive_file}") + sd.create_case(archive_file) + break + + finally: + # Ensure the temporary directory is cleaned up after the test + shutil.rmtree(temp_dir) + if __name__ == '__main__': unittest.main()