Skip to content

Commit

Permalink
Merge pull request #393 from takluyver/pep621
Browse files Browse the repository at this point in the history
Support [project] table in pyproject.toml (PEP 621)
  • Loading branch information
takluyver authored Mar 15, 2021
2 parents 69848aa 746afdc commit 0b97848
Show file tree
Hide file tree
Showing 21 changed files with 618 additions and 73 deletions.
2 changes: 1 addition & 1 deletion doc/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 57,7 @@
# built documents.
#
# The short X.Y version.
version = '3.1.0'
version = '3.2.0'
# The full version, including alpha/beta/rc tags.
release = version # '.1'

Expand Down
2 changes: 1 addition & 1 deletion flit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 12,7 @@
from .config import ConfigError
from .log import enable_colourful_output

__version__ = '3.1.0'
__version__ = '3.2.0'

log = logging.getLogger(__name__)

Expand Down
6 changes: 3 additions & 3 deletions flit/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,12 256,12 @@ def install_requirements(self):
def install_reqs_my_python_if_needed(self):
"""Install requirements to this environment if needed.
We can normally get the module's docstring and version number without
importing it, but if we do need to import it, we may need to install
We can normally get the summary and version number without import the
module, but if we do need to import it, we may need to install
its requirements for the Python where flit is running.
"""
try:
common.get_info_from_module(self.module)
common.get_info_from_module(self.module, self.ini_info.dynamic_metadata)
except ImportError:
if self.deps == 'none':
raise # We were asked not to install deps, so bail out.
Expand Down
2 changes: 1 addition & 1 deletion flit_core/flit_core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 4,4 @@
All the convenient development features live in the main 'flit' package.
"""

__version__ = '3.1.0'
__version__ = '3.2.0'
9 changes: 7 additions & 2 deletions flit_core/flit_core/buildapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 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)
# If we can get the module info from the AST, we don't need any extra
# If we can get version & description from pyproject.toml (PEP 621), or
# by parsing the module (_via_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.
want_summary = 'description' in info.dynamic_metadata
want_version = 'version' in info.dynamic_metadata

module = Module(info.module, Path.cwd())
docstring, version = get_docstring_and_version_via_ast(module)
if (docstring is None) or (version is None):

if (want_summary and not docstring) or (want_version and not version):
return info.metadata.get('requires_dist', [])
else:
return []
Expand Down
52 changes: 33 additions & 19 deletions flit_core/flit_core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,45 143,65 @@ def get_docstring_and_version_via_ast(target):
return ast.get_docstring(node), version


# To ensure we're actually loading the specified file, give it a unique name to
# avoid any cached import. In normal use we'll only load one module per process,
# so it should only matter for the tests, but we'll do it anyway.
_import_i = 0


def get_docstring_and_version_via_import(target):
"""
Return a tuple like (docstring, version) for the given module,
extracted by importing the module and pulling __doc__ & __version__
from it.
"""
global _import_i
_import_i = 1

log.debug("Loading module %s", target.file)
from importlib.machinery import SourceFileLoader
sl = SourceFileLoader(target.name, str(target.file))
sl = SourceFileLoader('flit_core.dummy.import%d' % _import_i, str(target.file))
with _module_load_ctx():
m = sl.load_module()
docstring = m.__dict__.get('__doc__', None)
version = m.__dict__.get('__version__', None)
return docstring, version


def get_info_from_module(target):
def get_info_from_module(target, for_fields=('version', 'description')):
"""Load the module/package, get its docstring and __version__
"""
if not for_fields:
return {}

# What core metadata calls Summary, PEP 621 calls description
want_summary = 'description' in for_fields
want_version = 'version' in for_fields

log.debug("Loading module %s", target.file)

# Attempt to extract our docstring & version by parsing our target's
# AST, falling back to an import if that fails. This allows us to
# build without necessarily requiring that our built package's
# requirements are installed.
docstring, version = get_docstring_and_version_via_ast(target)
if not (docstring and version):
if (want_summary and not docstring) or (want_version and not version):
docstring, version = get_docstring_and_version_via_import(target)

if (not docstring) or not docstring.strip():
raise NoDocstringError('Flit cannot package module without docstring, '
'or empty docstring. Please add a docstring to your module '
'({}).'.format(target.file))
res = {}

if want_summary:
if (not docstring) or not docstring.strip():
raise NoDocstringError(
'Flit cannot package module without docstring, or empty docstring. '
'Please add a docstring to your module ({}).'.format(target.file)
)
res['summary'] = docstring.lstrip().splitlines()[0]

version = check_version(version)
if want_version:
res['version'] = check_version(version)

docstring_lines = docstring.lstrip().splitlines()
return {'summary': docstring_lines[0],
'version': version}
return res

def check_version(version):
"""
Expand Down Expand Up @@ -297,6 317,7 @@ class Metadata(object):
metadata_version = "2.1"

def __init__(self, data):
data = data.copy()
self.name = data.pop('name')
self.version = data.pop('version')
self.summary = data.pop('summary')
Expand Down Expand Up @@ -363,17 384,10 @@ def supports_py2(self):

def make_metadata(module, ini_info):
md_dict = {'name': module.name, 'provides': [module.name]}
md_dict.update(get_info_from_module(module))
md_dict.update(get_info_from_module(module, ini_info.dynamic_metadata))
md_dict.update(ini_info.metadata)
return Metadata(md_dict)

def metadata_and_module_from_ini_path(ini_path):
from .config import read_flit_config
ini_path = str(ini_path)
ini_info = read_flit_config(ini_path)
module = Module(ini_info.module, osp.dirname(ini_path))
metadata = make_metadata(module, ini_info)
return metadata,module


def normalize_dist_name(name: str, version: str) -> str:
Expand Down
Loading

0 comments on commit 0b97848

Please sign in to comment.