setup.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import os
  2. import subprocess
  3. from setuptools import setup, find_packages
  4. def parse_requirements(filename):
  5. with open(filename) as f:
  6. lines = f.read().splitlines()
  7. requires = []
  8. for line in lines:
  9. if "http" in line:
  10. pkg_name_without_url = line.split('@')[0].strip()
  11. requires.append(pkg_name_without_url)
  12. else:
  13. requires.append(line)
  14. return requires
  15. def get_version():
  16. command = ["git", "describe", "--tags"]
  17. try:
  18. version = subprocess.check_output(command).decode().strip()
  19. version_parts = version.split("-")
  20. if len(version_parts) > 1 and version_parts[0].startswith("magic_pdf"):
  21. return version_parts[1]
  22. else:
  23. raise ValueError(f"Invalid version tag {version}. Expected format is magic_pdf-<version>-released.")
  24. except Exception as e:
  25. print(e)
  26. return "0.0.0"
  27. def write_version_to_commons(version):
  28. commons_path = os.path.join(os.path.dirname(__file__), 'magic_pdf', 'libs', 'version.py')
  29. with open(commons_path, 'w') as f:
  30. f.write(f'__version__ = "{version}"\n')
  31. if __name__ == '__main__':
  32. version_name = get_version()
  33. write_version_to_commons(version_name)
  34. setup(
  35. name="magic_pdf", # 项目名
  36. version=version_name, # 自动从tag中获取版本号
  37. packages=find_packages(), # 包含所有的包
  38. install_requires=parse_requirements('requirements.txt'), # 项目依赖的第三方库
  39. python_requires=">=3.9", # 项目依赖的 Python 版本
  40. # entry_points={"console_scripts": ["my_command=my_project.main:run"]}, # 项目提供的可执行命令
  41. include_package_data=True, # 是否包含非代码文件,如数据文件、配置文件等
  42. zip_safe=False, # 是否使用 zip 文件格式打包,一般设为 False
  43. )