setup: configure py2exe config via environment variables
authorGregory Szorc <gregory.szorc@gmail.com>
Thu, 07 Mar 2019 12:15:32 -0800
changeset 41916 260305e8ddbd
parent 41915 a2e191a937a9
child 41917 ac32e04e887f
setup: configure py2exe config via environment variables The Inno Setup and WiX installers ship a different set of packages with py2exe builds. And there are multiple WiX installer variants (e.g. TortoiseHG). Since there are multiple variants of py2exe configs and they can be defined by entities not in our repository, let's provide a mechanism for setup.py to supplement behavior via environment variables. This is slighly less hacky than a setup.cfg file IMO since the caller doesn't need to worry about mutating global state of the source directory. Differential Revision: https://phab.mercurial-scm.org/D6092
contrib/packaging/hgpackaging/inno.py
contrib/packaging/hgpackaging/py2exe.py
setup.py
--- a/contrib/packaging/hgpackaging/inno.py	Thu Mar 07 15:43:54 2019 -0800
+++ b/contrib/packaging/hgpackaging/inno.py	Thu Mar 07 12:15:32 2019 -0800
@@ -20,6 +20,14 @@
 )
 
 
+EXTRA_PACKAGES = {
+    'dulwich',
+    'keyring',
+    'pygments',
+    'win32ctypes',
+}
+
+
 def build(source_dir: pathlib.Path, build_dir: pathlib.Path,
           python_exe: pathlib.Path, iscc_exe: pathlib.Path,
           version=None):
@@ -40,7 +48,7 @@
                         'inno' / 'requirements.txt')
 
     build_py2exe(source_dir, build_dir, python_exe, 'inno',
-                 requirements_txt)
+                 requirements_txt, extra_packages=EXTRA_PACKAGES)
 
     # hg.exe depends on VC9 runtime DLLs. Copy those into place.
     for f in find_vc_runtime_files(vc_x64):
--- a/contrib/packaging/hgpackaging/py2exe.py	Thu Mar 07 15:43:54 2019 -0800
+++ b/contrib/packaging/hgpackaging/py2exe.py	Thu Mar 07 12:15:32 2019 -0800
@@ -23,7 +23,9 @@
 
 def build_py2exe(source_dir: pathlib.Path, build_dir: pathlib.Path,
                  python_exe: pathlib.Path, build_name: str,
-                 venv_requirements_txt: pathlib.Path):
+                 venv_requirements_txt: pathlib.Path,
+                 extra_packages=None, extra_excludes=None,
+                 extra_dll_excludes=None):
     """Build Mercurial with py2exe.
 
     Build files will be placed in ``build_dir``.
@@ -103,6 +105,14 @@
     env['DISTUTILS_USE_SDK'] = '1'
     env['MSSdk'] = '1'
 
+    if extra_packages:
+        env['HG_PY2EXE_EXTRA_PACKAGES'] = ' '.join(sorted(extra_packages))
+    if extra_excludes:
+        env['HG_PY2EXE_EXTRA_EXCLUDES'] = ' '.join(sorted(extra_excludes))
+    if extra_dll_excludes:
+        env['HG_PY2EXE_EXTRA_DLL_EXCLUDES'] = ' '.join(
+            sorted(extra_dll_excludes))
+
     py2exe_py_path = venv_path / 'Lib' / 'site-packages' / 'py2exe'
     if not py2exe_py_path.exists():
         print('building py2exe')
--- a/setup.py	Thu Mar 07 15:43:54 2019 -0800
+++ b/setup.py	Thu Mar 07 12:15:32 2019 -0800
@@ -1251,6 +1251,9 @@
     'mercurial.pure',
 ]
 
+py2exeexcludes = []
+py2exedllexcludes = []
+
 if issetuptools:
     extra['python_requires'] = supportedpy
 
@@ -1264,33 +1267,20 @@
     # put dlls in sub directory so that they won't pollute PATH
     extra['zipfile'] = 'lib/library.zip'
 
-    try:
-        import dulwich
-        dulwich.__version__
-        py2exepackages.append('dulwich')
-    except ImportError:
-        pass
-
-    try:
-        import keyring
-        keyring.util
-        py2exepackages.append('keyring')
-    except ImportError:
-        pass
+    # We allow some configuration to be supplemented via environment
+    # variables. This is better than setup.cfg files because it allows
+    # supplementing configs instead of replacing them.
+    extrapackages = os.environ.get('HG_PY2EXE_EXTRA_PACKAGES')
+    if extrapackages:
+        py2exepackages.extend(extrapackages.split(' '))
 
-    try:
-        import pygments
-        pygments.__version__
-        py2exepackages.append('pygments')
-    except ImportError:
-        pass
+    excludes = os.environ.get('HG_PY2EXE_EXTRA_EXCLUDES')
+    if excludes:
+        py2exeexcludes.extend(excludes.split(' '))
 
-    try:
-        import win32ctypes
-        win32ctypes.__version__
-        py2exepackages.append('win32ctypes')
-    except ImportError:
-        pass
+    dllexcludes = os.environ.get('HG_PY2EXE_EXTRA_DLL_EXCLUDES')
+    if dllexcludes:
+        py2exedllexcludes.extend(dllexcludes.split(' '))
 
 if os.name == 'nt':
     # Windows binary file versions for exe/dll files must have the
@@ -1371,6 +1361,8 @@
       distclass=hgdist,
       options={
           'py2exe': {
+              'dll_excludes': py2exedllexcludes,
+              'excludes': py2exeexcludes,
               'packages': py2exepackages,
           },
           'bdist_mpkg': {