"vscode:/vscode.git/clone" did not exist on "dd949ace23d6d112d9e3e1e2020deb3278cc8b3c"
publish.py 2.27 KB
Newer Older
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
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
# Copyright (c) Facebook, Inc. and its affiliates. All rights reserved.
import os
import subprocess
from pathlib import Path
from typing import List


dest = "s3://dl.fbaipublicfiles.com/pytorch3d/packaging/wheels/"

output = Path("output")


def fs3cmd(args, allow_failure: bool = False) -> List[str]:
    """
    This function returns the args for subprocess to mimic the bash command
    fs3cmd available in the fairusers_aws module on the FAIR cluster.
    """
    os.environ["FAIR_CLUSTER_NAME"] = os.environ["FAIR_ENV_CLUSTER"].lower()
    cmd_args = ["/public/apps/fairusers_aws/bin/fs3cmd"] + args
    return cmd_args


def fs3_exists(path) -> bool:
    """
    Returns True if the path exists inside dest on S3.
    In fact, will also return True if there is a file which has the given
    path as a prefix, but we are careful about this.
    """
    out = subprocess.check_output(fs3cmd(["ls", path]))
    return len(out) != 0


def get_html_wrappers() -> None:
    for directory in sorted(output.iterdir()):
        output_wrapper = directory / "download.html"
        assert not output_wrapper.exists()
        dest_wrapper = dest + directory.name + "/download.html"
        if fs3_exists(dest_wrapper):
            subprocess.check_call(fs3cmd(["get", dest_wrapper, str(output_wrapper)]))


def write_html_wrappers() -> None:
    html = """
    <a href="$">$</a><br>
    """

    for directory in sorted(output.iterdir()):
        files = list(directory.glob("*.whl"))
        assert len(files) == 1, files
        [wheel] = files

        this_html = html.replace("$", wheel.name)
        output_wrapper = directory / "download.html"
        if output_wrapper.exists():
            contents = output_wrapper.read_text()
            if this_html not in contents:
                with open(output_wrapper, "a") as f:
                    f.write(this_html)
        else:
            output_wrapper.write_text(this_html)


def to_aws() -> None:
    for directory in output.iterdir():
        for file in directory.iterdir():
            print(file)
            subprocess.check_call(
                fs3cmd(["put", str(file), dest + str(file.relative_to(output))])
            )


if __name__ == "__main__":
    # Uncomment this for subsequent releases.
    # get_html_wrappers()
    write_html_wrappers()
    to_aws()