Browse Source

add `Package.get_readme()` function, use it in setuptools and fix the `core` check

shut
Niklas Rosenstein 10 months ago
parent
commit
d3bf5e5a96
No known key found for this signature in database GPG Key ID: 6D269B33D25F6C6
3 changed files with 20 additions and 18 deletions
  1. + 13
    - 0
      src/shore/model.py
  2. + 1
    - 1
      src/shore/plugins/core.py
  3. + 6
    - 17
      src/shore/plugins/setuptools.py

+ 13
- 0
src/shore/model.py

@ -41,6 +41,7 @@ import collections
import copy
import logging
import os
import nr.fs
import re
import shlex
import yaml
@ -741,6 +742,8 @@ class Package(BaseObject):
#: Advertise a "setuptools:wheel" build target for the package. Defaults to True.
wheel = Field(bool, default=True)
_PackageReadme = collections.namedtuple('PackageReadme', 'path,is_inside')
@property
def local_name(self) -> str:
if self.monorepo:
@ -803,6 +806,16 @@ class Package(BaseObject):
tag_format = self.get_tag_format()
return tag_format.format(name=self.name, version=version)
def get_readme(self) -> Optional[_PackageReadme]:
if self.readme:
abs_path = os.path.abspath(os.path.join(self.directory, self.readme))
is_inside = nr.fs.issub(os.path.relpath(abs_path, self.directory))
return Package._PackageReadme(self.readme, is_inside)
filename = find_readme_file(package.directory)
if filename:
return Package._PackageReadme(filename, True)
return None
def on_load_hook(self):
""" Called when the package is loaded. Attempts to find the Monorepo that
belongs to this package and load it. """

+ 1
- 1
src/shore/plugins/core.py

@ -46,7 +46,7 @@ class CorePlugin:
def check_package(self, package: Package) -> Iterable[CheckResult]:
yield from self._unhandled_keys(package)
if not find_readme_file(package.directory):
if not package.get_readme():
yield CheckResult(package, 'WARNING', 'No README file found.')
if not package.get_author():

+ 6
- 17
src/shore/plugins/setuptools.py

@ -23,7 +23,6 @@
plugin is used by default in packages. """
from ._util import find_readme_file, readme_content_type
from nr.fs import issub as issubpath
from nr.interface import implements, override
from shore.core.plugins import (
BuildResult,
@ -243,28 +242,18 @@ class SetuptoolsRenderer:
version = re.search(r"__version__\s*=\s*'(.*)'", fp.read()).group(1)
''').format(entrypoint_file=_normpath(entry_file)))
# Write the part that reads the readme for the long description.
if package.readme:
rel_readme = package.readme
abs_readme = os.path.abspath(os.path.join(package.directory, rel_readme))
if issubpath(os.path.relpath(abs_readme, package.directory)):
rel_readme, readme = None, rel_readme
else:
readme = os.path.basename(rel_readme)
else:
rel_readme = None
readme = find_readme_file(package.directory)
readme = package.get_readme()
if readme:
fp.write('\nreadme_file = {!r}\n'.format(readme))
if rel_readme:
readme_relative_path = readme.path if readme.is_inside else os.path.basename(readme.path)
fp.write('\nreadme_file = {!r}\n'.format(readme_relative_path))
if not readme.is_inside:
# Copy the relative README file if it exists.
fp.write(textwrap.dedent('''
source_readme_file = {!r}
if not os.path.isfile(readme_file) and os.path.isfile(source_readme_file):
import shutil; shutil.copyfile(source_readme_file, readme_file)
import atexit; atexit.register(lambda: os.remove(readme_file))
''').format(rel_readme).lstrip())
''').format(readme.path).lstrip())
fp.write(textwrap.dedent('''
if os.path.isfile(readme_file):
with io.open(readme_file, encoding='utf8') as fp:
@ -350,7 +339,7 @@ class SetuptoolsRenderer:
description=package.description.replace('\n\n', '%%%%').replace('\n', ' ').replace('%%%%', '\n').strip(),
long_description_content_type=(
package.long_description_content_type or
(readme_content_type(readme) if readme else 'text/plain')
(readme_content_type(readme.path) if readme else 'text/plain')
),
extras_require=extras_require,
tests_require=tests_require,

Loading…
Cancel
Save