Skip to content

Commit

Permalink
Merge pull request #361 from takluyver/build-deps-maybe
Browse files Browse the repository at this point in the history
Provide build requirements only if module needs to be imported for version or docstring
  • Loading branch information
takluyver committed Jul 16, 2020
2 parents f4af9e0 + d7ca739 commit 6a2a8c6
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 5 deletions.
17 changes: 14 additions & 3 deletions flit_core/flit_core/buildapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
import os.path as osp
from pathlib import Path

from .common import Module, make_metadata, write_entry_points, dist_info_name
from .common import (
Module, make_metadata, write_entry_points, dist_info_name,
get_docstring_and_version_via_ast,
)
from .config import read_flit_config
from .wheel import make_wheel_in, _write_wheel_file
from .sdist import SdistBuilder
Expand All @@ -18,9 +21,17 @@
def get_requires_for_build_wheel(config_settings=None):
"""Returns a list of requirements for building, as strings"""
info = read_flit_config(pyproj_toml)
return info.metadata.get('requires_dist', [])
# If we can get the module info from the AST, we don't need any extra
# dependencies. If not, we'll need to try importing it, so report any
# runtime dependencies as build dependencies.
module = Module(info.module, Path.cwd())
docstring, version = get_docstring_and_version_via_ast(module)
if (docstring is None) or (version is None):
return info.metadata.get('requires_dist', [])
else:
return []

# For now, we require all dependencies to build either a wheel or an sdist.
# Requirements to build an sdist are the same as for a wheel
get_requires_for_build_sdist = get_requires_for_build_wheel

def prepare_metadata_for_build_wheel(metadata_directory, config_settings=None):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[build-system]
requires = ["flit_core >=2,<4"]
build-backend = "flit_core.buildapi"

[tool.flit.metadata]
module = "module1"
author = "Sir Robin"
author-email = "robin@camelot.uk"
home-page = "http://github.com/sirrobin/module1"
requires = [
"numpy >=1.16.0",
]
11 changes: 10 additions & 1 deletion flit_core/flit_core/tests/test_buildapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,17 @@ def cwd(directory):
os.chdir(prev)

def test_get_build_requires():
expected = ["requests >= 2.18", "docutils"]
# This module can be inspected (for docstring & __version__) without
# importing it, so there are no build dependencies.
with cwd(osp.join(samples_dir,'pep517')):
assert buildapi.get_requires_for_build_wheel() == []
assert buildapi.get_requires_for_build_sdist() == []

def test_get_build_requires_import():
# This one has to be imported, so its runtime dependencies are also
# build dependencies.
expected = ["numpy >=1.16.0"]
with cwd(osp.join(samples_dir, 'constructed_version')):
assert buildapi.get_requires_for_build_wheel() == expected
assert buildapi.get_requires_for_build_sdist() == expected

Expand Down
2 changes: 1 addition & 1 deletion flit_core/flit_core/tests/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def test_get_info_from_module(self):
'version': '0.1'}
)

info = get_info_from_module(Module('modulewithconstructedversion', samples_dir))
info = get_info_from_module(Module('module1', samples_dir / 'constructed_version'))
self.assertEqual(info, {'summary': 'This module has a __version__ that requires runtime interpretation',
'version': '1.2.3'}
)
Expand Down

0 comments on commit 6a2a8c6

Please sign in to comment.