Skip to content
This repository has been archived by the owner on Oct 16, 2022. It is now read-only.

Commit

Permalink
BUG: move monkey_patch to avoid some weird distutils auto-imports.
Browse files Browse the repository at this point in the history
  • Loading branch information
cournape committed Aug 28, 2012
1 parent 01947f5 commit 079aab7
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 46 deletions.
39 changes: 0 additions & 39 deletions bento/distutils/__init__.py
@@ -1,39 +0,0 @@
import sys

def monkey_patch():
# XXX: keep the import here to avoid any side-effects from mere import of
# bento.distutils
from bento.distutils.utils \
import \
_is_setuptools_activated
import bento.distutils.dist

# Install it throughout the distutils
_MODULES = []
if _is_setuptools_activated():
import setuptools.dist
_MODULES.append(setuptools.dist)
import distutils.dist, distutils.core, distutils.cmd
_MODULES.extend([distutils.dist, distutils.core, distutils.cmd])
for module in _MODULES:
module.Distribution = bento.distutils.dist.BentoDistribution

def setup(mode=None):
"""Call setup after monkey-patching with the given mode.
Parameters
----------
mode: None/str
'distutils' or 'setuptools' for now
"""
if mode is None:
if "setuptools" in sys.modules:
mode = "setuptools"
else:
mode = "distutils"
if not mode in ("distutils", "setuptools"):
raise ValueError("Only 'setuptools' and 'distutils' are supported modes")
__import__(mode)
monkey_patch()
from distutils.core import setup as _setup
return _setup()
41 changes: 41 additions & 0 deletions bento/distutils/monkey_patch.py
@@ -0,0 +1,41 @@
import sys

def monkey_patch():
# XXX: keep the import here to avoid any side-effects from mere import of
# bento.distutils
from bento.distutils.utils \
import \
_is_setuptools_activated
import bento.distutils.dist

# Install it throughout the distutils
_MODULES = []
if _is_setuptools_activated():
import setuptools.dist
_MODULES.append(setuptools.dist)
import distutils.dist, distutils.core, distutils.cmd
_MODULES.extend([distutils.dist, distutils.core, distutils.cmd])
for module in _MODULES:
module.Distribution = bento.distutils.dist.BentoDistribution

def setup(mode=None):
"""Call setup after monkey-patching with the given mode.
Parameters
----------
mode: None/str
'distutils' or 'setuptools' for now
"""
import traceback
traceback.print_stack()

This comment has been minimized.

Copy link
@sjagoe

sjagoe Dec 17, 2013

@cournape Hmm. Do you want this print_stack() here?

if mode is None:
if "setuptools" in sys.modules:
mode = "setuptools"
else:
mode = "distutils"
if not mode in ("distutils", "setuptools"):
raise ValueError("Only 'setuptools' and 'distutils' are supported modes")
__import__(mode)
monkey_patch()
from distutils.core import setup as _setup
return _setup()
2 changes: 1 addition & 1 deletion bento/distutils/tests/test_sdist.py
Expand Up @@ -9,7 +9,7 @@
from bento.distutils.commands.sdist \
import \
sdist

class TestSdistCommand(DistutilsCommandTestBase):
def test_simple(self):
fid = open(op.join(self.new_cwd, "bento.info"), "wt")
Expand Down
4 changes: 2 additions & 2 deletions doc/main_page/index.html
Expand Up @@ -76,8 +76,8 @@ <h1><a href="./">Bento</a></h1>
</div>
<div class="col-2">
<pre><code>import setuptools
import bento.distutils
bento.distutils.monkey_patch()
from bento.distutils.monkey_patch import monkey_patch
monkey_patch()
setuptools.setup()
</code></pre>
<pre><code><span style="color: #e3d796;">$</span> easy_install your-bento-package
Expand Down
13 changes: 10 additions & 3 deletions doc/source/transition.rst
Expand Up @@ -100,8 +100,8 @@ pip, pip currently does not know anything about bento. To help transition,
bento has a distutils compatibility layer. A setup.py as simple as::

import setuptools
import bento.distutils
bento.distutils.monkey_patch()
from bento.distutils.monkey_patch import monkey_patch
monkey_patch()

from setuptools import setup

Expand All @@ -115,7 +115,14 @@ will enable commands such as::

to work as expected, taking all the package information from bento.info file.
Note that the monkey-patching done by bento.distutils on top of setuptools is
explicit - solely importing bento.distutils will not monkey patch anything.
explicit - solely importing bento.distutils will not monkey patch anything. A
simpler, setuptools-style monkey patch is also possible::

import setuptools
from bento.distutils.monkey_patch import setup

if __name__ == '__main__':
setup()

Note:: obviously, this mode will not enable all the features offered by bento.
If it were possible, bento would not have been written in the first place.
Expand Down
2 changes: 1 addition & 1 deletion setup.py
@@ -1,2 +1,2 @@
from bento.distutils import setup
from bento.distutils.monkey_patch import setup
setup()

0 comments on commit 079aab7

Please sign in to comment.