From cec84af5b0051bfa7ed30b15c6b25ef528a53027 Mon Sep 17 00:00:00 2001 From: Raul Farkas Date: Fri, 28 Oct 2022 16:11:53 +0100 Subject: MLIA-630 Fix links to other files in README.md * Update setup.py to replace relative paths in README.md with links to files on review.mlplatform.org * Add a mechanism that replaces relative paths to images with the link to the plain image on review.mlplatform.org Change-Id: Ifeb17e3c03e8366bd12665eb9f4a630fc9933e26 --- README.md | 2 +- setup.py | 41 ++++++++++++++++++++++++++++++++++++++- tests/test_setup.py | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++ tox.ini | 3 +-- 4 files changed, 98 insertions(+), 4 deletions(-) create mode 100644 tests/test_setup.py diff --git a/README.md b/README.md index c100bdb..9b5b2c1 100644 --- a/README.md +++ b/README.md @@ -409,7 +409,7 @@ Additional useful information: ## License -ML Inference Advisor is licensed under [Apache License 2.0](LICENSE.txt). +ML Inference Advisor is licensed under [Apache License 2.0](LICENSES/Apache-2.0.txt). ## Trademarks and copyrights diff --git a/setup.py b/setup.py index 4707ea5..ad1ed8d 100644 --- a/setup.py +++ b/setup.py @@ -1,7 +1,46 @@ # SPDX-FileCopyrightText: Copyright 2022, Arm Limited and/or its affiliates. # SPDX-License-Identifier: Apache-2.0 """Module to setup the python package.""" +from __future__ import annotations + +import os +import re +from pathlib import Path +from string import Template + from setuptools import setup -setup() +def replace_markdown_relative_paths( + path: Path, file_name: str, revision_tag: str +) -> str: + """Replace relative paths in md file with links to review.mlplatform.org.""" + md_url = Template( + "https://review.mlplatform.org/plugins/gitiles/ml/mlia/+/refs/tags/$tag/$link" + ) + img_url = Template("https://git.mlplatform.org/ml/mlia.git/plain/$link?h=$tag") + md_link_pattern = r"(!?\[.+?\]\((.+?)\))" + + content = path.joinpath(file_name).read_text() + # Find all md links with these formats: [title](url) or ![title](url) + for match, link in re.findall(md_link_pattern, content): + if path.joinpath(link).exists(): + # Choose appropriate url template depending on wheteher the + # orginal link points to a file or an image. + template = img_url if match[0] == "!" else md_url + new_url = template.substitute(tag=revision_tag, link=link) + md_link = match.replace(link, new_url) + # Replace existing links with new ones + content = content.replace(match, md_link) + if os.getenv("MLIA_DEBUG"): + # Generate an md file to verify the changes + path.joinpath("PYPI.md").write_text(content) + return content + + +if __name__ == "__main__": + from setuptools_scm import get_version # pylint: disable=import-error + + tag = get_version() + long_description = replace_markdown_relative_paths(Path.cwd(), "README.md", tag) + setup(long_description=long_description) diff --git a/tests/test_setup.py b/tests/test_setup.py new file mode 100644 index 0000000..1850319 --- /dev/null +++ b/tests/test_setup.py @@ -0,0 +1,56 @@ +# SPDX-FileCopyrightText: Copyright 2022, Arm Limited and/or its affiliates. +# SPDX-License-Identifier: Apache-2.0 +"""Test for module setup.""" +from __future__ import annotations + +from unittest.mock import Mock +from unittest.mock import patch + +import pytest + +from setup import replace_markdown_relative_paths + + +@pytest.mark.parametrize( + "linked_file_found, file_content, expected_result", + [ + [ + True, + "[Test](README.md)", + "[Test](https://review.mlplatform.org/plugins/gitiles/" + "ml/mlia/+/refs/tags/0.1.0/README.md)", + ], + [ + True, + "![Test](image.png)", + "![Test](https://git.mlplatform.org/ml/mlia.git/plain/image.png?h=0.1.0)", + ], + [ + False, + "[Test](http://review.mlplatform.org)", + "[Test](http://review.mlplatform.org)", + ], + [False, "[Test](README.md)", "[Test](README.md)"], + [ + True, + "Lorem ipsum dolor sit amet, consectetur adipiscing elit", + "Lorem ipsum dolor sit amet, consectetur adipiscing elit", + ], + ], +) +@patch("pathlib.Path") +def test_replace_markdown_relative_paths( + path_mock: Mock, + linked_file_found: bool, + file_content: str, + expected_result: str, +) -> None: + """Test replacement of relative md paths with links to review.mlplatform.org.""" + # Set a mock setuptools scm version for testing + tag = "0.1.0" + path_mock.read_text.return_value = file_content + path_mock.exists.return_value = linked_file_found + path_mock.joinpath.return_value = path_mock + + result = replace_markdown_relative_paths(path_mock, "test.md", tag) + assert result == expected_result diff --git a/tox.ini b/tox.ini index d8b3c76..017ee04 100644 --- a/tox.ini +++ b/tox.ini @@ -2,6 +2,7 @@ # SPDX-License-Identifier: Apache-2.0 [tox] envlist = test +isolated_build = true [testenv:test] description = Run the unit tests. @@ -23,7 +24,6 @@ commands = description = Run and setup the pre-commit hooks. # Re-use the same env for both lint and lint_setup envdir={toxworkdir}/lint -usedevelop = True extras = dev # Workaround to resolve an issue with markdownlint in a docker environment @@ -42,7 +42,6 @@ commands = [testenv:docs] description = Create the documentation. allowlist_externals = make -usedevelop = True deps = Sphinx==4.5.0 sphinx-rtd-theme==1.0.0 -- cgit v1.2.1