"vscode:/vscode.git/clone" did not exist on "c142714ba4a3485036aab2e0ef9d87aa67827d46"
setup.py 5.53 KB
Newer Older
Soumith Chintala's avatar
Soumith Chintala committed
1
#!/usr/bin/env python
2
import distutils.command.clean
3
import os
4
import re
moto's avatar
moto committed
5
import shutil
6
import subprocess
7
import sys
moto's avatar
moto committed
8
from pathlib import Path
9

10
import torch
11
from setuptools import find_packages, setup
moto's avatar
moto committed
12
from tools import setup_helpers
13

moto's avatar
moto committed
14
ROOT_DIR = Path(__file__).parent.resolve()
15
16


17
18
def _run_cmd(cmd):
    try:
19
        return subprocess.check_output(cmd, cwd=ROOT_DIR, stderr=subprocess.DEVNULL).decode("ascii").strip()
20
21
22
23
    except Exception:
        return None


moto's avatar
moto committed
24
def _get_version(sha):
25
26
    with open(ROOT_DIR / "version.txt", "r") as f:
        version = f.read().strip()
27
28
    if os.getenv("BUILD_VERSION"):
        version = os.getenv("BUILD_VERSION")
moto's avatar
moto committed
29
    elif sha is not None:
30
        version += "+" + sha[:7]
moto's avatar
moto committed
31
    return version
32
33


moto's avatar
moto committed
34
def _make_version_file(version, sha):
35
36
37
    sha = "Unknown" if sha is None else sha
    version_path = ROOT_DIR / "torchaudio" / "version.py"
    with open(version_path, "w") as f:
moto's avatar
moto committed
38
39
        f.write(f"__version__ = '{version}'\n")
        f.write(f"git_version = '{sha}'\n")
40

41

moto's avatar
moto committed
42
def _get_pytorch_version():
43
    if "PYTORCH_VERSION" in os.environ:
moto's avatar
moto committed
44
        return f"torch=={os.environ['PYTORCH_VERSION']}"
45
    return "torch"
46

moto's avatar
moto committed
47
48
49
50
51
52
53

class clean(distutils.command.clean.clean):
    def run(self):
        # Run default behavior first
        distutils.command.clean.clean.run(self)

        # Remove torchaudio extension
54
55
        for path in (ROOT_DIR / "torchaudio").glob("**/*.so"):
            print(f"removing '{path}'")
moto's avatar
moto committed
56
57
58
            path.unlink()
        # Remove build directory
        build_dirs = [
59
            ROOT_DIR / "build",
moto's avatar
moto committed
60
61
62
        ]
        for path in build_dirs:
            if path.exists():
63
                print(f"removing '{path}' (and everything under it)")
moto's avatar
moto committed
64
                shutil.rmtree(str(path), ignore_errors=True)
peterjc123's avatar
peterjc123 committed
65

66

moto's avatar
moto committed
67
def _get_packages(branch_name, tag):
68
69
70
71
72
73
74
75
    exclude = [
        "build*",
        "test*",
        "torchaudio.csrc*",
        "third_party*",
        "tools*",
    ]
    exclude_prototype = False
76
    if branch_name is not None and branch_name.startswith("release/"):
77
        exclude_prototype = True
78
    if tag is not None and re.match(r"v[\d.]+(-rc\d+)?", tag):
79
80
        exclude_prototype = True
    if exclude_prototype:
81
        print("Excluding torchaudio.prototype from the package.")
82
        exclude.append("torchaudio.prototype*")
83
84
85
    return find_packages(exclude=exclude)


86
def _init_submodule():
87
    print(" --- Initializing submodules")
88
    try:
89
90
        subprocess.check_call(["git", "submodule", "init"])
        subprocess.check_call(["git", "submodule", "update"])
91
    except Exception:
92
93
        print(" --- Submodule initalization failed")
        print("Please run:\n\tgit submodule update --init --recursive")
94
        sys.exit(1)
95
    print(" --- Initialized submodule")
96
97


moto's avatar
moto committed
98
def _parse_url(path):
99
    with open(path, "r") as file_:
moto's avatar
moto committed
100
        for line in file_:
101
            match = re.match(r"^\s*URL\s+(https:\/\/.+)$", line)
moto's avatar
moto committed
102
103
104
105
106
            if match:
                url = match.group(1)
                yield url


107
def _parse_sources():
108
    third_party_dir = ROOT_DIR / "third_party"
moto's avatar
moto committed
109
    libs = ["zlib", "bzip2", "lzma", "sox"]
110
    archive_dir = third_party_dir / "archives"
moto's avatar
moto committed
111
112
    archive_dir.mkdir(exist_ok=True)
    for lib in libs:
113
        cmake_file = third_party_dir / lib / "CMakeLists.txt"
moto's avatar
moto committed
114
115
116
        for url in _parse_url(cmake_file):
            path = archive_dir / os.path.basename(url)
            yield path, url
117
118


moto's avatar
moto committed
119
120
def _fetch_archives(src):
    for dest, url in src:
121
        if not dest.exists():
122
            print(f" --- Fetching {os.path.basename(dest)}")
123
124
125
126
            torch.hub.download_url_to_file(url, dest, progress=False)


def _fetch_third_party_libraries():
127
    _init_submodule()
128
    if os.name != "nt":
129
        _fetch_archives(_parse_sources())
130
131


moto's avatar
moto committed
132
def _main():
133
134
135
136
137
138
    sha = _run_cmd(["git", "rev-parse", "HEAD"])
    branch = _run_cmd(["git", "rev-parse", "--abbrev-ref", "HEAD"])
    tag = _run_cmd(["git", "describe", "--tags", "--exact-match", "@"])
    print("-- Git branch:", branch)
    print("-- Git SHA:", sha)
    print("-- Git tag:", tag)
moto's avatar
moto committed
139
    pytorch_package_dep = _get_pytorch_version()
140
    print("-- PyTorch dependency:", pytorch_package_dep)
moto's avatar
moto committed
141
    version = _get_version(sha)
142
    print("-- Building version", version)
moto's avatar
moto committed
143
144

    _make_version_file(version, sha)
145
    _fetch_third_party_libraries()
moto's avatar
moto committed
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167

    setup(
        name="torchaudio",
        version=version,
        description="An audio package for PyTorch",
        url="https://github.com/pytorch/audio",
        author="Soumith Chintala, David Pollack, Sean Naren, Peter Goldsborough",
        author_email="soumith@pytorch.org",
        classifiers=[
            "Environment :: Plugins",
            "Intended Audience :: Developers",
            "Intended Audience :: Science/Research",
            "License :: OSI Approved :: BSD License",
            "Operating System :: MacOS :: MacOS X",
            "Operating System :: Microsoft :: Windows",
            "Operating System :: POSIX",
            "Programming Language :: C++",
            "Programming Language :: Python :: 3.7",
            "Programming Language :: Python :: 3.8",
            "Programming Language :: Python :: 3.9",
            "Programming Language :: Python :: Implementation :: CPython",
            "Topic :: Multimedia :: Sound/Audio",
168
            "Topic :: Scientific/Engineering :: Artificial Intelligence",
moto's avatar
moto committed
169
170
171
172
        ],
        packages=_get_packages(branch, tag),
        ext_modules=setup_helpers.get_ext_modules(),
        cmdclass={
173
174
            "build_ext": setup_helpers.CMakeBuild,
            "clean": clean,
moto's avatar
moto committed
175
176
177
178
179
180
        },
        install_requires=[pytorch_package_dep],
        zip_safe=False,
    )


181
if __name__ == "__main__":
moto's avatar
moto committed
182
    _main()