setup.py 4.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
moto's avatar
moto committed
7
from pathlib import Path
limm's avatar
limm committed
8
from get_version import get_version
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 _parse_url(path):
67
    with open(path, "r") as file_:
moto's avatar
moto committed
68
        for line in file_:
69
            match = re.match(r"^\s*URL\s+(https:\/\/.+)$", line)
moto's avatar
moto committed
70
71
72
73
74
75
76
            if match:
                url = match.group(1)
                yield url


def _fetch_archives(src):
    for dest, url in src:
77
        if not dest.exists():
78
            print(f" --- Fetching {os.path.basename(dest)}")
79
80
81
            torch.hub.download_url_to_file(url, dest, progress=False)


moto's avatar
moto committed
82
def _main():
83
84
85
86
87
88
    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
89
    pytorch_package_dep = _get_pytorch_version()
90
    print("-- PyTorch dependency:", pytorch_package_dep)
moto's avatar
moto committed
91
    version = _get_version(sha)
92
    print("-- Building version", version)
moto's avatar
moto committed
93
94
95

    _make_version_file(version, sha)

96
97
98
    with open("README.md") as f:
        long_description = f.read()

moto's avatar
moto committed
99
100
    setup(
        name="torchaudio",
limm's avatar
limm committed
101
        version=get_version(),
moto's avatar
moto committed
102
        description="An audio package for PyTorch",
103
104
        long_description=long_description,
        long_description_content_type="text/markdown",
moto's avatar
moto committed
105
        url="https://github.com/pytorch/audio",
moto's avatar
moto committed
106
107
108
109
        author=(
            "Soumith Chintala, David Pollack, Sean Naren, Peter Goldsborough, "
            "Moto Hira, Caroline Chen, Jeff Hwang, Zhaoheng Ni, Xiaohui Zhang"
        ),
moto's avatar
moto committed
110
        author_email="soumith@pytorch.org",
111
112
        maintainer="Moto Hira, Caroline Chen, Jeff Hwang, Zhaoheng Ni, Xiaohui Zhang",
        maintainer_email="moto@meta.com",
moto's avatar
moto committed
113
114
115
116
117
118
119
120
121
122
123
        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
124
            "Programming Language :: Python :: 3.10",
125
            "Programming Language :: Python :: 3.11",
moto's avatar
moto committed
126
127
            "Programming Language :: Python :: Implementation :: CPython",
            "Topic :: Multimedia :: Sound/Audio",
128
            "Topic :: Scientific/Engineering :: Artificial Intelligence",
moto's avatar
moto committed
129
        ],
130
        packages=find_packages(where="src"),
moto-meta's avatar
moto-meta committed
131
        package_dir={"": "src"},
moto's avatar
moto committed
132
133
        ext_modules=setup_helpers.get_ext_modules(),
        cmdclass={
134
135
            "build_ext": setup_helpers.CMakeBuild,
            "clean": clean,
moto's avatar
moto committed
136
137
138
139
140
141
        },
        install_requires=[pytorch_package_dep],
        zip_safe=False,
    )


142
if __name__ == "__main__":
moto's avatar
moto committed
143
    _main()