change if bad to assert not bad, message
[vvp/validation-scripts.git] / install_win_deps.py
1 # ============LICENSE_START====================================================
2 # org.onap.vvp/validation-scripts
3 # ===================================================================
4 # Copyright © 2019 AT&T Intellectual Property. All rights reserved.
5 # ===================================================================
6 #
7 # Unless otherwise specified, all software contained herein is licensed
8 # under the Apache License, Version 2.0 (the "License");
9 # you may not use this software except in compliance with the License.
10 # You may obtain a copy of the License at
11 #
12 #             http://www.apache.org/licenses/LICENSE-2.0
13 #
14 # Unless required by applicable law or agreed to in writing, software
15 # distributed under the License is distributed on an "AS IS" BASIS,
16 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 # See the License for the specific language governing permissions and
18 # limitations under the License.
19 #
20 #
21 #
22 # Unless otherwise specified, all documentation contained herein is licensed
23 # under the Creative Commons License, Attribution 4.0 Intl. (the "License");
24 # you may not use this documentation except in compliance with the License.
25 # You may obtain a copy of the License at
26 #
27 #             https://creativecommons.org/licenses/by/4.0/
28 #
29 # Unless required by applicable law or agreed to in writing, documentation
30 # distributed under the License is distributed on an "AS IS" BASIS,
31 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
32 # See the License for the specific language governing permissions and
33 # limitations under the License.
34 #
35 # ============LICENSE_END============================================
36 #
37 import os
38 import platform
39 import subprocess  # nosec
40 import sys
41 import tempfile
42 from urllib import request
43
44 PREBUILT_DOWNLOAD_SITE = "https://download.lfd.uci.edu/pythonlibs/n5jyqt7p/"
45 PREBUILT_WIN_LIBS = [
46     "yappi-1.0-cp{python_version}-cp{python_version}m-{arch}.whl",
47     "setproctitle-1.1.10-cp{python_version}-cp{python_version}m-{arch}.whl",
48 ]
49
50
51 def is_windows():
52     return os.name == "nt"
53
54
55 def python_version():
56     return sys.version[:3].replace(".", "")
57
58
59 def system_architecture():
60     arch = platform.architecture()
61     return "win32" if arch[0] != "64bit" else "win_amd64"
62
63
64 def download_url(url):
65     resp = request.urlopen(url)  # nosec
66     return resp.read()
67
68
69 def read_file(path):
70     with open(path, "r") as f:
71         return f.read()
72
73
74 def write_file(data, path, mode="w"):
75     with open(path, mode) as f:
76         f.write(data)
77
78
79 def install_prebuilt_binaries_on_windows():
80     if not is_windows():
81         return
82     temp_dir = tempfile.mkdtemp()
83     for lib in PREBUILT_WIN_LIBS:
84         filename = lib.format(
85             python_version=python_version(), arch=system_architecture()
86         )
87         url = PREBUILT_DOWNLOAD_SITE + filename
88         print(f"Downloading {url}")
89         contents = download_url(url)
90         file_path = os.path.join(temp_dir, filename)
91         write_file(contents, file_path, mode="wb")
92         print("Download complete. Installing dependency.")
93         subprocess.call(["pip", "install", file_path])  # nosec
94
95
96 if __name__ == "__main__":
97     install_prebuilt_binaries_on_windows()