setup.py 5.5 KB
Newer Older
H
Hui Zhang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
H
Hui Zhang 已提交
14 15
import contextlib
import inspect
H
Hui Zhang 已提交
16 17 18
import io
import os
import re
H
Hui Zhang 已提交
19
import subprocess as sp
H
Hui Zhang 已提交
20
import sys
H
Hui Zhang 已提交
21
from pathlib import Path
H
Hui Zhang 已提交
22

H
Hui Zhang 已提交
23
from setuptools import Command
H
Hui Zhang 已提交
24 25
from setuptools import find_packages
from setuptools import setup
H
Hui Zhang 已提交
26 27 28
from setuptools.command.develop import develop
from setuptools.command.install import install

H
Hui Zhang 已提交
29
HERE = Path(os.path.abspath(os.path.dirname(__file__)))
H
Hui Zhang 已提交
30

31

H
Hui Zhang 已提交
32 33 34 35
@contextlib.contextmanager
def pushd(new_dir):
    old_dir = os.getcwd()
    os.chdir(new_dir)
36
    print(new_dir)
H
Hui Zhang 已提交
37 38
    yield
    os.chdir(old_dir)
39
    print(old_dir)
H
Hui Zhang 已提交
40 41


H
Hui Zhang 已提交
42
def read(*names, **kwargs):
H
Hui Zhang 已提交
43 44 45
    with io.open(
            os.path.join(os.path.dirname(__file__), *names),
            encoding=kwargs.get("encoding", "utf8")) as fp:
H
Hui Zhang 已提交
46 47 48
        return fp.read()


H
Hui Zhang 已提交
49
def check_call(cmd: str, shell=False, executable=None):
H
Hui Zhang 已提交
50
    try:
H
Hui Zhang 已提交
51 52 53 54
        sp.check_call(
            cmd.split(),
            shell=shell,
            executable="/bin/bash" if shell else executable)
H
Hui Zhang 已提交
55 56 57 58 59
    except sp.CalledProcessError as e:
        print(
            f"{__file__}:{inspect.currentframe().f_lineno}: CMD: {cmd}, Error:",
            e.output,
            file=sys.stderr)
H
Hui Zhang 已提交
60
        raise e
H
Hui Zhang 已提交
61 62


63 64 65 66 67 68
def _remove(files: str):
    for f in files:
        f.unlink()


def _post_install(install_lib_dir):
H
Hui Zhang 已提交
69
    # apt
70
    check_call("apt-get update -y")
H
Hui Zhang 已提交
71 72
    check_call("apt-get install -y " + 'vim tig tree sox pkg-config ' +
               'libsndfile1 libflac-dev libogg-dev ' +
73
               'libvorbis-dev libboost-dev swig python3-dev ')
H
Hui Zhang 已提交
74
    print("apt install.")
75

H
Hui Zhang 已提交
76 77
    # tools/make
    tool_dir = HERE / "tools"
78
    _remove(tool_dir.glob("*.done"))
H
Hui Zhang 已提交
79
    with pushd(tool_dir):
H
Hui Zhang 已提交
80
        check_call("make")
H
Hui Zhang 已提交
81
    print("tools install.")
H
Hui Zhang 已提交
82

H
Hui Zhang 已提交
83 84 85
    # install autolog
    tools_extrs_dir = HERE / 'tools/extras'
    with pushd(tools_extrs_dir):
86 87
        print(os.getcwd())
        check_call(f"./install_autolog.sh")
H
Hui Zhang 已提交
88
    print("autolog install.")
H
Hui Zhang 已提交
89

H
Hui Zhang 已提交
90
    # ctcdecoder
91
    ctcdecoder_dir = HERE / 'deepspeech/decoders/ctcdecoder/swig'
H
Hui Zhang 已提交
92 93 94 95
    with pushd(ctcdecoder_dir):
        check_call("bash -e setup.sh")
    print("ctcdecoder install.")

H
Hui Zhang 已提交
96
    # install third_party
H
Hui Zhang 已提交
97
    third_party_dir = HERE / 'third_party'
H
Hui Zhang 已提交
98 99 100
    with pushd(third_party_dir):
        check_call("bash -e install.sh")
    print("third_party install.")
H
Hui Zhang 已提交
101 102 103 104 105


class DevelopCommand(develop):
    def run(self):
        develop.run(self)
106
        # must after develop.run, or pkg install by shell will not see
H
Hui Zhang 已提交
107 108 109 110 111 112
        self.execute(_post_install, (self.install_lib, ), msg="Post Install...")


class InstallCommand(install):
    def run(self):
        install.run(self)
113
        # must after install.run, or pkg install by shell will not see
H
Hui Zhang 已提交
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
        self.execute(_post_install, (self.install_lib, ), msg="Post Install...")


# cmd: python setup.py upload
class UploadCommand(Command):
    description = "Build and publish the package."
    user_options = []

    def initialize_options(self):
        pass

    def finalize_options(self):
        pass

    def run(self):
        try:
            print("Removing previous dist/ ...")
H
Hui Zhang 已提交
131
            shutil.rmtree(str(HERE / "dist"))
H
Hui Zhang 已提交
132 133 134 135 136 137 138 139 140
        except OSError:
            pass
        print("Building source distribution...")
        sp.check_call([sys.executable, "setup.py", "sdist"])
        print("Uploading package to PyPi...")
        sp.check_call(["twine", "upload", "dist/*"])
        sys.exit()


H
Hui Zhang 已提交
141 142
setup_info = dict(
    # Metadata
H
Hui Zhang 已提交
143
    name='paddle-speech',
H
Hui Zhang 已提交
144
    version='2.1.2',
H
Hui Zhang 已提交
145
    author='PaddleSL Speech Team',
H
Hui Zhang 已提交
146
    author_email='',
H
Hui Zhang 已提交
147
    url='https://github.com/PaddlePaddle/DeepSpeech',
H
Hui Zhang 已提交
148
    license='Apache 2',
H
Hui Zhang 已提交
149
    description='Speech tools and models based on Paddlepaddle',
H
Hui Zhang 已提交
150
    long_description=read("README.md"),
H
Hui Zhang 已提交
151
    long_description_content_type="text/markdown",
H
Hui Zhang 已提交
152 153 154 155 156 157 158 159 160 161 162 163 164 165
    keywords=[
        "speech",
        "asr",
        "tts",
        "text frontend",
        "MFA",
        "paddlepaddle",
        "transformer",
        "conformer",
        "fastspeech",
        "vocoder",
        "pwgan",
        "gan",
    ],
H
Hui Zhang 已提交
166
    python_requires='>=3.6',
H
Hui Zhang 已提交
167
    install_requires=[d.strip() for d in read('requirements.txt').split()],
H
Hui Zhang 已提交
168
    extras_require={
H
Hui Zhang 已提交
169 170 171 172
        'doc': [
            "sphinx", "sphinx-rtd-theme", "numpydoc", "myst_parser",
            "recommonmark>=0.5.0", "sphinx-markdown-tables", "sphinx-autobuild"
        ],
H
Hui Zhang 已提交
173
    },
H
Hui Zhang 已提交
174 175 176 177 178
    cmdclass={
        'develop': DevelopCommand,
        'install': InstallCommand,
        'upload': UploadCommand,
    },
H
Hui Zhang 已提交
179 180 181 182 183 184 185 186 187 188

    # Package info
    packages=find_packages(exclude=('tests', 'tests.*')),
    zip_safe=True,
    classifiers=[
        'Development Status :: 4 - Beta',
        'Intended Audience :: Developers',
        'Intended Audience :: Science/Research',
        'Topic :: Scientific/Engineering :: Artificial Intelligence',
        'License :: OSI Approved :: Apache Software License',
H
Hui Zhang 已提交
189 190
        'Programming Language :: Python',
        'Programming Language :: Python :: 3',
H
Hui Zhang 已提交
191 192
        'Programming Language :: Python :: 3.6',
        'Programming Language :: Python :: 3.7',
H
Hui Zhang 已提交
193
        'Programming Language :: Python :: 3.8',
H
Hui Zhang 已提交
194
    ], )
H
Hui Zhang 已提交
195 196

setup(**setup_info)