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

RF: more of common logistics into setup_support #3600

Merged
merged 14 commits into from
Aug 20, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
61 changes: 7 additions & 54 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,45 +7,18 @@
# ## ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##

from os.path import (
dirname,
join as opj,
sep as pathsep,
splitext,
)

from setuptools import (
findall,
find_packages,
setup,
)

from setup_support import (
BuildConfigInfo,
BuildManPage,
BuildRSTExamplesFromScripts,
BuildSchema,
get_version,
setup_entry_points,
)


def findsome(subdir, extensions):
"""Find files under subdir having specified extensions
findsome,
datalad_setup)

Leading directory (datalad) gets stripped
"""
return [
f.split(pathsep, 1)[1] for f in findall(opj('datalad', subdir))
if splitext(f)[-1].lstrip('.') in extensions
]

# datalad version to be installed
version = get_version()

# Only recentish versions of find_packages support include
# datalad_pkgs = find_packages('.', include=['datalad*'])
# so we will filter manually for maximal compatibility
datalad_pkgs = [pkg for pkg in find_packages('.') if pkg.startswith('datalad')]

requires = {
'core': [
Expand Down Expand Up @@ -153,22 +126,6 @@ def findsome(subdir, extensions):
# 'build_py': DataladBuild
}

# PyPI doesn't render markdown yet. Workaround for a sane appearance
# https://github.com/pypa/pypi-legacy/issues/148#issuecomment-227757822
README = opj(dirname(__file__), 'README.md')
try:
import pypandoc
long_description = pypandoc.convert(README, 'rst')
except (ImportError, OSError) as exc:
# attempting to install pandoc via brew on OSX currently hangs and
# pypandoc imports but throws OSError demanding pandoc
print(
"WARNING: pypandoc failed to import or thrown an error while converting"
" README.md to RST: %r .md version will be used as is" % exc
)
long_description = open(README).read()


#
# Avoid using entry_points due to their hefty overhead
#
Expand Down Expand Up @@ -196,25 +153,21 @@ def findsome(subdir, extensions):
]})
setup_kwargs['entry_points'] = entry_points

setup(
name="datalad",
author="The DataLad Team and Contributors",
author_email="team@datalad.org",
version=version,
datalad_setup(
'datalad',
description="data distribution geared toward scientific datasets",
long_description=long_description,
packages=datalad_pkgs,
install_requires=
requires['core'] + requires['downloaders'] +
requires['publish'] + requires['metadata'],
extras_require=requires,
cmdclass=cmdclass,
package_data={
'datalad':
findsome('resources', {'sh', 'html', 'js', 'css', 'png', 'svg', 'txt', 'py'}) +
findsome('resources',
{'sh', 'html', 'js', 'css', 'png', 'svg', 'txt', 'py'}) +
findsome(opj('downloaders', 'configs'), {'cfg'}) +
findsome(opj('distribution', 'tests'), {'yaml'}) +
findsome(opj('metadata', 'tests', 'data'), {'mp3', 'jpg', 'pdf'})
},
**setup_kwargs
)
)
84 changes: 81 additions & 3 deletions setup_support.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,27 @@
import sys
from genericpath import exists
from os import linesep, makedirs
from os.path import dirname, join as opj
from os.path import dirname, join as opj, sep as pathsep, splitext

from distutils.core import Command
from distutils.errors import DistutilsOptionError
import datetime

from setuptools import findall, find_packages, setup

import formatters as fmt


def _path_rel2file(p):
return opj(dirname(__file__), p)


def get_version():
def get_version(name):
"""Load version of datalad from version.py without entailing any imports
Copy link
Contributor

Choose a reason for hiding this comment

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

docstring should be updated to drop hard-coded "datalad".

Copy link
Member Author

Choose a reason for hiding this comment

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

adjusted and pushed

"""
# This might entail lots of imports which might not yet be available
# so let's do ad-hoc parsing of the version.py
with open(opj(dirname(__file__), 'datalad', 'version.py')) as f:
with open(opj(dirname(__file__), name, 'version.py')) as f:
version_lines = list(filter(lambda x: x.startswith('__version__'), f))
assert (len(version_lines) == 1)
return version_lines[0].split('=')[1].strip(" '\"\t\n")
Expand Down Expand Up @@ -361,3 +364,78 @@ def _provide_lean_script_contents(
setup_kwargs['scripts'] = scripts

return setup_kwargs


def get_long_description_from_README():
"""Read README.md, convert to .rst using pypandoc

If pypandoc is not available or fails - just output original .md
"""
# PyPI doesn't render markdown yet. Workaround for a sane appearance
# https://github.com/pypa/pypi-legacy/issues/148#issuecomment-227757822
yarikoptic marked this conversation as resolved.
Show resolved Hide resolved
README = opj(dirname(__file__), 'README.md')
try:
import pypandoc
return pypandoc.convert(README, 'rst')
except (ImportError, OSError) as exc:
# attempting to install pandoc via brew on OSX currently hangs and
# pypandoc imports but throws OSError demanding pandoc
print(
"WARNING: pypandoc failed to import or thrown an error while "
"converting"
" README.md to RST: %r .md version will be used as is" % exc
)
return open(README).read()


def findsome(subdir, extensions):
"""Find files under subdir having specified extensions

Leading directory (datalad) gets stripped
"""
return [
f.split(pathsep, 1)[1] for f in findall(opj('datalad', subdir))
if splitext(f)[-1].lstrip('.') in extensions
]


def datalad_setup(name, **kwargs):
"""A helper for a typical invocation of setuptools.setup.

If not provided in kwargs, following fields will be autoset to the defaults
or obtained from the present on the file system files:

- author
- author_email
- packages -- all found packages which start with `name`
- long_description -- converted to .rst using pypandoc README.md
- version -- parsed `__version__` within `name/version.py`

Parameters
----------
name: str
Name of the Python package
**kwargs:
The rest of the keyword arguments passed to setuptools.setup as is
"""
# Simple defaults
for k, v in {
'author': "The DataLad Team and Contributors",
'author_email': "team@datalad.org"
}.items():
if kwargs.get(k) is None:
kwargs[k] = v

# More complex, requiring some function call

# Only recentish versions of find_packages support include
# packages = find_packages('.', include=['datalad*'])
# so we will filter manually for maximal compatibility
if kwargs.get('packages') is None:
kwargs['packages'] = [pkg for pkg in find_packages('.') if pkg.startswith(name)]
if kwargs.get('long_description') is None:
kwargs['long_description'] = get_long_description_from_README()
if kwargs.get('version') is None:
kwargs['version'] = get_version(name)

return setup(name=name, **kwargs)