From 01cb129677348824a20905baea112d501e3bf642 Mon Sep 17 00:00:00 2001 From: Nick Date: Mon, 23 May 2022 09:54:24 -0400 Subject: [PATCH] Use setuptools_scm (#48) * Use setuptools_scm Follows up https://github.com/ufoym/imbalanced-dataset-sampler/pull/47 - on [pypi](https://pypi.org/project/torchsampler/0.1.1/) it's 0.1.1 - on github it's [0.1.0](https://github.com/ufoym/imbalanced-dataset-sampler/releases/tag/v0.1.0) - but the attached [wheel](https://github.com/ufoym/imbalanced-dataset-sampler/releases/download/v0.1.0/torchsampler-0.1.1-py3-none-any.whl) and [sdist](https://github.com/ufoym/imbalanced-dataset-sampler/releases/download/v0.1.0/torchsampler-0.1.1.tar.gz) are 0.1.1 [`setuptools-scm`](https://pypi.org/project/setuptools-scm/) makes git the single-source of truth for the version of the package, and works well with the build script in #47 (which is triggered by tagging a new version in git). `setuptools-scm` also replaces most of MANIFEST.in, so this removes the redundant lines from that too. * Deprecate torchsampler.__about__ It's better to keep all metadata in setup.py and use importlib.metadata. Otherwise, you need to 'import __about__' during the build process means importlib-metadata needs to be installed during the build if building on python 3.6 or python 3.7, and potentially running an unbounded amount of other code during the build. It's easier to just sidestep the issue. * Squash a build warning > .../site-packages/setuptools/dist.py:726: UserWarning: Usage of dash-separated 'description-file' will not be supported in future versions. Please use the underscore name 'description_file' instead description_file has been superseded recently in 4c4a6e3d9a819b3cd7c3b9e580d28254351ec833 anyway. --- .github/workflows/ci_install-pkg.yml | 2 -- MANIFEST.in | 19 +++------------ requirements.txt | 2 ++ setup.cfg | 1 - setup.py | 22 +++++++---------- torchsampler/__about__.py | 35 ++++++++++++++++++++++------ 6 files changed, 41 insertions(+), 40 deletions(-) diff --git a/.github/workflows/ci_install-pkg.yml b/.github/workflows/ci_install-pkg.yml index 193dc07..95dccf3 100644 --- a/.github/workflows/ci_install-pkg.yml +++ b/.github/workflows/ci_install-pkg.yml @@ -19,8 +19,6 @@ jobs: - name: Check package run: | - pip install check-manifest - check-manifest python setup.py check --metadata --strict - name: Create package diff --git a/MANIFEST.in b/MANIFEST.in index cf448b8..712f2e7 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -1,18 +1,7 @@ -# Manifest syntax https://docs.python.org/2/distutils/sourcedist.html -graft wheelhouse +# Manifest syntax https://setuptools.pypa.io/en/latest/deprecated/distutils/commandref.html#sdist-cmd -recursive-exclude __pycache__ *.py[cod] *.orig - -# Include the README -include *.md - -# Include the license file -include LICENSE - -recursive-include torchsampler *.py - -# Include the Requirements -include requirements.txt +# FIXME: switching to a src/ layout would obviate the need for this file entirely. +# see https://setuptools.pypa.io/en/latest/userguide/package_discovery.html#src-layout # Exclude build configs exclude *.yml @@ -20,5 +9,3 @@ exclude *.yaml prune .github prune example* -prune temp* -prune test* diff --git a/requirements.txt b/requirements.txt index 6b4e9db..b09bf97 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,5 @@ torch>=1.3 torchvision>=0.5 pandas +# only needed by __about__.py, which is deprecated: +importlib_metadata; python_version<'3.8' diff --git a/setup.cfg b/setup.cfg index 152f83b..946216e 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,6 +1,5 @@ [metadata] license_file = LICENSE -description-file = README.md [flake8] max-line-length = 120 diff --git a/setup.py b/setup.py index 456dc49..589ece4 100644 --- a/setup.py +++ b/setup.py @@ -1,17 +1,9 @@ """A setuptools based setup module.""" -import sys from pathlib import Path from setuptools import setup -try: - from torchsampler import __about__ as about -except ImportError: - # alternative https://stackoverflow.com/a/67692/4521646 - sys.path.append("torchsampler") - import __about__ as about - PATH_HERE = Path(__file__).parent with open(PATH_HERE / 'requirements.txt', encoding='utf-8') as fp: @@ -20,16 +12,18 @@ setup( name='torchsampler', - version=about.__version__, - url=about.__homepage__, - author=about.__author__, - author_email=about.__author_email__, - license=about.__license__, - description=about.__doc__, + use_scm_version=True, + url="https://github.com/ufoym/imbalanced-dataset-sample", + author="Ming, et al.", + author_email="a@ufoym.com", + license="MIT", + description="A (PyTorch) imbalanced dataset sampler for oversampling low classes" + "and undersampling high frequent ones.", long_description=long_description, long_description_content_type='text/markdown', packages=['torchsampler'], keywords='sampler,pytorch,dataloader', + setup_requires=['setuptools_scm'], install_requires=requirements, python_requires='>=3.6', include_package_data=True, diff --git a/torchsampler/__about__.py b/torchsampler/__about__.py index bd953c7..cef1e9e 100644 --- a/torchsampler/__about__.py +++ b/torchsampler/__about__.py @@ -1,10 +1,31 @@ -__version__ = "0.1.1" -__author__ = "Ming, et al." -__author_email__ = "a@ufoym.com" -__license__ = "MIT" -__homepage__ = "https://github.com/ufoym/imbalanced-dataset-sampler" -__doc__ = "A (PyTorch) imbalanced dataset sampler for oversampling low frequent classes" \ - " and undersampling high frequent ones." +import sys + +if sys.version_info < (3, 8): + from importlib_metadata import distribution, PackageNotFoundError +else: + from importlib.metadata import distribution, PackageNotFoundError + +import warnings + +warnings.warn( + "Use importlib.metadata.distribution('torchsampler').metadata instead of torchsampler.__about__.", + category=DeprecationWarning +) + +try: + _m = distribution('torchsampler').metadata + __version__ = _m['version'] + __author__ = _m['author'] + __author_email__ = _m['author-email'] + __license__ = _m['license'] + __homepage__ = _m['homepage'] + __doc__ = _m['summary'] + del _m +except PackageNotFoundError: + # package is not installed (i.e. we're probably in the build process itself) + pass + +del sys, distribution, PackageNotFoundError, warnings __all__ = [ "__author__",