Patchwork [3,of,5,import-refactor] setup: rewrite @MODULELOADPOLICY@ during install

login
register
mail settings
Submitter Gregory Szorc
Date Nov. 22, 2015, 6:14 a.m.
Message ID <b1f0ae9d3abf60a1326e.1448172845@ubuntu-main>
Download mbox | patch
Permalink /patch/11578/
State Accepted
Headers show

Comments

Gregory Szorc - Nov. 22, 2015, 6:14 a.m.
# HG changeset patch
# User Gregory Szorc <gregory.szorc@gmail.com>
# Date 1448168053 28800
#      Sat Nov 21 20:54:13 2015 -0800
# Node ID b1f0ae9d3abf60a1326e305237c31ce3fee7262b
# Parent  594a8931ee9b1eb82f754a3a4eab3cfc7b3811b1
setup: rewrite @MODULELOADPOLICY@ during install

This patch enforces a sane module load policy by rewriting the "hg" script
to define a policy that is appropriate for the installation settings.

For CPython, the policy will be to require C extensions. This is the
default behavior if "hg" isn't rewritten. But being explicit is nice.

If a pure Python installation is performed, the module load policy
is automatically changed to 'py' so we don't even attempt to load C
extensions.

Patch

diff --git a/setup.py b/setup.py
--- a/setup.py
+++ b/setup.py
@@ -420,19 +420,18 @@  class hginstalllib(install_lib):
         file_util.copy_file = copyfileandsetmode
         try:
             install_lib.run(self)
         finally:
             file_util.copy_file = realcopyfile
 
 class hginstallscripts(install_scripts):
     '''
-    This is a specialization of install_scripts that replaces the @LIBDIR@ with
-    the configured directory for modules. If possible, the path is made relative
-    to the directory for scripts.
+    This is a specialization of install_scripts that replaces @VAR@ with
+    variables.
     '''
 
     def initialize_options(self):
         install_scripts.initialize_options(self)
 
         self.install_lib = None
 
     def finalize_options(self):
@@ -450,26 +449,33 @@  class hginstallscripts(install_scripts):
             libdir = self.install_lib
         else:
             common = os.path.commonprefix((self.install_dir, self.install_lib))
             rest = self.install_dir[len(common):]
             uplevel = len([n for n in os.path.split(rest) if n])
 
             libdir =  uplevel * ('..' + os.sep) + self.install_lib[len(common):]
 
+        if self.distribution.pure:
+            policy = 'py'
+        else:
+            policy = 'c'
+
         for outfile in self.outfiles:
             fp = open(outfile, 'rb')
             data = fp.read()
             fp.close()
 
             # skip binary files
             if b('\0') in data:
                 continue
 
             data = data.replace(b('@LIBDIR@'), libdir.encode(libdir_escape))
+            data = data.replace(b('@MODULELOADPOLICY@'), policy)
+
             fp = open(outfile, 'wb')
             fp.write(data)
             fp.close()
 
 cmdclass = {'build': hgbuild,
             'build_mo': hgbuildmo,
             'build_ext': hgbuildext,
             'build_py': hgbuildpy,