packaging: extract python exe info to own function
authorGregory Szorc <gregory.szorc@gmail.com>
Thu, 07 Mar 2019 10:49:59 -0800
changeset 41914 7d1211168863
parent 41913 5e923355c595
child 41915 a2e191a937a9
packaging: extract python exe info to own function This is generic functionality. We'll need it for WIX. As part of the port, we expose the full version and return the data as a dict. Differential Revision: https://phab.mercurial-scm.org/D6090
contrib/packaging/hgpackaging/inno.py
contrib/packaging/hgpackaging/util.py
--- a/contrib/packaging/hgpackaging/inno.py	Thu Mar 07 10:36:20 2019 -0800
+++ b/contrib/packaging/hgpackaging/inno.py	Thu Mar 07 10:49:59 2019 -0800
@@ -19,14 +19,10 @@
     extract_tar_to_directory,
     extract_zip_to_directory,
     find_vc_runtime_files,
+    python_exe_info,
 )
 
 
-PRINT_PYTHON_INFO = '''
-import platform, sys; print("%s:%d" % (platform.architecture()[0], sys.version_info[0]))
-'''.strip()
-
-
 def build(source_dir: pathlib.Path, build_dir: pathlib.Path,
           python_exe: pathlib.Path, iscc_exe: pathlib.Path,
           version=None):
@@ -50,23 +46,18 @@
     # architecture.
     vc_x64 = r'\x64' in os.environ['LIB']
 
-    res = subprocess.run(
-        [str(python_exe), '-c', PRINT_PYTHON_INFO],
-        capture_output=True, check=True)
-
-    py_arch, py_version = res.stdout.decode('utf-8').split(':')
-    py_version = int(py_version)
+    py_info = python_exe_info(python_exe)
 
     if vc_x64:
-        if py_arch != '64bit':
+        if py_info['arch'] != '64bit':
             raise Exception('architecture mismatch: Visual C++ environment '
                             'is configured for 64-bit but Python is 32-bit')
     else:
-        if py_arch != '32bit':
+        if py_info['arch'] != '32bit':
             raise Exception('architecture mismatch: Visual C++ environment '
                             'is configured for 32-bit but Python is 64-bit')
 
-    if py_version != 2:
+    if py_info['py3']:
         raise Exception('Only Python 2 is currently supported')
 
     build_dir.mkdir(exist_ok=True)
--- a/contrib/packaging/hgpackaging/util.py	Thu Mar 07 10:36:20 2019 -0800
+++ b/contrib/packaging/hgpackaging/util.py	Thu Mar 07 10:49:59 2019 -0800
@@ -7,8 +7,10 @@
 
 # no-check-code because Python 3 native.
 
+import distutils.version
 import os
 import pathlib
+import subprocess
 import tarfile
 import zipfile
 
@@ -46,3 +48,26 @@
         d / 'msvcr90.dll',
         winsxs / 'Manifests' / ('%s.manifest' % version),
     ]
+
+
+PRINT_PYTHON_INFO = '''
+import platform; print("%s:%s" % (platform.architecture()[0], platform.python_version()))
+'''.strip()
+
+
+def python_exe_info(python_exe: pathlib.Path):
+    """Obtain information about a Python executable."""
+
+    res = subprocess.run(
+        [str(python_exe), '-c', PRINT_PYTHON_INFO],
+        capture_output=True, check=True)
+
+    arch, version = res.stdout.decode('utf-8').split(':')
+
+    version = distutils.version.LooseVersion(version)
+
+    return {
+        'arch': arch,
+        'version': version,
+        'py3': version >= distutils.version.LooseVersion('3'),
+    }