|
| 1 | +"""Utilities for generating a placeholder package to reserve a name on PyPI.""" |
| 2 | + |
| 3 | +import argparse |
| 4 | +import shutil |
| 5 | +import tempfile |
| 6 | +from pathlib import Path |
| 7 | +from typing import Optional |
| 8 | + |
| 9 | +from ci_tools.build import build_packages |
| 10 | + |
| 11 | + |
| 12 | +def _parse_args(argv: Optional[list[str]] = None) -> argparse.Namespace: |
| 13 | + parser = argparse.ArgumentParser( |
| 14 | + description=( |
| 15 | + "Generate a minimal Python distribution that can be published to PyPI " |
| 16 | + "in order to reserve a package name." |
| 17 | + ) |
| 18 | + ) |
| 19 | + parser.add_argument( |
| 20 | + "--working_dir", |
| 21 | + help=( |
| 22 | + "Temporary workspace used to author the placeholder project. " |
| 23 | + "Defaults to the current platform temp directory." |
| 24 | + ), |
| 25 | + default=None, |
| 26 | + ) |
| 27 | + parser.add_argument( |
| 28 | + "--output_dir", |
| 29 | + required=True, |
| 30 | + help="Directory where the built distributions will be written.", |
| 31 | + ) |
| 32 | + parser.add_argument( |
| 33 | + "--package_version", |
| 34 | + default="0.0.0", |
| 35 | + help="The distribution version to reserve on PyPI (Defaults to 0.0.0).", |
| 36 | + ) |
| 37 | + |
| 38 | + parser.add_argument( |
| 39 | + "package_name", |
| 40 | + help="The distribution name to reserve on PyPI (e.g. azure-mgmt-servicename).", |
| 41 | + ) |
| 42 | + return parser.parse_args(argv) |
| 43 | + |
| 44 | + |
| 45 | +def _normalise_module_name(dist_name: str) -> str: |
| 46 | + return dist_name.replace("-", "_") |
| 47 | + |
| 48 | + |
| 49 | +def _write_placeholder_project(project_dir: Path, package_name: str, package_version: str) -> None: |
| 50 | + module_name = _normalise_module_name(package_name) |
| 51 | + src_dir = project_dir / "src" / module_name |
| 52 | + src_dir.mkdir(parents=True, exist_ok=True) |
| 53 | + |
| 54 | + readme = project_dir / "README.md" |
| 55 | + readme.write_text( |
| 56 | + ( |
| 57 | + f""" |
| 58 | +# This package will be `{package_name}` |
| 59 | +
|
| 60 | +The `Azure SDK` team is planning to release this package for public usage soon. This is a placeholder on PyPI for planning purposes and do not yet contain any code. |
| 61 | +
|
| 62 | +If you have any questions about the future availability of this package, please create an issue on https://github.com/Azure/azure-sdk-for-python/issues or contact the email on this PyPI page. |
| 63 | +""" |
| 64 | + ), |
| 65 | + encoding="utf-8", |
| 66 | + ) |
| 67 | + |
| 68 | + pyproject = project_dir / "pyproject.toml" |
| 69 | + pyproject.write_text( |
| 70 | + ( |
| 71 | + f""" |
| 72 | +[build-system] |
| 73 | +requires = ["setuptools>=77.0.3", "wheel"] |
| 74 | +build-backend = "setuptools.build_meta" |
| 75 | +
|
| 76 | +[project] |
| 77 | +name = "{package_name}" |
| 78 | +authors = [ |
| 79 | + {{name = "Microsoft Corporation", email = "[email protected]"}}, |
| 80 | +] |
| 81 | +description = "This package will be released in the near future. Stay tuned!" |
| 82 | +keywords = ["azure", "azure sdk"] |
| 83 | +requires-python = ">=3.9" |
| 84 | +license = "MIT" |
| 85 | +version = "{package_version}" |
| 86 | +classifiers = [ |
| 87 | + "Development Status :: 1 - Planning", |
| 88 | + "Programming Language :: Python", |
| 89 | + "Programming Language :: Python :: 3 :: Only", |
| 90 | + "Programming Language :: Python :: 3", |
| 91 | + "Programming Language :: Python :: 3.9", |
| 92 | + "Programming Language :: Python :: 3.10", |
| 93 | + "Programming Language :: Python :: 3.11", |
| 94 | + "Programming Language :: Python :: 3.12", |
| 95 | + "Programming Language :: Python :: 3.13", |
| 96 | + "Programming Language :: Python :: 3.14", |
| 97 | +] |
| 98 | +dependencies = [] |
| 99 | +dynamic = ["readme"] |
| 100 | +
|
| 101 | +[project.urls] |
| 102 | +"Bug Reports" = "https://github.com/Azure/azure-sdk-for-python/issues" |
| 103 | +repository = "https://github.com/Azure/azure-sdk-for-python" |
| 104 | +
|
| 105 | +[tool.setuptools.dynamic] |
| 106 | +readme = {{file = ["README.md"], content-type = "text/markdown"}} |
| 107 | +""" |
| 108 | + ), |
| 109 | + encoding="utf-8", |
| 110 | + ) |
| 111 | + |
| 112 | + |
| 113 | +def _build_distributions(project_dir: str, output_dir: str) -> None: |
| 114 | + build_packages([project_dir], distribution_directory=output_dir) |
| 115 | + |
| 116 | + |
| 117 | +def generate_main(argv: Optional[list[str]] = None) -> int: |
| 118 | + args = _parse_args(argv) |
| 119 | + |
| 120 | + print(f"Generating name reservation package for {args.package_name}=={args.package_version}") |
| 121 | + |
| 122 | + work_root = Path(args.working_dir) if args.working_dir else Path(tempfile.gettempdir()) |
| 123 | + work_root.mkdir(parents=True, exist_ok=True) |
| 124 | + |
| 125 | + project_dir = work_root / f"{args.package_name}" |
| 126 | + |
| 127 | + if project_dir.exists(): |
| 128 | + print(f"Removing existing project directory {project_dir}") |
| 129 | + shutil.rmtree(project_dir) |
| 130 | + |
| 131 | + project_dir.mkdir(parents=True, exist_ok=True) |
| 132 | + |
| 133 | + print(f"Creating placeholder project for {args.package_name} in {project_dir}") |
| 134 | + _write_placeholder_project(project_dir, args.package_name, args.package_version) |
| 135 | + |
| 136 | + try: |
| 137 | + _build_distributions(str(project_dir), args.output_dir) |
| 138 | + finally: |
| 139 | + print(f"Cleaning up working directory {project_dir}") |
| 140 | + shutil.rmtree(project_dir, ignore_errors=True) |
| 141 | + |
| 142 | + print(f"Finished generating distributions for {args.package_name}") |
| 143 | + return 0 |
| 144 | + |
| 145 | + |
| 146 | +if __name__ == "__main__": |
| 147 | + raise SystemExit(generate_main()) |
0 commit comments