Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix namespace packages handling of wheels #1215

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
v38.2.2
-------

* #1214: fix handling of namespace packages when installing
from a wheel.

v38.2.1
-------

Expand Down
32 changes: 32 additions & 0 deletions setuptools/tests/test_wheel.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,38 @@ def __repr__(self):
),
),

dict(
id='namespace_package',
file_defs={
'foo': {
'bar': {
'__init__.py': ''
},
},
},
setup_kwargs=dict(
namespace_packages=['foo'],
packages=['foo.bar'],
),
install_tree=DALS(
'''
foo-1.0-py{py_version}.egg/
|-- foo-1.0-py{py_version}-nspkg.pth
|-- EGG-INFO/
| |-- DESCRIPTION.rst
| |-- PKG-INFO
| |-- RECORD
| |-- WHEEL
| |-- metadata.json
| |-- namespace_packages.txt
| |-- top_level.txt
|-- foo/
| |-- __init__.py
| |-- bar/
| | |-- __init__.py
'''),
),

)

@pytest.mark.parametrize(
Expand Down
18 changes: 18 additions & 0 deletions setuptools/wheel.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@
)\.whl$""",
re.VERBOSE).match

NAMESPACE_PACKAGE_INIT = '''\
try:
__import__('pkg_resources').declare_namespace(__name__)
except ImportError:
__path__ = __import__('pkgutil').extend_path(__path__, __name__)
'''


class Wheel(object):

Expand Down Expand Up @@ -124,3 +131,14 @@ def raw_req(req):
os.rmdir(subdir)
if os.path.exists(dist_data):
os.rmdir(dist_data)
# Fix namespace packages.
namespace_packages = os.path.join(egg_info, 'namespace_packages.txt')
Copy link
Contributor

@leorochael leorochael Dec 1, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apropos @reinout's comment on #1214, shouldn't the line above be like this instead?

try:
    namespace_packages = zf.read(
        '%s/namespace_packages.txt' % (dist_info,)
    ).splitlines()
except KeyError:
    pass
else:
    for mod in namespace_packages:
        [...]

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your code isn't completely clear. The %s, what is that?

The problem is that egg_info instead of *dist-info/ is used (I believe).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The dist-info directory is renamed to EGG-INFO as part of the conversion to an unzipped-egg.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Normally, buildout lets setuptools download the release and then calls setuptools.archive_util.unpack_archive() when it gets an .egg file from setuptools download step.

Now suddenly setuptools starts feeding buildout wheels :-) I'm calling unpack_archive() on those, too. It sounds like I should use a different function?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your code isn't completely clear. The %s, what is that?

@reinout, good catch. I've fixed my comment. that part should be:

'%s/namespace_packages.txt' % (dist_info,)

The gist of it is that we should read namespace_packages.txt from the original zipped wheel metadata, instead of the converted metadata.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@leorochael: yes, that ought to help get the __init__.py generated, even when I'm using the regular .unpack_archive() function.
(I don't know if there's more that we're missing if we don't let setuptools convert it to an egg).

Copy link
Member Author

@benoit-pierre benoit-pierre Dec 1, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@leorochael: Why? This particular metadata is unchanged during the conversion.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like I just said in #1214, I was simply calling the wrong function. I'm letting setuptools extract the .whl now and it does the right thing.

if os.path.exists(namespace_packages):
with open(namespace_packages) as fp:
namespace_packages = fp.read().split()
for mod in namespace_packages:
mod_dir = os.path.join(destination_eggdir, *mod.split('.'))
mod_init = os.path.join(mod_dir, '__init__.py')
if os.path.exists(mod_dir) and not os.path.exists(mod_init):
with open(mod_init, 'w') as fp:
fp.write(NAMESPACE_PACKAGE_INIT)