Updates for deprecated modules in Python 3.12 and Invalid escape sequence issues - #2124
Conversation
- Replace distutils.version with packaging/distutils/custom fallback in WaagentLib.py - Replace imp module with importlib.util fallback in WAAgentUtil.py - Fix invalid escape sequences in HandlerUtil.py JSON processing - Add setuptools fallback for distutils removal in setup.py Resolves VMBackup extension failure on Debian 12 with Python 3.12. All changes maintain backward compatibility with older Python versions.
- Added conditional import for distro package - Implemented multi-tier fallback strategy in get_dist_info() - Enhanced error handling with proper logging - Maintains backward compatibility across all Python versions - Resolves AttributeError: module 'platform' has no attribute 'dist'
- Add try/except block for crypt import to handle Python 3.13+ where crypt module was removed - Update gen_password_hash() to gracefully handle missing crypt module - Add TODO comment for future cleanup of unused code in VMBackup extension - Maintains backward compatibility with older Python versions - Resolves import error while preserving all existing functionality
|
Extension success status result logs are present in the work item in PR description (ADO: 34492797). Also the rpc names are there @arisettisanjana |
The tests were run to verify on py3.13 versions so, just wanted to be sure that we have tested these changes for backward compatibility i.e with lesser python versions |
I haven't tested specifically for lesser python versions. But code will pickup the imports and logic for lesser python versions wherever needed. |
… 51000001082652)
Ubuntu 26.04 / Py 3.14 removed the stdlib modules 'crypt' (Py 3.13),
'distutils' (Py 3.12), and 'imp' (Py 3.12). VMBackup imports all three
unconditionally, so handle.py -enable crashes at file-load time before
any snapshot logic runs -> CRP reports VMExtensionProvisioningTimeout ->
customer backup fails.
Changes (2 files):
* VMBackup/main/WaagentLib.py
- Add PEP 263 encoding declaration (defense-in-depth vs future Unicode)
- Wrap 'import crypt' in try/except; crypt = None fallback
- Replace 'from distutils.version import LooseVersion' with 3-tier ladder:
packaging.version -> distutils.version -> ASCII-only hand-written class
- Guard gen_password_hash() with 'if crypt is None: raise NotImplementedError'
* VMBackup/main/Utils/WAAgentUtil.py
- Replace 'except ImportError' fallback with hasattr(importlib.util,
'module_from_spec') capability check (Py 2.7 raised AttributeError,
not ImportError - this was one of the PR Azure#2124 regressions)
- Drop outer 'except Exception: raise Exception(Cant load waagent)'
wrapper that was hiding the real traceback in ICM logs
This is a corrected re-landing of PR Azure#2124 (reverted by PR Azure#2163 due to
ICM 783505554). Defenses vs those regressions:
- Pure ASCII in modified files (byte-scan verified: 0 non-ASCII bytes)
- PEP 263 encoding declaration future-proofs against Unicode leaks
- hasattr() capability check is exception-type-independent
- Tier 3 LooseVersion class inherits from 'object' (new-style, correct
Py 2.7 rich-comparison dispatch)
Tested on Py 2.7 (Docker), 3.10, 3.12, 3.13, 3.14: 18/18 PASS.
End-to-end portal-triggered backup on Py 3.14 with branch code in
/var/lib/waagent/... completed with extension status: success and
snapshot URIs written to blob storage.
Fixes: ICM 51000001082652
Re-lands: PR Azure#2124 (reverted in PR Azure#2163)
Regression-tests: ICM 783505554
Related: Azure#2172
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
ADO Task: 34492797
Fix Python 3.12 compatibility issues
Resolves VMBackup extension failure on Debian 12 with Python 3.12. All changes maintain backward compatibility with older Python versions.
Fix platform.dist() deprecation error for Python 3.8+
Fix Python 3.13+ compatibility: Handle deprecated crypt module import