Patchwork [2,of,3] modulepolicy: create a module for the modulepolicy

login
register
mail settings
Submitter timeless@mozdev.org
Date March 9, 2016, 4:19 p.m.
Message ID <71cb80801ec4e3b6aa51.1457540350@waste.org>
Download mbox | patch
Permalink /patch/13727/
State Accepted
Delegated to: Yuya Nishihara
Headers show

Comments

timeless@mozdev.org - March 9, 2016, 4:19 p.m.
# HG changeset patch
# User timeless <timeless@mozdev.org>
# Date 1457538421 0
#      Wed Mar 09 15:47:01 2016 +0000
# Node ID 71cb80801ec4e3b6aa519da48b4601c3c7a41615
# Parent  f9363da1d708516fbf365d41c9075018b2ce020c
modulepolicy: create a module for the modulepolicy

Instead of rewriting __init__ to define the modulepolicy,
write out a __modulepolicy__.py file like __version__.py
Pierre-Yves David - March 9, 2016, 6:19 p.m.
On 03/09/2016 04:19 PM, timeless wrote:
> # HG changeset patch
> # User timeless <timeless@mozdev.org>
> # Date 1457538421 0
> #      Wed Mar 09 15:47:01 2016 +0000
> # Node ID 71cb80801ec4e3b6aa519da48b4601c3c7a41615
> # Parent  f9363da1d708516fbf365d41c9075018b2ce020c
> modulepolicy: create a module for the modulepolicy
>
> Instead of rewriting __init__ to define the modulepolicy,
> write out a __modulepolicy__.py file like __version__.py

Why is this better?
timeless - March 9, 2016, 8:18 p.m.
yuja asked me for it.

I don't claim it's better.

On Wed, Mar 9, 2016 at 1:19 PM, Pierre-Yves David
<pierre-yves.david@ens-lyon.org> wrote:
>
>
> On 03/09/2016 04:19 PM, timeless wrote:
>>
>> # HG changeset patch
>> # User timeless <timeless@mozdev.org>
>> # Date 1457538421 0
>> #      Wed Mar 09 15:47:01 2016 +0000
>> # Node ID 71cb80801ec4e3b6aa519da48b4601c3c7a41615
>> # Parent  f9363da1d708516fbf365d41c9075018b2ce020c
>> modulepolicy: create a module for the modulepolicy
>>
>> Instead of rewriting __init__ to define the modulepolicy,
>> write out a __modulepolicy__.py file like __version__.py
>
>
> Why is this better?
>
> --
> Pierre-Yves David
>
> _______________________________________________
> Mercurial-devel mailing list
> Mercurial-devel@mercurial-scm.org
> https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel
Pierre-Yves David - March 9, 2016, 8:30 p.m.
On 03/09/2016 08:18 PM, timeless wrote:
> yuja asked me for it.
>
> I don't claim it's better.

What's yuya rational for this?

> On Wed, Mar 9, 2016 at 1:19 PM, Pierre-Yves David
> <pierre-yves.david@ens-lyon.org> wrote:
>>
>>
>> On 03/09/2016 04:19 PM, timeless wrote:
>>>
>>> # HG changeset patch
>>> # User timeless <timeless@mozdev.org>
>>> # Date 1457538421 0
>>> #      Wed Mar 09 15:47:01 2016 +0000
>>> # Node ID 71cb80801ec4e3b6aa519da48b4601c3c7a41615
>>> # Parent  f9363da1d708516fbf365d41c9075018b2ce020c
>>> modulepolicy: create a module for the modulepolicy
>>>
>>> Instead of rewriting __init__ to define the modulepolicy,
>>> write out a __modulepolicy__.py file like __version__.py
>>
>>
>> Why is this better?
>>
>> --
>> Pierre-Yves David
>>
>> _______________________________________________
>> Mercurial-devel mailing list
>> Mercurial-devel@mercurial-scm.org
>> https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel
> _______________________________________________
> Mercurial-devel mailing list
> Mercurial-devel@mercurial-scm.org
> https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel
>
Yuya Nishihara - March 10, 2016, 1:40 p.m.
On Wed, 9 Mar 2016 20:30:08 +0000, Pierre-Yves David wrote:
> On 03/09/2016 08:18 PM, timeless wrote:
> > yuja asked me for it.
> >
> > I don't claim it's better.
> 
> What's yuya rational for this?

We don't need two modulepolicy hacks. @MODULELOADPOLICY@ for system
installation, and mercurial/modulepolicy for in-place use. Both of them
can be handled by a single __modulepolicy__.py as Python modules should
be installed without extra effort.
Yuya Nishihara - March 10, 2016, 2:52 p.m.
On Wed, 09 Mar 2016 10:19:10 -0600, timeless wrote:
> # HG changeset patch
> # User timeless <timeless@mozdev.org>
> # Date 1457538421 0
> #      Wed Mar 09 15:47:01 2016 +0000
> # Node ID 71cb80801ec4e3b6aa519da48b4601c3c7a41615
> # Parent  f9363da1d708516fbf365d41c9075018b2ce020c
> modulepolicy: create a module for the modulepolicy
> 
> Instead of rewriting __init__ to define the modulepolicy,
> write out a __modulepolicy__.py file like __version__.py

Dropped the previous version and pushed this series, thanks!

> --- a/Makefile
> +++ b/Makefile
> @@ -63,7 +63,7 @@
>  		\( -name '*.py[cdo]' -o -name '*.so' \) -exec rm -f '{}' ';'
>  	rm -f $(addprefix mercurial/,$(notdir $(wildcard mercurial/pure/[a-z]*.py)))
>  	rm -f MANIFEST MANIFEST.in hgext/__index__.py tests/*.err
> -	if test -d .hg; then rm -f mercurial/__version__.py; fi
> +	if test -d .hg; then rm -f mercurial/__modulepolicy__.py mercurial/__version__.py; fi

Moved mercurial/__modulepolicy__.py out of "if test -d .hg". __modulepolicy__
can be generated without .hg tree.

> --- a/setup.py
> +++ b/setup.py
> @@ -334,28 +334,17 @@
>          build_py.finalize_options(self)
>  
>          if self.distribution.pure:
> +            modulepolicy = 'py'
>              self.distribution.ext_modules = []
>          else:
> +            modulepolicy = 'c'
>              h = os.path.join(get_python_inc(), 'Python.h')
>              if not os.path.exists(h):
>                  raise SystemExit('Python headers are required to build '
>                                   'Mercurial but weren\'t found in %s' % h)
> -
> -    def copy_file(self, *args, **kwargs):
> -        dst, copied = build_py.copy_file(self, *args, **kwargs)
> -
> -        if copied and dst.endswith('__init__.py'):
> -            if self.distribution.pure:
> -                modulepolicy = 'py'
> -            else:
> -                modulepolicy = 'c'
> -            content = open(dst, 'rb').read()
> -            content = content.replace(b'@MODULELOADPOLICY@',
> -                                      modulepolicy.encode(libdir_escape))
> -            with open(dst, 'wb') as fh:
> -                fh.write(content)
> -
> -        return dst, copied
> +        with open("mercurial/__modulepolicy__.py", "w") as f:
> +            f.write('# this file is autogenerated by setup.py\n')
> +            f.write('modulepolicy = "%s"\n' % modulepolicy)

Doing it in finalize_options() seems wrong. I've changed it to wrap run().

Patch

diff --git a/.hgignore b/.hgignore
--- a/.hgignore
+++ b/.hgignore
@@ -40,6 +40,7 @@ 
 MANIFEST
 MANIFEST.in
 patches
+mercurial/__modulepolicy__.py
 mercurial/__version__.py
 mercurial/hgpythonlib.h
 mercurial.egg-info
diff --git a/Makefile b/Makefile
--- a/Makefile
+++ b/Makefile
@@ -63,7 +63,7 @@ 
 		\( -name '*.py[cdo]' -o -name '*.so' \) -exec rm -f '{}' ';'
 	rm -f $(addprefix mercurial/,$(notdir $(wildcard mercurial/pure/[a-z]*.py)))
 	rm -f MANIFEST MANIFEST.in hgext/__index__.py tests/*.err
-	if test -d .hg; then rm -f mercurial/__version__.py; fi
+	if test -d .hg; then rm -f mercurial/__modulepolicy__.py mercurial/__version__.py; fi
 	rm -rf build mercurial/locale
 	$(MAKE) -C doc clean
 
diff --git a/mercurial/__init__.py b/mercurial/__init__.py
--- a/mercurial/__init__.py
+++ b/mercurial/__init__.py
@@ -19,11 +19,14 @@ 
 #    c - require C extensions
 #    allow - allow pure Python implementation when C loading fails
 #    py - only load pure Python modules
-modulepolicy = '@MODULELOADPOLICY@'
 
 # By default, require the C extensions for performance reasons.
-if modulepolicy == '@' 'MODULELOADPOLICY' '@':
-    modulepolicy = 'c'
+modulepolicy = 'c'
+try:
+    from . import __modulepolicy__
+    modulepolicy = __modulepolicy__.modulepolicy
+except ImportError:
+    pass
 
 # PyPy doesn't load C extensions.
 #
diff --git a/setup.py b/setup.py
--- a/setup.py
+++ b/setup.py
@@ -334,28 +334,17 @@ 
         build_py.finalize_options(self)
 
         if self.distribution.pure:
+            modulepolicy = 'py'
             self.distribution.ext_modules = []
         else:
+            modulepolicy = 'c'
             h = os.path.join(get_python_inc(), 'Python.h')
             if not os.path.exists(h):
                 raise SystemExit('Python headers are required to build '
                                  'Mercurial but weren\'t found in %s' % h)
-
-    def copy_file(self, *args, **kwargs):
-        dst, copied = build_py.copy_file(self, *args, **kwargs)
-
-        if copied and dst.endswith('__init__.py'):
-            if self.distribution.pure:
-                modulepolicy = 'py'
-            else:
-                modulepolicy = 'c'
-            content = open(dst, 'rb').read()
-            content = content.replace(b'@MODULELOADPOLICY@',
-                                      modulepolicy.encode(libdir_escape))
-            with open(dst, 'wb') as fh:
-                fh.write(content)
-
-        return dst, copied
+        with open("mercurial/__modulepolicy__.py", "w") as f:
+            f.write('# this file is autogenerated by setup.py\n')
+            f.write('modulepolicy = "%s"\n' % modulepolicy)
 
 class buildhgextindex(Command):
     description = 'generate prebuilt index of hgext (for frozen package)'