File: write_release.py

package info (click to toggle)
numpy 1%3A2.3.3%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 85,944 kB
  • sloc: python: 255,476; asm: 232,483; ansic: 212,559; cpp: 157,437; f90: 1,575; sh: 845; fortran: 567; makefile: 427; sed: 139; xml: 109; java: 97; perl: 82; cs: 62; javascript: 53; objc: 33; lex: 13; yacc: 9
file content (118 lines) | stat: -rw-r--r-- 2,880 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
"""
Standalone script for writing release doc::

    python tools/write_release <version>

Example::

    python tools/write_release.py 1.7.0

Needs to be run from the root of the repository and assumes
that the output is in `release` and wheels and sdist in
`release/installers`.

Translation from rst to md markdown requires Pandoc, you
will need to rely on your distribution to provide that.

"""
import argparse
import os
import subprocess
import textwrap
from hashlib import md5, sha256
from pathlib import Path

# Name of the notes directory
NOTES_DIR = "doc/source/release"
# Name of the output directory
OUTPUT_DIR = "release"
# Output base name, `.rst` or `.md` will be appended
OUTPUT_FILE = "README"

def compute_hash(wheel_dir, hash_func):
    """
    Compute hashes of files in wheel_dir.

    Parameters
    ----------
    wheel_dir: str
        Path to wheel directory from repo root.
    hash_func: function
        Hash function, i.e., md5, sha256, etc.

    Returns
    -------
    list_of_strings: list
        List of of strings. Each string is the hash
        followed by the file basename.

    """
    released = os.listdir(wheel_dir)
    checksums = []
    for fn in sorted(released):
        fn_path = Path(f"{wheel_dir}/{fn}")
        m = hash_func(fn_path.read_bytes())
        checksums.append(f"{m.hexdigest()}  {fn}")
    return checksums


def write_release(version):
    """
    Copy the <version>-notes.rst file to the OUTPUT_DIR, append
    the md5 and sha256 hashes of the wheels and sdist, and produce
    README.rst and README.md files.

    Parameters
    ----------
    version: str
       Release version, e.g., '2.3.2', etc.

    Returns
    -------
    None.

    """
    notes = Path(NOTES_DIR) / f"{version}-notes.rst"
    wheel_dir = Path(OUTPUT_DIR) / "installers"
    target_md = Path(OUTPUT_DIR) / f"{OUTPUT_FILE}.md"
    target_rst = Path(OUTPUT_DIR) / f"{OUTPUT_FILE}.rst"

    os.system(f"cp {notes} {target_rst}")

    with open(str(target_rst), 'a') as f:
        f.writelines(textwrap.dedent(
            """
            Checksums
            =========

            MD5
            ---
            ::

            """))
        f.writelines([f'    {c}\n' for c in compute_hash(wheel_dir, md5)])

        f.writelines(textwrap.dedent(
            """
            SHA256
            ------
            ::

            """))
        f.writelines([f'    {c}\n' for c in compute_hash(wheel_dir, sha256)])

    # translate README.rst to md for posting on GitHub
    subprocess.run(
        ["pandoc", "-s", "-o", str(target_md), str(target_rst), "--wrap=preserve"],
        check=True,
    )


if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "version",
        help="NumPy version of the release, e.g. 2.3.2, etc.")

    args = parser.parse_args()
    write_release(args.version)