|
14 | 14 | from setuptools.command.build_ext import build_ext
|
15 | 15 |
|
16 | 16 | HERE = pathlib.Path(__file__).absolute().parent
|
| 17 | +VERSION_FILE = HERE / 'torchopt' / 'version.py' |
17 | 18 |
|
18 |
| -sys.path.insert(0, str(HERE / 'torchopt')) |
19 |
| -import version # noqa |
| 19 | +try: |
| 20 | + from torchopt import version # noqa |
| 21 | +except ImportError: |
| 22 | + sys.path.insert(0, str(VERSION_FILE.parent)) |
| 23 | + import version # noqa |
20 | 24 |
|
21 | 25 |
|
22 | 26 | class CMakeExtension(Extension):
|
@@ -81,10 +85,28 @@ def build_extension(self, ext):
|
81 | 85 | os.chdir(HERE)
|
82 | 86 |
|
83 | 87 |
|
84 |
| -setup( |
85 |
| - version=version.__version__, |
86 |
| - package_data={'sharedlib': ['*.so', '*.pyd']}, |
87 |
| - include_package_data=True, |
88 |
| - cmdclass={'build_ext': cmake_build_ext}, |
89 |
| - ext_modules=[CMakeExtension('torchopt._C', source_dir=HERE)], |
90 |
| -) |
| 88 | +VERSION_CONTENT = None |
| 89 | +if not version.__release__: |
| 90 | + import re |
| 91 | + |
| 92 | + VERSION_CONTENT = VERSION_FILE.read_text(encoding='UTF-8') |
| 93 | + VERSION_FILE.write_text( |
| 94 | + data=re.sub( |
| 95 | + r"""__version__\s*=\s*('[^']+'|"[^"]+")""", |
| 96 | + r"__version__ = '{}'".format(version.__version__), |
| 97 | + string=VERSION_CONTENT, |
| 98 | + ), |
| 99 | + encoding='UTF-8', |
| 100 | + ) |
| 101 | + |
| 102 | +try: |
| 103 | + setup( |
| 104 | + version=version.__version__, |
| 105 | + package_data={'sharedlib': ['*.so', '*.pyd']}, |
| 106 | + include_package_data=True, |
| 107 | + cmdclass={'build_ext': cmake_build_ext}, |
| 108 | + ext_modules=[CMakeExtension('torchopt._C', source_dir=HERE)], |
| 109 | + ) |
| 110 | +finally: |
| 111 | + if VERSION_CONTENT is not None: |
| 112 | + VERSION_FILE.write_text(data=VERSION_CONTENT, encoding='UTF-8') |
0 commit comments