File size: 3,508 Bytes
1bac49a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/env python3

import os
import re
from collections import defaultdict
from pathlib import Path
from typing import Dict, List, Tuple

from update_readme import generate_url, get_all_files


class Wheel:
    def __init__(self, full_name: str, url: str):
        """
        Args:
          full_name:
            Example: k2-1.24.3.dev20230720+cpu.torch1.10.0-cp37-cp37m-win_amd64.whl
        """
        self.full_name = full_name
        #  pattern = r"k2-(\d)\.(\d+)(\.(\d+))?\.dev(\d{8})+cpu\.torch(\d\.\d+)"
        pattern = (
            r"k2-(\d)\.(\d)+((\.)(\d))?\.dev(\d{8})\+cpu\.torch(\d\.\d+\.\d)-cp(\d+)"
        )
        m = re.search(pattern, full_name)

        self.k2_major = int(m.group(1))
        self.k2_minor = int(m.group(2))
        self.k2_patch = int(m.group(5))
        self.k2_date = int(m.group(6))
        self.torch_version = m.group(7)
        self.py_version = int(m.group(8))
        self.url = url

    def __str__(self):
        return self.url

    def __repr__(self):
        return self.url


def generate_index(filename: str, torch_versions) -> str:
    b = []
    for i in torch_versions:
        b.append(f"   ./{i}.rst")
    b = "\n".join(b)

    s = f"""\
Pre-compiled CPU wheels (Windows)
=================================

This page describes pre-compiled ``CPU`` wheels for `k2`_ on Windows.

.. toctree::
   :maxdepth: 2

{b}
    """
    with open(filename, "w") as f:
        f.write(s)


def sort_by_wheel(x: Wheel):
    return x.k2_major, x.k2_minor, x.k2_patch, x.k2_date, x.py_version


def sort_by_torch(x):
    major, minor, patch = x.split(".")
    return int(major), int(minor), int(patch)


def get_all_torch_versions(wheels: List[Wheel]) -> List[str]:
    ans = set()
    for w in wheels:
        ans.add(w.torch_version)

    # sort torch version from high to low
    ans = list(ans)
    ans.sort(reverse=True, key=sort_by_torch)
    return ans


def get_doc_dir():
    k2_dir = os.getenv("K2_DIR")
    if k2_dir is None:
        raise ValueError("Please set the environment variable k2_dir")

    cpu_dir = Path(k2_dir) / "docs/source/installation/pre-compiled-cpu-wheels-windows"

    if not Path(cpu_dir).is_dir():
        raise ValueError(f"{cpu_dir} does not exist")

    print(f"k2 doc cpu_dir: {cpu_dir}")
    return cpu_dir


def remove_all_files(d: str):
    files = get_all_files(d, "*.rst")
    for f in files:
        print(f"removing {f}")
        os.remove(f)


def get_all_cpu_wheels():
    cpu = get_all_files("windows-cpu", suffix="*.whl")
    cpu_wheels = generate_url(cpu)
    return cpu_wheels


def generate_file(d: str, torch_version: str, wheels: List[Wheel]) -> str:
    s = f"torch {torch_version}\n"
    s += "=" * len(f"torch {torch_version}")
    s += "\n" * 3
    wheels = filter(lambda w: w.torch_version == torch_version, wheels)
    wheels = list(wheels)
    wheels.sort(reverse=True, key=sort_by_wheel)
    for w in wheels:
        s += f"- `{w.full_name} <{w.url}>`_\n"

    with open(f"{d}/{torch_version}.rst", "w") as f:
        f.write(s)


def main():
    d = get_doc_dir()
    remove_all_files(d)

    urls = get_all_cpu_wheels()

    wheels = []
    for url in urls:
        full_name = url.rsplit("/", maxsplit=1)[1]
        wheels.append(Wheel(full_name, url))
    torch_versions = get_all_torch_versions(wheels)

    content = []
    for t in torch_versions:
        s = generate_file(d, t, wheels)

    generate_index(f"{d}/index.rst", torch_versions)


if __name__ == "__main__":
    main()