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.5 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. )
  87. rc = process.wait()
  88. if rc:
  89. sys.exit(1)
  90. class EggInfo(egg_info):
  91. """Egg info."""
  92. def run(self):
  93. self.build_dependencies()
  94. egg_info_dir = os.path.join(os.path.dirname(__file__), 'mindinsight.egg-info')
  95. shutil.rmtree(egg_info_dir, ignore_errors=True)
  96. super().run()
  97. update_permissions(egg_info_dir)
  98. def build_dependencies(self):
  99. build_dir = os.path.join(os.path.dirname(__file__), 'build')
  100. sys.stdout.write('building crc32 ...\n')
  101. crc32_script = os.path.join(build_dir, 'scripts', 'crc32.sh')
  102. run_script(crc32_script)
  103. sys.stdout.write('building ui ...\n')
  104. ui_script = os.path.join(build_dir, 'scripts', 'ui.sh')
  105. run_script(ui_script)
  106. class BuildPy(build_py):
  107. """Build py files."""
  108. def run(self):
  109. mindinsight_lib_dir = os.path.join(os.path.dirname(__file__), 'build', 'lib', 'mindinsight')
  110. shutil.rmtree(mindinsight_lib_dir, ignore_errors=True)
  111. super().run()
  112. update_permissions(mindinsight_lib_dir)
  113. class Install(install):
  114. """Install."""
  115. def run(self):
  116. super().run()
  117. if sys.argv[-1] == 'install':
  118. pip = import_module('pip')
  119. mindinsight_dir = os.path.join(os.path.dirname(pip.__path__[0]), 'mindinsight')
  120. update_permissions(mindinsight_dir)
  121. if __name__ == '__main__':
  122. version_info = sys.version_info
  123. if (version_info.major, version_info.minor) < (3, 7):
  124. sys.stderr.write('Python version should be at least 3.7\r\n')
  125. sys.exit(1)
  126. setup(
  127. name='mindinsight',
  128. version=get_version(),
  129. author='The MindSpore Authors',
  130. author_email='contact@mindspore.cn',
  131. url='https://www.mindspore.cn',
  132. download_url='https://gitee.com/mindspore/mindinsight/tags',
  133. project_urls={
  134. 'Sources': 'https://gitee.com/mindspore/mindinsight',
  135. 'Issue Tracker': 'https://gitee.com/mindspore/mindinsight/issues',
  136. },
  137. description=get_description(),
  138. packages=['mindinsight'],
  139. platforms=[get_os()],
  140. include_package_data=True,
  141. cmdclass={
  142. 'egg_info': EggInfo,
  143. 'build_py': BuildPy,
  144. 'install': Install,
  145. },
  146. entry_points={
  147. 'console_scripts': [
  148. 'mindinsight=mindinsight.utils.command:main',
  149. 'mindconverter=mindinsight.mindconverter.cli:cli_entry',
  150. ],
  151. },
  152. python_requires='>=3.7',
  153. install_requires=get_install_requires(),
  154. classifiers=[
  155. 'Development Status :: 4 - Beta',
  156. 'Environment :: Console',
  157. 'Environment :: Web Environment',
  158. 'Intended Audience :: Science/Research',
  159. 'Intended Audience :: Developers',
  160. 'License :: OSI Approved :: Apache Software License',
  161. 'Programming Language :: Python :: 3 :: Only',
  162. 'Programming Language :: Python :: 3.7',
  163. 'Programming Language :: Python :: 3.8',
  164. 'Topic :: Scientific/Engineering',
  165. 'Topic :: Scientific/Engineering :: Artificial Intelligence',
  166. 'Topic :: Software Development',
  167. 'Topic :: Software Development :: Libraries',
  168. 'Topic :: Software Development :: Libraries :: Python Modules',
  169. ],
  170. license='Apache 2.0',
  171. keywords='mindinsight',
  172. )