forked from WasmEdge/WasmEdge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall.py
1562 lines (1376 loc) · 55.3 KB
/
install.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import (
division,
print_function,
absolute_import,
unicode_literals,
with_statement,
)
from contextlib import contextmanager
import shutil
import sys
import argparse
from os.path import expanduser, join, dirname, abspath, exists, islink, lexists, isdir
from os import (
getenv,
geteuid,
listdir,
makedirs,
mkdir,
readlink,
remove,
getpid,
symlink,
)
import tempfile
import tarfile
import zipfile
import platform
import subprocess
import re
import logging
download_url = None
# Define version specific things
if sys.version_info[0] == 3:
import urllib.request
import urllib.error
def wrap_download_url(url, *args):
try:
return urllib.request.urlretrieve(url, *args)
except urllib.error.HTTPError as e:
logging.error("Download error from urllib: %s", e)
logging.error("URL: %s", url)
exit(1)
download_url = wrap_download_url
def reraise(tp, value=None, tb=None):
if value is None:
value = tp
if value.__traceback__ is not tb:
raise value.with_traceback(tb)
raise value
else:
exec("def reraise(tp, value=None, tb=None):\n raise tp, value, tb\n")
import urllib
def wrap_download_url(url, *args):
headers = ""
try:
_, headers = urllib.urlretrieve(url, *args)
except:
logging.error("Download error from urllib")
logging.error("URL: %s", url)
logging.debug("Header response: %s", headers)
exit(1)
if "text/plain" in str(headers) and not "uninstall" in args[0]:
logging.error("Download error from urllib")
logging.error("URL: %s", url)
logging.debug("Header response: %s", headers)
exit(1)
download_url = wrap_download_url
def show_progress(block_num, block_size, total_size):
downloaded = block_num * block_size
downloaded_lim = min(1, downloaded / (total_size))
print(
end=(
"\r|%-60s|" % ("=" * int(60 * downloaded_lim))
+ "%6.2f %%" % (downloaded_lim * 100)
)
)
if downloaded < total_size:
pass
else:
logging.info("Downloaded")
@contextmanager
def opened_w_error(filename, mode="r"):
try:
f = open(filename, mode)
except IOError as err:
logging.critical("Error opening file: %s error: %s", filename, err.strerror)
yield None
else:
try:
yield f
finally:
f.close()
def _is_tarxz(filename):
return filename.endswith(".tar.xz")
def _is_tar(filename):
return filename.endswith(".tar")
def _is_targz(filename):
return filename.endswith(".tar.gz")
def _is_tgz(filename):
return filename.endswith(".tgz")
def _is_zip(filename):
return filename.endswith(".zip")
def extract_archive(
from_path, ipath, to_path=None, remove_finished=False, env_file_path=None
):
files_extracted = []
if to_path is None:
to_path = dirname(from_path)
if _is_tar(from_path):
with tarfile.open(from_path, "r") as tar:
tar.extractall(path=to_path)
files_extracted = tar.getnames()
elif _is_targz(from_path) or _is_tgz(from_path):
with tarfile.open(from_path, "r:gz") as tar:
tar.extractall(path=to_path)
files_extracted = tar.getnames()
elif _is_tarxz(from_path):
with tarfile.open(from_path, "r:xz") as tar:
tar.extractall(path=to_path)
files_extracted = tar.getnames()
elif _is_zip(from_path):
with zipfile.ZipFile(from_path, "r") as z:
z.extractall(to_path)
files_extracted = z.namelist()
else:
reraise(ValueError("Extraction of {} not supported".format(from_path)))
logging.debug("Writing installed files to %s file", env_file_path)
with opened_w_error(env_file_path, "a") as env_file:
if env_file is not None:
for filename in files_extracted:
fname = filename.replace(CONST_ipkg, ipath)
if "._" in filename:
remove(join(to_path, filename))
continue
# Skip if it ends with "wasmedge" as it is going to be removed at a later stage
if fname.endswith("wasmedge") and not fname.endswith("bin/wasmedge"):
continue
# replace wasmedge folder name with include
if is_default_path(args):
fname = fname.replace("/lib64/", "/" + CONST_lib_dir + "/")
if fname.endswith("/lib64"):
fname = fname[:-5] + "lib"
if fname.startswith("/usr") and "lib64" in fname:
fname = fname.replace("lib64", "lib", 1)
if to_path.endswith("Plugins"):
if is_default_path(args):
fname = fname.replace(
join(ipath, CONST_lib_dir, "wasmedge/"), ""
)
fname = join(ipath, "plugin", fname)
else:
fname = join(ipath, CONST_lib_dir, "wasmedge", fname)
else:
if ipath not in fname:
fname = join(ipath, fname)
# replace GNUSparseFile.0 with nothing
fname = fname.replace("/GNUSparseFile.0", "")
# Don't append system directories
if (not is_default_path(args)) and isdir(fname):
continue
env_file.write("#" + fname + "\n")
logging.debug("Appending:%s", fname)
else:
logging.warning("Unable to write to env file")
if remove_finished:
remove(from_path)
# https://stackoverflow.com/questions/1868714/
# how-do-i-copy-an-entire-directory-of-files-
# into-an-existing-directory-using-pyth
def copytree(src, dst, symlinks=True, ignore=None):
if not exists(dst):
makedirs(dst)
shutil.copystat(src, dst)
lst = listdir(src)
if ignore:
excl = ignore(src, lst)
lst = [x for x in lst if x not in excl]
for item in lst:
s = join(src, item)
d = join(dst, item)
if symlinks and islink(s):
if lexists(d):
remove(d)
symlink(readlink(s), d)
elif isdir(s):
copytree(s, d, symlinks, ignore)
else:
shutil.copy2(s, d)
class VersionString:
def __init__(self, version):
self.version = version
def __str__(self):
return self.version
def __repr__(self):
return "VersionString:" + self.version
def _preprocess(self, v, separator, ignorecase):
if ignorecase:
v = v.lower()
return [
(
int(x)
if x.isdigit()
else [
int(y) if y.isdigit() else y for y in re.findall("\d+|[a-zA-Z]+", x)
]
)
for x in re.split(separator, v)
]
def compare(self, version2, separator=". |-", ignorecase=True):
"""
# return 1 if self.version > version2
# return 0 if self.version == version2
# return -1 if self.version < version2
# return False if not comparable
"""
if "rc" in self.version and not "rc" in version2:
a = self._preprocess(
self.version.split("rc")[0].strip("-"), separator, ignorecase
)
b = b = self._preprocess(version2, separator, ignorecase)
if ((a > b) - (a < b)) == 0:
return -1
else:
return (a > b) - (a < b)
else:
a = self._preprocess(self.version, separator, ignorecase)
b = self._preprocess(version2, separator, ignorecase)
try:
return (a > b) - (a < b)
except:
return False
SUPPORTED_PLATFORM_MACHINE = {
"Linux": ["x86_64", "amd64", "arm64", "armv8", "aarch64"],
"Darwin": ["x86_64", "arm64", "arm"],
}
SUPPORTED_MIN_VERSION = {
"Linux" + "x86_64": VersionString("0.13.0"),
"Linux" + "amd64": VersionString("0.13.0"),
"Linux" + "arm64": VersionString("0.13.0"),
"Linux" + "armv8": VersionString("0.13.0"),
"Linux" + "aarch64": VersionString("0.13.0"),
"Darwin" + "x86_64": VersionString("0.13.0"),
"Darwin" + "arm64": VersionString("0.13.0"),
"Darwin" + "arm": VersionString("0.13.0"),
}
WASMEDGE = "WasmEdge"
WASMEDGE_UNINSTALLER = "WasmEdge_Uninstaller"
TENSORFLOW = "tensorflow"
TENSORFLOW_LITE = "tensorflow_lite"
TENSORFLOW_LITE_P = "tensorflowlite"
TENSORFLOW_DEPS = "tensorflow_deps"
TENSORFLOW_LITE_DEPS = "tensorflow_lite_deps"
IMAGE = "image"
WASI_NN_OPENVINO = "wasi_nn-openvino"
WASI_CRYPTO = "wasi_crypto"
WASI_NN_PYTORCH = "wasi_nn-pytorch"
WASI_NN_TENSORFLOW_LITE = "wasi_nn-tensorflowlite"
WASI_NN_GGML = "wasi_nn-ggml"
WASI_NN_GGML_CUDA = "wasi_nn-ggml-cuda"
WASI_NN_GGML_NOAVX = "wasi_nn-ggml-noavx"
WASI_LOGGING = "wasi_logging"
WASMEDGE_TENSORFLOW_PLUGIN = WASMEDGE.lower() + "_" + TENSORFLOW
WASMEDGE_TENSORFLOW_LITE_PLUGIN = WASMEDGE.lower() + "_" + TENSORFLOW_LITE_P
WASMEDGE_IMAGE_PLUGIN = WASMEDGE.lower() + "_" + IMAGE
WASMEDGE_RUSTLS = "wasmedge_rustls"
WASM_BPF = "wasm_bpf"
PLUGINS_AVAILABLE = [
WASI_NN_OPENVINO,
WASI_CRYPTO,
WASI_NN_PYTORCH,
WASI_NN_TENSORFLOW_LITE,
WASI_NN_GGML,
WASI_NN_GGML_CUDA,
WASI_NN_GGML_NOAVX,
WASI_LOGGING,
WASMEDGE_TENSORFLOW_PLUGIN,
WASMEDGE_TENSORFLOW_LITE_PLUGIN,
WASMEDGE_IMAGE_PLUGIN,
WASMEDGE_RUSTLS,
WASM_BPF,
]
SUPPORTTED_PLUGINS = {
"ubuntu20.04" + "x86_64" + WASI_CRYPTO: VersionString("0.13.0"),
"manylinux2014" + "x86_64" + WASI_CRYPTO: VersionString("0.13.0"),
"manylinux2014" + "aarch64" + WASI_CRYPTO: VersionString("0.13.0"),
"manylinux2014" + "arm64" + WASI_CRYPTO: VersionString("0.13.0"),
"ubuntu20.04" + "x86_64" + WASI_NN_OPENVINO: VersionString("0.13.0"),
"ubuntu20.04" + "x86_64" + WASI_NN_PYTORCH: VersionString("0.13.0"),
"ubuntu20.04" + "x86_64" + WASI_NN_GGML: VersionString("0.13.4"),
"ubuntu20.04" + "aarch64" + WASI_NN_GGML: VersionString("0.13.5"),
"ubuntu20.04" + "x86_64" + WASI_NN_GGML_NOAVX: VersionString("0.13.5"),
"ubuntu20.04" + "x86_64" + WASI_NN_GGML_CUDA: VersionString("0.13.4"),
"ubuntu20.04" + "aarch64" + WASI_NN_GGML_CUDA: VersionString("0.13.5"),
"manylinux2014" + "x86_64" + WASI_NN_PYTORCH: VersionString("0.13.0"),
"manylinux2014" + "x86_64" + WASI_NN_TENSORFLOW_LITE: VersionString("0.13.0"),
"manylinux2014" + "x86_64" + WASI_NN_GGML: VersionString("0.13.4"),
"manylinux2014" + "aarch64" + WASI_NN_TENSORFLOW_LITE: VersionString("0.13.0"),
"manylinux2014" + "aarch64" + WASI_NN_GGML: VersionString("0.13.4"),
"darwin" + "x86_64" + WASI_NN_GGML: VersionString("0.13.4"),
"darwin" + "arm64" + WASI_NN_GGML: VersionString("0.13.4"),
"ubuntu20.04" + "x86_64" + WASI_NN_TENSORFLOW_LITE: VersionString("0.13.0"),
"darwin" + "x86_64" + WASMEDGE_TENSORFLOW_PLUGIN: VersionString("0.13.0"),
"darwin" + "arm64" + WASMEDGE_TENSORFLOW_PLUGIN: VersionString("0.13.0"),
"manylinux2014" + "x86_64" + WASMEDGE_TENSORFLOW_PLUGIN: VersionString("0.13.0"),
"manylinux2014" + "aarch64" + WASMEDGE_TENSORFLOW_PLUGIN: VersionString("0.13.0"),
"ubuntu20.04" + "x86_64" + WASMEDGE_TENSORFLOW_PLUGIN: VersionString("0.13.0"),
"darwin" + "x86_64" + WASMEDGE_TENSORFLOW_LITE_PLUGIN: VersionString("0.13.0"),
"darwin" + "arm64" + WASMEDGE_TENSORFLOW_LITE_PLUGIN: VersionString("0.13.0"),
"manylinux2014"
+ "x86_64"
+ WASMEDGE_TENSORFLOW_LITE_PLUGIN: VersionString("0.13.0"),
"manylinux2014"
+ "aarch64"
+ WASMEDGE_TENSORFLOW_LITE_PLUGIN: VersionString("0.13.0"),
"ubuntu20.04" + "x86_64" + WASMEDGE_TENSORFLOW_LITE_PLUGIN: VersionString("0.13.0"),
"darwin" + "x86_64" + WASMEDGE_IMAGE_PLUGIN: VersionString("0.13.0"),
"darwin" + "arm64" + WASMEDGE_IMAGE_PLUGIN: VersionString("0.13.0"),
"manylinux2014" + "x86_64" + WASMEDGE_IMAGE_PLUGIN: VersionString("0.13.0"),
"manylinux2014" + "aarch64" + WASMEDGE_IMAGE_PLUGIN: VersionString("0.13.0"),
"ubuntu20.04" + "x86_64" + WASMEDGE_IMAGE_PLUGIN: VersionString("0.13.0"),
"darwin" + "x86_64" + WASI_LOGGING: VersionString("0.14.0"),
"darwin" + "arm64" + WASI_LOGGING: VersionString("0.13.5"),
"manylinux2014" + "aarch64" + WASI_LOGGING: VersionString("0.13.5"),
"manylinux2014" + "x86_64" + WASI_LOGGING: VersionString("0.13.5"),
"ubuntu20.04" + "x86_64" + WASI_LOGGING: VersionString("0.13.5"),
"ubuntu20.04" + "aarch64" + WASI_LOGGING: VersionString("0.14.0"),
"darwin" + "x86_64" + WASMEDGE_RUSTLS: VersionString("0.13.4"),
"darwin" + "arm64" + WASMEDGE_RUSTLS: VersionString("0.13.4"),
"manylinux2014" + "aarch64" + WASMEDGE_RUSTLS: VersionString("0.13.5"),
"manylinux2014" + "x86_64" + WASMEDGE_RUSTLS: VersionString("0.13.4"),
"ubuntu20.04" + "x86_64" + WASMEDGE_RUSTLS: VersionString("0.13.4"),
"ubuntu20.04" + "aarch64" + WASMEDGE_RUSTLS: VersionString("0.13.5"),
"ubuntu20.04" + "x86_64" + WASM_BPF: VersionString("0.13.2"),
"manylinux2014" + "x86_64" + WASM_BPF: VersionString("0.13.2"),
}
HOME = expanduser("~")
PATH = join(HOME, ".wasmedge")
SHELL = getenv("SHELL", "bash").split("/")[-1]
TEMP_PATH = join(tempfile.gettempdir(), "wasmedge." + str(getpid()))
CONST_shell_config = None
CONST_shell_profile = None
CONST_env = None
CONST_urls = None
CONST_release_pkg = None
CONST_ipkg = None
CONST_lib_ext = None
CONST_env_path = None
CONST_lib_dir = "lib"
CONST_PATH_NOT_EXIST_STR = "/DOES NOT EXIST;"
try:
mkdir(TEMP_PATH)
except:
pass
def set_env(args, compat):
global CONST_env, CONST_env_path, CONST_lib_dir
CONST_env = """#!/bin/sh
# wasmedge shell setup
# affix colons on either side of $PATH to simplify matching
case :"${1}": in
*:"{0}/bin":*)
;;
*)
# Prepending path in case a system-installed wasmedge needs to be overridden
if [ -n "${1}" ]; then
export PATH="{0}/bin:$PATH"
else
export PATH="{0}/bin"
fi
;;
esac
case :"${2}": in
*:"{0}/{6}":*)
;;
*)
# Prepending path in case a system-installed wasmedge libs needs to be overridden
if [ -n "${2}" ]; then
export {2}="{0}/{6}:${2}"
else
export {2}="{0}/{6}"
fi
;;
esac
case :"${3}": in
*:"{0}/{6}":*)
;;
*)
if [ -n "${3}" ]; then
export LIBRARY_PATH="{0}/{6}:$LIBRARY_PATH"
else
export LIBRARY_PATH="{0}/{6}"
fi
;;
esac
case :"${4}": in
*:"{0}/include":*)
;;
*)
if [ -n "${4}" ]; then
export C_INCLUDE_PATH="{0}/include:$C_INCLUDE_PATH"
else
export C_INCLUDE_PATH="{0}/include"
fi
;;
esac
case :"${5}": in
*:"{0}/include":*)
;;
*)
if [ -n "${5}" ]; then
export CPLUS_INCLUDE_PATH="{0}/include:$CPLUS_INCLUDE_PATH"
else
export CPLUS_INCLUDE_PATH="{0}/include"
fi
;;
esac
if [ -z ${{WASMEDGE_LIB_DIR+x}} ]; then
export WASMEDGE_LIB_DIR="{0}/{6}"
fi
# Please do not edit comments below this for uninstallation purpose
""".format(
args.path,
"PATH",
compat.ld_library_path,
"LIBRARY_PATH",
"C_INCLUDE_PATH",
"CPLUS_INCLUDE_PATH",
CONST_lib_dir,
)
try:
mkdir(args.path)
if is_default_path(args):
mkdir(join(args.path, "plugin"))
except:
pass
CONST_env_path = join(args.path, "env")
mode = "w+" if not exists(CONST_env_path) else "w"
with opened_w_error(CONST_env_path, mode) as env:
if env is not None:
env.write(CONST_env)
else:
logging.error("Not able to write to env file")
def shell_configure(args, compat):
global CONST_shell_profile, CONST_shell_config
source_string = '\n. "{0}"\n'.format(join(args.path, "env"))
if ("bash" in SHELL) or ("zsh" in SHELL):
CONST_shell_config = join(HOME, "." + SHELL + "rc")
if "zsh" in SHELL:
CONST_shell_profile = join(HOME, "." + "zshenv")
else:
CONST_shell_profile = join(HOME, "." + SHELL + "_profile")
if not exists(CONST_shell_config) and compat.platform != "Darwin":
open(CONST_shell_config, "a").close()
write_shell = False
if compat.platform != "Darwin":
with opened_w_error(CONST_shell_config, "r") as shell_config:
if shell_config is not None:
if source_string not in shell_config.read():
write_shell = True
else:
write_shell = True
# On Darwin: Append to shell config only if shell_profile does not exist
# On Linux: Append to shell config anyway
if write_shell and compat.platform != "Darwin":
with opened_w_error(CONST_shell_config, "a") as shell_config:
if shell_config is not None:
shell_config.write(source_string)
write_shell = False
if exists(CONST_shell_profile):
with opened_w_error(CONST_shell_profile, "r") as shell_profile:
if shell_profile is not None:
if source_string not in shell_profile.read():
write_shell = True
if write_shell:
with opened_w_error(CONST_shell_profile, "a") as shell_profile:
if shell_profile is not None:
shell_profile.write(source_string)
write_shell = False
elif compat.platform == "Darwin" and "zsh" in SHELL:
open(CONST_shell_profile, "a").close()
with opened_w_error(CONST_shell_profile, "r") as shell_config:
if shell_config is not None:
if source_string not in shell_config.read():
write_shell = True
else:
write_shell = True
if write_shell:
with opened_w_error(CONST_shell_profile, "a") as shell_profile:
if shell_profile is not None:
shell_profile.write(source_string)
write_shell = False
else:
logging.error("Unknown shell found")
return -1
logging.info("shell configuration updated")
return 0
def fix_gnu_sparse(args):
# Fix GNUSparseFile.0 folder in macOS if exists
global CONST_lib_ext, CONST_lib_dir
for dir in listdir(args.path):
if not isdir(join(args.path, dir)):
continue
if "GNUSparseFile" in dir:
for file in listdir(join(args.path, dir)):
if file.endswith(CONST_lib_ext):
if isdir(join(args.path, CONST_lib_dir)):
shutil.move(
join(args.path, dir, file), join(args.path, CONST_lib_dir)
)
else:
logging.error(
"%s directory not found", join(args.path, CONST_lib_dir)
)
try:
mkdir(join(args.path, CONST_lib_dir))
shutil.move(
join(args.path, dir, file),
join(args.path, CONST_lib_dir),
)
except:
pass
elif (
file.endswith(".h")
or file.endswith(".hpp")
or file.endswith(".inc")
):
shutil.move(join(args.path, dir, file), join(args.path, "include"))
else:
shutil.move(join(args.path, dir, file), join(args.path, "bin"))
for sub_dir in listdir(join(args.path, dir)):
if not isdir(join(args.path, dir, sub_dir)):
continue
if "GNUSparseFile" in sub_dir:
for file in listdir(join(args.path, dir, sub_dir)):
shutil.move(
join(args.path, dir, sub_dir, file), join(args.path, dir)
)
if len(listdir(join(args.path, dir, sub_dir))) == 0:
shutil.rmtree(join(args.path, dir, sub_dir))
def check_nvcc(platform):
if platform == "Linux":
cmd = "/usr/local/cuda/bin/nvcc --version 2>/dev/null"
output = run_shell_command(cmd)
logging.debug("%s: %s", cmd, output)
if "nvcc: NVIDIA (R) Cuda compiler driver" in output:
return True
else:
logging.info("CUDA cannot be detected via nvcc")
return False
else:
logging.info("CUDA is only supported on Linux")
return False
def check_nvidia_smi(platform):
if platform == "Linux":
cmd = "nvidia-smi -q 2>/dev/null | grep CUDA | cut -f2 -d ':'"
output = run_shell_command(cmd)
logging.debug("%s: %s", cmd, output)
if "12" in output: # Check if CUDA 12.x is installed
return True
else:
logging.info("CUDA 12.x cannot be detected via nvidia-smi")
return False
else:
logging.info("CUDA is only supported on Linux")
return False
def check_libcudart(platform):
if platform == "Linux":
cmd = "ldconfig -p | grep libcudart.so"
output = run_shell_command(cmd)
logging.debug("%s: %s", cmd, output)
if "libcudart.so" in output:
return True
else:
logging.info("Cannot find libcudart.so")
return False
return False
def ldconfig(args, compat):
if geteuid() == 0:
# Only run ldconfig or update_dyld_shared_cache when user is root/sudoer
if compat.platform == "Linux":
cmd = "ldconfig {0}".format(join(args.path, CONST_lib_dir))
output = run_shell_command(cmd)
logging.debug("%s: %s", cmd, output)
elif compat.platform == "Darwin":
cmd = "update_dyld_shared_cache {0}".format(join(args.path, CONST_lib_dir))
output = run_shell_command(cmd)
logging.debug("%s: %s", cmd, output)
else:
logging.warning("Help adding ldconfig for your platform")
else:
logging.debug("Not root or sudoer, skip ldconfig")
def is_default_path(args):
global PATH
return args.path == abspath(PATH) or args.path[:4] != "/usr"
def install_tensorflow_extension(
args,
compat,
download_tf_deps_=False,
download_tf_lite_deps_=False,
):
global CONST_release_pkg, CONST_lib_ext, CONST_lib_dir, CONST_env_path
download_tf_deps = download_tf_deps_
download_tf_lite_deps = download_tf_lite_deps_
logging.debug(
"install_tensorflow_extension: %s %s",
download_tf_deps,
download_tf_lite_deps,
)
if (
not get_remote_version_availability(
"second-state/WasmEdge-tensorflow-deps", args.tf_deps_version
)
and download_tf_deps
):
logging.debug(
"Tensorflow Deps extension version not found: {0}".format(
args.tf_deps_version
)
)
download_tf_deps = False
if compat.machine == "aarch64":
download_tf_deps = False
logging.warning(
"Cannot download WasmEdge Tensorflow, Tools & Deps because it is aarch64"
)
local_release_package = CONST_release_pkg
# From WasmEdge 0.11.1, we have the Ubuntu release.
# Installation of ubuntu version extensions when the ubuntu version of WasmEdge selected.
if VersionString(args.version).compare("0.11.1") >= 0:
local_release_package = compat.release_package_wasmedge
logging.debug("Downloading dist package: {0}".format(local_release_package))
if download_tf_deps:
tf_deps_pkg = (
"WasmEdge-tensorflow-deps-TF-"
+ args.tf_deps_version
+ "-"
+ CONST_release_pkg
)
logging.info("Downloading tensorflow-deps")
download_url(
CONST_urls[TENSORFLOW_DEPS], join(TEMP_PATH, tf_deps_pkg), show_progress
)
# Extract archive
extract_archive(
join(TEMP_PATH, tf_deps_pkg),
join(args.path, CONST_lib_dir),
join(TEMP_PATH, "WasmEdge-tensorflow-deps", CONST_lib_dir),
env_file_path=CONST_env_path,
remove_finished=True,
)
copytree(join(TEMP_PATH, "WasmEdge-tensorflow-deps"), args.path)
if download_tf_lite_deps:
tf_deps_lite_pkg = (
"WasmEdge-tensorflow-deps-TFLite-"
+ args.tf_deps_version
+ "-"
+ CONST_release_pkg
)
logging.info("Downloading tensorflow-lite-deps")
download_url(
CONST_urls[TENSORFLOW_LITE_DEPS],
join(TEMP_PATH, tf_deps_lite_pkg),
show_progress,
)
# Extract archive
extract_archive(
join(TEMP_PATH, tf_deps_lite_pkg),
join(args.path, CONST_lib_dir),
join(TEMP_PATH, "WasmEdge-tensorflow-lite-deps", CONST_lib_dir),
env_file_path=CONST_env_path,
remove_finished=True,
)
copytree(join(TEMP_PATH, "WasmEdge-tensorflow-lite-deps"), args.path)
fix_gnu_sparse(args)
all_files = run_shell_command("ls -R {0}".format(TEMP_PATH))
if not isdir(join(args.path, CONST_lib_dir)):
logging.error("Strange: No %s directory found", CONST_lib_dir)
for file in listdir(join(args.path, CONST_lib_dir)):
if CONST_lib_ext not in file:
# ignore files that are not libraries
continue
if file not in all_files:
# ignore files that are not downloaded by this script
continue
if "tensorflow" not in file:
continue
# check if it contains any digits
if not any(i.isdigit() for i in file):
continue
if compat.platform == "Linux":
name, version = file.split(CONST_lib_ext, 1)
if version[0] == ".":
version = version[1:]
if version != "" and version.count(".") >= 2:
no_v_name = name + CONST_lib_ext
single_v_name = name + CONST_lib_ext + "." + version.split(".")[0]
dual_v_name = (
name
+ CONST_lib_ext
+ "."
+ version.split(".")[0]
+ "."
+ version.split(".")[1]
)
file_path = join(args.path, CONST_lib_dir, file)
single_v_file_path = join(args.path, CONST_lib_dir, single_v_name)
dual_v_file_path = join(args.path, CONST_lib_dir, dual_v_name)
no_v_file_path = join(args.path, CONST_lib_dir, no_v_name)
try:
symlink(file_path, single_v_file_path)
symlink(file_path, dual_v_file_path)
symlink(file_path, no_v_file_path)
except Exception as e:
logging.debug(e)
else:
continue
elif compat.platform == "Darwin":
name, version = file.split(CONST_lib_ext, 1)[0].split(".", 1)
if version != "" and version.count(".") >= 2:
no_v_name = name + CONST_lib_ext
single_v_name = name + "." + version.split(".")[0] + CONST_lib_ext
dual_v_name = (
name
+ "."
+ version.split(".")[0]
+ "."
+ version.split(".")[1]
+ CONST_lib_ext
)
file_path = join(args.path, CONST_lib_dir, file)
single_v_file_path = join(args.path, CONST_lib_dir, single_v_name)
dual_v_file_path = join(args.path, CONST_lib_dir, dual_v_name)
no_v_file_path = join(args.path, CONST_lib_dir, no_v_name)
try:
symlink(file_path, single_v_file_path)
symlink(file_path, dual_v_file_path)
symlink(file_path, no_v_file_path)
except Exception as e:
logging.debug(e)
else:
continue
else:
reraise(Exception("Not implemented for {0}".format(compat.platform)))
with opened_w_error(CONST_env_path, "a") as env_file:
if env_file is not None:
env_file.write("#" + single_v_file_path + "\n")
logging.debug("Appending:%s", single_v_file_path)
env_file.write("#" + dual_v_file_path + "\n")
logging.debug("Appending:%s", dual_v_file_path)
env_file.write("#" + no_v_file_path + "\n")
logging.debug("Appending:%s", no_v_file_path)
else:
logging.error("Not able to append installed files to env file")
for main_dir in ["WasmEdge-tensorflow", "WasmEdge-tensorflow-lite"]:
if not isdir(join(TEMP_PATH, main_dir)):
continue
for directory_file in listdir(join(TEMP_PATH, main_dir)):
if isdir(directory_file):
wasmedge_tf_folder = join(TEMP_PATH, main_dir, directory_file)
for _file in listdir(wasmedge_tf_folder):
if (
_file == "wasmedge"
and isdir(join(wasmedge_tf_folder, _file))
and is_default_path(args)
):
copytree(
join(wasmedge_tf_folder, _file),
join(args.path, "include", "wasmedge"),
)
elif CONST_lib_ext in _file:
if isdir(join(args.path, CONST_lib_dir)):
shutil.move(
join(wasmedge_tf_folder, _file),
join(args.path, CONST_lib_dir, _file),
)
else:
logging.error(
"%s is not a directory", join(args.path, CONST_lib_dir)
)
try:
mkdir(join(args.path, CONST_lib_dir))
shutil.move(
join(wasmedge_tf_folder, _file),
join(args.path, CONST_lib_dir, _file),
)
except:
pass
elif isdir(join(wasmedge_tf_folder, _file)):
copytree(
join(wasmedge_tf_folder, _file),
join(args.path, _file),
)
else:
shutil.move(
join(wasmedge_tf_folder, _file),
join(args.path, "bin", _file),
)
return 0
def install_plugins(args, compat):
global CONST_lib_dir
url_root = "https://github.com/WasmEdge/WasmEdge/releases/download/"
url_root += "$VERSION$/WasmEdge-plugin-$PLUGIN_NAME$-$VERSION$-$DIST$_$ARCH$.tar.gz"
if len(args.plugins) >= 1:
for plugin_name in args.plugins:
# Reset the url_root, due to the wasi-nn-ggml plugin with the build number will change the url
url_root = "https://github.com/WasmEdge/WasmEdge/releases/download/"
url_root += (
"$VERSION$/WasmEdge-plugin-$PLUGIN_NAME$-$VERSION$-$DIST$_$ARCH$.tar.gz"
)
plugin_version_supplied = None
plugin_wasi_nn_ggml_bypass_check = False
if plugin_name.find(":") != -1:
plugin_name, plugin_version_supplied = plugin_name.split(":")
# Deprecated rustls after 0.14.0
# Only allow users to install rustls with 0.13.5
if (
plugin_name.startswith(WASMEDGE_RUSTLS)
and compat.version.compare("0.13.5") != 0
):
logging.warning("WasmEdge Rustls plugin is only available in 0.13.5")
logging.warning("Please use -v 0.13.5 as a workaround")
logging.warning("Skip installing WasmEdge Rustls plugin")
continue
# Deprecated wasi-logging after 0.14.1-rc.1
if (
plugin_name.startswith(WASI_LOGGING)
and compat.version.compare("0.14.1-rc.1") != -1
):
logging.warning(
"WASI-Logging plugin is bundled into libwasmedge in 0.14.1-rc.1"
)
logging.warning("No need to install the WASI-Logging plugin")
continue
# Split the WASI-NN-GGML plugin and the others
if plugin_name.startswith(WASI_NN_GGML):
# Re-write the plugin name if the build number is supplied
# E.g. wasi_nn-ggml-b2330, wasi_nn-ggml-cuda-b2330, wasi_nn-ggml-cuda-11-b2330
# "https://github.com/second-state/WASI-NN-GGML-PLUGIN-REGISTRY/raw/main/"
# "$VERSION$/"
# "$BUILD_NUMBER$/"
# "WasmEdge-plugin"
# "-$PLUGIN_NAME$"
# "-$VERSION$"
# "-$DIST$"
# "_$ARCH$" # ".tar.gz"
# If the build number is supplied, bypass the checks
plugin_wasi_nn_ggml_bypass_check = True
if plugin_name.startswith(WASI_NN_GGML) and "-b" in plugin_name:
[plugin_name, plugin_build_number] = plugin_name.split("-b")
url_root = "https://github.com/second-state/WASI-NN-GGML-PLUGIN-REGISTRY/releases/download/"
url_root += "b$BUILD_NUMBER$/WasmEdge-plugin-$PLUGIN_NAME$-$VERSION$-$DIST$_$ARCH$.tar.gz"
url_root = url_root.replace("$BUILD_NUMBER$", plugin_build_number)
# Re-write the plugin name if CUDA is available
if plugin_name == WASI_NN_GGML and compat.cuda:
plugin_name = WASI_NN_GGML_CUDA
# Normal plugin
if (
plugin_name not in PLUGINS_AVAILABLE
and not plugin_wasi_nn_ggml_bypass_check
):
logging.error(
"%s plugin not found, available names - %s",
plugin_name,
PLUGINS_AVAILABLE,
)
continue
if (
compat.dist + compat.machine + plugin_name not in SUPPORTTED_PLUGINS
and not plugin_wasi_nn_ggml_bypass_check
):
logging.error(
"Plugin not compatible: %s",
compat.dist + compat.machine + plugin_name,
)
logging.debug("Supported: %s", SUPPORTTED_PLUGINS)
continue
else:
if plugin_version_supplied is None:
plugin_version_supplied = args.version
elif (
SUPPORTTED_PLUGINS[
compat.dist + compat.machine + plugin_name
].compare(plugin_version_supplied)
> 0
):
logging.error(