setup.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. import os
  2. import os.path as osp
  3. import platform
  4. import shutil
  5. import sys
  6. import warnings
  7. from setuptools import find_packages, setup
  8. def readme():
  9. with open('README.md', encoding='utf-8') as f:
  10. content = f.read()
  11. return content
  12. version_file = 'mmpose/version.py'
  13. def get_version():
  14. with open(version_file, 'r') as f:
  15. exec(compile(f.read(), version_file, 'exec'))
  16. import sys
  17. # return short version for sdist
  18. if 'sdist' in sys.argv or 'bdist_wheel' in sys.argv:
  19. return locals()['short_version']
  20. else:
  21. return locals()['__version__']
  22. def parse_requirements(fname='requirements.txt', with_version=True):
  23. """Parse the package dependencies listed in a requirements file but strips
  24. specific versioning information.
  25. Args:
  26. fname (str): path to requirements file
  27. with_version (bool, default=False): if True include version specs
  28. Returns:
  29. List[str]: list of requirements items
  30. CommandLine:
  31. python -c "import setup; print(setup.parse_requirements())"
  32. """
  33. import re
  34. import sys
  35. from os.path import exists
  36. require_fpath = fname
  37. def parse_line(line):
  38. """Parse information from a line in a requirements text file."""
  39. if line.startswith('-r '):
  40. # Allow specifying requirements in other files
  41. target = line.split(' ')[1]
  42. for info in parse_require_file(target):
  43. yield info
  44. else:
  45. info = {'line': line}
  46. if line.startswith('-e '):
  47. info['package'] = line.split('#egg=')[1]
  48. elif '@git+' in line:
  49. info['package'] = line
  50. else:
  51. # Remove versioning from the package
  52. pat = '(' + '|'.join(['>=', '==', '>']) + ')'
  53. parts = re.split(pat, line, maxsplit=1)
  54. parts = [p.strip() for p in parts]
  55. info['package'] = parts[0]
  56. if len(parts) > 1:
  57. op, rest = parts[1:]
  58. if ';' in rest:
  59. # Handle platform specific dependencies
  60. # http://setuptools.readthedocs.io/en/latest/setuptools.html#declaring-platform-specific-dependencies
  61. version, platform_deps = map(str.strip,
  62. rest.split(';'))
  63. info['platform_deps'] = platform_deps
  64. else:
  65. version = rest # NOQA
  66. info['version'] = (op, version)
  67. yield info
  68. def parse_require_file(fpath):
  69. with open(fpath, 'r') as f:
  70. for line in f.readlines():
  71. line = line.strip()
  72. if line and not line.startswith('#'):
  73. for info in parse_line(line):
  74. yield info
  75. def gen_packages_items():
  76. if exists(require_fpath):
  77. for info in parse_require_file(require_fpath):
  78. parts = [info['package']]
  79. if with_version and 'version' in info:
  80. parts.extend(info['version'])
  81. if not sys.version.startswith('3.4'):
  82. # apparently package_deps are broken in 3.4
  83. platform_deps = info.get('platform_deps')
  84. if platform_deps is not None:
  85. parts.append(';' + platform_deps)
  86. item = ''.join(parts)
  87. yield item
  88. packages = list(gen_packages_items())
  89. return packages
  90. def add_mim_extension():
  91. """Add extra files that are required to support MIM into the package.
  92. These files will be added by creating a symlink to the originals if the
  93. package is installed in `editable` mode (e.g. pip install -e .), or by
  94. copying from the originals otherwise.
  95. """
  96. # parse installment mode
  97. if 'develop' in sys.argv:
  98. # installed by `pip install -e .`
  99. if platform.system() == 'Windows':
  100. mode = 'copy'
  101. else:
  102. mode = 'symlink'
  103. elif 'sdist' in sys.argv or 'bdist_wheel' in sys.argv:
  104. # installed by `pip install .`
  105. # or create source distribution by `python setup.py sdist`
  106. mode = 'copy'
  107. else:
  108. return
  109. filenames = ['tools', 'configs', 'demo', 'model-index.yml']
  110. repo_path = osp.dirname(__file__)
  111. mim_path = osp.join(repo_path, 'mmpose', '.mim')
  112. os.makedirs(mim_path, exist_ok=True)
  113. for filename in filenames:
  114. if osp.exists(filename):
  115. src_path = osp.join(repo_path, filename)
  116. tar_path = osp.join(mim_path, filename)
  117. if osp.isfile(tar_path) or osp.islink(tar_path):
  118. os.remove(tar_path)
  119. elif osp.isdir(tar_path):
  120. shutil.rmtree(tar_path)
  121. if mode == 'symlink':
  122. src_relpath = osp.relpath(src_path, osp.dirname(tar_path))
  123. os.symlink(src_relpath, tar_path)
  124. elif mode == 'copy':
  125. if osp.isfile(src_path):
  126. shutil.copyfile(src_path, tar_path)
  127. elif osp.isdir(src_path):
  128. shutil.copytree(src_path, tar_path)
  129. else:
  130. warnings.warn(f'Cannot copy file {src_path}.')
  131. else:
  132. raise ValueError(f'Invalid mode {mode}')
  133. if __name__ == '__main__':
  134. add_mim_extension()
  135. setup(
  136. name='mmpose',
  137. version=get_version(),
  138. description='OpenMMLab Pose Estimation Toolbox and Benchmark.',
  139. author='MMPose Contributors',
  140. author_email='openmmlab@gmail.com',
  141. keywords='computer vision, pose estimation',
  142. long_description=readme(),
  143. long_description_content_type='text/markdown',
  144. packages=find_packages(exclude=('configs', 'tools', 'demo')),
  145. include_package_data=True,
  146. package_data={'mmpose.ops': ['*/*.so']},
  147. classifiers=[
  148. 'Development Status :: 4 - Beta',
  149. 'License :: OSI Approved :: Apache Software License',
  150. 'Operating System :: OS Independent',
  151. 'Programming Language :: Python :: 3',
  152. 'Programming Language :: Python :: 3.7',
  153. 'Programming Language :: Python :: 3.8',
  154. 'Programming Language :: Python :: 3.9',
  155. ],
  156. url='https://github.com/open-mmlab/mmpose',
  157. license='Apache License 2.0',
  158. python_requires='>=3.7',
  159. install_requires=parse_requirements('requirements/runtime.txt'),
  160. extras_require={
  161. 'all': parse_requirements('requirements.txt'),
  162. 'tests': parse_requirements('requirements/tests.txt'),
  163. 'optional': parse_requirements('requirements/optional.txt'),
  164. 'mim': parse_requirements('requirements/mminstall.txt'),
  165. },
  166. zip_safe=False)