1
+ import os
2
+ import re
3
+ import sys
4
+ import platform
5
+ import subprocess
6
+ import datetime
7
+
8
+ from setuptools import setup , Extension
9
+ from setuptools .command .build_ext import build_ext
10
+ from wheel .bdist_wheel import bdist_wheel as bdist_wheel_
11
+ from distutils .util import get_platform
12
+ from distutils .version import LooseVersion
13
+
14
+
15
+ def get_git_commit_datetime ():
16
+ try :
17
+ commit_hash = subprocess .check_output ("git rev-parse HEAD" , shell = True , stderr = subprocess .STDOUT ).decode ("utf-8" ).strip ()
18
+ commit_datetime = subprocess .check_output ("git show -s --format=%ci " + commit_hash , shell = True , stderr = subprocess .STDOUT ).decode ("utf-8" ).strip ()
19
+ print (commit_datetime )
20
+ datetime_object = datetime .datetime .strptime (commit_datetime , '%Y-%m-%d %H:%M:%S +%f' )
21
+ print ("{:%Y%m%d%H%M%S}" .format (datetime_object ))
22
+ return "{:%Y%m%d%H%M%S}" .format (datetime_object )
23
+ except subprocess .CalledProcessError as cpe :
24
+ print (cpe .output )
25
+ return "00000000000000"
26
+
27
+
28
+ class CMakeExtension (Extension ):
29
+ def __init__ (self , name , sourcedir = '' ):
30
+ Extension .__init__ (self , name , sources = [])
31
+ self .sourcedir = os .path .abspath (sourcedir )
32
+
33
+
34
+ class CMakeBuild (build_ext ):
35
+ def run (self ):
36
+ try :
37
+ out = subprocess .check_output (['cmake' , '--version' ])
38
+ except OSError :
39
+ raise RuntimeError ("CMake must be installed to build the following extensions: " +
40
+ ", " .join (e .name for e in self .extensions ))
41
+
42
+ if platform .system () == "Windows" :
43
+ cmake_version = LooseVersion (re .search (r'version\s*([\d.]+)' , out .decode ()).group (1 ))
44
+ if cmake_version < '3.1.0' :
45
+ raise RuntimeError ("CMake >= 3.1.0 is required on Windows" )
46
+
47
+ for ext in self .extensions :
48
+ self .build_extension (ext )
49
+
50
+ def build_extension (self , ext ):
51
+ extdir = os .path .abspath (os .path .dirname (self .get_ext_fullpath (ext .name )))
52
+ cmake_args = ['-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=' + extdir ,
53
+ '-DPYTHON_EXECUTABLE=' + sys .executable ,
54
+ '-DBUILD_DEMO=OFF' ,
55
+ '-DBUILD_PYTHON_BINDINGS=ON' ,
56
+ '-DMODULE_DEV_TAG=' + version_extension ]
57
+
58
+ cfg = 'Debug' if self .debug else 'Release'
59
+ build_args = ['--config' , cfg ]
60
+
61
+ if platform .system () == "Windows" :
62
+ cmake_args += ['-DCMAKE_LIBRARY_OUTPUT_DIRECTORY_{}={}' .format (cfg .upper (), extdir )]
63
+ if sys .maxsize > 2 ** 32 :
64
+ cmake_args += ['-A' , 'x64' ]
65
+ build_args += ['--' , '/m' ]
66
+ else :
67
+ cmake_args += ['-DCMAKE_BUILD_TYPE=' + cfg ]
68
+ build_args += ['--' , '-j2' ]
69
+
70
+ env = os .environ .copy ()
71
+ env ['CXXFLAGS' ] = '{} -DVERSION_INFO=\\ "{}\\ "' .format (env .get ('CXXFLAGS' , '' ),
72
+ self .distribution .get_version ())
73
+ if not os .path .exists (self .build_temp ):
74
+ os .makedirs (self .build_temp )
75
+
76
+ subprocess .check_call (['cmake' , ext .sourcedir ] + cmake_args , cwd = self .build_temp , env = env )
77
+ subprocess .check_call (['cmake' , '--build' , '.' ] + build_args , cwd = self .build_temp )
78
+
79
+
80
+ class bdist_wheel (bdist_wheel_ ):
81
+ def finalize_options (self ):
82
+ from sys import platform as _platform
83
+ platform_name = get_platform ()
84
+ if _platform == "linux" or _platform == "linux2" :
85
+ # Linux
86
+ platform_name = 'manylinux1_x86_64'
87
+
88
+ bdist_wheel_ .finalize_options (self )
89
+ self .universal = True
90
+ self .plat_name_supplied = True
91
+ self .plat_name = platform_name
92
+
93
+
94
+ # Read version from file
95
+ vfrendering_version = open ('Version.txt' ).read ()
96
+
97
+
98
+ # Set extra version tag for dev builds if corresponding env variable is set
99
+ version_extension = ""
100
+ add_version_extension = os .environ .get ("VFRENDERING_ADD_VERSION_EXTENSION" , "" )
101
+ if add_version_extension .lower () in ("yes" , "true" , "t" , "1" ):
102
+ timepoint_string = get_git_commit_datetime ()
103
+ if timepoint_string == "00000000000000" :
104
+ timepoint_string = "{:%Y%m%d%H%M}" .format (datetime .datetime .now ())
105
+ version_extension = ".dev" + timepoint_string
106
+ print ("setup.py: package version suffix = " , version_extension )
107
+
108
+
109
+ # Setup configuration
110
+ setup (
111
+ name = 'pyVFRendering' ,
112
+ version = vfrendering_version + version_extension ,
113
+ author = 'Florian Rhiem' ,
114
+ author_email = 'f.rhiem@fz-juelich.de' ,
115
+ url = 'https://github.com/FlorianRhiem/VFRendering' ,
116
+ description = 'VFRendering python bindings' ,
117
+ long_description = '' ,
118
+ ext_modules = [CMakeExtension ('pyVFRendering' )],
119
+ cmdclass = {'build_ext' : CMakeBuild , 'bdist_wheel' : bdist_wheel },
120
+ )
0 commit comments