mitmproxy/release/build.py

384 lines
11 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
from __future__ import annotations
import hashlib
import os
import platform
2022-07-28 14:16:24 +00:00
import re
import shutil
import subprocess
import tarfile
import urllib.request
2023-11-03 11:21:55 +00:00
import warnings
import zipfile
from datetime import datetime
from pathlib import Path
import click
import cryptography.fernet
here = Path(__file__).absolute().parent
TEMP_DIR = here / "build"
DIST_DIR = here / "dist"
@click.group(chain=True)
@click.option("--dirty", is_flag=True)
def cli(dirty):
if dirty:
print("Keeping temporary files.")
else:
print("Cleaning up temporary files...")
if TEMP_DIR.exists():
shutil.rmtree(TEMP_DIR)
if DIST_DIR.exists():
shutil.rmtree(DIST_DIR)
TEMP_DIR.mkdir()
DIST_DIR.mkdir()
@cli.command()
def wheel():
"""Build the wheel for PyPI."""
print("Building wheel...")
subprocess.check_call(
[
"python",
"-m",
"build",
"--outdir",
DIST_DIR,
]
)
if os.environ.get("GITHUB_REF", "").startswith("refs/tags/"):
ver = version() # assert for tags that the version matches the tag.
else:
ver = "*"
(whl,) = DIST_DIR.glob(f"mitmproxy-{ver}-py3-none-any.whl")
print(f"Found wheel package: {whl}")
subprocess.check_call(["tox", "-e", "wheeltest", "--", whl])
class ZipFile2(zipfile.ZipFile):
# ZipFile and tarfile have slightly different APIs. Let's fix that.
def add(self, name: str, arcname: str) -> None:
return self.write(name, arcname)
def __enter__(self) -> ZipFile2:
return self
@property
def name(self) -> str:
assert self.filename
return self.filename
def archive(path: Path) -> tarfile.TarFile | ZipFile2:
if platform.system() == "Windows":
return ZipFile2(path.with_name(f"{path.name}.zip"), "w")
else:
return tarfile.open(path.with_name(f"{path.name}.tar.gz"), "w:gz")
def version() -> str:
2024-04-19 19:28:05 +00:00
if ref := os.environ.get("GITHUB_REF", ""):
if ref.startswith("refs/tags/") and not ref.startswith("refs/tags/v"):
raise AssertionError(f"Unexpected tag: {ref}")
return (
ref.removeprefix("refs/heads/")
.removeprefix("refs/pull/")
.removeprefix("refs/tags/v")
.replace("/", "-")
)
else:
return os.environ.get("BUILD_VERSION", "dev")
2023-11-03 11:21:55 +00:00
def operating_system() -> str:
match platform.system():
case "Windows":
system = "windows"
case "Linux":
system = "linux"
case "Darwin":
system = "macos"
case other:
warnings.warn("Unexpected system.")
system = other
match platform.machine():
case "AMD64" | "x86_64":
machine = "x86_64"
case "arm64":
machine = "arm64"
case other:
warnings.warn("Unexpected platform.")
machine = other
return f"{system}-{machine}"
def _pyinstaller(specfile: str) -> None:
print(f"Invoking PyInstaller with {specfile}...")
subprocess.check_call(
[
"pyinstaller",
"--clean",
"--workpath",
TEMP_DIR / "pyinstaller/temp",
"--distpath",
2023-11-03 11:21:55 +00:00
TEMP_DIR / "pyinstaller/out",
specfile,
],
cwd=here / "specs",
)
@cli.command()
def standalone_binaries():
2023-11-03 11:21:55 +00:00
"""Windows and Linux: Build the standalone binaries generated with PyInstaller"""
with archive(DIST_DIR / f"mitmproxy-{version()}-{operating_system()}") as f:
_pyinstaller("standalone.spec")
2023-11-03 11:21:55 +00:00
_test_binaries(TEMP_DIR / "pyinstaller/out")
2022-11-04 10:41:46 +00:00
for tool in ["mitmproxy", "mitmdump", "mitmweb"]:
2023-11-03 11:21:55 +00:00
executable = TEMP_DIR / "pyinstaller/out" / tool
if platform.system() == "Windows":
executable = executable.with_suffix(".exe")
f.add(str(executable), str(executable.name))
print(f"Packed {f.name!r}.")
2023-11-03 11:21:55 +00:00
@cli.command()
@click.option("--keychain")
@click.option("--team-id")
@click.option("--apple-id")
@click.option("--password")
def macos_app(
keychain: str | None,
team_id: str | None,
apple_id: str | None,
password: str | None,
) -> None:
"""
macOS: Build into mitmproxy.app.
If you do not specify options, notarization is skipped.
"""
_pyinstaller("onedir.spec")
_test_binaries(TEMP_DIR / "pyinstaller/out/mitmproxy.app/Contents/MacOS")
if keychain:
assert isinstance(team_id, str)
assert isinstance(apple_id, str)
assert isinstance(password, str)
# Notarize the app bundle.
subprocess.check_call(
[
"xcrun",
"notarytool",
"store-credentials",
"AC_PASSWORD",
*(["--keychain", keychain]),
*(["--team-id", team_id]),
*(["--apple-id", apple_id]),
*(["--password", password]),
]
)
subprocess.check_call(
[
"ditto",
"-c",
"-k",
"--keepParent",
TEMP_DIR / "pyinstaller/out/mitmproxy.app",
TEMP_DIR / "notarize.zip",
]
)
subprocess.check_call(
[
"xcrun",
"notarytool",
"submit",
TEMP_DIR / "notarize.zip",
*(["--keychain", keychain]),
*(["--keychain-profile", "AC_PASSWORD"]),
"--wait",
]
)
# 2023: it's not possible to staple to unix executables.
# subprocess.check_call([
# "xcrun",
# "stapler",
# "staple",
# TEMP_DIR / "pyinstaller/out/mitmproxy.app",
# ])
else:
warnings.warn("Notarization skipped.")
with archive(DIST_DIR / f"mitmproxy-{version()}-{operating_system()}") as f:
f.add(str(TEMP_DIR / "pyinstaller/out/mitmproxy.app"), "mitmproxy.app")
print(f"Packed {f.name!r}.")
2023-11-03 11:21:55 +00:00
def _ensure_pyinstaller_onedir():
if not (TEMP_DIR / "pyinstaller/out/onedir").exists():
_pyinstaller("onedir.spec")
_test_binaries(TEMP_DIR / "pyinstaller/out/onedir")
2022-11-04 10:41:46 +00:00
def _test_binaries(binary_directory: Path) -> None:
for tool in ["mitmproxy", "mitmdump", "mitmweb"]:
2022-11-04 10:41:46 +00:00
executable = binary_directory / tool
if platform.system() == "Windows":
executable = executable.with_suffix(".exe")
print(f"> {tool} --version")
subprocess.check_call([executable, "--version"])
2022-11-10 12:23:10 +00:00
if tool == "mitmproxy":
continue # requires a TTY, which we don't have here.
2022-11-04 10:41:46 +00:00
print(f"> {tool} -s selftest.py")
subprocess.check_call([executable, "-s", here / "selftest.py"])
@cli.command()
def msix_installer():
"""Windows: Build the MSIX installer for the Windows Store."""
_ensure_pyinstaller_onedir()
shutil.copytree(
2023-11-03 11:21:55 +00:00
TEMP_DIR / "pyinstaller/out/onedir",
TEMP_DIR / "msix",
dirs_exist_ok=True,
)
shutil.copytree(here / "windows-installer", TEMP_DIR / "msix", dirs_exist_ok=True)
manifest = TEMP_DIR / "msix/AppxManifest.xml"
app_version = version()
2022-07-28 14:16:24 +00:00
if not re.match(r"\d+\.\d+\.\d+", app_version):
2022-11-29 13:28:41 +00:00
app_version = (
datetime.now()
.strftime("%y%m.%d.%H%M")
.replace(".0", ".")
.replace(".0", ".")
.replace(".0", ".")
)
manifest.write_text(manifest.read_text().replace("1.2.3", app_version))
makeappx_exe = (
Path(os.environ["ProgramFiles(x86)"])
/ "Windows Kits/10/App Certification Kit/makeappx.exe"
)
subprocess.check_call(
[
makeappx_exe,
"pack",
"/d",
TEMP_DIR / "msix",
"/p",
DIST_DIR / f"mitmproxy-{version()}-installer.msix",
],
)
assert (DIST_DIR / f"mitmproxy-{version()}-installer.msix").exists()
@cli.command()
def installbuilder_installer():
"""Windows: Build the InstallBuilder installer."""
_ensure_pyinstaller_onedir()
IB_VERSION = "23.4.0"
IB_SETUP_SHA256 = "e4ff212ed962f9e0030d918b8a6e4d6dd8a9adc8bf8bc1833459351ee649eff3"
IB_DIR = here / "installbuilder"
IB_SETUP = IB_DIR / "setup" / f"{IB_VERSION}-installer.exe"
IB_CLI = Path(
rf"C:\Program Files\InstallBuilder Enterprise {IB_VERSION}\bin\builder-cli.exe"
)
IB_LICENSE = IB_DIR / "license.xml"
if not IB_LICENSE.exists():
print("Decrypt InstallBuilder license...")
f = cryptography.fernet.Fernet(os.environ["CI_BUILD_KEY"].encode())
Bump the github-actions group with 3 updates (#6701) Bumps the github-actions group with 3 updates: [install-pinned/ruff](https://github.com/install-pinned/ruff), [apple-actions/import-codesign-certs](https://github.com/apple-actions/import-codesign-certs) and [docker/setup-buildx-action](https://github.com/docker/setup-buildx-action). Updates `install-pinned/ruff` from fe472defb50a6a2c00ea3a3982534e86e69991e8 to 38b373a3a8635c2be31d92314e816a491fda910a <details> <summary>Commits</summary> <ul> <li><a href="https://github.com/install-pinned/ruff/commit/38b373a3a8635c2be31d92314e816a491fda910a"><code>38b373a</code></a> update README.md (ruff 0.3.0)</li> <li><a href="https://github.com/install-pinned/ruff/commit/06af3ea1c373cd2677585074ab30c71d85424629"><code>06af3ea</code></a> update pins (ruff 0.3.0)</li> <li><a href="https://github.com/install-pinned/ruff/commit/be1c354876e78dc826f323b569172d562e94c34a"><code>be1c354</code></a> update README.md (ruff 0.2.2)</li> <li><a href="https://github.com/install-pinned/ruff/commit/c9779bbd5bbeee0bd30ec6af46435bb50983da6e"><code>c9779bb</code></a> update pins (ruff 0.2.2)</li> <li><a href="https://github.com/install-pinned/ruff/commit/48831a86ce1ccf6386ae7e04eb47efc8780b4b5e"><code>48831a8</code></a> update README.md (ruff 0.2.1)</li> <li><a href="https://github.com/install-pinned/ruff/commit/6775b5f352f196b7b074ede5706349427b9598bf"><code>6775b5f</code></a> update pins (ruff 0.2.1)</li> <li><a href="https://github.com/install-pinned/ruff/commit/bc12a64c2f0fd73d270f1b96fecb9f7706478bff"><code>bc12a64</code></a> update README.md (ruff 0.2.0)</li> <li><a href="https://github.com/install-pinned/ruff/commit/3b8cceff45bcd350eb966a5122a8a258775944d3"><code>3b8ccef</code></a> update pins (ruff 0.2.0)</li> <li>See full diff in <a href="https://github.com/install-pinned/ruff/compare/fe472defb50a6a2c00ea3a3982534e86e69991e8...38b373a3a8635c2be31d92314e816a491fda910a">compare view</a></li> </ul> </details> <br /> Updates `apple-actions/import-codesign-certs` from 5565bb656f60c98c8fc515f3444dd8db73545dc2 to 493007ed063995cf2d4fbca064704150548f8bb5 <details> <summary>Commits</summary> <ul> <li><a href="https://github.com/Apple-Actions/import-codesign-certs/commit/493007ed063995cf2d4fbca064704150548f8bb5"><code>493007e</code></a> Merge pull request <a href="https://redirect.github.com/apple-actions/import-codesign-certs/issues/62">#62</a> from himself65/patch-1</li> <li><a href="https://github.com/Apple-Actions/import-codesign-certs/commit/2e5aa07267146829e3b8877c13292e75e21b7c8d"><code>2e5aa07</code></a> Update README.md</li> <li>See full diff in <a href="https://github.com/apple-actions/import-codesign-certs/compare/5565bb656f60c98c8fc515f3444dd8db73545dc2...493007ed063995cf2d4fbca064704150548f8bb5">compare view</a></li> </ul> </details> <br /> Updates `docker/setup-buildx-action` from 3.0.0 to 3.1.0 <details> <summary>Release notes</summary> <p><em>Sourced from <a href="https://github.com/docker/setup-buildx-action/releases">docker/setup-buildx-action's releases</a>.</em></p> <blockquote> <h2>v3.1.0</h2> <ul> <li><code>cache-binary</code> input to enable/disable caching binary to GHA cache backend by <a href="https://github.com/crazy-max"><code>@​crazy-max</code></a> in <a href="https://redirect.github.com/docker/setup-buildx-action/pull/300">docker/setup-buildx-action#300</a></li> <li>build(deps): bump <code>@​babel/traverse</code> from 7.17.3 to 7.23.2 in <a href="https://redirect.github.com/docker/setup-buildx-action/pull/282">docker/setup-buildx-action#282</a></li> <li>build(deps): bump <code>@​docker/actions-toolkit</code> from 0.12.0 to 0.17.0 in <a href="https://redirect.github.com/docker/setup-buildx-action/pull/281">docker/setup-buildx-action#281</a> <a href="https://redirect.github.com/docker/setup-buildx-action/pull/284">docker/setup-buildx-action#284</a> <a href="https://redirect.github.com/docker/setup-buildx-action/pull/299">docker/setup-buildx-action#299</a></li> <li>build(deps): bump uuid from 9.0.0 to 9.0.1 in <a href="https://redirect.github.com/docker/setup-buildx-action/pull/271">docker/setup-buildx-action#271</a></li> <li>build(deps): bump undici from 5.26.3 to 5.28.3 in <a href="https://redirect.github.com/docker/setup-buildx-action/pull/297">docker/setup-buildx-action#297</a></li> </ul> <p><strong>Full Changelog</strong>: <a href="https://github.com/docker/setup-buildx-action/compare/v3.0.0...v3.1.0">https://github.com/docker/setup-buildx-action/compare/v3.0.0...v3.1.0</a></p> </blockquote> </details> <details> <summary>Commits</summary> <ul> <li><a href="https://github.com/docker/setup-buildx-action/commit/0d103c3126aa41d772a8362f6aa67afac040f80c"><code>0d103c3</code></a> Merge pull request <a href="https://redirect.github.com/docker/setup-buildx-action/issues/300">#300</a> from crazy-max/cache-binary</li> <li><a href="https://github.com/docker/setup-buildx-action/commit/f19477aacd2ccdd80bdd9fbec62762494a0a767e"><code>f19477a</code></a> chore: update generated content</li> <li><a href="https://github.com/docker/setup-buildx-action/commit/a4180f835da34b09329293a0b481b5666f80fe81"><code>a4180f8</code></a> cache-binary input to enable/disable caching binary to GHA cache backend</li> <li><a href="https://github.com/docker/setup-buildx-action/commit/524315340de4f701033248e1ee6a0197904b1134"><code>5243153</code></a> Merge pull request <a href="https://redirect.github.com/docker/setup-buildx-action/issues/299">#299</a> from docker/dependabot/npm_and_yarn/docker/actions-to...</li> <li><a href="https://github.com/docker/setup-buildx-action/commit/3679a540239bdf202e26eeb4431159bcdea9c39c"><code>3679a54</code></a> chore: update generated content</li> <li><a href="https://github.com/docker/setup-buildx-action/commit/37a22a2fb2c4ebcab21ecf45608143b890d732c1"><code>37a22a2</code></a> build(deps): bump <code>@​docker/actions-toolkit</code> from 0.14.0 to 0.17.0</li> <li><a href="https://github.com/docker/setup-buildx-action/commit/65afe610a110d2c4d102a28949259935a323043c"><code>65afe61</code></a> Merge pull request <a href="https://redirect.github.com/docker/setup-buildx-action/issues/297">#297</a> from docker/dependabot/npm_and_yarn/undici-5.28.3</li> <li><a href="https://github.com/docker/setup-buildx-action/commit/fcb8f722fd7e9a83a2f57fc6c9124259661542f3"><code>fcb8f72</code></a> chore: update generated content</li> <li><a href="https://github.com/docker/setup-buildx-action/commit/f62b9a17c0f199a60048d54f30e75f61af13aa85"><code>f62b9a1</code></a> Merge pull request <a href="https://redirect.github.com/docker/setup-buildx-action/issues/298">#298</a> from crazy-max/bump-gha</li> <li><a href="https://github.com/docker/setup-buildx-action/commit/74c5b717e5e6f00c85a46548cbc0bfd7016969c6"><code>74c5b71</code></a> bump codecov/codecov-action from 3 to 4</li> <li>Additional commits viewable in <a href="https://github.com/docker/setup-buildx-action/compare/f95db51fddba0c2d1ec667646a06c2ce06100226...0d103c3126aa41d772a8362f6aa67afac040f80c">compare view</a></li> </ul> </details> <br /> Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting `@dependabot rebase`. [//]: # (dependabot-automerge-start) [//]: # (dependabot-automerge-end) --- <details> <summary>Dependabot commands and options</summary> <br /> You can trigger Dependabot actions by commenting on this PR: - `@dependabot rebase` will rebase this PR - `@dependabot recreate` will recreate this PR, overwriting any edits that have been made to it - `@dependabot merge` will merge this PR after your CI passes on it - `@dependabot squash and merge` will squash and merge this PR after your CI passes on it - `@dependabot cancel merge` will cancel a previously requested merge and block automerging - `@dependabot reopen` will reopen this PR if it is closed - `@dependabot close` will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually - `@dependabot show <dependency name> ignore conditions` will show all of the ignore conditions of the specified dependency - `@dependabot ignore <dependency name> major version` will close this group update PR and stop Dependabot creating any more for the specific dependency's major version (unless you unignore this specific dependency's major version or upgrade to it yourself) - `@dependabot ignore <dependency name> minor version` will close this group update PR and stop Dependabot creating any more for the specific dependency's minor version (unless you unignore this specific dependency's minor version or upgrade to it yourself) - `@dependabot ignore <dependency name>` will close this group update PR and stop Dependabot creating any more for the specific dependency (unless you unignore this specific dependency or upgrade to it yourself) - `@dependabot unignore <dependency name>` will remove all of the ignore conditions of the specified dependency - `@dependabot unignore <dependency name> <ignore condition>` will remove the ignore condition of the specified dependency and ignore conditions </details> --------- Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
2024-03-06 20:54:11 +00:00
with (
open(IB_LICENSE.with_suffix(".xml.enc"), "rb") as infile,
open(IB_LICENSE, "wb") as outfile,
):
outfile.write(f.decrypt(infile.read()))
if not IB_CLI.exists():
if not IB_SETUP.exists():
url = (
f"https://github.com/mitmproxy/installbuilder-mirror/releases/download/"
f"{IB_VERSION}/installbuilder-enterprise-{IB_VERSION}-windows-x64-installer.exe"
)
print(f"Downloading InstallBuilder from {url}...")
def report(block, blocksize, total):
done = block * blocksize
if round(100 * done / total) != round(100 * (done - blocksize) / total):
print(f"Downloading... {round(100 * done / total)}%")
tmp = IB_SETUP.with_suffix(".tmp")
urllib.request.urlretrieve(
url,
tmp,
reporthook=report,
)
tmp.rename(IB_SETUP)
ib_setup_hash = hashlib.sha256()
with IB_SETUP.open("rb") as fp:
while True:
data = fp.read(65_536)
if not data:
break
ib_setup_hash.update(data)
if ib_setup_hash.hexdigest() != IB_SETUP_SHA256: # pragma: no cover
2022-11-29 13:28:41 +00:00
raise RuntimeError(
f"InstallBuilder hashes don't match: {ib_setup_hash.hexdigest()}"
)
print("Install InstallBuilder...")
subprocess.run(
[IB_SETUP, "--mode", "unattended", "--unattendedmodeui", "none"], check=True
)
assert IB_CLI.is_file()
print("Run InstallBuilder...")
subprocess.check_call(
[
IB_CLI,
"build",
str(IB_DIR / "mitmproxy.xml"),
"windows-x64",
"--license",
str(IB_LICENSE),
"--setvars",
f"project.version={version()}",
"--verbose",
2022-10-29 14:12:16 +00:00
],
cwd=IB_DIR,
)
2022-10-29 14:12:16 +00:00
installer = DIST_DIR / f"mitmproxy-{version()}-windows-x64-installer.exe"
assert installer.exists()
# unify filenames
installer = installer.rename(
installer.with_name(installer.name.replace("x64", "x86_64"))
)
print("Run installer...")
subprocess.run(
[installer, "--mode", "unattended", "--unattendedmodeui", "none"], check=True
)
2022-11-04 10:41:46 +00:00
_test_binaries(Path(r"C:\Program Files\mitmproxy\bin"))
if __name__ == "__main__":
cli()