|
| 1 | +import io |
| 2 | +import json |
| 3 | +import os |
| 4 | +import platform |
| 5 | +import shutil |
| 6 | +import subprocess as sp |
| 7 | +import sys |
| 8 | +import tarfile |
| 9 | +import tempfile |
| 10 | +from pathlib import Path |
| 11 | +from urllib.request import urlopen |
| 12 | + |
| 13 | +# Download the latest release of zola |
| 14 | +url = "https://api.github.com/repos/getzola/zola/releases/latest" |
| 15 | +data = json.load(urlopen(url)) |
| 16 | +tag_name = data["tag_name"] |
| 17 | +print(f"Latest zola version is {tag_name}") |
| 18 | + |
| 19 | +# Download the zola binary |
| 20 | +url = ( |
| 21 | + f"https://github.com/getzola/zola/releases/download/{tag_name}/zola-{tag_name}-{platform.processor()}-" |
| 22 | + + ( |
| 23 | + "pc-windows-msvc" if sys.platform == "win32" |
| 24 | + else "apple-darwin" if sys.platform == "darwin" |
| 25 | + else "unknown-linux-gnu" |
| 26 | + ) |
| 27 | + + ".tar.gz" |
| 28 | +) |
| 29 | + |
| 30 | +output_file_name = "zola.exe" if sys.platform == "win32" else "zola" |
| 31 | + |
| 32 | +with tempfile.TemporaryDirectory() as dir: |
| 33 | + output_file = Path(dir) / output_file_name |
| 34 | + print(f"Downloading {url} to {output_file}...") |
| 35 | + |
| 36 | + # Decompress the tarball and extract the zola binary |
| 37 | + with open(output_file, "wb") as f: |
| 38 | + with urlopen(url) as response: |
| 39 | + buffer = io.BytesIO() |
| 40 | + shutil.copyfileobj(response, buffer) |
| 41 | + buffer.seek(0) |
| 42 | + with tarfile.open(fileobj=buffer, mode="r:gz") as tar: |
| 43 | + shutil.copyfileobj(tar.extractfile(output_file_name), f) |
| 44 | + |
| 45 | + # Make the zola binary executable |
| 46 | + os.chmod(output_file, 0o755) |
| 47 | + print(f"Changed file attributes of {output_file_name}") |
| 48 | + |
| 49 | + # Build the website |
| 50 | + print("Running build command...") |
| 51 | + sp.run([output_file, "build"], check=True) |
| 52 | + |
| 53 | +# Add the Keybase proof |
| 54 | +KEYBASE_PROOF = os.getenv("KEYBASE_PROOF", "") |
| 55 | +if KEYBASE_PROOF: |
| 56 | + with open("public/keybase.txt", "w") as f: |
| 57 | + f.write(KEYBASE_PROOF) |
| 58 | + print("Keybase proof added") |
| 59 | +else: |
| 60 | + print("Keybase proof not found") |
| 61 | + |
| 62 | +# Add the Google proof |
| 63 | +GOOGLE_PROOF = os.getenv("GOOGLE_PROOF", "") |
| 64 | +if GOOGLE_PROOF: |
| 65 | + with open(f"public/google{GOOGLE_PROOF}.html", "w") as f: |
| 66 | + f.write(f"google-site-verification: google{GOOGLE_PROOF}.html") |
| 67 | + print("Google proof added") |
| 68 | +else: |
| 69 | + print("Google proof not found") |
0 commit comments