From c26c8d8f407de386038d5fb13297233a8aa052e7 Mon Sep 17 00:00:00 2001 From: Jirka Borovec <6035284+Borda@users.noreply.github.com> Date: Tue, 8 Nov 2022 16:37:33 +0100 Subject: [PATCH] prune setup (#942) --- pl_bolts/setup_tools.py | 78 ------------------------------------ requirements.txt | 2 +- setup.py | 89 ++++++++++++++++++++++++++++++++++------- 3 files changed, 75 insertions(+), 94 deletions(-) delete mode 100644 pl_bolts/setup_tools.py diff --git a/pl_bolts/setup_tools.py b/pl_bolts/setup_tools.py deleted file mode 100644 index 0d6228a4d9..0000000000 --- a/pl_bolts/setup_tools.py +++ /dev/null @@ -1,78 +0,0 @@ -#!/usr/bin/env python -# Copyright The PyTorch Lightning team. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -import os -import re -from typing import List - -_PROJECT_ROOT = os.path.dirname(os.path.dirname(__file__)) - - -def _load_requirements(path_dir: str, file_name: str = "requirements.txt", comment_char: str = "#") -> List[str]: - """Load requirements from a file. - - >>> _load_requirements(_PROJECT_ROOT) # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE - ['pytorch-lightning...'] - """ - with open(os.path.join(path_dir, file_name)) as file: - lines = [ln.strip() for ln in file.readlines()] - reqs = [] - for ln in lines: - # filer all comments - if comment_char in ln: - ln = ln[: ln.index(comment_char)].strip() - # skip directly installed dependencies - if ln.startswith("http"): - continue - if ln: # if requirement is not empty - reqs.append(ln) - return reqs - - -def _load_readme_description(path_dir: str, homepage: str, ver: str) -> str: - """Load readme as decribtion. - - >>> _load_readme_description(_PROJECT_ROOT, "", "") # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE - '
...' - """ - path_readme = os.path.join(path_dir, "README.md") - text = open(path_readme, encoding="utf-8").read() - - # drop images from readme - text = text.replace("![PT to PL](docs/source/_images/general/pl_quick_start_full_compressed.gif)", "") - - # https://github.com/PyTorchLightning/pytorch-lightning/raw/master/docs/source/_images/lightning_module/pt_to_png - github_source_url = os.path.join(homepage, "raw", ver) - # replace relative repository path to absolute link to the release - # do not replace all "docs" as in the readme we reger some other sources with particular path to docs - text = text.replace("docs/source/_images/", f"{os.path.join(github_source_url, 'docs/source/_images/')}") - - # readthedocs badge - text = text.replace("badge/?version=stable", f"badge/?version={ver}") - text = text.replace("lightning-bolts.readthedocs.io/en/stable/", f"lightning-bolts.readthedocs.io/en/{ver}") - # codecov badge - text = text.replace("/branch/master/graph/badge.svg", f"/release/{ver}/graph/badge.svg") - # replace github badges for release ones - text = text.replace("badge.svg?branch=master&event=push", f"badge.svg?tag={ver}") - - skip_begin = r"" - skip_end = r"" - # todo: wrap content as commented description - text = re.sub(rf"{skip_begin}.+?{skip_end}", "", text, flags=re.IGNORECASE + re.DOTALL) - - # # https://github.com/Borda/pytorch-lightning/releases/download/1.1.0a6/codecov_badge.png - # github_release_url = os.path.join(homepage, "releases", "download", ver) - # # download badge and replace url with local file - # text = _parse_for_badge(text, github_release_url) - return text diff --git a/requirements.txt b/requirements.txt index b24d71c2b2..64a66968da 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,3 @@ pytorch-lightning>=1.7.0 lightning-utilities>=0.3.0, !=0.4.0 # this is needed for PL 1.7 -torchvision>=0.10.* +torchvision>=0.10.0 diff --git a/setup.py b/setup.py index 7dbbf21de1..d2ce109342 100755 --- a/setup.py +++ b/setup.py @@ -1,36 +1,95 @@ #!/usr/bin/env python import os -import sys +import re +from importlib.util import module_from_spec, spec_from_file_location +from typing import List -# Always prefer setuptools over distutils from setuptools import find_packages, setup _PATH_ROOT = os.path.realpath(os.path.dirname(__file__)) _PATH_REQUIRE = os.path.join(_PATH_ROOT, "requirements") -try: - from pl_bolts import __about__ as about - from pl_bolts import setup_tools -except ImportError: - # alternative https://stackoverflow.com/a/67692/4521646 - sys.path.append("pl_bolts") - import __about__ as about - import setup_tools + +def _load_py_module(fname, pkg="pl_bolts"): + spec = spec_from_file_location(os.path.join(pkg, fname), os.path.join(_PATH_ROOT, pkg, fname)) + py = module_from_spec(spec) + spec.loader.exec_module(py) + return py + + +def _load_requirements(path_dir: str, file_name: str = "requirements.txt", comment_char: str = "#") -> List[str]: + """Load requirements from a file. + + >>> _load_requirements(_PATH_ROOT) # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE + ['pytorch-lightning...'] + """ + with open(os.path.join(path_dir, file_name)) as file: + lines = [ln.strip() for ln in file.readlines()] + reqs = [] + for ln in lines: + # filer all comments + if comment_char in ln: + ln = ln[: ln.index(comment_char)].strip() + # skip directly installed dependencies + if ln.startswith("http"): + continue + if ln: # if requirement is not empty + reqs.append(ln) + return reqs + + +def _load_readme_description(path_dir: str, homepage: str, ver: str) -> str: + """Load readme as decribtion. + + >>> _load_readme_description(_PATH_ROOT, "", "") # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE + '
...' + """ + path_readme = os.path.join(path_dir, "README.md") + text = open(path_readme, encoding="utf-8").read() + + # drop images from readme + text = text.replace("![PT to PL](docs/source/_images/general/pl_quick_start_full_compressed.gif)", "") + + # https://github.com/PyTorchLightning/pytorch-lightning/raw/master/docs/source/_images/lightning_module/pt_to_png + github_source_url = os.path.join(homepage, "raw", ver) + # replace relative repository path to absolute link to the release + # do not replace all "docs" as in the readme we reger some other sources with particular path to docs + text = text.replace("docs/source/_images/", f"{os.path.join(github_source_url, 'docs/source/_images/')}") + + # readthedocs badge + text = text.replace("badge/?version=stable", f"badge/?version={ver}") + text = text.replace("lightning-bolts.readthedocs.io/en/stable/", f"lightning-bolts.readthedocs.io/en/{ver}") + # codecov badge + text = text.replace("/branch/master/graph/badge.svg", f"/release/{ver}/graph/badge.svg") + # replace github badges for release ones + text = text.replace("badge.svg?branch=master&event=push", f"badge.svg?tag={ver}") + + skip_begin = r"" + skip_end = r"" + # todo: wrap content as commented description + text = re.sub(rf"{skip_begin}.+?{skip_end}", "", text, flags=re.IGNORECASE + re.DOTALL) + + # # https://github.com/Borda/pytorch-lightning/releases/download/1.1.0a6/codecov_badge.png + # github_release_url = os.path.join(homepage, "releases", "download", ver) + # # download badge and replace url with local file + # text = _parse_for_badge(text, github_release_url) + return text def _prepare_extras(): extras = { - "loggers": setup_tools._load_requirements(path_dir=_PATH_REQUIRE, file_name="loggers.txt"), - "models": setup_tools._load_requirements(path_dir=_PATH_REQUIRE, file_name="models.txt"), - "test": setup_tools._load_requirements(path_dir=_PATH_REQUIRE, file_name="test.txt"), + "loggers": _load_requirements(path_dir=_PATH_REQUIRE, file_name="loggers.txt"), + "models": _load_requirements(path_dir=_PATH_REQUIRE, file_name="models.txt"), + "test": _load_requirements(path_dir=_PATH_REQUIRE, file_name="test.txt"), } extras["extra"] = extras["models"] + extras["loggers"] extras["dev"] = extras["extra"] + extras["test"] return extras -long_description = setup_tools._load_readme_description( +about = _load_py_module("__about__.py") +long_description = _load_readme_description( _PATH_ROOT, homepage=about.__homepage__, ver=about.__version__, @@ -58,7 +117,7 @@ def _prepare_extras(): keywords=["deep learning", "pytorch", "AI"], python_requires=">=3.7", setup_requires=["wheel"], - install_requires=setup_tools._load_requirements(_PATH_ROOT), + install_requires=_load_requirements(_PATH_ROOT), extras_require=_prepare_extras(), project_urls={ "Bug Tracker": "https://github.com/PyTorchLightning/lightning-bolts/issues",