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') 27device_target = os.getenv('BACKEND_TARGET') 28commit_id = os.getenv('COMMIT_ID').replace("\n", "") 29package_name = os.getenv('MS_PACKAGE_NAME').replace("\n", "") 30build_path = os.getenv('BUILD_PATH') 31 32pwd = os.path.dirname(os.path.realpath(__file__)) 33pkg_dir = os.path.join(build_path, 'package') 34 35 36def _read_file(filename): 37 with open(os.path.join(pwd, filename), encoding='UTF-8') as f: 38 return f.read() 39 40 41version = _read_file('version.txt').replace("\n", "") 42readme = _read_file('README.md') 43 44 45def _write_version(file): 46 file.write("__version__ = '{}'\n".format(version)) 47 48 49def _write_config(file): 50 file.write("__backend__ = '{}'\n".format(backend_policy)) 51 52 53def _write_commit_file(file): 54 file.write("__commit_id__ = '{}'\n".format(commit_id)) 55 56 57def _write_package_name(file): 58 file.write("__package_name__ = '{}'\n".format(package_name)) 59 60 61def _write_device_target(file): 62 file.write("__device_target__ = '{}'\n".format(device_target)) 63 64 65def build_dependencies(): 66 """generate python file""" 67 version_file = os.path.join(pkg_dir, 'mindspore', 'version.py') 68 with open(version_file, 'w') as f: 69 _write_version(f) 70 71 version_file = os.path.join(pwd, 'mindspore', 'version.py') 72 with open(version_file, 'w') as f: 73 _write_version(f) 74 75 config_file = os.path.join(pkg_dir, 'mindspore', 'default_config.py') 76 with open(config_file, 'w') as f: 77 _write_config(f) 78 79 config_file = os.path.join(pwd, 'mindspore', 'default_config.py') 80 with open(config_file, 'w') as f: 81 _write_config(f) 82 83 target = os.path.join(pkg_dir, 'mindspore', 'default_config.py') 84 with open(target, 'a') as f: 85 _write_device_target(f) 86 87 target = os.path.join(pwd, 'mindspore', 'default_config.py') 88 with open(target, 'a') as f: 89 _write_device_target(f) 90 91 package_info = os.path.join(pkg_dir, 'mindspore', 'default_config.py') 92 with open(package_info, 'a') as f: 93 _write_package_name(f) 94 95 package_info = os.path.join(pwd, 'mindspore', 'default_config.py') 96 with open(package_info, 'a') as f: 97 _write_package_name(f) 98 99 commit_file = os.path.join(pkg_dir, 'mindspore', '.commit_id') 100 with open(commit_file, 'w') as f: 101 _write_commit_file(f) 102 103 commit_file = os.path.join(pwd, 'mindspore', '.commit_id') 104 with open(commit_file, 'w') as f: 105 _write_commit_file(f) 106 107 108build_dependencies() 109 110required_package = [ 111 'numpy >= 1.17.0', 112 'protobuf >= 3.13.0', 113 'asttokens >= 1.1.13', 114 'pillow >= 6.2.0', 115 'scipy >= 1.5.2', 116 'packaging >= 20.0', 117 'psutil >= 5.6.1' 118] 119 120package_data = { 121 '': [ 122 '*.so*', 123 '*.pyd', 124 '*.dll', 125 'bin/*', 126 'lib/*.so*', 127 'lib/*.a', 128 'lib/*.dylib*', 129 '.commit_id', 130 'config/*', 131 'ops/bprop_mindir/*', 132 'include/*', 133 'include/*/*', 134 'include/*/*/*', 135 'include/*/*/*/*' 136 ] 137} 138 139 140def update_permissions(path): 141 """ 142 Update permissions. 143 144 Args: 145 path (str): Target directory path. 146 """ 147 if platform.system() == "Windows": 148 return 149 150 for dirpath, dirnames, filenames in os.walk(path): 151 for dirname in dirnames: 152 dir_fullpath = os.path.join(dirpath, dirname) 153 os.chmod(dir_fullpath, stat.S_IREAD | stat.S_IWRITE | 154 stat.S_IEXEC | stat.S_IRGRP | stat.S_IXGRP) 155 for filename in filenames: 156 file_fullpath = os.path.join(dirpath, filename) 157 os.chmod(file_fullpath, stat.S_IREAD) 158 159 160class EggInfo(egg_info): 161 """Egg info.""" 162 163 def run(self): 164 super().run() 165 egg_info_dir = os.path.join(pkg_dir, 'mindspore.egg-info') 166 update_permissions(egg_info_dir) 167 168 169class BuildPy(build_py): 170 """BuildPy.""" 171 172 def run(self): 173 super().run() 174 mindspore_dir = os.path.join(pkg_dir, 'build', 'lib', 'mindspore') 175 update_permissions(mindspore_dir) 176 mindspore_dir = os.path.join(pkg_dir, 'build', 'lib', 'mindspore', '_akg') 177 update_permissions(mindspore_dir) 178 179 180setup( 181 name=package_name, 182 version=version, 183 author='The MindSpore Authors', 184 author_email='contact@mindspore.cn', 185 url='https://www.mindspore.cn', 186 download_url='https://github.com/mindspore-ai/mindspore/tags', 187 project_urls={ 188 'Sources': 'https://github.com/mindspore-ai/mindspore', 189 'Issue Tracker': 'https://github.com/mindspore-ai/mindspore/issues', 190 }, 191 description='MindSpore is a new open source deep learning training/inference ' 192 'framework that could be used for mobile, edge and cloud scenarios.', 193 long_description=readme, 194 long_description_content_type="text/markdown", 195 packages=find_packages(), 196 package_data=package_data, 197 include_package_data=True, 198 cmdclass={ 199 'egg_info': EggInfo, 200 'build_py': BuildPy, 201 }, 202 entry_points={ 203 'console_scripts': [ 204 'cache_admin=mindspore.dataset.engine.cache_admin: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