D6092: setup: configure py2exe config via environment variables

indygreg (Gregory Szorc) phabricator at mercurial-scm.org
Fri Mar 8 18:49:57 UTC 2019


indygreg created this revision.
Herald added a subscriber: mercurial-devel.
Herald added a reviewer: hg-reviewers.

REVISION SUMMARY
  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.

REPOSITORY
  rHG Mercurial

REVISION DETAIL
  https://phab.mercurial-scm.org/D6092

AFFECTED FILES
  contrib/packaging/hgpackaging/inno.py
  contrib/packaging/hgpackaging/py2exe.py
  setup.py

CHANGE DETAILS

diff --git a/setup.py b/setup.py
--- a/setup.py
+++ b/setup.py
@@ -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': {
diff --git a/contrib/packaging/hgpackaging/py2exe.py b/contrib/packaging/hgpackaging/py2exe.py
--- a/contrib/packaging/hgpackaging/py2exe.py
+++ b/contrib/packaging/hgpackaging/py2exe.py
@@ -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')
diff --git a/contrib/packaging/hgpackaging/inno.py b/contrib/packaging/hgpackaging/inno.py
--- a/contrib/packaging/hgpackaging/inno.py
+++ b/contrib/packaging/hgpackaging/inno.py
@@ -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):



To: indygreg, #hg-reviewers
Cc: mercurial-devel


More information about the Mercurial-devel mailing list