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

89 statements  

« prev     ^ index     » next       coverage.py v7.11.3, created at 2025-12-01 19:40 +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 = "v39.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 == 'mingw': 

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

37 is_zip = True 

38 libname = '_wasmtime.dll' 

39 elif platform == 'darwin': 

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

41 libname = '_libwasmtime.dylib' 

42 elif platform == 'android': 

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

44 libname = '_libwasmtime.so' 

45 dirname = 'android-{}'.format(arch) 

46 else: 

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

48 

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

50 url += filename 

51 print('Download', url) 

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

53 try: 

54 shutil.rmtree(dst.parent) 

55 except Exception: 

56 pass 

57 try: 

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

59 except Exception: 

60 pass 

61 

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

63 contents = f.read() 

64 

65 def final_loc(name): 

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

67 if '/min/' in name: 

68 return None 

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

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

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

72 if '-min.' in name: 

73 return None 

74 return dst 

75 else: 

76 return None 

77 

78 if is_zip: 

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

80 for member in t.namelist(): 

81 loc = final_loc(member) 

82 if not loc: 

83 continue 

84 loc.parent.mkdir(parents=True, exist_ok=True) 

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

86 contents = t.read(member) 

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

88 f.write(contents) 

89 else: 

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

91 for member in t.getmembers(): 

92 loc = final_loc(member.name) 

93 if not loc: 

94 continue 

95 loc.parent.mkdir(parents=True, exist_ok=True) 

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

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

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

99 f.write(contents) 

100 

101 if not dst.exists(): 

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

103 

104 

105if __name__ == '__main__': 

106 if len(sys.argv) > 2: 

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

108 else: 

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