setup.py 4.96 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
moto's avatar
moto committed
7
from pathlib import Path
8

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

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


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


moto's avatar
moto committed
23
def _get_version(sha):
24
25
    with open(ROOT_DIR / "version.txt", "r") as f:
        version = f.read().strip()
26
27
    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
    sha = "Unknown" if sha is None else sha
moto-meta's avatar
moto-meta committed
35
    version_path = ROOT_DIR / "src" / "torchaudio" / "version.py"
36
    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
moto-meta's avatar
moto-meta committed
53
        for path in (ROOT_DIR / "src").glob("**/*.so"):
54
            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):
moto-meta's avatar
moto-meta committed
67
    exclude = []
68
    exclude_prototype = False
69
    if branch_name is not None and branch_name.startswith("release/"):
70
        exclude_prototype = True
71
    if tag is not None and re.match(r"v[\d.]+(-rc\d+)?", tag):
72
73
        exclude_prototype = True
    if exclude_prototype:
74
        print("Excluding torchaudio.prototype from the package.")
75
        exclude.append("torchaudio.prototype*")
moto-meta's avatar
moto-meta committed
76
    return find_packages(where="src", exclude=exclude)
77
78


moto's avatar
moto committed
79
def _parse_url(path):
80
    with open(path, "r") as file_:
moto's avatar
moto committed
81
        for line in file_:
82
            match = re.match(r"^\s*URL\s+(https:\/\/.+)$", line)
moto's avatar
moto committed
83
84
85
86
87
88
89
            if match:
                url = match.group(1)
                yield url


def _fetch_archives(src):
    for dest, url in src:
90
        if not dest.exists():
91
            print(f" --- Fetching {os.path.basename(dest)}")
92
93
94
            torch.hub.download_url_to_file(url, dest, progress=False)


moto's avatar
moto committed
95
def _main():
96
97
98
99
100
101
    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
102
    pytorch_package_dep = _get_pytorch_version()
103
    print("-- PyTorch dependency:", pytorch_package_dep)
moto's avatar
moto committed
104
    version = _get_version(sha)
105
    print("-- Building version", version)
moto's avatar
moto committed
106
107
108

    _make_version_file(version, sha)

109
110
111
    with open("README.md") as f:
        long_description = f.read()

moto's avatar
moto committed
112
113
114
115
    setup(
        name="torchaudio",
        version=version,
        description="An audio package for PyTorch",
116
117
        long_description=long_description,
        long_description_content_type="text/markdown",
moto's avatar
moto committed
118
        url="https://github.com/pytorch/audio",
moto's avatar
moto committed
119
120
121
122
        author=(
            "Soumith Chintala, David Pollack, Sean Naren, Peter Goldsborough, "
            "Moto Hira, Caroline Chen, Jeff Hwang, Zhaoheng Ni, Xiaohui Zhang"
        ),
moto's avatar
moto committed
123
        author_email="soumith@pytorch.org",
124
125
        maintainer="Moto Hira, Caroline Chen, Jeff Hwang, Zhaoheng Ni, Xiaohui Zhang",
        maintainer_email="moto@meta.com",
moto's avatar
moto committed
126
127
128
129
130
131
132
133
134
135
136
        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.8",
            "Programming Language :: Python :: 3.9",
Wei Wang's avatar
Wei Wang committed
137
            "Programming Language :: Python :: 3.10",
138
            "Programming Language :: Python :: 3.11",
moto's avatar
moto committed
139
140
            "Programming Language :: Python :: Implementation :: CPython",
            "Topic :: Multimedia :: Sound/Audio",
141
            "Topic :: Scientific/Engineering :: Artificial Intelligence",
moto's avatar
moto committed
142
143
        ],
        packages=_get_packages(branch, tag),
moto-meta's avatar
moto-meta committed
144
        package_dir={"": "src"},
moto's avatar
moto committed
145
146
        ext_modules=setup_helpers.get_ext_modules(),
        cmdclass={
147
148
            "build_ext": setup_helpers.CMakeBuild,
            "clean": clean,
moto's avatar
moto committed
149
150
151
152
153
154
        },
        install_requires=[pytorch_package_dep],
        zip_safe=False,
    )


155
if __name__ == "__main__":
moto's avatar
moto committed
156
    _main()