|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +""" |
| 4 | +Updates the bundled certifi module. |
| 5 | +
|
| 6 | +Run as "./update_certifi.py YYYY.MM.DD" to get a specific release from PyPI. |
| 7 | +""" |
| 8 | + |
| 9 | +import pathlib |
| 10 | +import re |
| 11 | +import shutil |
| 12 | +import subprocess |
| 13 | +import sys |
| 14 | +import tempfile |
| 15 | + |
| 16 | + |
| 17 | +class Utilities: |
| 18 | + def download_wheel(self, version, dest_dir): |
| 19 | + """Download the certifi wheel from PyPI.""" |
| 20 | + print(f"Downloading certifi {version}") |
| 21 | + subprocess.check_output( |
| 22 | + [ |
| 23 | + "pip", |
| 24 | + "download", |
| 25 | + f"certifi=={version}", |
| 26 | + "--no-deps", |
| 27 | + "--index-url", |
| 28 | + "https://pypi.org/simple/", |
| 29 | + "-d", |
| 30 | + str(dest_dir), |
| 31 | + ] |
| 32 | + ) |
| 33 | + |
| 34 | + def unzip_archive(self, file_path, temp_dir): |
| 35 | + """Unzip in a temp dir.""" |
| 36 | + print(f"Unzipping {file_path.name}") |
| 37 | + subprocess.check_output(["unzip", str(file_path), "-d", str(temp_dir)]) |
| 38 | + |
| 39 | + def remove_folder(self, path): |
| 40 | + """Remove a folder recursively.""" |
| 41 | + print(f"Removing the folder {path}") |
| 42 | + shutil.rmtree(path, ignore_errors=True) |
| 43 | + |
| 44 | + def git_remove(self, target): |
| 45 | + print(f"Removing {target} in git.") |
| 46 | + try: |
| 47 | + subprocess.check_output(["git", "rm", "-rf"] + target) |
| 48 | + except Exception: |
| 49 | + pass |
| 50 | + |
| 51 | + def copy_folder(self, source, target): |
| 52 | + """Copy a folder recursively.""" |
| 53 | + shutil.copytree(source, target) |
| 54 | + |
| 55 | + def update_requirements(self, req_file, version): |
| 56 | + """Update the certifi version pin in requirements.txt.""" |
| 57 | + content = req_file.read_text() |
| 58 | + content = re.sub(r"certifi==[\d.]+", f"certifi=={version}", content) |
| 59 | + req_file.write_text(content) |
| 60 | + |
| 61 | + |
| 62 | +def main(temp_path, repo_root, version): |
| 63 | + certifi_dir = repo_root / "shotgun_api3" / "lib" / "certifi" |
| 64 | + req_file = repo_root / "shotgun_api3" / "lib" / "requirements.txt" |
| 65 | + |
| 66 | + utilities = Utilities() |
| 67 | + |
| 68 | + # Download the wheel from PyPI |
| 69 | + utilities.download_wheel(version, temp_path) |
| 70 | + |
| 71 | + # Find the downloaded wheel file |
| 72 | + wheels = list(temp_path.glob("certifi-*.whl")) |
| 73 | + if not wheels: |
| 74 | + raise RuntimeError("No certifi wheel found after download") |
| 75 | + |
| 76 | + # Unzip into a temp dir |
| 77 | + unzipped = temp_path / "unzipped" |
| 78 | + unzipped.mkdir() |
| 79 | + utilities.unzip_archive(wheels[0], unzipped) |
| 80 | + |
| 81 | + # Remove old certifi from git and disk |
| 82 | + utilities.git_remove([str(certifi_dir)]) |
| 83 | + utilities.remove_folder(certifi_dir) |
| 84 | + |
| 85 | + # Copy new certifi into place (only the certifi/ package, not .dist-info) |
| 86 | + print("Copying new version of certifi") |
| 87 | + utilities.copy_folder(str(unzipped / "certifi"), str(certifi_dir)) |
| 88 | + |
| 89 | + # Update requirements.txt version pin |
| 90 | + print("Updating requirements.txt") |
| 91 | + utilities.update_requirements(req_file, version) |
| 92 | + |
| 93 | + # Stage changes |
| 94 | + print("Adding to git") |
| 95 | + subprocess.check_output( |
| 96 | + ["git", "add", str(certifi_dir), str(req_file)] |
| 97 | + ) # nosec B607 |
| 98 | + |
| 99 | + |
| 100 | +def find_repo_root(): |
| 101 | + path = pathlib.Path(__file__).resolve() |
| 102 | + for parent in [path, *path.parents]: |
| 103 | + if (parent / ".git").exists(): |
| 104 | + return parent |
| 105 | + raise RuntimeError("Could not find repo root (no .git directory found)") |
| 106 | + |
| 107 | + |
| 108 | +if __name__ == "__main__": |
| 109 | + try: |
| 110 | + temp_path = pathlib.Path(tempfile.mkdtemp()) |
| 111 | + main(temp_path, find_repo_root(), sys.argv[1]) |
| 112 | + finally: |
| 113 | + shutil.rmtree(temp_path) |
0 commit comments