Be more cautious initing mimetypes

We use mimetypes to set file mime types for upload to log server
locations. We also override yaml files mime type to text/plain when
doing so. If we then call mimetypes.init() again this overrides the
previous yaml update to text/plain. This wasn't a problem until
python3.12 but python3.12 (on Ubuntu Noble specifically) seems to import
test cases in a different order which results in the mimetypes.init()
call in generate_manifest.py overriding the yaml mimetype set by the log
upload test cases.

Simply check if mimetypes is already inited before we init it again
which should avoid the problem entirely.

Note that this is likely to only ever by a testing issues as typically
ansible wouldn't import all of this code together but the unittest
runner does.

Change-Id: Ifb9137ddd89713cad546129c462ad94315100940
This commit is contained in:
Clark Boylan 2024-06-26 15:04:53 -07:00
parent 7a58814cda
commit a9a2f5ab50
3 changed files with 9 additions and 3 deletions

View File

@ -23,7 +23,9 @@ import sys
from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.basic import AnsibleModule
mimetypes.init() if not mimetypes.inited:
# We don't want to reinit and override any previously added types.
mimetypes.init()
def path_in_tree(root, path): def path_in_tree(root, path):

View File

@ -60,7 +60,9 @@ try:
except ImportError: except ImportError:
from collections import Sequence from collections import Sequence
mimetypes.init() if not mimetypes.inited:
# We don't want to reinit and override any previously inited types.
mimetypes.init()
mimetypes.add_type('text/plain', '.yaml') mimetypes.add_type('text/plain', '.yaml')
MAX_UPLOAD_THREADS = 24 MAX_UPLOAD_THREADS = 24

View File

@ -47,7 +47,9 @@ try:
except ImportError: except ImportError:
from collections import Sequence from collections import Sequence
mimetypes.init() if not mimetypes.inited:
# We don't want to reinit and override any previously added types.
mimetypes.init()
mimetypes.add_type('text/plain', '.yaml') mimetypes.add_type('text/plain', '.yaml')
MAX_UPLOAD_THREADS = 24 MAX_UPLOAD_THREADS = 24