From f50822c9065084a92594bec7583f7b503c4b7781 Mon Sep 17 00:00:00 2001 From: Stanislav Pankevich Date: Mon, 3 Feb 2025 20:56:19 +0100 Subject: [PATCH] tasks: add release task for publishing Pip --- .github/workflows/ci-linux-ubuntu-latest.yml | 2 +- .github/workflows/ci-mac.yml | 2 +- .github/workflows/ci-windows.yml | 4 +- .gitignore | 2 + ...deps.py => pip_install_html2print_deps.py} | 12 ++-- hpdf/hpdf.py => html2print/html2print.py | 14 ++-- pyproject.toml | 15 +++-- requirements.development.txt | 2 + tasks.py | 66 +++++++++++++++++-- tests/integration/lit.cfg | 4 +- 10 files changed, 92 insertions(+), 31 deletions(-) rename developer/{pip_install_hpdf_deps.py => pip_install_html2print_deps.py} (86%) rename hpdf/hpdf.py => html2print/html2print.py (97%) diff --git a/.github/workflows/ci-linux-ubuntu-latest.yml b/.github/workflows/ci-linux-ubuntu-latest.yml index c16fce6..4a5230c 100644 --- a/.github/workflows/ci-linux-ubuntu-latest.yml +++ b/.github/workflows/ci-linux-ubuntu-latest.yml @@ -38,7 +38,7 @@ jobs: - name: Install HPDF dependencies. run: | - python developer/pip_install_hpdf_deps.py + python developer/pip_install_html2print_deps.py - name: Run Lint tasks run: | diff --git a/.github/workflows/ci-mac.yml b/.github/workflows/ci-mac.yml index 72f3c69..5a84528 100644 --- a/.github/workflows/ci-mac.yml +++ b/.github/workflows/ci-mac.yml @@ -36,7 +36,7 @@ jobs: - name: Install HPDF dependencies. run: | - python developer/pip_install_hpdf_deps.py + python developer/pip_install_html2print_deps.py - name: Run Lint tasks run: | diff --git a/.github/workflows/ci-windows.yml b/.github/workflows/ci-windows.yml index 88047fe..939517f 100644 --- a/.github/workflows/ci-windows.yml +++ b/.github/workflows/ci-windows.yml @@ -56,7 +56,7 @@ jobs: - name: Install HPDF dependencies. run: | - python developer/pip_install_hpdf_deps.py + python developer/pip_install_html2print_deps.py - name: Run Lint tasks run: | @@ -68,7 +68,7 @@ jobs: - name: Download ChromeDriver run: | - python hpdf/hpdf.py get_driver + python html2print/html2print.py get_driver - name: Run tests (Bash) run: | diff --git a/.gitignore b/.gitignore index d098703..ad82c22 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,7 @@ .idea/ **/.wdm/ +build/ +dist/ tests/integration/.lit_test_times.txt tests/integration/**/Output/ diff --git a/developer/pip_install_hpdf_deps.py b/developer/pip_install_html2print_deps.py similarity index 86% rename from developer/pip_install_hpdf_deps.py rename to developer/pip_install_html2print_deps.py index 2a9fd1d..9733170 100644 --- a/developer/pip_install_hpdf_deps.py +++ b/developer/pip_install_html2print_deps.py @@ -29,7 +29,7 @@ def check_if_package_installed(package_name: str): print( # noqa: T201 - "pip_install_hpdf_deps.py: " + "pip_install_html2print_deps.py: " "checking if the current Python environment has all packages installed" ".", flush=True, @@ -48,7 +48,7 @@ def check_if_package_installed(package_name: str): check_if_package_installed(dependency) except PackageNotFound: print( # noqa: T201 - f"pip_install_hpdf_deps.py: " + f"pip_install_html2print_deps.py: " f"Package is not installed: '{dependency}'.", flush=True, ) @@ -57,8 +57,8 @@ def check_if_package_installed(package_name: str): except PackageVersionConflict as exception_: print( # noqa: T201 ( - f"pip_install_hpdf_deps.py: version conflict between " - f"hpdf's requirement '{dependency}' " + f"pip_install_html2print_deps.py: version conflict between " + f"html2print's requirement '{dependency}' " f"and the already installed package: " f"{exception_.args[0]}." ), @@ -69,13 +69,13 @@ def check_if_package_installed(package_name: str): if not needs_installation: print( # noqa: T201 - "pip_install_hpdf_deps.py: all packages seem to be installed.", + "pip_install_html2print_deps.py: all packages seem to be installed.", flush=True, ) sys.exit(0) print( # noqa: T201 - "pip_install_hpdf_deps.py: will install packages.", flush=True + "pip_install_html2print_deps.py: will install packages.", flush=True ) all_packages = "\n".join(dependencies) + "\n" diff --git a/hpdf/hpdf.py b/html2print/html2print.py similarity index 97% rename from hpdf/hpdf.py rename to html2print/html2print.py index c908781..ecbca3b 100644 --- a/hpdf/hpdf.py +++ b/html2print/html2print.py @@ -23,9 +23,9 @@ from webdriver_manager.core.http import HttpClient from webdriver_manager.core.os_manager import OperationSystemManager -__version__ = "0.0.1" +__version__ = "0.0.2" -DEFAULT_CACHE_DIR = os.path.join(Path.home(), ".hpdf", "chromedriver") +DEFAULT_CACHE_DIR = os.path.join(Path.home(), ".html2print", "chromedriver") # HTML2PDF.js prints unicode symbols to console. The following makes it work on # Windows which otherwise complains: @@ -35,7 +35,7 @@ sys.stdout = open(sys.stdout.fileno(), mode="w", encoding="utf8", closefd=False) -class HTML2PDF_HTTPClient(HttpClient): +class HTML2Print_HTTPClient(HttpClient): def get(self, url, params=None, **kwargs) -> Response: last_error: Optional[Exception] = None for attempt in range(1, 3): @@ -58,7 +58,7 @@ def get(self, url, params=None, **kwargs) -> Response: ) -class HTML2PDF_CacheManager(DriverCacheManager): +class HTML2Print_CacheManager(DriverCacheManager): def __init__(self, file_manager: FileManager, path_to_cache_dir: str): super().__init__(file_manager=file_manager) self.path_to_cache_dir: str = path_to_cache_dir @@ -187,12 +187,12 @@ class Done(Exception): def get_chrome_driver(path_to_cache_dir: str) -> str: - cache_manager = HTML2PDF_CacheManager( + cache_manager = HTML2Print_CacheManager( file_manager=FileManager(os_system_manager=OperationSystemManager()), path_to_cache_dir=path_to_cache_dir, ) - http_client = HTML2PDF_HTTPClient() + http_client = HTML2Print_HTTPClient() download_manager = WDMDownloadManager(http_client) path_to_chrome = ChromeDriverManager( download_manager=download_manager, cache_manager=cache_manager @@ -239,7 +239,7 @@ def main(): # You can override this setting and save binaries to project.root/.wdm. os.environ["WDM_LOCAL"] = "1" - parser = argparse.ArgumentParser(description="HTML2PDF printer script.") + parser = argparse.ArgumentParser(description="HTML2Print printer script.") parser.add_argument( "-v", "--version", action="version", version=__version__ diff --git a/pyproject.toml b/pyproject.toml index ad5767b..f84aa5a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -3,11 +3,11 @@ requires = ["hatchling"] build-backend = "hatchling.build" [tool.hatch.version] -path = "hpdf/hpdf.py" +path = "html2print/html2print.py" [tool.hatch.build] include = [ - "/hpdf/", + "/html2print/", "LICENSE", "README.md", "pyproject.toml" @@ -19,18 +19,21 @@ exclude = [ ] [project] -name = "hpdf" +name = "html2print" dynamic = ["version"] description = "Python client for HTML2PDF JavaScript library." readme = "README.md" -license = "TBD" +# https://github.com/pypa/twine/issues/1216 license-files is broken as of 2025-02-03 +# Using [] as a suggested workaround. +# license-files = [ "LICENSE" ] +license-files = [] requires-python = ">=3.8" authors = [ { name = "Stanislav Pankevich", email = "s.pankevich@gmail.com" }, { name = "Maryna Balioura", email = "mettta@gmail.com" }, ] classifiers = [ - "License :: OSI Approved :: BSD License", + # "License :: OSI Approved :: BSD License", "Operating System :: OS Independent", "Programming Language :: Python :: 3", "Programming Language :: Python :: 3.8", @@ -58,7 +61,7 @@ development = [ ] [project.scripts] -hpdf = "hpdf:main" +html2print = "html2print:main" [project.urls] Changelog = "https://github.com/mettta/html2pdf_python/releases/" diff --git a/requirements.development.txt b/requirements.development.txt index ff5ec4e..b3f7d5b 100644 --- a/requirements.development.txt +++ b/requirements.development.txt @@ -1,7 +1,9 @@ invoke toml +build packaging setuptools +twine # # Lint diff --git a/tasks.py b/tasks.py index 423fe63..b883ab8 100644 --- a/tasks.py +++ b/tasks.py @@ -84,7 +84,7 @@ def get_chrome_driver( run_invoke( context, """ - python hpdf/hpdf.py get_driver + python html2print/html2print.py get_driver """, ) @@ -97,7 +97,7 @@ def lint_ruff_format(context): ruff format *.py - hpdf/ + html2print/ tests/integration/ """, ) @@ -113,7 +113,7 @@ def lint_ruff(context): run_invoke( context, """ - ruff check *.py hpdf/ --fix --cache-dir build/ruff + ruff check *.py html2print/ --fix --cache-dir build/ruff """, ) @@ -126,7 +126,7 @@ def lint_mypy(context): run_invoke( context, """ - mypy hpdf/ + mypy html2print/ --show-error-codes --disable-error-code=import --disable-error-code=misc @@ -156,7 +156,7 @@ def test_integration( cwd = os.getcwd() - html2pdf_exec = f'python3 \\"{cwd}/hpdf/hpdf.py\\"' + html2pdf_exec = f'python3 \\"{cwd}/html2print/html2print.py\\"' focus_or_none = f"--filter {focus}" if focus else "" debug_opts = "-vv --show-all" if debug else "" @@ -167,7 +167,7 @@ def test_integration( itest_command = f""" lit --threads 1 - --param HTML2PDF_EXEC="{html2pdf_exec}" + --param HTML2PRINT_EXEC="{html2pdf_exec}" -v {debug_opts} {focus_or_none} @@ -191,3 +191,57 @@ def clean_itest_artifacts(context): # The command sometimes exits with 1 even if the files are deleted. # warn=True ensures that the execution continues. run_invoke(context, find_command, warn=True) + + +@task +def release(context, test_pypi=False, username=None, password=None): + """ + A release can be made to PyPI or test package index (TestPyPI): + https://pypi.org/project/html2print/ + https://test.pypi.org/project/html2print/ + """ + + # When a username is provided, we also need password, and then we don't use + # tokens set up on a local machine. + assert username is None or password is not None + + repository_argument_or_none = ( + "" + if username + else ( + "--repository html2print_test" + if test_pypi + else "--repository html2print_release" + ) + ) + user_password = f"-u{username} -p{password}" if username is not None else "" + + run_invoke( + context, + """ + rm -rfv dist/ + """, + ) + run_invoke( + context, + """ + python3 -m build + """, + ) + run_invoke( + context, + """ + twine check dist/* + """, + ) + # The token is in a core developer's .pypirc file. + # https://test.pypi.org/manage/account/token/ + # https://packaging.python.org/en/latest/specifications/pypirc/#pypirc + run_invoke( + context, + f""" + twine upload dist/html2print-*.tar.gz + {repository_argument_or_none} + {user_password} + """, + ) diff --git a/tests/integration/lit.cfg b/tests/integration/lit.cfg index e1a083a..6c8c1a2 100644 --- a/tests/integration/lit.cfg +++ b/tests/integration/lit.cfg @@ -4,12 +4,12 @@ import subprocess import lit.formats -config.name = "HTML2PDF Python API integration tests" +config.name = "html2print Python API integration tests" config.test_format = lit.formats.ShTest("0") current_dir = os.getcwd() -html2pdf_exec = lit_config.params['HTML2PDF_EXEC'] +html2pdf_exec = lit_config.params['HTML2PRINT_EXEC'] assert(html2pdf_exec) config.substitutions.append(('%project_root', current_dir))