publish.py 2.47 KB
Newer Older
Patrick Labatut's avatar
Patrick Labatut committed
1
2
3
4
5
6
# Copyright (c) Facebook, Inc. and its affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.

Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
7
8
9
10
11
12
13
14
15
16
import subprocess
from pathlib import Path
from typing import List


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

output = Path("output")


Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
17
def aws_s3_cmd(args) -> List[str]:
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
18
    """
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
19
20
    This function returns the full args for subprocess to do a command
    with aws.
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
21
    """
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
22
    cmd_args = ["aws", "s3", "--profile", "saml"] + args
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
23
24
25
26
27
28
29
30
31
    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.
    """
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
32
    out = subprocess.check_output(aws_s3_cmd(["ls", path]))
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
33
34
35
36
37
38
39
40
41
    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):
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
42
            subprocess.check_call(aws_s3_cmd(["cp", dest_wrapper, str(output_wrapper)]))
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
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


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(
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
71
                aws_s3_cmd(["cp", str(file), dest + str(file.relative_to(output))])
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
72
73
74
75
76
77
78
79
            )


if __name__ == "__main__":
    # Uncomment this for subsequent releases.
    # get_html_wrappers()
    write_html_wrappers()
    to_aws()
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
80
81
82
83
84
85
86
87


# see all files with
#  aws s3 --profile saml ls --recursive s3://dl.fbaipublicfiles.com/pytorch3d/

# empty current with
#  aws s3 --profile saml rm --recursive
#                 s3://dl.fbaipublicfiles.com/pytorch3d/packaging/wheels/