You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

setup.py 6.8 kB

5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. # Copyright 2019 Huawei Technologies Co., Ltd.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.
  14. # ==============================================================================
  15. """Setup."""
  16. import sys
  17. import os
  18. import shutil
  19. import stat
  20. import platform
  21. import shlex
  22. import subprocess
  23. import types
  24. from importlib import import_module
  25. from setuptools import setup
  26. from setuptools.command.egg_info import egg_info
  27. from setuptools.command.build_py import build_py
  28. from setuptools.command.install import install
  29. def get_version():
  30. """Get version."""
  31. machinery = import_module('importlib.machinery')
  32. version_path = os.path.join(os.path.dirname(__file__), 'mindinsight', '_version.py')
  33. module_name = '__mindinsightversion__'
  34. version_module = types.ModuleType(module_name)
  35. loader = machinery.SourceFileLoader(module_name, version_path)
  36. loader.exec_module(version_module)
  37. return version_module.VERSION
  38. def get_os():
  39. """Get OS."""
  40. os_system = platform.system().lower()
  41. return os_system
  42. def get_description():
  43. """Get description."""
  44. os_info = get_os()
  45. cpu_info = platform.machine()
  46. cmd = "git log --format='[sha1]:%h, [branch]:%d' -1"
  47. process = subprocess.Popen(
  48. shlex.split(cmd),
  49. shell=False,
  50. stdin=subprocess.PIPE,
  51. stdout=subprocess.PIPE,
  52. stderr=subprocess.PIPE
  53. )
  54. stdout, _ = process.communicate()
  55. if not process.returncode:
  56. git_version = stdout.decode()
  57. return 'mindinsight platform: %s, cpu: %s, git version: %s' % (os_info, cpu_info, git_version)
  58. return 'mindinsight platform: %s, cpu: %s' % (os_info, cpu_info)
  59. def get_install_requires():
  60. """Get install requirements."""
  61. with open('requirements.txt') as file:
  62. return file.read().splitlines()
  63. def update_permissions(path):
  64. """
  65. Update permissions.
  66. Args:
  67. path (str): Target directory path.
  68. """
  69. for dirpath, dirnames, filenames in os.walk(path):
  70. for dirname in dirnames:
  71. dir_fullpath = os.path.join(dirpath, dirname)
  72. os.chmod(dir_fullpath, stat.S_IREAD | stat.S_IWRITE | stat.S_IEXEC | stat.S_IRGRP | stat.S_IXGRP)
  73. for filename in filenames:
  74. file_fullpath = os.path.join(dirpath, filename)
  75. os.chmod(file_fullpath, stat.S_IREAD)
  76. def run_script(script):
  77. """
  78. Run script.
  79. Args:
  80. script (str): Target script file path.
  81. """
  82. cmd = '/bin/bash {}'.format(script)
  83. process = subprocess.Popen(
  84. shlex.split(cmd),
  85. shell=False,
  86. stdin=subprocess.PIPE,
  87. stdout=subprocess.PIPE,
  88. stderr=subprocess.STDOUT
  89. )
  90. while True:
  91. line = process.stdout.readline()
  92. if not line and process.poll() is not None:
  93. break
  94. if line:
  95. sys.stdout.write(line.decode())
  96. if process.returncode:
  97. sys.exit(1)
  98. class EggInfo(egg_info):
  99. """Egg info."""
  100. def run(self):
  101. self.build_dependencies()
  102. egg_info_dir = os.path.join(os.path.dirname(__file__), 'mindinsight.egg-info')
  103. shutil.rmtree(egg_info_dir, ignore_errors=True)
  104. super().run()
  105. update_permissions(egg_info_dir)
  106. def build_dependencies(self):
  107. build_dir = os.path.join(os.path.dirname(__file__), 'build')
  108. sys.stdout.write('building crc32 ...\n')
  109. crc32_script = os.path.join(build_dir, 'scripts', 'crc32.sh')
  110. run_script(crc32_script)
  111. sys.stdout.write('building ui ...\n')
  112. ui_script = os.path.join(build_dir, 'scripts', 'ui.sh')
  113. run_script(ui_script)
  114. class BuildPy(build_py):
  115. """Build py files."""
  116. def run(self):
  117. mindinsight_lib_dir = os.path.join(os.path.dirname(__file__), 'build', 'lib', 'mindinsight')
  118. shutil.rmtree(mindinsight_lib_dir, ignore_errors=True)
  119. super().run()
  120. update_permissions(mindinsight_lib_dir)
  121. class Install(install):
  122. """Install."""
  123. def run(self):
  124. super().run()
  125. if sys.argv[-1] == 'install':
  126. pip = import_module('pip')
  127. mindinsight_dir = os.path.join(os.path.dirname(pip.__path__[0]), 'mindinsight')
  128. update_permissions(mindinsight_dir)
  129. if __name__ == '__main__':
  130. version_info = sys.version_info
  131. if (version_info.major, version_info.minor) < (3, 7):
  132. sys.stderr.write('Python version should be at least 3.7\r\n')
  133. sys.exit(1)
  134. setup(
  135. name='mindinsight',
  136. version=get_version(),
  137. author='The MindSpore Authors',
  138. author_email='contact@mindspore.cn',
  139. url='https://www.mindspore.cn',
  140. download_url='https://gitee.com/mindspore/mindinsight/tags',
  141. project_urls={
  142. 'Sources': 'https://gitee.com/mindspore/mindinsight',
  143. 'Issue Tracker': 'https://gitee.com/mindspore/mindinsight/issues',
  144. },
  145. description=get_description(),
  146. packages=['mindinsight'],
  147. platforms=[get_os()],
  148. include_package_data=True,
  149. cmdclass={
  150. 'egg_info': EggInfo,
  151. 'build_py': BuildPy,
  152. 'install': Install,
  153. },
  154. entry_points={
  155. 'console_scripts': [
  156. 'mindinsight=mindinsight.utils.command:main',
  157. 'mindconverter=mindinsight.mindconverter.cli:cli_entry',
  158. ],
  159. },
  160. python_requires='>=3.7',
  161. install_requires=get_install_requires(),
  162. classifiers=[
  163. 'Development Status :: 4 - Beta',
  164. 'Environment :: Console',
  165. 'Environment :: Web Environment',
  166. 'Intended Audience :: Science/Research',
  167. 'Intended Audience :: Developers',
  168. 'License :: OSI Approved :: Apache Software License',
  169. 'Programming Language :: Python :: 3 :: Only',
  170. 'Programming Language :: Python :: 3.7',
  171. 'Programming Language :: Python :: 3.8',
  172. 'Topic :: Scientific/Engineering',
  173. 'Topic :: Scientific/Engineering :: Artificial Intelligence',
  174. 'Topic :: Software Development',
  175. 'Topic :: Software Development :: Libraries',
  176. 'Topic :: Software Development :: Libraries :: Python Modules',
  177. ],
  178. license='Apache 2.0',
  179. keywords='mindinsight',
  180. )