Pier Angelo Vendrame pushed to branch main at The Tor Project / Applications / tor-browser-build

Commits:

1 changed file:

Changes:

  • tools/fetch-manual.py
    1
    +#!/usr/bin/env python3
    
    2
    +import hashlib
    
    3
    +from pathlib import Path
    
    4
    +import sys
    
    5
    +
    
    6
    +import requests
    
    7
    +import yaml
    
    8
    +
    
    9
    +
    
    10
    +GITLAB = "https://gitlab.torproject.org"
    
    11
    +API_URL = f"{GITLAB}/api/v4"
    
    12
    +PROJECT_ID = 23
    
    13
    +REF_NAME = "main"
    
    14
    +
    
    15
    +
    
    16
    +token_file = Path(__file__).parent / ".changelogs_token"
    
    17
    +if not token_file.exists():
    
    18
    +    print("This scripts uses the same access token as fetch-changelog.py.")
    
    19
    +    print("However, the file has not been found.")
    
    20
    +    print(
    
    21
    +        "Please run fetch-changelog.py to get the instructions on how to "
    
    22
    +        "generate it."
    
    23
    +    )
    
    24
    +    sys.exit(1)
    
    25
    +with token_file.open() as f:
    
    26
    +    headers = {"PRIVATE-TOKEN": f.read().strip()}
    
    27
    +
    
    28
    +r = requests.get(f"{API_URL}/projects/{PROJECT_ID}/jobs", headers=headers)
    
    29
    +if r.status_code == 401:
    
    30
    +    print("Unauthorized! Maybe the token has expired.")
    
    31
    +    sys.exit(2)
    
    32
    +found = False
    
    33
    +for job in r.json():
    
    34
    +    if job["ref"] != REF_NAME:
    
    35
    +        continue
    
    36
    +    for art in job["artifacts"]:
    
    37
    +        if art["filename"] == "artifacts.zip":
    
    38
    +            found = True
    
    39
    +            break
    
    40
    +    if found:
    
    41
    +        break
    
    42
    +if not found:
    
    43
    +    print("Cannot find a usable job.")
    
    44
    +    sys.exit(3)
    
    45
    +
    
    46
    +pipeline_id = job["pipeline"]["id"]
    
    47
    +conf_file = Path(__file__).parent.parent / "projects/manual/config"
    
    48
    +with conf_file.open() as f:
    
    49
    +    config = yaml.load(f, yaml.SafeLoader)
    
    50
    +if int(config["version"]) == int(pipeline_id):
    
    51
    +    print(
    
    52
    +        "projects/manual/config is already using the latest pipeline. Nothing to do."
    
    53
    +    )
    
    54
    +    sys.exit(0)
    
    55
    +
    
    56
    +manual_dir = Path(__file__).parent.parent / "out/manual"
    
    57
    +manual_dir.mkdir(0o755, parents=True, exist_ok=True)
    
    58
    +manual_file = manual_dir / f"manual_{pipeline_id}.zip"
    
    59
    +sha256 = hashlib.sha256()
    
    60
    +if manual_file.exists():
    
    61
    +    with manual_file.open("rb") as f:
    
    62
    +        while chunk := f.read(8192):
    
    63
    +            sha256.update(chunk)
    
    64
    +    print("You already have the latest manual version in your out directory.")
    
    65
    +    print("Please update projects/manual/config to:")
    
    66
    +else:
    
    67
    +    print("Downloading the new version of the manual...")
    
    68
    +    url = f"{API_URL}/projects/{PROJECT_ID}/jobs/artifacts/{REF_NAME}/download?job={job['name']}"
    
    69
    +    r = requests.get(url, headers=headers, stream=True)
    
    70
    +    # https://stackoverflow.com/a/16696317
    
    71
    +    r.raise_for_status()
    
    72
    +    with manual_file.open("wb") as f:
    
    73
    +        for chunk in r.iter_content(chunk_size=8192):
    
    74
    +            f.write(chunk)
    
    75
    +            sha256.update(chunk)
    
    76
    +    print(f"File downloaded as {manual_file}.")
    
    77
    +    print(
    
    78
    +        "Please upload it to people.torproject.org and then update projects/manual/config:"
    
    79
    +    )
    
    80
    +sha256 = sha256.hexdigest()
    
    81
    +
    
    82
    +print(f"\tversion: {pipeline_id}")
    
    83
    +print(f"\tSHA256: {sha256}")