12
12
# See the License for the specific language governing permissions and
13
13
# limitations under the License.
14
14
15
- import setuptools
15
+ import platform
16
16
from setuptools import setup
17
+ from setuptools .command .build_ext import build_ext
18
+ from setuptools .extension import Extension
19
+ from urllib .request import Request , urlopen
20
+ import zipfile
21
+ from io import BytesIO
22
+
17
23
import versioneer
18
24
19
25
# read the contents of your README file
20
26
from pathlib import Path
21
27
this_directory = Path (__file__ ).parent
22
28
long_description = (this_directory / "README.md" ).read_text ()
23
29
30
+ ZITI_SDK_BASE = 'https://github.com/openziti/ziti-sdk-c/releases/download'
31
+
32
+
33
+ class GetZitilib (build_ext ):
34
+
35
+ def build_extension (self , ext ) -> None :
36
+ ziti_ver = self .get_sdk_version ()
37
+ osname , arch , libname = self .get_platform ()
38
+ sdk_distro = self .download_sdk (ziti_ver , osname , arch )
39
+ self .extract_zitilib (sdk_distro , libname , self .build_lib )
40
+
41
+ def get_platform (self ):
42
+ osname = platform .system ()
43
+ mach = platform .machine ()
44
+ arch , _ = platform .architecture ()
45
+
46
+ if osname == 'Linux' :
47
+ if mach .startswith ('arm' ):
48
+ if arch == '32bit' :
49
+ mach = 'arm'
50
+ elif arch == '64bit' :
51
+ mach = 'arm64'
52
+ return osname , mach , 'libziti.so'
53
+
54
+ if osname == 'Darwin' :
55
+ return osname , mach , 'libziti.dylib'
56
+
57
+ if osname == 'Windows' :
58
+ return osname , mach , 'ziti.dll'
59
+
60
+ def get_sdk_version (self ):
61
+ opts = self .distribution .get_option_dict ('openziti' )
62
+ _ , ver = opts ['ziti_sdk_version' ]
63
+ return ver
64
+
65
+ def extract_zitilib (self , distro , libname , target ):
66
+ with zipfile .ZipFile (BytesIO (distro )) as zipf :
67
+ return zipf .extract (member = f'lib/{ libname } ' , path = f'{ target } /openziti' )
68
+
69
+ def download_sdk (self , version , osname , arch ):
70
+ filename = f'{ ZITI_SDK_BASE } /{ version } /ziti-sdk-{ version } -{ osname } -{ arch } .zip'
71
+ headers = {}
72
+ req = Request (url = filename , headers = headers )
73
+ with urlopen (req ) as response :
74
+ length = response .getheader ('content-length' )
75
+ if response .status != 200 :
76
+ raise Exception (f'Could not download "{ filename } "' )
77
+ print (f"Downloading { length } from { filename } " )
78
+ return response .read ()
79
+
80
+ class ZitilibExt (Extension ):
81
+ def __init__ (self , name , sourcedir = '' ):
82
+ Extension .__init__ (self , name , sources = [])
83
+
84
+
85
+ cmds = dict (build_ext = GetZitilib )
86
+ cmds = versioneer .get_cmdclass (cmds )
87
+
24
88
setup (
25
89
version = versioneer .get_version (),
26
- cmdclass = versioneer .get_cmdclass (),
90
+ cmdclass = cmds ,
91
+ ext_modules = [
92
+ ZitilibExt ('_get_ziti_lib' ),
93
+ ],
27
94
28
95
packages = ['openziti' ],
29
- include_package_data = True ,
30
- package_data = {
31
- "openziti" : ["lib/*" ],
32
- },
33
96
)
0 commit comments