Coverage for ci/download-wasmtime.py: 14%

81 statements  

« prev     ^ index     » next       coverage.py v7.6.12, created at 2025-02-20 16:25 +0000

1# Helper script to download a precompiled binary of the wasmtime dll for the 

2# current platform. 

3 

4import io 

5import platform 

6import shutil 

7import sys 

8import tarfile 

9import urllib.request 

10import zipfile 

11from pathlib import Path 

12 

13# set to "dev" to download the latest or pick a tag from 

14# https://github.com/bytecodealliance/wasmtime/tags 

15WASMTIME_VERSION = "v30.0.0" 

16 

17 

18def main(platform, arch): 

19 is_zip = False 

20 version = WASMTIME_VERSION 

21 

22 if arch == 'AMD64': 

23 arch = 'x86_64' 

24 if arch == 'arm64' or arch == 'ARM64': 

25 arch = 'aarch64' 

26 dirname = '{}-{}'.format(platform, arch) 

27 if platform == 'linux' or platform == 'musl': 

28 filename = 'wasmtime-{}-{}-{}-c-api.tar.xz'.format(version, arch, platform) 

29 libname = '_libwasmtime.so' 

30 dirname = 'linux-{}'.format(arch) 

31 elif platform == 'win32': 

32 filename = 'wasmtime-{}-{}-windows-c-api.zip'.format(version, arch) 

33 is_zip = True 

34 libname = '_wasmtime.dll' 

35 elif platform == 'darwin': 

36 filename = 'wasmtime-{}-{}-macos-c-api.tar.xz'.format(version, arch) 

37 libname = '_libwasmtime.dylib' 

38 else: 

39 raise RuntimeError("unknown platform: " + sys.platform) 

40 

41 url = 'https://github.com/bytecodealliance/wasmtime/releases/download/{}/'.format(version) 

42 url += filename 

43 print('Download', url) 

44 dst = Path('wasmtime') / dirname / libname 

45 try: 

46 shutil.rmtree(dst.parent) 

47 except Exception: 

48 pass 

49 try: 

50 shutil.rmtree(Path('wasmtime/include')) 

51 except Exception: 

52 pass 

53 Path(dst).parent.mkdir(parents=True) 

54 (Path('wasmtime') / 'include/wasmtime').mkdir(parents=True) 

55 

56 with urllib.request.urlopen(url) as f: 

57 contents = f.read() 

58 

59 def final_loc(name): 

60 parts = name.split('include/') 

61 if '/min/' in name: 

62 return None 

63 elif len(parts) > 1 and name.endswith('.h'): 

64 return Path('wasmtime') / 'include' / parts[1] 

65 elif name.endswith('.dll') or name.endswith('.so') or name.endswith('.dylib'): 

66 if '-min.' in name: 

67 return None 

68 return dst 

69 else: 

70 return None 

71 

72 if is_zip: 

73 t = zipfile.ZipFile(io.BytesIO(contents)) 

74 for member in t.namelist(): 

75 loc = final_loc(member) 

76 if not loc: 

77 continue 

78 print(f'{member} => {loc}') 

79 contents = t.read(member) 

80 with open(loc, "wb") as f: 

81 f.write(contents) 

82 else: 

83 t = tarfile.open(fileobj=io.BytesIO(contents)) 

84 for member in t.getmembers(): 

85 loc = final_loc(member.name) 

86 if not loc: 

87 continue 

88 print(f'{member.name} => {loc}') 

89 contents = t.extractfile(member).read() 

90 with open(loc, "wb") as f: 

91 f.write(contents) 

92 

93 if not dst.exists(): 

94 raise RuntimeError("failed to find dynamic library") 

95 

96 

97if __name__ == '__main__': 

98 if len(sys.argv) > 2: 

99 main(sys.argv[1], sys.argv[2]) 

100 else: 

101 main(sys.platform, platform.machine())