• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2# encoding: utf-8
3# Copyright 2020 Huawei Technologies Co., Ltd
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16# ============================================================================
17"""setup package."""
18import os
19import stat
20import platform
21
22from setuptools import setup, find_packages
23from setuptools.command.egg_info import egg_info
24from setuptools.command.build_py import build_py
25
26backend_policy = os.getenv('BACKEND_POLICY')
27commit_id = os.getenv('COMMIT_ID').replace("\n", "")
28package_name = os.getenv('MS_PACKAGE_NAME').replace("\n", "")
29build_path = os.getenv('BUILD_PATH')
30
31pwd = os.path.dirname(os.path.realpath(__file__))
32pkg_dir = os.path.join(build_path, 'package')
33
34
35def _read_file(filename):
36    with open(os.path.join(pwd, filename), encoding='UTF-8') as f:
37        return f.read()
38
39
40version = _read_file('version.txt').replace("\n", "")
41readme = _read_file('README.md')
42
43
44def _write_version(file):
45    file.write("__version__ = '{}'\n".format(version))
46
47
48def _write_config(file):
49    file.write("__backend__ = '{}'\n".format(backend_policy))
50
51
52def _write_commit_file(file):
53    file.write("__commit_id__ = '{}'\n".format(commit_id))
54
55
56def _write_package_name(file):
57    file.write("__package_name__ = '{}'\n".format(package_name))
58
59
60def build_dependencies():
61    """generate python file"""
62    version_file = os.path.join(pkg_dir, 'mindspore', 'version.py')
63    with open(version_file, 'w') as f:
64        _write_version(f)
65
66    version_file = os.path.join(pwd, 'mindspore/python/mindspore', 'version.py')
67    with open(version_file, 'w') as f:
68        _write_version(f)
69
70    config_file = os.path.join(pkg_dir, 'mindspore', 'default_config.py')
71    with open(config_file, 'w') as f:
72        _write_config(f)
73
74    config_file = os.path.join(pwd, 'mindspore/python/mindspore', 'default_config.py')
75    with open(config_file, 'w') as f:
76        _write_config(f)
77
78    package_info = os.path.join(pkg_dir, 'mindspore', 'default_config.py')
79    with open(package_info, 'a') as f:
80        _write_package_name(f)
81
82    package_info = os.path.join(pwd, 'mindspore/python/mindspore', 'default_config.py')
83    with open(package_info, 'a') as f:
84        _write_package_name(f)
85
86    commit_file = os.path.join(pkg_dir, 'mindspore', '.commit_id')
87    with open(commit_file, 'w') as f:
88        _write_commit_file(f)
89
90    commit_file = os.path.join(pwd, 'mindspore/python/mindspore', '.commit_id')
91    with open(commit_file, 'w') as f:
92        _write_commit_file(f)
93
94
95build_dependencies()
96
97required_package = [
98    'numpy >= 1.20.0,<2.0.0',
99    'protobuf >= 3.13.0',
100    'asttokens >= 2.0.4',
101    'pillow >= 6.2.0',
102    'scipy >= 1.5.4',
103    'packaging >= 20.0',
104    'psutil >= 5.6.1',
105    'astunparse >= 1.6.3'
106]
107
108package_data = {
109    '': [
110        '*.so*',
111        '*.pyd',
112        '*.dll',
113        '*.pdb',
114        'bin/*',
115        'lib/plugin/*',
116        'lib/plugin/*/*',
117        'lib/plugin/*/*/*',
118        'lib/plugin/*/*/*/*',
119        'lib/plugin/*/*/*/*/*',
120        'lib/plugin/*/*/*/*/*/*',
121        'lib/plugin/*/*/*/*/*/*/*',
122        'lib/plugin/*/*/*/*/*/*/*/*',
123        'lib/plugin/*/*/*/*/*/*/*/*/*',
124        'lib/plugin/*/*/*/*/*/*/*/*/*/*',
125        'lib/*.so*',
126        'lib/*.a',
127        'lib/*.dylib*',
128        '.commit_id',
129        'config/*',
130        'include/*',
131        'include/*/*',
132        'include/*/*/*',
133        'include/*/*/*/*',
134        'Third_Party_Open_Source_Software_Notice'
135    ]
136}
137
138
139def update_permissions(path):
140    """
141    Update permissions.
142
143    Args:
144        path (str): Target directory path.
145    """
146    if platform.system() == "Windows":
147        return
148
149    for dirpath, dirnames, filenames in os.walk(path):
150        for dirname in dirnames:
151            dir_fullpath = os.path.join(dirpath, dirname)
152            os.chmod(dir_fullpath, stat.S_IREAD | stat.S_IWRITE |
153                     stat.S_IEXEC | stat.S_IRGRP | stat.S_IXGRP)
154        for filename in filenames:
155            file_fullpath = os.path.join(dirpath, filename)
156            os.chmod(file_fullpath, stat.S_IREAD)
157
158
159class EggInfo(egg_info):
160    """Egg info."""
161
162    def run(self):
163        super().run()
164        egg_info_dir = os.path.join(pkg_dir, 'mindspore.egg-info')
165        update_permissions(egg_info_dir)
166
167
168class BuildPy(build_py):
169    """BuildPy."""
170
171    def run(self):
172        super().run()
173        mindspore_dir = os.path.join(pkg_dir, 'build', 'lib', 'mindspore')
174        update_permissions(mindspore_dir)
175        mindspore_dir = os.path.join(pkg_dir, 'build', 'lib', 'mindspore', '_akg')
176        update_permissions(mindspore_dir)
177
178
179setup(
180    name=package_name,
181    version=version,
182    author='The MindSpore Authors',
183    author_email='contact@mindspore.cn',
184    url='https://www.mindspore.cn',
185    download_url='https://github.com/mindspore-ai/mindspore/tags',
186    project_urls={
187        'Sources': 'https://github.com/mindspore-ai/mindspore',
188        'Issue Tracker': 'https://github.com/mindspore-ai/mindspore/issues',
189    },
190    description='MindSpore is a new open source deep learning training/inference '
191                'framework that could be used for mobile, edge and cloud scenarios.',
192    long_description=readme,
193    long_description_content_type="text/markdown",
194    packages=find_packages(),
195    package_data=package_data,
196    include_package_data=True,
197    cmdclass={
198        'egg_info': EggInfo,
199        'build_py': BuildPy,
200    },
201    entry_points={
202        'console_scripts': [
203            'cache_admin=mindspore.dataset.engine.cache_admin:main',
204            'msrun=mindspore.parallel.cluster.run:main'
205        ],
206    },
207    python_requires='>=3.7',
208    install_requires=required_package,
209    classifiers=[
210        'Development Status :: 4 - Beta',
211        'Environment :: Console',
212        'Intended Audience :: Science/Research',
213        'Intended Audience :: Developers',
214        'License :: OSI Approved :: Apache Software License',
215        'Programming Language :: Python :: 3 :: Only',
216        'Programming Language :: Python :: 3.7',
217        'Programming Language :: Python :: 3.8',
218        'Programming Language :: C++',
219        'Topic :: Scientific/Engineering',
220        'Topic :: Scientific/Engineering :: Artificial Intelligence',
221        'Topic :: Software Development',
222        'Topic :: Software Development :: Libraries',
223        'Topic :: Software Development :: Libraries :: Python Modules',
224    ],
225    license='Apache 2.0',
226    keywords='mindspore machine learning',
227)
228