2020-12-01 21:58:32 +00:00
|
|
|
#!/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
|
2021-01-26 04:08:13 +00:00
|
|
|
from typing import List
|
2020-12-01 21:58:32 +00:00
|
|
|
|
2021-03-22 12:39:19 +00:00
|
|
|
_PROJECT_ROOT = os.path.dirname(os.path.dirname(__file__))
|
2020-12-01 21:58:32 +00:00
|
|
|
|
|
|
|
|
2021-07-26 11:37:35 +00:00
|
|
|
def _load_requirements(path_dir: str, file_name: str = "requirements.txt", comment_char: str = "#") -> List[str]:
|
2021-09-06 12:49:09 +00:00
|
|
|
"""Load requirements from a file.
|
2020-12-01 21:58:32 +00:00
|
|
|
|
2020-12-23 23:11:42 +00:00
|
|
|
>>> _load_requirements(_PROJECT_ROOT) # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE
|
2020-12-01 21:58:32 +00:00
|
|
|
['numpy...', 'torch...', ...]
|
|
|
|
"""
|
2021-07-26 12:38:12 +00:00
|
|
|
with open(os.path.join(path_dir, file_name)) as file:
|
2020-12-01 21:58:32 +00:00
|
|
|
lines = [ln.strip() for ln in file.readlines()]
|
|
|
|
reqs = []
|
|
|
|
for ln in lines:
|
|
|
|
# filer all comments
|
|
|
|
if comment_char in ln:
|
2021-07-26 11:37:35 +00:00
|
|
|
ln = ln[: ln.index(comment_char)].strip()
|
2020-12-01 21:58:32 +00:00
|
|
|
# skip directly installed dependencies
|
2021-07-26 11:37:35 +00:00
|
|
|
if ln.startswith("http") or "@http" in ln:
|
2020-12-01 21:58:32 +00:00
|
|
|
continue
|
|
|
|
if ln: # if requirement is not empty
|
|
|
|
reqs.append(ln)
|
|
|
|
return reqs
|
|
|
|
|
|
|
|
|
2021-03-22 12:39:19 +00:00
|
|
|
def _load_readme_description(path_dir: str, homepage: str, version: str) -> str:
|
2021-09-06 12:49:09 +00:00
|
|
|
"""Load readme as decribtion.
|
2020-12-01 21:58:32 +00:00
|
|
|
|
2021-03-22 12:39:19 +00:00
|
|
|
>>> _load_readme_description(_PROJECT_ROOT, "", "") # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE
|
2020-12-01 21:58:32 +00:00
|
|
|
'<div align="center">...'
|
|
|
|
"""
|
2020-12-09 09:59:44 +00:00
|
|
|
path_readme = os.path.join(path_dir, "README.md")
|
|
|
|
text = open(path_readme, encoding="utf-8").read()
|
|
|
|
|
2020-12-10 00:52:39 +00:00
|
|
|
# drop images from readme
|
2021-07-26 11:37:35 +00:00
|
|
|
text = text.replace("![PT to PL](docs/source/_static/images/general/pl_quick_start_full_compressed.gif)", "")
|
2020-12-10 00:52:39 +00:00
|
|
|
|
2021-02-03 15:08:19 +00:00
|
|
|
# https://github.com/PyTorchLightning/pytorch-lightning/raw/master/docs/source/_static/images/lightning_module/pt_to_pl.png
|
2021-01-26 04:08:13 +00:00
|
|
|
github_source_url = os.path.join(homepage, "raw", version)
|
2020-12-01 21:58:32 +00:00
|
|
|
# replace relative repository path to absolute link to the release
|
2020-12-09 09:59:44 +00:00
|
|
|
# do not replace all "docs" as in the readme we reger some other sources with particular path to docs
|
2021-02-03 15:08:19 +00:00
|
|
|
text = text.replace("docs/source/_static/", f"{os.path.join(github_source_url, 'docs/source/_static/')}")
|
2020-12-09 09:59:44 +00:00
|
|
|
|
|
|
|
# readthedocs badge
|
2021-07-26 11:37:35 +00:00
|
|
|
text = text.replace("badge/?version=stable", f"badge/?version={version}")
|
|
|
|
text = text.replace("pytorch-lightning.readthedocs.io/en/stable/", f"pytorch-lightning.readthedocs.io/en/{version}")
|
2020-12-09 09:59:44 +00:00
|
|
|
# codecov badge
|
2021-07-26 11:37:35 +00:00
|
|
|
text = text.replace("/branch/master/graph/badge.svg", f"/release/{version}/graph/badge.svg")
|
2020-12-09 09:59:44 +00:00
|
|
|
# replace github badges for release ones
|
2021-07-26 11:37:35 +00:00
|
|
|
text = text.replace("badge.svg?branch=master&event=push", f"badge.svg?tag={version}")
|
2021-02-18 22:51:09 +00:00
|
|
|
# Azure...
|
2021-07-26 11:37:35 +00:00
|
|
|
text = text.replace("?branchName=master", f"?branchName=refs%2Ftags%2F{version}")
|
|
|
|
text = re.sub(r"\?definitionId=\d+&branchName=master", f"?definitionId=2&branchName=refs%2Ftags%2F{version}", text)
|
2020-12-09 09:59:44 +00:00
|
|
|
|
2021-07-26 11:37:35 +00:00
|
|
|
skip_begin = r"<!-- following section will be skipped from PyPI description -->"
|
|
|
|
skip_end = r"<!-- end skipping PyPI description -->"
|
2020-12-23 22:37:33 +00:00
|
|
|
# todo: wrap content as commented description
|
2021-07-26 11:37:35 +00:00
|
|
|
text = re.sub(rf"{skip_begin}.+?{skip_end}", "<!-- -->", text, flags=re.IGNORECASE + re.DOTALL)
|
2020-12-23 22:37:33 +00:00
|
|
|
|
2020-12-09 09:59:44 +00:00
|
|
|
# # https://github.com/Borda/pytorch-lightning/releases/download/1.1.0a6/codecov_badge.png
|
2021-01-26 04:08:13 +00:00
|
|
|
# github_release_url = os.path.join(homepage, "releases", "download", version)
|
2020-12-09 09:59:44 +00:00
|
|
|
# # download badge and replace url with local file
|
|
|
|
# text = _parse_for_badge(text, github_release_url)
|
2020-12-01 21:58:32 +00:00
|
|
|
return text
|