packaging: move Inno Setup core logic into a module
authorGregory Szorc <gregory.szorc@gmail.com>
Thu, 07 Mar 2019 15:43:14 -0800
changeset 41911 dc7827a9ba64
parent 41910 1440dd6164f2
child 41912 d4bf73ea06de
packaging: move Inno Setup core logic into a module Aspects of building the Inno Setup and WIX installers are shared. It will make sense for them to share code. Plus, having code in a reusable library (as opposed to a standalone script) is just a better approach. This commit moves the core logic to build the Inno Setup installer into the hgpackaging package. inno/build.py is now a simple frontend script that calls into a module to do the bulk of the work. As part of this change, I also found a typo in build() where it was referencing "iscc" instead of "iscc_exe." Because "iscc" was in the global scope via the only caller, things just happened to work before. Another benefit of always using functions and not putting global code for __main__ in the same file as library code. Differential Revision: https://phab.mercurial-scm.org/D6087
contrib/packaging/hgpackaging/inno.py
contrib/packaging/inno/build.py
tests/test-check-code.t
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/contrib/packaging/hgpackaging/inno.py	Thu Mar 07 15:43:14 2019 -0800
@@ -0,0 +1,169 @@
+# inno.py - Inno Setup functionality.
+#
+# Copyright 2019 Gregory Szorc <gregory.szorc@gmail.com>
+#
+# This software may be used and distributed according to the terms of the
+# GNU General Public License version 2 or any later version.
+
+# no-check-code because Python 3 native.
+
+import os
+import pathlib
+import shutil
+import subprocess
+import tempfile
+
+from .downloads import (
+    download_entry,
+)
+from .util import (
+    extract_tar_to_directory,
+    extract_zip_to_directory,
+    find_vc_runtime_files,
+)
+
+
+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):
+    """Build the Inno installer.
+
+    Build files will be placed in ``build_dir``.
+
+    py2exe's setup.py doesn't use setuptools. It doesn't have modern logic
+    for finding the Python 2.7 toolchain. So, we require the environment
+    to already be configured with an active toolchain.
+    """
+    if not iscc_exe.exists():
+        raise Exception('%s does not exist' % iscc_exe)
+
+    if 'VCINSTALLDIR' not in os.environ:
+        raise Exception('not running from a Visual C++ build environment; '
+                        'execute the "Visual C++ <version> Command Prompt" '
+                        'application shortcut or a vcsvarsall.bat file')
+
+    # Identity x86/x64 and validate the environment matches the Python
+    # 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)
+
+    if vc_x64:
+        if py_arch != '64bit':
+            raise Exception('architecture mismatch: Visual C++ environment '
+                            'is configured for 64-bit but Python is 32-bit')
+    else:
+        if py_arch != '32bit':
+            raise Exception('architecture mismatch: Visual C++ environment '
+                            'is configured for 32-bit but Python is 64-bit')
+
+    if py_version != 2:
+        raise Exception('Only Python 2 is currently supported')
+
+    build_dir.mkdir(exist_ok=True)
+
+    gettext_pkg, gettext_entry = download_entry('gettext', build_dir)
+    gettext_dep_pkg = download_entry('gettext-dep', build_dir)[0]
+    virtualenv_pkg, virtualenv_entry = download_entry('virtualenv', build_dir)
+    py2exe_pkg, py2exe_entry = download_entry('py2exe', build_dir)
+
+    venv_path = build_dir / ('venv-inno-%s' % ('x64' if vc_x64 else 'x86'))
+
+    gettext_root = build_dir / (
+        'gettext-win-%s' % gettext_entry['version'])
+
+    if not gettext_root.exists():
+        extract_zip_to_directory(gettext_pkg, gettext_root)
+        extract_zip_to_directory(gettext_dep_pkg, gettext_root)
+
+    with tempfile.TemporaryDirectory() as td:
+        td = pathlib.Path(td)
+
+        # This assumes Python 2.
+        extract_tar_to_directory(virtualenv_pkg, td)
+        extract_zip_to_directory(py2exe_pkg, td)
+
+        virtualenv_src_path = td / ('virtualenv-%s' %
+            virtualenv_entry['version'])
+        py2exe_source_path = td / ('py2exe-%s' %
+            py2exe_entry['version'])
+
+        virtualenv_py = virtualenv_src_path / 'virtualenv.py'
+
+        if not venv_path.exists():
+            print('creating virtualenv with dependencies')
+            subprocess.run(
+                [str(python_exe), str(virtualenv_py), str(venv_path)],
+                check=True)
+
+        venv_python = venv_path / 'Scripts' / 'python.exe'
+        venv_pip = venv_path / 'Scripts' / 'pip.exe'
+
+        requirements_txt = (source_dir / 'contrib' / 'packaging' /
+                            'inno' / 'requirements.txt')
+        subprocess.run([str(venv_pip), 'install', '-r', str(requirements_txt)],
+                       check=True)
+
+        # Force distutils to use VC++ settings from environment, which was
+        # validated above.
+        env = dict(os.environ)
+        env['DISTUTILS_USE_SDK'] = '1'
+        env['MSSdk'] = '1'
+
+        py2exe_py_path = venv_path / 'Lib' / 'site-packages' / 'py2exe'
+        if not py2exe_py_path.exists():
+            print('building py2exe')
+            subprocess.run([str(venv_python), 'setup.py', 'install'],
+                           cwd=py2exe_source_path,
+                           env=env,
+                           check=True)
+
+        # Register location of msgfmt and other binaries.
+        env['PATH'] = '%s%s%s' % (
+            env['PATH'], os.pathsep, str(gettext_root / 'bin'))
+
+        print('building Mercurial')
+        subprocess.run(
+            [str(venv_python), 'setup.py',
+             'py2exe', '-b', '3' if vc_x64 else '2',
+             'build_doc', '--html'],
+            cwd=str(source_dir),
+            env=env,
+            check=True)
+
+        # hg.exe depends on VC9 runtime DLLs. Copy those into place.
+        for f in find_vc_runtime_files(vc_x64):
+            if f.name.endswith('.manifest'):
+                basename = 'Microsoft.VC90.CRT.manifest'
+            else:
+                basename = f.name
+
+            dest_path = source_dir / 'dist' / basename
+
+            print('copying %s to %s' % (f, dest_path))
+            shutil.copyfile(f, dest_path)
+
+        print('creating installer')
+
+        args = [str(iscc_exe)]
+
+        if vc_x64:
+            args.append('/dARCH=x64')
+
+        if version:
+            args.append('/dVERSION=%s' % version)
+
+        args.append('/Odist')
+        args.append('contrib/packaging/inno/mercurial.iss')
+
+        subprocess.run(args, cwd=str(source_dir), check=True)
--- a/contrib/packaging/inno/build.py	Thu Mar 07 10:22:09 2019 -0800
+++ b/contrib/packaging/inno/build.py	Thu Mar 07 15:43:14 2019 -0800
@@ -13,165 +13,7 @@
 import argparse
 import os
 import pathlib
-import shutil
-import subprocess
 import sys
-import tempfile
-
-
-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):
-    """Build the Inno installer.
-
-    Build files will be placed in ``build_dir``.
-
-    py2exe's setup.py doesn't use setuptools. It doesn't have modern logic
-    for finding the Python 2.7 toolchain. So, we require the environment
-    to already be configured with an active toolchain.
-    """
-    from hgpackaging.downloads import (
-        download_entry,
-    )
-    from hgpackaging.util import (
-        extract_tar_to_directory,
-        extract_zip_to_directory,
-        find_vc_runtime_files,
-    )
-
-    if not iscc.exists():
-        raise Exception('%s does not exist' % iscc)
-
-    if 'VCINSTALLDIR' not in os.environ:
-        raise Exception('not running from a Visual C++ build environment; '
-                        'execute the "Visual C++ <version> Command Prompt" '
-                        'application shortcut or a vcsvarsall.bat file')
-
-    # Identity x86/x64 and validate the environment matches the Python
-    # 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)
-
-    if vc_x64:
-        if py_arch != '64bit':
-            raise Exception('architecture mismatch: Visual C++ environment '
-                            'is configured for 64-bit but Python is 32-bit')
-    else:
-        if py_arch != '32bit':
-            raise Exception('architecture mismatch: Visual C++ environment '
-                            'is configured for 32-bit but Python is 64-bit')
-
-    if py_version != 2:
-        raise Exception('Only Python 2 is currently supported')
-
-    build_dir.mkdir(exist_ok=True)
-
-    gettext_pkg, gettext_entry = download_entry('gettext', build_dir)
-    gettext_dep_pkg = download_entry('gettext-dep', build_dir)[0]
-    virtualenv_pkg, virtualenv_entry = download_entry('virtualenv', build_dir)
-    py2exe_pkg, py2exe_entry = download_entry('py2exe', build_dir)
-
-    venv_path = build_dir / ('venv-inno-%s' % ('x64' if vc_x64 else 'x86'))
-
-    gettext_root = build_dir / (
-        'gettext-win-%s' % gettext_entry['version'])
-
-    if not gettext_root.exists():
-        extract_zip_to_directory(gettext_pkg, gettext_root)
-        extract_zip_to_directory(gettext_dep_pkg, gettext_root)
-
-    with tempfile.TemporaryDirectory() as td:
-        td = pathlib.Path(td)
-
-        # This assumes Python 2.
-        extract_tar_to_directory(virtualenv_pkg, td)
-        extract_zip_to_directory(py2exe_pkg, td)
-
-        virtualenv_src_path = td / ('virtualenv-%s' %
-            virtualenv_entry['version'])
-        py2exe_source_path = td / ('py2exe-%s' %
-            py2exe_entry['version'])
-
-        virtualenv_py = virtualenv_src_path / 'virtualenv.py'
-
-        if not venv_path.exists():
-            print('creating virtualenv with dependencies')
-            subprocess.run(
-                [str(python_exe), str(virtualenv_py), str(venv_path)],
-                check=True)
-
-        venv_python = venv_path / 'Scripts' / 'python.exe'
-        venv_pip = venv_path / 'Scripts' / 'pip.exe'
-
-        requirements_txt = (source_dir / 'contrib' / 'packaging' /
-                            'inno' / 'requirements.txt')
-        subprocess.run([str(venv_pip), 'install', '-r', str(requirements_txt)],
-                       check=True)
-
-        # Force distutils to use VC++ settings from environment, which was
-        # validated above.
-        env = dict(os.environ)
-        env['DISTUTILS_USE_SDK'] = '1'
-        env['MSSdk'] = '1'
-
-        py2exe_py_path = venv_path / 'Lib' / 'site-packages' / 'py2exe'
-        if not py2exe_py_path.exists():
-            print('building py2exe')
-            subprocess.run([str(venv_python), 'setup.py', 'install'],
-                           cwd=py2exe_source_path,
-                           env=env,
-                           check=True)
-
-        # Register location of msgfmt and other binaries.
-        env['PATH'] = '%s%s%s' % (
-            env['PATH'], os.pathsep, str(gettext_root / 'bin'))
-
-        print('building Mercurial')
-        subprocess.run(
-            [str(venv_python), 'setup.py',
-             'py2exe', '-b', '3' if vc_x64 else '2',
-             'build_doc', '--html'],
-            cwd=str(source_dir),
-            env=env,
-            check=True)
-
-        # hg.exe depends on VC9 runtime DLLs. Copy those into place.
-        for f in find_vc_runtime_files(vc_x64):
-            if f.name.endswith('.manifest'):
-                basename = 'Microsoft.VC90.CRT.manifest'
-            else:
-                basename = f.name
-
-            dest_path = source_dir / 'dist' / basename
-
-            print('copying %s to %s' % (f, dest_path))
-            shutil.copyfile(f, dest_path)
-
-        print('creating installer')
-
-        args = [str(iscc_exe)]
-
-        if vc_x64:
-            args.append('/dARCH=x64')
-
-        if version:
-            args.append('/dVERSION=%s' % version)
-
-        args.append('/Odist')
-        args.append('contrib/packaging/inno/mercurial.iss')
-
-        subprocess.run(args, cwd=str(source_dir), check=True)
 
 
 if __name__ == '__main__':
@@ -200,5 +42,7 @@
 
     sys.path.insert(0, str(source_dir / 'contrib' / 'packaging'))
 
+    from hgpackaging.inno import build
+
     build(source_dir, build_dir, pathlib.Path(args.python), iscc,
           version=args.version)
--- a/tests/test-check-code.t	Thu Mar 07 10:22:09 2019 -0800
+++ b/tests/test-check-code.t	Thu Mar 07 15:43:14 2019 -0800
@@ -13,6 +13,7 @@
   > -X mercurial/thirdparty \
   > | sed 's-\\-/-g' | "$check_code" --warnings --per-file=0 - || false
   Skipping contrib/packaging/hgpackaging/downloads.py it has no-che?k-code (glob)
+  Skipping contrib/packaging/hgpackaging/inno.py it has no-che?k-code (glob)
   Skipping contrib/packaging/hgpackaging/util.py it has no-che?k-code (glob)
   Skipping contrib/packaging/inno/build.py it has no-che?k-code (glob)
   Skipping i18n/polib.py it has no-che?k-code (glob)