paddlenlp
/
setup.py
198 строк · 6.5 Кб
1# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14import errno15import io16import os17import subprocess18
19import setuptools20
21PADDLENLP_STABLE_VERSION = "PADDLENLP_STABLE_VERSION"22
23
24def read_requirements_file(filepath):25with open(filepath) as fin:26requirements = fin.read()27return requirements28
29
30def is_git_repo(dir: str) -> bool:31"""Is the given directory version-controlled with git?"""32return os.path.exists(os.path.join(dir, ".git"))33
34
35def have_git() -> bool:36"""Can we run the git executable?"""37try:38subprocess.check_output(["git", "--help"])39return True40except subprocess.CalledProcessError:41return False42except OSError:43return False44
45
46def git_revision(dir: str) -> bytes:47"""Get the SHA-1 of the HEAD of a git repository."""48return subprocess.check_output(["git", "rev-parse", "HEAD"], cwd=dir).strip()49
50
51def git_checkout(dir: str, filename: str) -> bytes:52"""Get the SHA-1 of the HEAD of a git repository."""53return subprocess.check_output(["git", "checkout", filename], cwd=dir).strip()54
55
56def is_dirty(dir: str) -> bool:57"""Check whether a git repository has uncommitted changes."""58output = subprocess.check_output(["git", "status", "-uno", "--porcelain"], cwd=dir)59return output.strip() != b""60
61
62commit = "unknown"63paddlenlp_dir = os.path.abspath(os.path.dirname(__file__))64if commit.endswith("unknown") and is_git_repo(paddlenlp_dir) and have_git():65commit = git_revision(paddlenlp_dir).decode("utf-8")66if is_dirty(paddlenlp_dir):67commit += ".dirty"68
69
70def write_version_py(filename="paddlenlp/version/__init__.py"):71cnt = '''# THIS FILE IS GENERATED FROM PADDLENLP SETUP.PY72commit = '%(commit)s'
73
74__all__ = ['show']
75
76def show():
77"""Get the corresponding commit id of paddlenlp.
78
79Returns:
80The commit-id of paddlenlp will be output.
81
82full_version: version of paddlenlp
83
84
85Examples:
86.. code-block:: python
87
88import paddlenlp
89
90paddlenlp.version.show()
91# commit: 1ef5b94a18773bb0b1bba1651526e5f5fc5b16fa
92
93"""
94print("commit:", commit)
95
96'''
97commit_id = commit98content = cnt % {"commit": commit_id}99
100dirname = os.path.dirname(filename)101
102try:103os.makedirs(dirname)104except OSError as e:105if e.errno != errno.EEXIST:106raise107
108with open(filename, "w") as f:109f.write(content)110
111
112__version__ = "2.7.1.post"113if os.getenv(PADDLENLP_STABLE_VERSION):114__version__ = __version__.replace(".post", "")115
116
117extras = {}118REQUIRED_PACKAGES = read_requirements_file("requirements.txt")119extras["tests"] = read_requirements_file("tests/requirements.txt")120extras["docs"] = read_requirements_file("docs/requirements.txt")121extras["autonlp"] = read_requirements_file("paddlenlp/experimental/autonlp/requirements.txt")122extras["dev"] = extras["tests"] + extras["docs"] + extras["autonlp"]123
124
125def read(*names, **kwargs):126with io.open(os.path.join(os.path.dirname(__file__), *names), encoding=kwargs.get("encoding", "utf8")) as fp:127return fp.read()128
129
130def get_package_data_files(package, data, package_dir=None):131"""132Helps to list all specified files in package including files in directories
133since `package_data` ignores directories.
134"""
135if package_dir is None:136package_dir = os.path.join(*package.split("."))137all_files = []138for f in data:139path = os.path.join(package_dir, f)140if os.path.isfile(path):141all_files.append(f)142continue143for root, _dirs, files in os.walk(path, followlinks=True):144root = os.path.relpath(root, package_dir)145for file in files:146file = os.path.join(root, file)147if file not in all_files:148all_files.append(file)149return all_files150
151
152if commit != "unknown":153write_version_py(filename="paddlenlp/version/__init__.py")154
155try:156setuptools.setup(157name="paddlenlp",158version=__version__,159author="PaddleNLP Team",160author_email="paddlenlp@baidu.com",161description="Easy-to-use and powerful NLP library with Awesome model zoo, supporting wide-range of NLP tasks from research to industrial applications, including Neural Search, Question Answering, Information Extraction and Sentiment Analysis end-to-end system.",162long_description=read("README_en.md"),163long_description_content_type="text/markdown",164url="https://github.com/PaddlePaddle/PaddleNLP",165license_files=("LICENSE",),166packages=setuptools.find_packages(167where=".",168exclude=("examples*", "tests*", "applications*", "fast_tokenizer*", "fast_generation*", "model_zoo*"),169),170package_data={171"paddlenlp.ops": get_package_data_files(172"paddlenlp.ops", ["CMakeLists.txt", "README.md", "cmake", "fast_transformer", "patches", "optimizer"]173),174"paddlenlp.transformers.layoutxlm": get_package_data_files(175"paddlenlp.transformers.layoutxlm", ["visual_backbone.yaml"]176),177},178setup_requires=["cython", "numpy"],179install_requires=REQUIRED_PACKAGES,180entry_points={"console_scripts": ["paddlenlp = paddlenlp.cli:main"]},181extras_require=extras,182python_requires=">=3.6",183classifiers=[184"Programming Language :: Python :: 3",185"Programming Language :: Python :: 3.6",186"Programming Language :: Python :: 3.7",187"Programming Language :: Python :: 3.8",188"Programming Language :: Python :: 3.9",189"License :: OSI Approved :: Apache Software License",190"Operating System :: OS Independent",191],192license="Apache 2.0",193)194except Exception as e:195git_checkout(paddlenlp_dir, "paddlenlp/version/__init__.py") if commit != "unknown" else None196raise e197
198git_checkout(paddlenlp_dir, "paddlenlp/version/__init__.py") if commit != "unknown" else None199