Skip to content

Commit

Permalink
fix: [main] tarfile python3.11 compatibility fixes #117
Browse files Browse the repository at this point in the history
  • Loading branch information
cvandeplas committed Nov 5, 2024
1 parent fb63092 commit bb14dec
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/sysdiagnose/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)}')

Expand All @@ -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')
Expand Down
19 changes: 19 additions & 0 deletions tests/test_sysdiagnose.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from tests import SysdiagnoseTestCase
from sysdiagnose import Sysdiagnose
import unittest
import tempfile
import shutil
import glob


class TestSysdiagnose(SysdiagnoseTestCase):
Expand Down Expand Up @@ -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()

0 comments on commit bb14dec

Please sign in to comment.