setup.py 5.48 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 setup, find_packages
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
27
    version = "0.11.0a0"
    if os.getenv("BUILD_VERSION"):
        version = os.getenv("BUILD_VERSION")
moto's avatar
moto committed
28
    elif sha is not None:
29
        version += "+" + sha[:7]
moto's avatar
moto committed
30
    return version
31
32


moto's avatar
moto committed
33
def _make_version_file(version, sha):
34
35
36
    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
37
38
        f.write(f"__version__ = '{version}'\n")
        f.write(f"git_version = '{sha}'\n")
39

40

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

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

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

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

65

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


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


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


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


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


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


moto's avatar
moto committed
131
def _main():
132
133
134
135
136
137
    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
138
    pytorch_package_dep = _get_pytorch_version()
139
    print("-- PyTorch dependency:", pytorch_package_dep)
moto's avatar
moto committed
140
    version = _get_version(sha)
141
    print("-- Building version", version)
moto's avatar
moto committed
142
143

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

    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",
167
            "Topic :: Scientific/Engineering :: Artificial Intelligence",
moto's avatar
moto committed
168
169
170
171
        ],
        packages=_get_packages(branch, tag),
        ext_modules=setup_helpers.get_ext_modules(),
        cmdclass={
172
173
            "build_ext": setup_helpers.CMakeBuild,
            "clean": clean,
moto's avatar
moto committed
174
175
176
177
178
179
        },
        install_requires=[pytorch_package_dep],
        zip_safe=False,
    )


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